From 75a5427287c291b79dca34f40e0bd5c2491e6f1c Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Wed, 15 Nov 2023 19:11:07 +0100 Subject: [PATCH] c++ify a little --- CMakeLists.txt | 1 + header/gMod_FileLoader.h | 2 +- header/uMod_Defines.h | 4 +++ header/uMod_TextureServer.h | 2 +- source/uMod_DX9_dll.cpp | 52 ++++++++++------------------------- source/uMod_TextureServer.cpp | 45 ++++++++++++++---------------- 6 files changed, 43 insertions(+), 63 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 69f30f7..2531ed5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,7 @@ target_link_libraries(gMod PRIVATE ${D3DX9_LIB} ${DXGUID_LIB} libzippp + psapi ) target_link_options(gMod PRIVATE "$<$:/NODEFAULTLIB:LIBCMT>" diff --git a/header/gMod_FileLoader.h b/header/gMod_FileLoader.h index 38526be..6bc6da3 100644 --- a/header/gMod_FileLoader.h +++ b/header/gMod_FileLoader.h @@ -9,7 +9,7 @@ struct TpfEntry { std::string name; - std::string entry; // Assuming ZipEntry is a string in your original code + std::string entry; uint32_t crc_hash; void* data; unsigned long long size; diff --git a/header/uMod_Defines.h b/header/uMod_Defines.h index 4bda2d5..7051c84 100644 --- a/header/uMod_Defines.h +++ b/header/uMod_Defines.h @@ -5,7 +5,11 @@ #ifdef LOG_MESSAGE extern FILE* gl_File; +#ifdef _DEBUG #define Message(...) { printf(__VA_ARGS__); } +#else +#define Message(...) +#endif #ifdef HOOK_INJECTION #define OpenMessage(...) {if (fopen_s( &gl_File, "uMod_log.txt", "wt")) gl_File=NULL; else fprintf( gl_File, "HI R40: 0000000\n");} #endif diff --git a/header/uMod_TextureServer.h b/header/uMod_TextureServer.h index ea5a98a..0038478 100644 --- a/header/uMod_TextureServer.h +++ b/header/uMod_TextureServer.h @@ -24,7 +24,7 @@ public: int AddClient(uMod_TextureClient* client, TextureFileStruct** update, int* number); // called from a Client int RemoveClient(uMod_TextureClient* client); // called from a Client - int MainLoop(); // is executed in a server thread + int Initialize(); // is executed in a server thread // following functions are only public for testing purpose !! diff --git a/source/uMod_DX9_dll.cpp b/source/uMod_DX9_dll.cpp index 7b05dd8..0fd8fc3 100644 --- a/source/uMod_DX9_dll.cpp +++ b/source/uMod_DX9_dll.cpp @@ -1,21 +1,13 @@ #include "uMod_DX9_dll.h" -#include "uMod_Main.h" -#include -#include -#pragma comment(lib, "Psapi.lib") -//#include "detours.h" -//#include "detourxs/detourxs/detourxs.h" -/* -#include "detourxs/detourxs/ADE32.cpp" -#include "detourxs/detourxs/detourxs.cpp" -*/ -/* - * global variable which are not linked external - */ +#include +#include +#include "uMod_Main.h" +#include + HINSTANCE gl_hOriginalDll = nullptr; HINSTANCE gl_hThisInstance = nullptr; -uMod_TextureServer* gl_TextureServer = nullptr; +std::unique_ptr gl_TextureServer = nullptr; HANDLE gl_ServerThread = nullptr; using Direct3DCreate9_type = IDirect3D9* (APIENTRY*)(UINT); @@ -74,7 +66,7 @@ DWORD WINAPI ServerThread(LPVOID lpParam) { UNREFERENCED_PARAMETER(lpParam); if (gl_TextureServer != nullptr) { - gl_TextureServer->MainLoop(); //This is and endless mainloop, it sleep till something is written into the pipe. + gl_TextureServer->Initialize(); //This is and endless mainloop, it sleep till something is written into the pipe. } return 0; } @@ -89,31 +81,26 @@ void InitInstance(HINSTANCE hModule) { OpenMessage(); Message("InitInstance: %p\n", hModule); - char uMod[MAX_PATH]; - for (auto i = 0; i < MAX_PATH; i++) { - uMod[i] = 0; - } + std::array uMod{}; - GetModuleFileNameA(hModule, uMod, MAX_PATH); + GetModuleFileNameA(hModule, uMod.data(), MAX_PATH); Message("InitInstance: %s\n", uMod); - gl_TextureServer = new uMod_TextureServer(game, uMod); //create the server which listen on the pipe and prepare the update for the texture clients + gl_TextureServer = std::make_unique(game, uMod.data()); //create the server which listen on the pipe and prepare the update for the texture clients LoadOriginalDll(); if (gl_hOriginalDll) { - Direct3DCreate9_fn = (Direct3DCreate9_type)GetProcAddress(gl_hOriginalDll, "Direct3DCreate9"); + Direct3DCreate9_fn = reinterpret_cast(GetProcAddress(gl_hOriginalDll, "Direct3DCreate9")); if (Direct3DCreate9_fn != nullptr) { Message("Detour: Direct3DCreate9\n"); Direct3DCreate9_fn = static_cast(DetourFunc((BYTE*)Direct3DCreate9_fn, (BYTE*)uMod_Direct3DCreate9, 5)); } - Direct3DCreate9Ex_fn = (Direct3DCreate9Ex_type)GetProcAddress(gl_hOriginalDll, "Direct3DCreate9Ex"); + Direct3DCreate9Ex_fn = reinterpret_cast(GetProcAddress(gl_hOriginalDll, "Direct3DCreate9Ex")); if (Direct3DCreate9Ex_fn != nullptr) { Message("Detour: Direct3DCreate9Ex\n"); Direct3DCreate9Ex_fn = static_cast(DetourFunc((BYTE*)Direct3DCreate9Ex_fn, (BYTE*)uMod_Direct3DCreate9Ex, 7)); } } - gl_ServerThread = CreateThread(nullptr, 0, ServerThread, nullptr, 0, nullptr); //creating a thread for the mainloop - if (gl_ServerThread == nullptr) { Message("InitInstance: Serverthread not started\n"); } - + gl_TextureServer->Initialize(); } } @@ -194,15 +181,6 @@ void LoadOriginalDll() void ExitInstance() { - if (gl_ServerThread != nullptr) { - CloseHandle(gl_ServerThread); // kill the server thread - gl_ServerThread = nullptr; - } - if (gl_TextureServer != nullptr) { - delete gl_TextureServer; //delete the texture server - gl_TextureServer = nullptr; - } - // Release the system's d3d9.dll if (gl_hOriginalDll != nullptr) { FreeLibrary(gl_hOriginalDll); @@ -255,7 +233,7 @@ IDirect3D9* APIENTRY uMod_Direct3DCreate9(UINT SDKVersion) } uMod_IDirect3D9* pIDirect3D9; if (pIDirect3D9_orig) { - pIDirect3D9 = new uMod_IDirect3D9(pIDirect3D9_orig, gl_TextureServer); //creating our uMod_IDirect3D9 object + pIDirect3D9 = new uMod_IDirect3D9(pIDirect3D9_orig, gl_TextureServer.get()); //creating our uMod_IDirect3D9 object } // we detour again @@ -305,7 +283,7 @@ HRESULT APIENTRY uMod_Direct3DCreate9Ex(UINT SDKVersion, IDirect3D9Ex** ppD3D) uMod_IDirect3D9Ex* pIDirect3D9Ex; if (pIDirect3D9Ex_orig) { - pIDirect3D9Ex = new uMod_IDirect3D9Ex(pIDirect3D9Ex_orig, gl_TextureServer); //creating our uMod_IDirect3D9 object + pIDirect3D9Ex = new uMod_IDirect3D9Ex(pIDirect3D9Ex_orig, gl_TextureServer.get()); //creating our uMod_IDirect3D9 object } // we detour again diff --git a/source/uMod_TextureServer.cpp b/source/uMod_TextureServer.cpp index fdaf50f..5144a22 100644 --- a/source/uMod_TextureServer.cpp +++ b/source/uMod_TextureServer.cpp @@ -497,42 +497,39 @@ int uMod_TextureServer::UnlockMutex() void uMod_TextureServer::LoadModsFromFile(char* source) { - Message("MainLoop: searching in %s\n", source); - // Attempt to open the file - FILE* file = fopen(source, "r"); - if (file) { - Message("MainLoop: found modlist.txt. Reading\n"); - // Read each line from the file - char line[MAX_PATH]; - while (fgets(line, sizeof(line), file) != nullptr) { - Message("MainLoop: loading file %s\n", line); - for (char& i : line) { - if (i == '\n') { - i = 0; - } - } + Message("Initialize: searching in %s\n", source); - const auto file = new gMod_FileLoader(line); - const auto entries = file->Load(); + std::ifstream file(source); + if (file.is_open()) { + Message("Initialize: found modlist.txt. Reading\n"); + std::string line; + while (std::getline(file, line)) { + Message("Initialize: loading file %s\n", line.c_str()); + + // Remove newline character + line.erase(std::ranges::remove(line, '\n').begin(), line.end()); + + auto file_loader = gMod_FileLoader(line); + const auto entries = file_loader.Load(); if (!entries.empty()) { - Message("MainLoop: Texture count %d %s\n", entries.size(), line); - for (const auto& entry : entries) { - //AddFile(static_cast(entry.Data), static_cast(entry.Size), ) + Message("Initialize: Texture count %zu %s\n", entries.size(), line.c_str()); + for (const auto& tpf_entry : entries) { + AddFile(static_cast(tpf_entry.data), static_cast(tpf_entry.size), tpf_entry.crc_hash, false); } PropagateUpdate(nullptr); } else { - Message("MainLoop: Failed to load any textures for %s\n", line); + Message("Initialize: Failed to load any textures for %s\n", line.c_str()); } } } } -int uMod_TextureServer::MainLoop() // run as a separated thread +int uMod_TextureServer::Initialize() { - Message("MainLoop: begin\n"); - Message("MainLoop: searching for modlist.txt\n"); + Message("Initialize: begin\n"); + Message("Initialize: searching for modlist.txt\n"); char gwpath[MAX_PATH]; GetModuleFileName(GetModuleHandle(nullptr), gwpath, MAX_PATH); //ask for name and path of this executable char* last_backslash = strrchr(gwpath, '\\'); @@ -556,7 +553,7 @@ int uMod_TextureServer::MainLoop() // run as a separated thread strcat(umodpath, "\\modlist.txt"); LoadModsFromFile(umodpath); - Message("MainLoop: end\n"); + Message("Initialize: end\n"); return RETURN_OK; }