From b7914dfb59a1d5fe56ecf58ba836e11eb448e9ce Mon Sep 17 00:00:00 2001 From: Silent Date: Sat, 11 May 2024 12:42:28 +0200 Subject: [PATCH] Simplify moonphases loading and DLL module management __ImageBase is a special linker symbol we can use here. --- SilentPatch/Common.cpp | 2 +- SilentPatchIII/SilentPatchIII.cpp | 9 +++---- SilentPatchSA/PlayerInfoSA.cpp | 6 ----- SilentPatchSA/PlayerInfoSA.h | 2 -- SilentPatchSA/SilentPatchSA.cpp | 40 +++++++++++++++++-------------- SilentPatchVC/SilentPatchVC.cpp | 8 +++---- 6 files changed, 29 insertions(+), 38 deletions(-) diff --git a/SilentPatch/Common.cpp b/SilentPatch/Common.cpp index a481370..f1741ee 100644 --- a/SilentPatch/Common.cpp +++ b/SilentPatch/Common.cpp @@ -183,7 +183,7 @@ namespace DelayedPatches if ( Func != nullptr ) Func(); // So we don't have to revert patches HMODULE hDummyHandle; - GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, (LPCTSTR)&Inject_MSS, &hDummyHandle); + GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, TEXT(""), &hDummyHandle); } } const auto pInjectMSS = Inject_MSS; diff --git a/SilentPatchIII/SilentPatchIII.cpp b/SilentPatchIII/SilentPatchIII.cpp index 9700305..d35fa0c 100644 --- a/SilentPatchIII/SilentPatchIII.cpp +++ b/SilentPatchIII/SilentPatchIII.cpp @@ -23,6 +23,8 @@ #pragma comment(lib, "shlwapi.lib") +EXTERN_C IMAGE_DOS_HEADER __ImageBase; + struct PsGlobalType { HWND window; @@ -53,9 +55,6 @@ struct RsGlobalType DebugMenuAPI gDebugMenuAPI; -static HMODULE hDLLModule; - - static void (*DrawRect)(const CRect&,const CRGBA&); static int* InstantHitsFiredByPlayer; static const void* HeadlightsFix_JumpBack; @@ -961,7 +960,7 @@ void InjectDelayedPatches_III_Common() // Obtain a path to the ASI wchar_t wcModulePath[MAX_PATH]; - GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension + GetModuleFileNameW(reinterpret_cast(&__ImageBase), wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); const bool hasDebugMenu = DebugMenuLoad(); @@ -1693,8 +1692,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) if ( fdwReason == DLL_PROCESS_ATTACH ) { - hDLLModule = hinstDLL; - const auto [width, height] = GetDesktopResolution(); sprintf_s(aNoDesktopMode, "Cannot find %ux%ux32 video mode", width, height); diff --git a/SilentPatchSA/PlayerInfoSA.cpp b/SilentPatchSA/PlayerInfoSA.cpp index c452821..292e4ca 100644 --- a/SilentPatchSA/PlayerInfoSA.cpp +++ b/SilentPatchSA/PlayerInfoSA.cpp @@ -38,9 +38,3 @@ CVehicle* FindPlayerVehicle( int playerID, bool withRC ) } return vehicle; } - -CPlayerInfo& CPlayerInfo::operator=( const CPlayerInfo& rhs ) -{ - memcpy( this, &rhs, sizeof(*this) ); - return *this; -} diff --git a/SilentPatchSA/PlayerInfoSA.h b/SilentPatchSA/PlayerInfoSA.h index 482ca01..dc1fb4a 100644 --- a/SilentPatchSA/PlayerInfoSA.h +++ b/SilentPatchSA/PlayerInfoSA.h @@ -12,8 +12,6 @@ private: uint8_t __pad[0xDC]; public: - CPlayerInfo& operator=( const CPlayerInfo& rhs ); - CPlayerPed* GetPlayerPed() const { return m_pPed; } CVehicle* GetControlledVehicle() const { return m_pControlledVehicle; } }; diff --git a/SilentPatchSA/SilentPatchSA.cpp b/SilentPatchSA/SilentPatchSA.cpp index f4eb998..d8dcb26 100644 --- a/SilentPatchSA/SilentPatchSA.cpp +++ b/SilentPatchSA/SilentPatchSA.cpp @@ -33,6 +33,8 @@ #include "debugmenu_public.h" #include "resource.h" +EXTERN_C IMAGE_DOS_HEADER __ImageBase; + // ============= Mod compatibility stuff ============= namespace ModCompat @@ -350,10 +352,8 @@ uint32_t& bDrawCrossHair = **AddressByVersion(0x58E7BF + 2, {"83 DebugMenuAPI gDebugMenuAPI; - // Custom variables static float fSunFarClip; -static HMODULE hDLLModule; static struct { char Extension[8]; @@ -1914,19 +1914,25 @@ namespace MoonphasesFix static void RenderOneXLUSprite_MoonPhases( float arg1, float arg2, float arg3, float arg4, float arg5, uint8_t red, uint8_t green, uint8_t blue, int16_t mult, float arg10, uint8_t alpha, uint8_t arg12, uint8_t arg13 ) { static RwTexture* gpMoonMask = [] () { - if ( GetFileAttributesW(L"lunar.png") != INVALID_FILE_ATTRIBUTES ) + + // load from file + RwTexture* mask = CPNGFile::ReadFromFile("lunar.png"); + if (mask == nullptr) { - // load from file - return CPNGFile::ReadFromFile("lunar.png"); + const HMODULE module = reinterpret_cast(&__ImageBase); + + // Load from memory + HRSRC resource = FindResource(module, MAKEINTRESOURCE(IDB_LUNAR64), RT_RCDATA); + if (resource != nullptr) + { + HGLOBAL loadedResource = LoadResource(module, resource); + if (loadedResource != nullptr) + { + mask = CPNGFile::ReadFromMemory(LockResource(loadedResource), SizeofResource(module, resource)); + } + } } - - // Load from memory - HRSRC resource = FindResource(hDLLModule, MAKEINTRESOURCE(IDB_LUNAR64), RT_RCDATA); - assert( resource != nullptr ); - - void* pMoonMask = LockResource( LoadResource(hDLLModule, resource) ); - - return CPNGFile::ReadFromMemory(pMoonMask, SizeofResource(hDLLModule, resource)); + return mask; } (); RwScopedRenderState alphaTest( rwRENDERSTATEALPHATESTFUNCTION ); @@ -3472,7 +3478,7 @@ BOOL InjectDelayedPatches_10() // Obtain a path to the ASI wchar_t wcModulePath[MAX_PATH]; - GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension + GetModuleFileNameW(reinterpret_cast(&__ImageBase), wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); const ModuleList moduleList; @@ -3937,7 +3943,7 @@ BOOL InjectDelayedPatches_11() // Obtain a path to the ASI wchar_t wcModulePath[MAX_PATH]; - GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension + GetModuleFileNameW(reinterpret_cast(&__ImageBase), wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); const ModuleList moduleList; @@ -4112,7 +4118,7 @@ BOOL InjectDelayedPatches_Steam() // Obtain a path to the ASI wchar_t wcModulePath[MAX_PATH]; - GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension + GetModuleFileNameW(reinterpret_cast(&__ImageBase), wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); const ModuleList moduleList; @@ -7134,8 +7140,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) if ( fdwReason == DLL_PROCESS_ATTACH ) { - hDLLModule = hinstDLL; - const HINSTANCE hInstance = GetModuleHandle( nullptr ); std::unique_ptr Protect = ScopedUnprotect::UnprotectSectionOrFullModule( hInstance, ".text" ); ScopedUnprotect::Section Protect2( hInstance, ".rdata" ); diff --git a/SilentPatchVC/SilentPatchVC.cpp b/SilentPatchVC/SilentPatchVC.cpp index cc57c74..85fc1a9 100644 --- a/SilentPatchVC/SilentPatchVC.cpp +++ b/SilentPatchVC/SilentPatchVC.cpp @@ -25,6 +25,8 @@ #pragma comment(lib, "shlwapi.lib") +EXTERN_C IMAGE_DOS_HEADER __ImageBase; + // ============= Mod compatibility stuff ============= namespace ModCompat @@ -57,8 +59,6 @@ struct RsGlobalType DebugMenuAPI gDebugMenuAPI; -static HMODULE hDLLModule; - static RsGlobalType* RsGlobal; static const void* SubtitlesShadowFix_JumpBack; @@ -893,7 +893,7 @@ void InjectDelayedPatches_VC_Common() // Obtain a path to the ASI wchar_t wcModulePath[MAX_PATH]; - GetModuleFileNameW(hDLLModule, wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension + GetModuleFileNameW(reinterpret_cast(&__ImageBase), wcModulePath, _countof(wcModulePath) - 3); // Minus max required space for extension PathRenameExtensionW(wcModulePath, L".ini"); const bool hasDebugMenu = DebugMenuLoad(); @@ -1572,8 +1572,6 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) if ( fdwReason == DLL_PROCESS_ATTACH ) { - hDLLModule = hinstDLL; - const auto [width, height] = GetDesktopResolution(); sprintf_s(aNoDesktopMode, "Cannot find %ux%ux32 video mode", width, height);