-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDllMain.cpp
More file actions
163 lines (130 loc) · 3.76 KB
/
DllMain.cpp
File metadata and controls
163 lines (130 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <algorithm>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <Windows.h>
#include "Armageddon.hpp"
///////////////////////////////////////////////////////////////
std::vector<WeaponEntry> g_customEntries;
///////////////////////////////////////////////////////////////
// Utility
HANDLE hstdin, hstdout;
FILE* pfstdin;
FILE* pfstdout;
static void initConsole()
{
AllocConsole();
freopen_s(&pfstdout, "CONOUT$", "w", stdout);
freopen_s(&pfstdin, "CONIN$", "r", stdin);
hstdin = GetStdHandle(STD_INPUT_HANDLE);
hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
}
static bool writeMemory(DWORD_PTR dwAddress, const void* cpvPatch, DWORD dwSize)
{
DWORD dwProtect;
if (VirtualProtect((void*)dwAddress, dwSize, PAGE_READWRITE, &dwProtect)) //Unprotect the memory
memcpy((void*)dwAddress, cpvPatch, dwSize);
else
return false;
return VirtualProtect((void*)dwAddress, dwSize, dwProtect, new DWORD); //Reprotect the memory
}
static Weapon stringToWeaponId(std::string const& weaponName)
{
for (int i = 0; i < Weapon::Count; ++i)
{
if (weaponName == WeaponStrings[i])
return static_cast<Weapon>(i);
}
return Weapon::Count;
}
static void readConfig(const char* sFileName)
{
std::fstream configFile(sFileName);
if (!configFile.is_open())
return;
std::string line;
while (std::getline(configFile, line))
{
// Ignore if line is empty or a comment
if (line.empty() || line[0] == ';')
continue;
// Remove spaces
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
// Split at '='
size_t splitPos = line.find('=');
if (splitPos == std::string::npos)
continue;
std::string sWeaponName = line.substr(0, splitPos);
std::string sCrates = line.substr(splitPos + 1);
Weapon weaponId = stringToWeaponId(sWeaponName);
unsigned int crateChance = atoi(sCrates.c_str());
if (weaponId == Weapon::Count)
continue;
// * 20 because that's how the game does it
g_customEntries.emplace_back(weaponId, crateChance * 20);
}
#ifdef _DEBUG
printf("Config from %s\n", sFileName);
for (auto weaponEntry : g_customEntries)
printf("WeaponID: %s\tChance: %d\n", WeaponStrings[weaponEntry.id], weaponEntry.chance);
printf("\n\n");
#endif // _DEBUG
}
///////////////////////////////////////////////////////////////
// Hook stuff
static void __stdcall overrideCrateChances(WeaponEntryArray* pArray)
{
if (g_customEntries.empty())
return;
#ifdef _DEBUG
// print original list for debug
for (int i = 0; i < pArray->size; ++i)
printf("WeaponID: %s\tChance: %d\n", WeaponStrings[pArray->entries[i].id], pArray->entries[i].chance);
printf("\n\n");
#endif // _DEBUG
pArray->size = g_customEntries.size();
for (int i = 0; i < g_customEntries.size(); ++i)
{
WeaponEntry* pEntry = &pArray->entries[i];
*pEntry = g_customEntries[i];
}
}
static void __declspec(naked) hookFunc()
{
__asm
{
lea eax, [esp +0x20]
push eax
call overrideCrateChances
// Original opcode replaced by call
mov eax, [ebp+0x34]
mov ecx, [eax+0x24]
ret
}
}
///////////////////////////////////////////////////////////////
void main()
{
#ifdef _DEBUG
initConsole();
#endif // _DEBUG
readConfig("wkCrateEditor.ini");
int hookAddress = reinterpret_cast<int>(&hookFunc);
int relative = hookAddress - 0x533575;
writeMemory(0x533570, "\xE8", 1); // call
writeMemory(0x533571, &relative, 4); // relative address to hookFunc
writeMemory(0x533575, "\x90", 1); // nop
}
DWORD WINAPI DllMain(HMODULE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)main, 0, 0, 0);
break;
case DLL_PROCESS_DETACH:
break;
}
return 1;
}