Skip to content

Commit

Permalink
add mutex for overload
Browse files Browse the repository at this point in the history
- update submodules
  • Loading branch information
ThirteenAG committed Jan 7, 2025
1 parent f999fe9 commit 3ec3632
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion external/d3d8to9
2 changes: 1 addition & 1 deletion external/injector
Submodule injector updated 3 files
+1 −1 kananlib
+1 −1 minhook
+1 −1 safetyhook
31 changes: 31 additions & 0 deletions source/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
extern "C" Direct3D8* WINAPI Direct3DCreate8(UINT SDKVersion);
#endif

extern "C" IMAGE_DOS_HEADER __ImageBase;
typedef NTSTATUS(NTAPI* LdrAddRefDll_t)(ULONG, HMODULE);

bool WINAPI IsUltimateASILoader()
{
return true;
Expand Down Expand Up @@ -1561,6 +1564,23 @@ namespace OverloadFromFolder

void HookAPIForOverload()
{
constexpr auto mutexName = L"Ultimate-ASI-Loader-OverloadFromFolder";

auto hMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, mutexName);
if (hMutex)
{
CloseHandle(hMutex);
return;
}

hMutex = CreateMutexW(nullptr, TRUE, mutexName);
if (!hMutex || GetLastError() == ERROR_ALREADY_EXISTS)
{
if (hMutex)
CloseHandle(hMutex);
return;
}

mhLoadLibraryExA = std::make_unique<FunctionHookMinHook>((uintptr_t)LoadLibraryExA, (uintptr_t)shCustomLoadLibraryExA);
mhLoadLibraryExW = std::make_unique<FunctionHookMinHook>((uintptr_t)LoadLibraryExW, (uintptr_t)shCustomLoadLibraryExW);
mhCreateFileA = std::make_unique<FunctionHookMinHook>((uintptr_t)CreateFileA, (uintptr_t)shCustomCreateFileA);
Expand Down Expand Up @@ -1590,6 +1610,17 @@ namespace OverloadFromFolder
mhFindNextFileW->create();
mhFindFirstFileExA->create();
mhFindFirstFileExW->create();

// increase the ref count in case this dll is unloaded before the game exit
auto hNtdll = GetModuleHandleW(L"ntdll.dll");
if (hNtdll)
{
auto pLdrAddRefDll = (LdrAddRefDll_t)GetProcAddress(hNtdll, "LdrAddRefDll");
if (pLdrAddRefDll)
{
pLdrAddRefDll(0, (HMODULE)&__ImageBase);
}
}
}
}

Expand Down

0 comments on commit 3ec3632

Please sign in to comment.