-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhobos.cpp
More file actions
317 lines (257 loc) · 8 KB
/
Phobos.cpp
File metadata and controls
317 lines (257 loc) · 8 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "Phobos.h"
#include <Drawing.h>
#include <SessionClass.h>
#include <Unsorted.h>
#include <Utilities/Debug.h>
#include <Utilities/Patch.h>
#include <Utilities/Macro.h>
#include "Utilities/AresHelper.h"
#include "Utilities/Parser.h"
#ifndef IS_RELEASE_VER
bool HideWarning = false;
#endif
HANDLE Phobos::hInstance = 0;
char Phobos::readBuffer[Phobos::readLength];
wchar_t Phobos::wideBuffer[Phobos::readLength];
const char* Phobos::AppIconPath = nullptr;
bool Phobos::DisplayDamageNumbers = false;
bool Phobos::IsLoadingSaveGame = false;
bool Phobos::Optimizations::Applied = false;
bool Phobos::Optimizations::DisableRadDamageOnBuildings = true;
bool Phobos::Optimizations::DisableSyncLogging = false;
#ifdef STR_GIT_COMMIT
const wchar_t* Phobos::VersionDescription = L"Phobos nightly build (" STR_GIT_COMMIT L" @ " STR_GIT_BRANCH L"). DO NOT SHIP IN MODS!";
#elif !defined(IS_RELEASE_VER)
const wchar_t* Phobos::VersionDescription = L"Phobos development build #" _STR(BUILD_NUMBER) L". Please test the build before shipping.";
#else
//const wchar_t* Phobos::VersionDescription = L"Phobos release build v" FILE_VERSION_STR L".";
#endif
void Phobos::CmdLineParse(char** ppArgs, int nNumArgs)
{
bool foundInheritance = false;
bool foundInclude = false;
bool dontSetExceptionHandler =
#ifdef DEBUG
true;
#else
false;
#endif // DEBUG
Parser<bool> boolParser { };
// > 1 because the exe path itself counts as an argument, too!
for (int i = 1; i < nNumArgs; i++)
{
const char* pArg = ppArgs[i];
std::string arg = pArg;
if (_stricmp(pArg, "-Icon") == 0)
{
Phobos::AppIconPath = ppArgs[++i];
}
#ifndef IS_RELEASE_VER
if (_stricmp(pArg, "-b=" _STR(BUILD_NUMBER)) == 0)
{
HideWarning = true;
}
#endif
if (_stricmp(pArg, "-Inheritance") == 0)
{
foundInheritance = true;
}
if (_stricmp(pArg, "-Include") == 0)
{
foundInclude = true;
}
if (arg.starts_with("-ExceptionHandler="))
{
auto delimIndex = arg.find("=");
auto value = arg.substr(delimIndex + 1, arg.size() - delimIndex - 1);
bool v = dontSetExceptionHandler;
if (boolParser.TryParse(value.c_str(), &v))
dontSetExceptionHandler = !v;
}
}
if (foundInclude)
{
Patch::Apply_RAW(0x474200, // Apply CCINIClass_ReadCCFile1_DisableAres
{ 0x8B, 0xF1, 0x8D, 0x54, 0x24, 0x0C }
);
Patch::Apply_RAW(0x474314, // Apply CCINIClass_ReadCCFile2_DisableAres
{ 0x81, 0xC4, 0xA8, 0x00, 0x00, 0x00 }
);
}
else
{
Patch::Apply_RAW(0x474230, // Revert CCINIClass_Load_Inheritance
{ 0x8B, 0xE8, 0x88, 0x5E, 0x40 }
);
}
if (foundInheritance)
{
Patch::Apply_RAW(0x528A10, // Apply INIClass_GetString_DisableAres
{ 0x83, 0xEC, 0x0C, 0x33, 0xC0 }
);
Patch::Apply_RAW(0x526CC0, // Apply INIClass_GetKeyName_DisableAres
{ 0x8B, 0x54, 0x24, 0x04, 0x83, 0xEC, 0x0C }
);
}
else
{
Patch::Apply_RAW(0x528BAC, // Revert INIClass_GetString_Inheritance_NoEntry
{ 0x8B, 0x7C, 0x24, 0x2C, 0x33, 0xC0, 0x8B, 0x4C, 0x24, 0x28 }
);
}
Game::DontSetExceptionHandler = dontSetExceptionHandler;
Debug::Log("Initialized version: " PRODUCT_VERSION "\n");
Debug::Log("ExceptionHandler is %s\n", dontSetExceptionHandler ? "not present" : "present");
}
void Phobos::ExeRun()
{
Patch::ApplyStatic();
#ifdef DEBUG
if (Phobos::DetachFromDebugger())
{
MessageBoxW(NULL,
L"You can now attach a debugger.\n\n"
L"Press OK to continue YR execution.",
L"Debugger Notice", MB_OK);
}
else
{
MessageBoxW(NULL,
L"You can now attach a debugger.\n\n"
L"To attach a debugger find the YR process in Process Hacker "
L"/ Visual Studio processes window and detach debuggers from it, "
L"then you can attach your own debugger. After this you should "
L"terminate Syringe.exe because it won't automatically exit when YR is closed.\n\n"
L"Press OK to continue YR execution.",
L"Debugger Notice", MB_OK);
}
if (!Console::Create())
{
MessageBoxW(NULL,
L"Failed to allocate the debug console!",
L"Debug Console Notice", MB_OK);
}
#endif
}
void Phobos::ExeTerminate()
{
Console::Release();
}
// =============================
// hooks
bool __stdcall DllMain(HANDLE hInstance, DWORD dwReason, LPVOID v)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
Phobos::hInstance = hInstance;
}
return true;
}
DEFINE_HOOK(0x7CD810, ExeRun, 0x9)
{
Phobos::ExeRun();
AresHelper::Init();
return 0;
}
// Avoid confusing the profiler unless really necessary
#ifdef DEBUG
DEFINE_NAKED_HOOK(0x7CD8EA, _ExeTerminate)
{
// Call WinMain
SET_REG32(EAX, 0x6BB9A0);
CALL(EAX);
PUSH_REG(EAX);
__asm {call Phobos::ExeTerminate};
// Jump back
POP_REG(EAX);
SET_REG32(EBX, 0x7CD8EF);
__asm {jmp ebx};
}
#endif
DEFINE_HOOK(0x52F639, _YR_CmdLineParse, 0x5)
{
GET(char**, ppArgs, ESI);
GET(int, nNumArgs, EDI);
Phobos::CmdLineParse(ppArgs, nNumArgs);
Debug::LogDeferredFinalize();
return 0;
}
DEFINE_HOOK(0x67E44D, LoadGame_SetFlag, 0x5)
{
Phobos::IsLoadingSaveGame = true;
return 0;
}
DEFINE_HOOK(0x67E68A, LoadGame_UnsetFlag, 0x5)
{
Phobos::IsLoadingSaveGame = false;
Phobos::ApplyOptimizations();
return 0;
}
DEFINE_HOOK(0x683E7F, ScenarioClass_Start_Optimizations, 0x7)
{
Phobos::ApplyOptimizations();
return 0;
}
#ifndef IS_RELEASE_VER
DEFINE_HOOK(0x4F4583, GScreenClass_DrawText, 0x6)
{
#ifndef STR_GIT_COMMIT
if (!HideWarning)
#endif // !STR_GIT_COMMIT
{
auto wanted = Drawing::GetTextDimensions(Phobos::VersionDescription, { 0,0 }, 0, 2, 0);
RectangleStruct rect = {
DSurface::Composite->GetWidth() - wanted.Width - 10,
0,
wanted.Width + 10,
wanted.Height + 10
};
Point2D location { rect.X + 5,5 };
DSurface::Composite->FillRect(&rect, COLOR_BLACK);
DSurface::Composite->DrawText(Phobos::VersionDescription, &location, COLOR_RED);
}
return 0;
}
#endif
// Mainly used to disable hooks for optimization.
// Called after loading saved game and at end of scenario start after all INI data etc has been initialized.
// Only executed once per game session.
void Phobos::ApplyOptimizations()
{
if (Phobos::Optimizations::Applied)
return;
// Disable BuildingClass_AI_Radiation
if (Phobos::Optimizations::DisableRadDamageOnBuildings)
Patch::Apply_RAW(0x43FB23, { 0x53, 0x55, 0x56, 0x8B, 0xF1 });
else
{
// Disable Random2Class_Random_SyncLog
Patch::Apply_RAW(0x65C7D0, { 0xC3, 0x90, 0x90, 0x90, 0x90, 0x90 });
// Disable Random2Class_RandomRanged_SyncLog
Patch::Apply_RAW(0x65C88A, { 0xC2, 0x08, 0x00, 0x90, 0x90, 0x90 });
// Disable FacingClass_Set_SyncLog
Patch::Apply_RAW(0x4C9300, { 0x83, 0xEC, 0x10, 0x53, 0x56 });
// Disable InfantryClass_AssignTarget_SyncLog
Patch::Apply_RAW(0x51B1F0, { 0x53, 0x56, 0x8B, 0xF1, 0x57 });
// Disable BuildingClass_AssignTarget_SyncLog
Patch::Apply_RAW(0x443B90, { 0x56, 0x8B, 0xF1, 0x57, 0x83, 0xBE, 0xAC, 0x0, 0x0, 0x0, 0x13 });
// Disable TechnoClass_AssignTarget_SyncLog
Patch::Apply_RAW(0x6FCDB0, { 0x83, 0xEC, 0x0C, 0x53, 0x56 });
// Disable AircraftClass_AssignDestination_SyncLog
Patch::Apply_RAW(0x41AA80, { 0x53, 0x56, 0x57, 0x8B, 0x7C, 0x24, 0x10 });
// Disable BuildingClass_AssignDestination_SyncLog
Patch::Apply_RAW(0x455D50, { 0x56, 0x8B, 0xF1, 0x83, 0xBE, 0xAC, 0x0, 0x0, 0x0, 0x13 });
// Disable InfantryClass_AssignDestination_SyncLog
Patch::Apply_RAW(0x51AA40, { 0x83, 0xEC, 0x2C, 0x53, 0x55 });
// Disable UnitClass_AssignDestination_SyncLog
Patch::Apply_RAW(0x741970, { 0x81, 0xEC, 0x80, 0x0, 0x0, 0x0 });
// Disable AircraftClass_OverrideMission_SyncLog
Patch::Apply_RAW(0x41BB30, { 0x8B, 0x81, 0xAC, 0x0, 0x0, 0x0 });
// Disable FootClass_OverrideMission_SyncLog
Patch::Apply_RAW(0x4D8F40, { 0x8B, 0x54, 0x24, 0x4, 0x56 });
// Disable TechnoClass_OverrideMission_SyncLog
Patch::Apply_RAW(0x7013A0, { 0x8B, 0x54, 0x24, 0x4, 0x56 });
Phobos::Optimizations::DisableSyncLogging = true;
}
Phobos::Optimizations::Applied = true;
}