From 3ebef09305eb38e8e71ee4759bf3fa2ffc1d5e0d Mon Sep 17 00:00:00 2001 From: Rupert Walker Date: Sun, 21 Jan 2024 16:28:33 +0000 Subject: [PATCH 1/4] Remove modifications of 0x86AEC, 0x84018, 0xD3D6E and 0x58F46 from CrashCatcher.cpp --- plugins/crash_catcher/CrashCatcher.cpp | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/plugins/crash_catcher/CrashCatcher.cpp b/plugins/crash_catcher/CrashCatcher.cpp index dfc22b1cc..4443e2e58 100644 --- a/plugins/crash_catcher/CrashCatcher.cpp +++ b/plugins/crash_catcher/CrashCatcher.cpp @@ -408,17 +408,6 @@ namespace Plugins::CrashCatcher global->bPatchInstalled = true; global->hModServerAC = GetModuleHandle("server.dll"); - if (global->hModServerAC) - { - // Patch the NPC visibility distance in MP to 6.5km (default - // is 2.5km) - float fDistance = 6500.0f * 6500.0f; - WriteProcMem((char*)global->hModServerAC + 0x86AEC, &fDistance, 4); - - FARPROC fpHook = (FARPROC)Cb_GetRoot; - ReadProcMem((char*)global->hModServerAC + 0x84018, &fpOldGetRootProc, 4); - WriteProcMem((char*)global->hModServerAC + 0x84018, &fpHook, 4); - } // Patch the time functions to work around bugs on multiprocessor // and virtual machines. @@ -517,12 +506,6 @@ namespace Plugins::CrashCatcher PatchCallAddr((char*)global->hModContentAC, 0xC702A, (char*)Cb_CrashProc6F671A0); PatchCallAddr((char*)global->hModContentAC, 0xC713B, (char*)Cb_CrashProc6F671A0); PatchCallAddr((char*)global->hModContentAC, 0xC7180, (char*)Cb_CrashProc6F671A0); - - // Patch the NPC persist distance in MP to 6.5km and patch the - // max spawn distance to 6.5km - float fDistance = 6500; - WriteProcMem((char*)global->hModContentAC + 0xD3D6E, &fDistance, 4); - WriteProcMem((char*)global->hModContentAC + 0x58F46, &fDistance, 4); } } } From e56e02b99020be6a0b7e8737f7a14c25f6d32652 Mon Sep 17 00:00:00 2001 From: Rupert Walker Date: Sun, 21 Jan 2024 16:36:31 +0000 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08dedfde4..7f0baf4fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 4.0.17 +- Remove modifications of 0x86AEC, 0x84018, 0xD3D6E and 0x58F46 in server.dll from crash catcher. These offsets were hardcoding several values that are often changed by mods manually relating to NPC spawn and scanner range. + ## 4.0.16 - Removed a PrintUserCmd from Autobuy that was printing on every repair. From 99983ae7eaf84f851097c26640cc513b1d09b1c0 Mon Sep 17 00:00:00 2001 From: Rupert Walker Date: Mon, 22 Jan 2024 13:25:27 +0000 Subject: [PATCH 3/4] Add the offsets back and into a config for CrashCatcher.cpp --- CHANGELOG.md | 2 +- plugins/crash_catcher/CrashCatcher.cpp | 37 +++++++++++++++++++++++++- plugins/crash_catcher/CrashCatcher.h | 24 ++++++++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f0baf4fb..7fd479cda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Changelog ## 4.0.17 -- Remove modifications of 0x86AEC, 0x84018, 0xD3D6E and 0x58F46 in server.dll from crash catcher. These offsets were hardcoding several values that are often changed by mods manually relating to NPC spawn and scanner range. +- Provided configs for 0x86AEC, 0x84018, 0xD3D6E and 0x58F46 in server.dll in crash catcher. These offsets were hardcoding several values that are often changed by mods manually relating to NPC spawn and scanner range. 0 values disables each respective hook. ## 4.0.16 - Removed a PrintUserCmd from Autobuy that was printing on every repair. diff --git a/plugins/crash_catcher/CrashCatcher.cpp b/plugins/crash_catcher/CrashCatcher.cpp index 4443e2e58..3185ecfd0 100644 --- a/plugins/crash_catcher/CrashCatcher.cpp +++ b/plugins/crash_catcher/CrashCatcher.cpp @@ -27,6 +27,12 @@ namespace Plugins::CrashCatcher { const std::unique_ptr global = std::make_unique(); + void LoadSettings() + { + auto config = Serializer::JsonToObject(); + global->config = std::make_unique(config); + } + /** @ingroup CrashCatcher * @brief Need to use our own logging functions since the nature of this plugin isn't compatible with FLHook's standard logging functionality. */ @@ -406,8 +412,20 @@ namespace Plugins::CrashCatcher if (!global->bPatchInstalled) { global->bPatchInstalled = true; + LoadSettings(); global->hModServerAC = GetModuleHandle("server.dll"); + if (global->hModServerAC && global->config->npcVisibilityDistance > 0.f) + { + // Patch the NPC visibility distance in MP to 6.5km (default + // is 2.5km) + float visDistance = global->config->npcVisibilityDistance * global->config->npcVisibilityDistance; + WriteProcMem((char*)global->hModServerAC + 0x86AEC, &visDistance, 4); + + FARPROC fpHook = (FARPROC)Cb_GetRoot; + ReadProcMem((char*)global->hModServerAC + 0x84018, &fpOldGetRootProc, 4); + WriteProcMem((char*)global->hModServerAC + 0x84018, &fpHook, 4); + } // Patch the time functions to work around bugs on multiprocessor // and virtual machines. @@ -506,6 +524,21 @@ namespace Plugins::CrashCatcher PatchCallAddr((char*)global->hModContentAC, 0xC702A, (char*)Cb_CrashProc6F671A0); PatchCallAddr((char*)global->hModContentAC, 0xC713B, (char*)Cb_CrashProc6F671A0); PatchCallAddr((char*)global->hModContentAC, 0xC7180, (char*)Cb_CrashProc6F671A0); + + // Patch the NPC persist distance in MP to 6.5km and patch the + // max spawn distance to 6.5km + auto persistDistance = global->config->npcPersistDistance; + auto spawnDistance = global->config->npcSpawnDistance; + + if (global->config->npcPersistDistance > 0.f) + { + WriteProcMem((char*)global->hModContentAC + 0xD3D6E, &persistDistance, 4); + } + + if (global->config->npcSpawnDistance > 0.f) + { + WriteProcMem((char*)global->hModContentAC + 0x58F46, &spawnDistance, 4); + } } } } @@ -587,6 +620,8 @@ namespace Plugins::CrashCatcher using namespace Plugins::CrashCatcher; +REFL_AUTO(type(Config), field(npcVisibilityDistance), field(npcPersistDistance), field(npcSpawnDistance)) + // Do things when the dll is loaded BOOL WINAPI DllMain([[maybe_unused]] HINSTANCE hinstDLL, DWORD fdwReason, [[maybe_unused]] LPVOID lpvReserved) { @@ -609,7 +644,7 @@ extern "C" EXPORT void ExportPluginInfo(PluginInfo* pi) pi->returnCode(&global->returncode); pi->versionMajor(PluginMajorVersion::VERSION_04); pi->versionMinor(PluginMinorVersion::VERSION_00); - pi->emplaceHook(HookedCall::FLHook__LoadSettings, &Init, HookStep::After); + pi->emplaceHook(HookedCall::FLHook__LoadSettings, &LoadSettings, HookStep::After); pi->emplaceHook(HookedCall::IServerImpl__RequestBestPath, &RequestBestPath); pi->emplaceHook(HookedCall::IServerImpl__TractorObjects, &TractorObjects); pi->emplaceHook(HookedCall::IServerImpl__JettisonCargo, &JettisonCargo); diff --git a/plugins/crash_catcher/CrashCatcher.h b/plugins/crash_catcher/CrashCatcher.h index 6381634ca..836ead7f6 100644 --- a/plugins/crash_catcher/CrashCatcher.h +++ b/plugins/crash_catcher/CrashCatcher.h @@ -5,6 +5,20 @@ namespace Plugins::CrashCatcher { + + //! Config data for this plugin + struct Config final : Reflectable + { + std::string File() override { return "config/crashcatcher.json"; } + + //! Sets the maximum distance at which NPCs become visible as per scanner settings, a value of 0 will disable this override. + float npcVisibilityDistance = 6500.f; + //! Sets the distnace at which NPCs will despawn, a value of 0 will disable this override. + float npcPersistDistance = 6500.f; + //! Sets the distance at which NPCs will spawn, a value of 0 will disable this override. + float npcSpawnDistance = 6500.f; + }; + //! Global data for this plugin struct Global final { @@ -18,12 +32,14 @@ namespace Plugins::CrashCatcher HMODULE hModContentAC; std::map mapSaveTimes; + //! Contains config data defined above + std::unique_ptr config = nullptr; }; - #define LOG_EXCEPTION_INTERNAL() \ - { \ - AddLogInternal("ERROR Exception in %s", __FUNCTION__); \ - AddExceptionInfoLog(); \ +#define LOG_EXCEPTION_INTERNAL() \ + { \ + AddLogInternal("ERROR Exception in %s", __FUNCTION__); \ + AddExceptionInfoLog(); \ }; } // namespace Plugins::CrashCatcher From 22e9a0f75c59f209742c5faf2e18c06341b561ca Mon Sep 17 00:00:00 2001 From: Rupert Walker Date: Mon, 22 Jan 2024 14:49:51 +0000 Subject: [PATCH 4/4] Fix settings and clean up variables --- plugins/crash_catcher/CrashCatcher.cpp | 27 ++++++++++---------------- plugins/crash_catcher/CrashCatcher.h | 2 -- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/plugins/crash_catcher/CrashCatcher.cpp b/plugins/crash_catcher/CrashCatcher.cpp index 3185ecfd0..7390714b8 100644 --- a/plugins/crash_catcher/CrashCatcher.cpp +++ b/plugins/crash_catcher/CrashCatcher.cpp @@ -27,12 +27,6 @@ namespace Plugins::CrashCatcher { const std::unique_ptr global = std::make_unique(); - void LoadSettings() - { - auto config = Serializer::JsonToObject(); - global->config = std::make_unique(config); - } - /** @ingroup CrashCatcher * @brief Need to use our own logging functions since the nature of this plugin isn't compatible with FLHook's standard logging functionality. */ @@ -405,21 +399,22 @@ namespace Plugins::CrashCatcher /** @ingroup CrashCatcher * @brief Install hooks */ - void Init() + void LoadSettings() { try { if (!global->bPatchInstalled) { global->bPatchInstalled = true; - LoadSettings(); + // Load the settings + auto config = Serializer::JsonToObject(); global->hModServerAC = GetModuleHandle("server.dll"); - if (global->hModServerAC && global->config->npcVisibilityDistance > 0.f) + if (global->hModServerAC && config.npcVisibilityDistance > 0.f) { // Patch the NPC visibility distance in MP to 6.5km (default // is 2.5km) - float visDistance = global->config->npcVisibilityDistance * global->config->npcVisibilityDistance; + float visDistance = std::powf(config.npcVisibilityDistance, 2.f); WriteProcMem((char*)global->hModServerAC + 0x86AEC, &visDistance, 4); FARPROC fpHook = (FARPROC)Cb_GetRoot; @@ -527,17 +522,15 @@ namespace Plugins::CrashCatcher // Patch the NPC persist distance in MP to 6.5km and patch the // max spawn distance to 6.5km - auto persistDistance = global->config->npcPersistDistance; - auto spawnDistance = global->config->npcSpawnDistance; - if (global->config->npcPersistDistance > 0.f) + if (config.npcPersistDistance > 0.f) { - WriteProcMem((char*)global->hModContentAC + 0xD3D6E, &persistDistance, 4); + WriteProcMem((char*)global->hModContentAC + 0xD3D6E, &config.npcPersistDistance, 4); } - if (global->config->npcSpawnDistance > 0.f) + if (config.npcSpawnDistance > 0.f) { - WriteProcMem((char*)global->hModContentAC + 0x58F46, &spawnDistance, 4); + WriteProcMem((char*)global->hModContentAC + 0x58F46, &config.npcSpawnDistance, 4); } } } @@ -626,7 +619,7 @@ REFL_AUTO(type(Config), field(npcVisibilityDistance), field(npcPersistDistance), BOOL WINAPI DllMain([[maybe_unused]] HINSTANCE hinstDLL, DWORD fdwReason, [[maybe_unused]] LPVOID lpvReserved) { if (fdwReason == DLL_PROCESS_ATTACH && CoreGlobals::c()->flhookReady) - Init(); + LoadSettings(); if (fdwReason == DLL_PROCESS_DETACH) Shutdown(); diff --git a/plugins/crash_catcher/CrashCatcher.h b/plugins/crash_catcher/CrashCatcher.h index 836ead7f6..51a997398 100644 --- a/plugins/crash_catcher/CrashCatcher.h +++ b/plugins/crash_catcher/CrashCatcher.h @@ -32,8 +32,6 @@ namespace Plugins::CrashCatcher HMODULE hModContentAC; std::map mapSaveTimes; - //! Contains config data defined above - std::unique_ptr config = nullptr; }; #define LOG_EXCEPTION_INTERNAL() \