c++ify a little

This commit is contained in:
DubbleClick
2023-11-15 19:11:07 +01:00
parent 7c471a4ceb
commit 75a5427287
6 changed files with 43 additions and 63 deletions
+1
View File
@@ -72,6 +72,7 @@ target_link_libraries(gMod PRIVATE
${D3DX9_LIB}
${DXGUID_LIB}
libzippp
psapi
)
target_link_options(gMod PRIVATE
"$<$<CONFIG:DEBUG>:/NODEFAULTLIB:LIBCMT>"
+1 -1
View File
@@ -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;
+4
View File
@@ -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
+1 -1
View File
@@ -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 !!
+15 -37
View File
@@ -1,21 +1,13 @@
#include "uMod_DX9_dll.h"
#include "uMod_Main.h"
#include <Windows.h>
#include <Psapi.h>
#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 <array>
#include <Windows.h>
#include "uMod_Main.h"
#include <Psapi.h>
HINSTANCE gl_hOriginalDll = nullptr;
HINSTANCE gl_hThisInstance = nullptr;
uMod_TextureServer* gl_TextureServer = nullptr;
std::unique_ptr<uMod_TextureServer> 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<char, MAX_PATH> 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<uMod_TextureServer>(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<Direct3DCreate9_type>(GetProcAddress(gl_hOriginalDll, "Direct3DCreate9"));
if (Direct3DCreate9_fn != nullptr) {
Message("Detour: Direct3DCreate9\n");
Direct3DCreate9_fn = static_cast<Direct3DCreate9_type>(DetourFunc((BYTE*)Direct3DCreate9_fn, (BYTE*)uMod_Direct3DCreate9, 5));
}
Direct3DCreate9Ex_fn = (Direct3DCreate9Ex_type)GetProcAddress(gl_hOriginalDll, "Direct3DCreate9Ex");
Direct3DCreate9Ex_fn = reinterpret_cast<Direct3DCreate9Ex_type>(GetProcAddress(gl_hOriginalDll, "Direct3DCreate9Ex"));
if (Direct3DCreate9Ex_fn != nullptr) {
Message("Detour: Direct3DCreate9Ex\n");
Direct3DCreate9Ex_fn = static_cast<Direct3DCreate9Ex_type>(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
+21 -24
View File
@@ -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<char*>(entry.Data), static_cast<unsigned int>(entry.Size), )
Message("Initialize: Texture count %zu %s\n", entries.size(), line.c_str());
for (const auto& tpf_entry : entries) {
AddFile(static_cast<char*>(tpf_entry.data), static_cast<unsigned int>(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;
}