1
1
#include < Windows.h>
2
2
#include < array>
3
+ #include < span>
3
4
4
5
/*
5
6
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
27
28
ScrapMechanic.exe.text + 416316 : 75 0C - jne ScrapMechanic.exe.text + 416324
28
29
*/
29
30
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
30
39
static constexpr std::uintptr_t offset = 0x4162F9 + 0x1000 ;
31
40
static constexpr std::array<std::uint8_t , 6 > originalBytes{ 0x0F , 0x86 , 0x3E , 0x01 , 0x00 , 0x00 };
32
41
static constexpr std::array<std::uint8_t , 6 > replacedBytes{ 0xE9 , 0x3F , 0x01 , 0x00 , 0x00 , 0x90 };
42
+ #else
43
+ # error Unsupported game version
44
+ #endif
33
45
34
- BOOL APIENTRY DllMain ( HMODULE hModule,
35
- DWORD ul_reason_for_call,
36
- LPVOID lpReserved
37
- )
46
+ static void * GetFinalAddress ()
38
47
{
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
+ }
43
50
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 ;
47
58
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);
57
63
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
+ }
61
66
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);
67
76
68
- return TRUE ;
77
+ return TRUE ;
69
78
}
0 commit comments