Simplify moonphases loading and DLL module management

__ImageBase is a special linker symbol we can use here.
This commit is contained in:
Silent 2024-05-11 12:42:28 +02:00
parent bc75ee4585
commit b7914dfb59
No known key found for this signature in database
GPG key ID: AE53149BB0C45AF1
6 changed files with 29 additions and 38 deletions

View file

@ -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;

View file

@ -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<HMODULE>(&__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);

View file

@ -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;
}

View file

@ -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; }
};

View file

@ -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<uint32_t**>(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
return CPNGFile::ReadFromFile("lunar.png");
}
RwTexture* mask = CPNGFile::ReadFromFile("lunar.png");
if (mask == nullptr)
{
const HMODULE module = reinterpret_cast<HMODULE>(&__ImageBase);
// 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));
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));
}
}
}
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<HMODULE>(&__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<HMODULE>(&__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<HMODULE>(&__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<ScopedUnprotect::Unprotect> Protect = ScopedUnprotect::UnprotectSectionOrFullModule( hInstance, ".text" );
ScopedUnprotect::Section Protect2( hInstance, ".rdata" );

View file

@ -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<HMODULE>(&__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);