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;
}
}

View File

@@ -0,0 +1,23 @@
/*
* 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/.
*/
#pragma once
#include "Host/ResourceLoader.hpp"
#include <variant>
namespace OpenVulkano
{
class EmbeddedResourceLoaderWindows final : public ResourceLoader
{
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;
private:
char* GetResourceType(const std::string& resourceName) const;
};
}

62
resources/icon.rc Normal file
View File

@@ -0,0 +1,62 @@
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE 9, 1
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
IDI_MADVOXEL_ICON ICON "madvoxel_icon.ico"
MADVOXEL_ICON ICON "madvoxel_icon.ico"

BIN
resources/madvoxel_icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

1
resources/resource.h Normal file
View File

@@ -0,0 +1 @@
#define IDI_MADVOXEL_ICON 101

View File

@@ -2,12 +2,16 @@ cmake_minimum_required(VERSION 3.28 FATAL_ERROR)
include(SetupVulkan) include(SetupVulkan)
include(Utils) include(Utils)
include(Filter)
file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") cmake_path(GET CMAKE_CURRENT_SOURCE_DIR PARENT_PATH ROOT_FOLDER)
file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp"
"${ROOT_FOLDER}/resources/*.rc" "${ROOT_FOLDER}/resources/*.h")
FilterPlatformPaths(SOURCES)
if (NOT ENABLE_CURL) if (NOT ENABLE_CURL)
list(FILTER SOURCES EXCLUDE REGEX "WebResourceLoader") list(FILTER SOURCES EXCLUDE REGEX "WebResourceLoader")
endif() endif()
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${SOURCES})
add_executable(OpenVulkano_Tests ${SOURCES}) add_executable(OpenVulkano_Tests ${SOURCES})
target_include_directories(OpenVulkano_Tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(OpenVulkano_Tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -0,0 +1,35 @@
/*
* 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 <catch2/catch_all.hpp>
#include "Host/Windows/EmbeddedResourceLoaderWindows.hpp"
#include "Base/Logger.hpp"
#include "../resources/resource.h"
#include <Windows.h>
#include <filesystem>
#include <fstream>
using namespace OpenVulkano;
TEST_CASE("Load icon")
{
Logger::SetupLogger("", "tests.log");
EmbeddedResourceLoaderWindows loader;
// the icon is split into RT_GROUP_ICON and RT_ICON. resource.h defines RT_GROUP_ICON entry
// RT_GROUP_ICON data
Array<char> groupData = loader.GetResource(MAKEINTRESOURCE(IDI_MADVOXEL_ICON), RT_GROUP_ICON);
// load resource that has no numeric ID
Array<char> groupData2 = loader.GetResource("MADVOXEL_ICON", RT_GROUP_ICON);
REQUIRE((!groupData.Empty() && groupData.Size() == groupData2.Size()));
// ICON data
Array<char> iconData = loader.GetResource(MAKEINTRESOURCE(1), RT_ICON);
REQUIRE((!iconData.Empty() && iconData.Size() == 194344));
}