code refactoring
This commit is contained in:
@@ -5,13 +5,11 @@
|
||||
*/
|
||||
|
||||
#include "EmbeddedResourceLoaderWindows.hpp"
|
||||
#include "Base/Utils.hpp"
|
||||
#include "Base/Logger.hpp"
|
||||
#include "Data/Containers/String.hpp"
|
||||
#include "../resources/resource.h"
|
||||
#include <windows.h>
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
@@ -27,46 +25,65 @@ namespace OpenVulkano
|
||||
|
||||
Array<char> EmbeddedResourceLoaderWindows::GetResource(const std::string& resourceName)
|
||||
{
|
||||
return {};
|
||||
//return GetResource(resourceName, GetResourceType(resourceName));
|
||||
OpenVulkano::String s(resourceName);
|
||||
auto tokens = s.Split("_");
|
||||
if (tokens.empty() || tokens.size() == 1)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
const std::string& resType = tokens.back();
|
||||
return GetResource(resourceName.c_str(), GetWinApiResourceType(resType));
|
||||
}
|
||||
|
||||
//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;
|
||||
//}
|
||||
LPSTR EmbeddedResourceLoaderWindows::GetWinApiResourceType(std::string resourceType) const
|
||||
{
|
||||
std::transform(resourceType.begin(), resourceType.end(), resourceType.begin(),
|
||||
[](char c) { return std::tolower(c); });
|
||||
if (resourceType == "icon")
|
||||
{
|
||||
return RT_GROUP_ICON;
|
||||
}
|
||||
else if (resourceType == "font")
|
||||
{
|
||||
return RT_FONT;
|
||||
}
|
||||
return RT_RCDATA;
|
||||
}
|
||||
|
||||
Array<char> EmbeddedResourceLoaderWindows::GetResource(LPSTR resId, LPSTR resourceType) const
|
||||
std::string EmbeddedResourceLoaderWindows::GetResourceName(const char* resId) const
|
||||
{
|
||||
// check if resId was provided as interger and not CString
|
||||
// https://stackoverflow.com/questions/3610565/why-does-makeintresource-work
|
||||
// first 64KB
|
||||
uintptr_t ptrT = reinterpret_cast<uintptr_t>(resId);
|
||||
// id that points to non-mappable memory
|
||||
if (ptrT <= 0xFFFF)
|
||||
{
|
||||
return std::to_string(static_cast<uint16_t>(0xFFFF & ptrT));
|
||||
}
|
||||
// C-string
|
||||
return std::string(resId);
|
||||
}
|
||||
|
||||
Array<char> EmbeddedResourceLoaderWindows::GetResource(const char* resId, char* resourceType)
|
||||
{
|
||||
HMODULE exe = GetModuleHandleA(NULL);
|
||||
LPCSTR resourceName = resId;
|
||||
HRSRC hRes = FindResourceA(exe, resourceName, resourceType);
|
||||
|
||||
if (!hRes)
|
||||
{
|
||||
Logger::APP->warn("Could not find embedded resource");
|
||||
Logger::DATA->debug("Could not find embedded resource {}. Error code = {}.", GetResourceName(resId),
|
||||
GetLastError());
|
||||
return {};
|
||||
}
|
||||
|
||||
HGLOBAL hResData = LoadResource(exe, hRes);
|
||||
if (!hResData)
|
||||
{
|
||||
Logger::APP->warn("Embedded resource found, but could not load.");
|
||||
|
||||
Logger::DATA->debug("Embedded resource {} found, but could not load. Error code = {}.", GetResourceName(resId),
|
||||
GetLastError());
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -77,4 +94,9 @@ namespace OpenVulkano
|
||||
return dst;
|
||||
}
|
||||
|
||||
Array<char> EmbeddedResourceLoaderWindows::GetResource(uint16_t id, char* resourceType)
|
||||
{
|
||||
return GetResource(MAKEINTRESOURCE(id), resourceType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,8 +16,10 @@ namespace OpenVulkano
|
||||
public:
|
||||
std::string GetResourcePath(const std::string& resourceName) override;
|
||||
Array<char> GetResource(const std::string& resourceName) override;
|
||||
Array<char> GetResource(char* resId, char* resourceType) const;
|
||||
Array<char> GetResource(const char* resId, char* resourceType);
|
||||
Array<char> GetResource(uint16_t id, char* resourceType);
|
||||
private:
|
||||
char* GetResourceType(const std::string& resourceName) const;
|
||||
char* GetWinApiResourceType(std::string resourceType) const;
|
||||
std::string GetResourceName(const char* resId) const;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user