implement first version of embedded resource loader for Windows

This commit is contained in:
ohyzha
2024-12-19 13:27:47 +02:00
parent b3ba4509d7
commit 982d2b613b
7 changed files with 207 additions and 2 deletions

View File

@@ -0,0 +1,80 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "EmbeddedResourceLoaderWindows.hpp"
#include "Base/Utils.hpp"
#include "Base/Logger.hpp"
#include "../resources/resource.h"
#include <windows.h>
#include <iostream>
#include <filesystem>
namespace OpenVulkano
{
namespace
{
void* HANDLE = ResourceLoader::RegisterResourceLoader(std::make_unique<EmbeddedResourceLoaderWindows>());
}
std::string EmbeddedResourceLoaderWindows::GetResourcePath(const std::string& resourceName)
{
return "";
}
Array<char> EmbeddedResourceLoaderWindows::GetResource(const std::string& resourceName)
{
return {};
//return GetResource(resourceName, GetResourceType(resourceName));
}
//LPSTR EmbeddedResourceLoaderWindows::GetResourceType(const std::string& resourceName) const
//{
// std::filesystem::path path(resourceName);
// std::string ext = path.extension().string();
// if (ext.empty() || ext == ".")
// {
// Logger::APP->warn("Could not load embedded resource {}. Unsupported extension", resourceName, ext);
// return {};
// }
// if (ext == ".ico" || ext == ".icon")
// {
// return RT_ICON;
// }
// else if (ext == ".ttf" || ext == ".fnt" || ext == ".font")
// {
// return RT_FONT;
// }
// // custom raw data
// return RT_RCDATA;
//}
Array<char> EmbeddedResourceLoaderWindows::GetResource(LPSTR resId, LPSTR resourceType) const
{
HMODULE exe = GetModuleHandleA(NULL);
LPCSTR resourceName = resId;
HRSRC hRes = FindResourceA(exe, resourceName, resourceType);
if (!hRes)
{
Logger::APP->warn("Could not find embedded resource");
return {};
}
HGLOBAL hResData = LoadResource(exe, hRes);
if (!hResData)
{
Logger::APP->warn("Embedded resource found, but could not load.");
return {};
}
void* data = LockResource(hResData);
DWORD size = SizeofResource(exe, hRes);
Array<char> dst(size);
std::memcpy(dst.Data(), data, size);
return dst;
}
}