Skip to content

Commit 158a4d2

Browse files
committed
version 0.7.1.772 support
1 parent 177f9f3 commit 158a4d2

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

NetworkingFix/NetworkingFix.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<ConformanceMode>true</ConformanceMode>
8080
<PrecompiledHeader>Use</PrecompiledHeader>
8181
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
82+
<LanguageStandard>stdcpp20</LanguageStandard>
8283
</ClCompile>
8384
<Link>
8485
<SubSystem>Windows</SubSystem>
@@ -96,6 +97,7 @@
9697
<ConformanceMode>true</ConformanceMode>
9798
<PrecompiledHeader>Use</PrecompiledHeader>
9899
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
100+
<LanguageStandard>stdcpp20</LanguageStandard>
99101
</ClCompile>
100102
<Link>
101103
<SubSystem>Windows</SubSystem>

NetworkingFix/dllmain.cpp

+38-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <Windows.h>
22
#include <array>
3+
#include <span>
34

45
/*
56
ScrapMechanic.exe.text + 4162CE: 48 8D 55 88 - lea rdx, [rbp - 78]
@@ -27,43 +28,51 @@ ScrapMechanic.exe.text + 416314 : A8 F9 - test al, -07
2728
ScrapMechanic.exe.text + 416316 : 75 0C - jne ScrapMechanic.exe.text + 416324
2829
*/
2930

31+
// Set any version that is supported
32+
#define _SM_VERSION_NUM 071772
33+
34+
#if _SM_VERSION_NUM == 071772
35+
static constexpr std::uintptr_t offset = 0x4073BC;
36+
static constexpr std::array<std::uint8_t, 6> originalBytes{ 0x0F, 0x86, 0x3B, 0x01, 0x00, 0x00 };
37+
static constexpr std::array<std::uint8_t, 6> replacedBytes{ 0xE9, 0x3C, 0x01, 0x00, 0x00, 0x90 };
38+
#elif _SM_VERSION_NUM == 066
3039
static constexpr std::uintptr_t offset = 0x4162F9 + 0x1000;
3140
static constexpr std::array<std::uint8_t, 6> originalBytes{ 0x0F, 0x86, 0x3E, 0x01, 0x00, 0x00 };
3241
static constexpr std::array<std::uint8_t, 6> replacedBytes{ 0xE9, 0x3F, 0x01, 0x00, 0x00, 0x90 };
42+
#else
43+
# error Unsupported game version
44+
#endif
3345

34-
BOOL APIENTRY DllMain( HMODULE hModule,
35-
DWORD ul_reason_for_call,
36-
LPVOID lpReserved
37-
)
46+
static void* GetFinalAddress()
3847
{
39-
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
40-
{
41-
const std::uintptr_t scrapmechanic = uintptr_t(GetModuleHandle(NULL));
42-
void* dest = reinterpret_cast<void*>(scrapmechanic + offset);
48+
return reinterpret_cast<void*>(std::uintptr_t(GetModuleHandle(NULL)) + offset);
49+
}
4350

44-
// Failsafe to not patch game if original bytes differ
45-
if (memcmp(dest, originalBytes.data(), originalBytes.size()))
46-
return TRUE;
51+
static bool ApplyPatch(
52+
void* dest,
53+
const std::array<std::uint8_t, 6>& orig_bytes,
54+
const std::array<std::uint8_t, 6>& bytes)
55+
{
56+
// Failsafe to not patch game if original bytes differ
57+
if (memcmp(dest, orig_bytes.data(), orig_bytes.size())) return false;
4758

48-
DWORD old;
49-
VirtualProtect(dest, originalBytes.size(), PAGE_EXECUTE_READWRITE, &old);
50-
memcpy_s(dest, originalBytes.size(), replacedBytes.data(), replacedBytes.size());
51-
VirtualProtect(dest, originalBytes.size(), old, &old);
52-
}
53-
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
54-
{
55-
const std::uintptr_t scrapmechanic = uintptr_t(GetModuleHandle(NULL));
56-
void* dest = reinterpret_cast<void*>(scrapmechanic + offset);
59+
DWORD v_oldProt;
60+
VirtualProtect(dest, orig_bytes.size(), PAGE_EXECUTE_READWRITE, &v_oldProt);
61+
memcpy_s(dest, orig_bytes.size(), bytes.data(), bytes.size());
62+
VirtualProtect(dest, orig_bytes.size(), v_oldProt, &v_oldProt);
5763

58-
// Failsafe to not patch game if original bytes differ
59-
if (memcmp(dest, replacedBytes.data(), replacedBytes.size()))
60-
return TRUE;
64+
return true;
65+
}
6166

62-
DWORD old;
63-
VirtualProtect(dest, replacedBytes.size(), PAGE_EXECUTE_READWRITE, &old);
64-
memcpy_s(dest, replacedBytes.size(), originalBytes.data(), originalBytes.size());
65-
VirtualProtect(dest, replacedBytes.size(), old, &old);
66-
}
67+
BOOL APIENTRY DllMain(
68+
HMODULE hModule,
69+
DWORD ul_reason_for_call,
70+
LPVOID lpReserved)
71+
{
72+
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
73+
ApplyPatch(GetFinalAddress(), originalBytes, replacedBytes);
74+
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
75+
ApplyPatch(GetFinalAddress(), replacedBytes, originalBytes);
6776

68-
return TRUE;
77+
return TRUE;
6978
}

0 commit comments

Comments
 (0)