Counter-based unloading logic

This commit is contained in:
mkrsym1 2023-06-21 14:25:22 +03:00
parent 326ccd188e
commit 5421487212
3 changed files with 25 additions and 2 deletions

View File

@ -0,0 +1,4 @@
#pragma once
void unload_ctr_inc();
void unload_ctr_dec();

View File

@ -4,4 +4,4 @@
#include <game.h>
void tp6_setup_patcher(struct game_data *game, HMODULE thisModule, HMODULE baseModule);
void tp6_setup_patcher(struct game_data *game, HMODULE baseModule);

View File

@ -6,12 +6,31 @@
#include <tp6.h>
#include <utils.h>
#include <main.h>
HMODULE this_module;
size_t unload_ctr = 0;
void unload_ctr_inc() {
unload_ctr++;
}
void unload_ctr_dec() {
unload_ctr--;
if (unload_ctr == 0) {
void *pFreeLibrary = GetProcAddress(GetModuleHandleA("kernel32.dll"), "FreeLibrary");
CreateThread(NULL, 0, pFreeLibrary, this_module, 0, NULL);
}
}
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) {
// Only listen to attach
if (reason != DLL_PROCESS_ATTACH) {
return TRUE;
}
this_module = instance;
// Dynamically link functions from ntdll
ntdll_link();
@ -27,7 +46,7 @@ BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) {
ace_load_driver_module();
// ...magic
tp6_setup_patcher(&game, instance, baseModule);
tp6_setup_patcher(&game, baseModule);
// Load the UnityPlayer module and invoke the callback
HMODULE unityModule = LoadLibraryA("UnityPlayer.dll");