-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhobos.Ext.cpp
More file actions
381 lines (332 loc) · 9.59 KB
/
Phobos.Ext.cpp
File metadata and controls
381 lines (332 loc) · 9.59 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <Phobos.h>
#include <LoadOptionsClass.h>
#include <Ext/Aircraft/Body.h>
#include <Ext/AnimType/Body.h>
#include <Ext/Anim/Body.h>
#include <Ext/Building/Body.h>
#include <Ext/BuildingType/Body.h>
#include <Ext/Bullet/Body.h>
#include <Ext/BulletType/Body.h>
#include <Ext/Cell/Body.h>
#include <Ext/EBolt/Body.h>
#include <Ext/House/Body.h>
#include <Ext/OverlayType/Body.h>
#include <Ext/ParticleSystemType/Body.h>
#include <Ext/RadSite/Body.h>
#include <Ext/Rules/Body.h>
#include <Ext/Scenario/Body.h>
#include <Ext/Script/Body.h>
#include <Ext/Side/Body.h>
#include <Ext/SWType/Body.h>
#include <Ext/SWType/NewSWType/NewSWType.h>
#include <Ext/TAction/Body.h>
#include <Ext/Team/Body.h>
#include <Ext/Techno/Body.h>
#include <Ext/TechnoType/Body.h>
#include <Ext/TerrainType/Body.h>
#include <Ext/Tiberium/Body.h>
#include <Ext/VoxelAnim/Body.h>
#include <Ext/VoxelAnimType/Body.h>
#include <Ext/WarheadType/Body.h>
#include <Ext/WeaponType/Body.h>
#include <New/Type/BannerTypeClass.h>
#include <New/Type/DigitalDisplayTypeClass.h>
#include <New/Type/LaserTrailTypeClass.h>
#include <New/Type/RadTypeClass.h>
#include <New/Entity/BannerClass.h>
#include <New/Type/SelectBoxTypeClass.h>
#include <utility>
#pragma region Implementation details
#pragma region Concepts
// a hack to check if some type can be used as a specialization of a template
template <template <class...> class Template, class... Args>
void DerivedFromSpecialization(const Template<Args...>&);
template <class T, template <class...> class Template>
concept DerivedFromSpecializationOf =
requires(const T & t) { DerivedFromSpecialization<Template>(t); };
template<typename TExt>
concept HasExtMap = requires { { TExt::ExtMap } -> DerivedFromSpecializationOf<Container>; };
template<typename TExt>
concept ExtDataConsiderPointerInvalidation = HasExtMap<TExt> && requires{
{ TExt::ShouldConsiderInvalidatePointer }->std::convertible_to<const bool>;
}&& TExt::ShouldConsiderInvalidatePointer == true;
template <typename T>
concept Clearable = requires { T::Clear(); };
template <typename T>
concept PointerInvalidationSubscribable =
requires (void* ptr, bool removed) { T::PointerGotInvalid(ptr, removed); };
template <typename T>
concept GlobalSaveLoadable = requires
{
T::LoadGlobals(std::declval<PhobosStreamReader&>());
T::SaveGlobals(std::declval<PhobosStreamWriter&>());
};
template <typename TAction, typename TProcessed, typename... ArgTypes>
concept DispatchesAction =
requires (ArgTypes... args) { TAction::template Process<TProcessed>(args...); };
#pragma endregion
// calls:
// T::Clear()
// T::ExtMap.Clear()
struct ClearAction
{
template <typename T>
static bool Process()
{
if constexpr (Clearable<T>)
T::Clear();
else if constexpr (HasExtMap<T>)
T::ExtMap.Clear();
return true;
}
};
// calls:
// T::PointerGotInvalid(void*, bool)
// T::ExtMap.PointerGotInvalid(void*, bool)
struct InvalidatePointerAction
{
template <typename T>
static bool Process(void* ptr, bool removed)
{
if constexpr (PointerInvalidationSubscribable<T>)
T::PointerGotInvalid(ptr, removed);
else if constexpr (ExtDataConsiderPointerInvalidation<T>)
T::ExtMap.PointerGotInvalid(ptr, removed);
return true;
}
};
// calls:
// T::LoadGlobals(PhobosStreamReader&)
struct LoadGlobalsAction
{
template <typename T>
static bool Process(IStream* pStm)
{
if constexpr (GlobalSaveLoadable<T>)
{
PhobosByteStream stm(0);
stm.ReadBlockFromStream(pStm);
PhobosStreamReader reader(stm);
return T::LoadGlobals(reader) && reader.ExpectEndOfBlock();
}
else
{
return true;
}
}
};
// calls:
// T::SaveGlobals(PhobosStreamWriter&)
struct SaveGlobalsAction
{
template <typename T>
static bool Process(IStream* pStm)
{
if constexpr (GlobalSaveLoadable<T>)
{
PhobosByteStream stm;
PhobosStreamWriter writer(stm);
return T::SaveGlobals(writer) && stm.WriteBlockToStream(pStm);
}
else
{
return true;
}
}
};
// this is a complicated thing that calls methods on classes. add types to the
// instantiation of this type, and the most appropriate method for each type
// will be called with no overhead of virtual functions.
template <typename... RegisteredTypes>
struct TypeRegistry
{
__forceinline static void Clear()
{
dispatch_mass_action<ClearAction>();
}
__forceinline static void InvalidatePointer(void* ptr, bool removed)
{
dispatch_mass_action<InvalidatePointerAction>(ptr, removed);
}
__forceinline static bool LoadGlobals(IStream* pStm)
{
return dispatch_mass_action<LoadGlobalsAction>(pStm);
}
__forceinline static bool SaveGlobals(IStream* pStm)
{
return dispatch_mass_action<SaveGlobalsAction>(pStm);
}
private:
// TAction: the method dispatcher class to call with each type
// ArgTypes: the argument types to call the method dispatcher's Process() method
template <typename TAction, typename... ArgTypes>
requires (DispatchesAction<TAction, RegisteredTypes, ArgTypes...> && ...)
__forceinline static bool dispatch_mass_action(ArgTypes... args)
{
// (pack expression op ...) is a fold expression which
// unfolds the parameter pack into a full expression
return (TAction::template Process<RegisteredTypes>(args...) && ...);
}
};
#pragma endregion
// Add more class names as you like
using PhobosTypeRegistry = TypeRegistry <
// Ext classes
AircraftExt,
AnimTypeExt,
AnimExt,
BuildingExt,
BuildingTypeExt,
BulletExt,
BulletTypeExt,
CellExt,
EBoltExt,
HouseExt,
OverlayTypeExt,
ParticleSystemTypeExt,
RadSiteExt,
RulesExt,
ScenarioExt,
ScriptExt,
SideExt,
SWTypeExt,
TActionExt,
TeamExt,
TechnoExt,
TechnoTypeExt,
TerrainTypeExt,
TiberiumExt,
VoxelAnimExt,
VoxelAnimTypeExt,
WarheadTypeExt,
WeaponTypeExt,
// New classes
ShieldTypeClass,
LaserTrailTypeClass,
RadTypeClass,
ShieldClass,
DigitalDisplayTypeClass,
BannerTypeClass,
BannerClass,
AttachEffectTypeClass,
AttachEffectClass,
NewSWType,
SelectBoxTypeClass
// other classes
> ;
DEFINE_HOOK(0x7258D0, AnnounceInvalidPointer, 0x6)
{
GET(AbstractClass* const, pInvalid, ECX);
GET(bool const, removed, EDX);
PhobosTypeRegistry::InvalidatePointer(pInvalid, removed);
return 0;
}
DEFINE_HOOK(0x685659, Scenario_ClearClasses, 0xa)
{
PhobosTypeRegistry::Clear();
return 0;
}
// Ares saves its things at the end of the save
// Phobos will save the things at the beginning of the save
// Considering how DTA gets the scenario name, I decided to save it after Rules - secsome
DEFINE_HOOK(0x67D32C, SaveGame_Phobos, 0x5)
{
GET(IStream*, pStm, ESI);
PhobosTypeRegistry::SaveGlobals(pStm);
return 0;
}
DEFINE_HOOK(0x67E826, LoadGame_Phobos, 0x6)
{
GET(IStream*, pStm, ESI);
PhobosTypeRegistry::LoadGlobals(pStm);
return 0;
}
DEFINE_HOOK(0x67D04E, GameSave_SavegameInformation, 0x7)
{
REF_STACK(SavegameInformation, Info, STACK_OFFSET(0x4A4, -0x3F4));
Info.InternalVersion = Info.InternalVersion + SAVEGAME_ID;
strncat(Info.ExecutableName.data(),
" + Phobos " FILE_VERSION_STR,
Info.ExecutableName.Size - sizeof(" + Phobos " FILE_VERSION_STR)
);
return 0;
}
DEFINE_HOOK_AGAIN(0x67FD9D, LoadOptionsClass_GetFileInfo, 0x7)
DEFINE_HOOK(0x67FDB1, LoadOptionsClass_GetFileInfo, 0x7)
{
GET(SavegameInformation*, Info, ESI);
Info->InternalVersion = Info->InternalVersion - SAVEGAME_ID;
return 0;
}
#ifdef DEBUG
#pragma warning (disable : 4091)
#pragma warning (disable : 4245)
#include <Dbghelp.h>
#include <tlhelp32.h>
bool Phobos::DetachFromDebugger()
{
auto GetDebuggerProcessId = [](DWORD dwSelfProcessId) -> DWORD
{
DWORD dwParentProcessId = -1;
HANDLE hSnapshot = CreateToolhelp32Snapshot(2, 0);
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnapshot, &pe32);
do
{
if (pe32.th32ProcessID == dwSelfProcessId)
{
dwParentProcessId = pe32.th32ParentProcessID;
break;
}
}
while (Process32Next(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return dwParentProcessId;
};
HMODULE hModule = LoadLibrary("ntdll.dll");
if (hModule != NULL)
{
auto const NtRemoveProcessDebug =
(NTSTATUS(__stdcall*)(HANDLE, HANDLE))GetProcAddress(hModule, "NtRemoveProcessDebug");
auto const NtSetInformationDebugObject =
(NTSTATUS(__stdcall*)(HANDLE, ULONG, PVOID, ULONG, PULONG))GetProcAddress(hModule, "NtSetInformationDebugObject");
auto const NtQueryInformationProcess =
(NTSTATUS(__stdcall*)(HANDLE, ULONG, PVOID, ULONG, PULONG))GetProcAddress(hModule, "NtQueryInformationProcess");
auto const NtClose =
(NTSTATUS(__stdcall*)(HANDLE))GetProcAddress(hModule, "NtClose");
HANDLE hDebug;
HANDLE hCurrentProcess = GetCurrentProcess();
NTSTATUS status = NtQueryInformationProcess(hCurrentProcess, 30, &hDebug, sizeof(HANDLE), 0);
if (0 <= status)
{
ULONG killProcessOnExit = FALSE;
status = NtSetInformationDebugObject(
hDebug,
1,
&killProcessOnExit,
sizeof(ULONG),
NULL
);
if (0 <= status)
{
const auto pid = GetDebuggerProcessId(GetProcessId(hCurrentProcess));
status = NtRemoveProcessDebug(hCurrentProcess, hDebug);
if (0 <= status)
{
HANDLE hDbgProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (INVALID_HANDLE_VALUE != hDbgProcess)
{
BOOL ret = TerminateProcess(hDbgProcess, EXIT_SUCCESS);
CloseHandle(hDbgProcess);
return ret;
}
}
}
NtClose(hDebug);
}
FreeLibrary(hModule);
}
return false;
}
#endif