Repository navigation
Expand file tree
/
Copy pathMapExt.pas
More file actions
365 lines (288 loc) · 9.67 KB
/
Copy pathMapExt.pas
File metadata and controls
365 lines (288 loc) · 9.67 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
unit MapExt;
{
DESCRIPTION: Editor extension support
AUTHOR: Alexander Shostak (aka Berserker aka EtherniDee aka BerSoft)
}
(***) interface (***)
uses
Windows, Math, SysUtils,
Utils, DataLib, Files, FilesEx, StrLib,
Debug, PatchApi, ApiJack, WinUtils, Log, DlgMes, CmdApp,
VfsImport,
BinPatching;
const
(* Command line arguments *)
CMDLINE_ARG_MODLIST = 'modlist';
ERA_EDITOR_VERSION = '3.9.20';
DEBUG_DIR = 'Debug\EraEditor';
DEBUG_MAPS_DIR = 'DebugMaps';
DEBUG_EVENT_LIST_PATH = DEBUG_DIR + '\event list.txt';
DEBUG_PATCH_LIST_PATH = DEBUG_DIR + '\patch list.txt';
DEBUG_MOD_LIST_PATH = DEBUG_DIR + '\mod list.txt';
DEBUG_X86_PATCH_LIST_PATH = DEBUG_DIR + '\x86 patches.txt';
(* Paths *)
MODS_DIR = 'Mods';
DEFAULT_MOD_LIST_FILE = MODS_DIR + '\list.txt';
PLUGINS_PATH = 'EraEditor';
PATCHES_PATH = 'EraEditor';
type
EAssertFailure = class (Exception) end;
PEvent = ^TEvent;
TEvent = packed record
Name: string;
{n} Data: pointer;
DataSize: integer;
end; // .record TEvent
TEventHandler = procedure (Event: PEvent); stdcall;
TEventInfo = class
protected
{On} fHandlers: TList {of TEventHandler};
fNumTimesFired: integer;
function GetNumHandlers: integer;
public
destructor Destroy; override;
procedure AddHandler (Handler: pointer);
property Handlers: {n} TList {of TEventHandler} read fHandlers;
property NumHandlers: integer read GetNumHandlers;
property NumTimesFired: integer read fNumTimesFired write fNumTimesFired;
end; // .class TEventInfo
var
{O} PluginsList: DataLib.TStrList {OF TDllHandle};
{O} Events: {O} DataLib.TDict {OF TEventInfo};
hMe: integer;
GameDir: string;
ModsDir: string;
DumpVfsOpt: boolean;
procedure AsmInit; assembler;
procedure GenerateDebugInfo;
procedure RegisterHandler (Handler: TEventHandler; const EventName: string); stdcall;
procedure FireEvent (const EventName: string; {n} EventData: pointer; DataSize: integer); stdcall;
(***) implementation (***)
destructor TEventInfo.Destroy;
begin
FreeAndNil(fHandlers);
end;
procedure TEventInfo.AddHandler (Handler: pointer);
begin
{!} Assert(Handler <> nil);
if fHandlers = nil then begin
fHandlers := DataLib.NewList(not Utils.OWNS_ITEMS);
end; // .if
fHandlers.Add(Handler);
end;
function TEventInfo.GetNumHandlers: integer;
begin
if fHandlers = nil then begin
result := 0;
end else begin
result := fHandlers.Count;
end; // .else
end;
procedure RegisterHandler (Handler: TEventHandler; const EventName: string);
var
{U} EventInfo: TEventInfo;
begin
{!} Assert(@Handler <> nil);
EventInfo := Events[EventName];
// * * * * * //
if EventInfo = nil then begin
EventInfo := TEventInfo.Create;
Events[EventName] := EventInfo;
end; // .if
EventInfo.AddHandler(@Handler);
end; // .procedure RegisterHandler
procedure FireEvent (const EventName: string; {n} EventData: pointer; DataSize: integer);
var
Event: TEvent;
{U} EventInfo: TEventInfo;
i: integer;
begin
{!} Assert(Utils.IsValidBuf(EventData, DataSize));
EventInfo := Events[EventName];
// * * * * * //
Event.Name := EventName;
Event.Data := EventData;
Event.DataSize := DataSize;
if EventInfo = nil then begin
EventInfo := TEventInfo.Create;
Events[EventName] := EventInfo;
end; // .if
EventInfo.NumTimesFired := EventInfo.NumTimesFired + 1;
if EventInfo.Handlers <> nil then begin
for i := 0 to EventInfo.Handlers.Count - 1 do begin
TEventHandler(EventInfo.Handlers[i])(@Event);
end; // .for
end; // .if
end; // .procedure FireEvent
procedure LoadPlugins;
var
DllHandle: THandle;
begin
with Files.Locate(GameDir + '\' + PLUGINS_PATH + '\*.dll', Files.ONLY_FILES) do begin
while FindNext do begin
if not FoundRec.IsDir and (FoundRec.Rec.Size > 0) then begin
DllHandle := Windows.LoadLibrary(pchar(FoundPath));
{!} Assert(DllHandle <> 0, 'Failed to load plugin DLL at "' + FoundPath + '"');
PluginsList.AddObj(FoundPath, Ptr(DllHandle));
end;
end;
end;
end; // .procedure LoadPlugins
procedure GenerateDebugInfo;
begin
FireEvent('OnGenerateDebugInfo', nil, 0);
end;
procedure DumpEventList;
var
{O} EventList: TStrList {of TEventInfo};
{U} EventInfo: TEventInfo;
i, j: integer;
begin
EventList := nil;
EventInfo := nil;
// * * * * * //
{!} Debug.ModuleContext.Lock;
with FilesEx.WriteFormattedOutput(GameDir + '\' + DEBUG_EVENT_LIST_PATH) do begin
Line('> Format: [Event name] ([Number of handlers], [Fired N times])');
EmptyLine;
EventList := DataLib.DictToStrList(Events, DataLib.CASE_INSENSITIVE);
EventList.Sort;
for i := 0 to EventList.Count - 1 do begin
EventInfo := TEventInfo(EventList.Values[i]);
Line(Format('%s (%d, %d)', [EventList[i], EventInfo.NumHandlers, EventInfo.NumTimesFired]));
end; // .for
EmptyLine; EmptyLine;
Line('> Event handlers');
EmptyLine;
for i := 0 to EventList.Count - 1 do begin
EventInfo := TEventInfo(EventList.Values[i]);
if EventInfo.NumHandlers > 0 then begin
Line(EventList[i] + ':');
end; // .if
Indent;
for j := 0 to EventInfo.NumHandlers - 1 do begin
Line(Debug.ModuleContext.AddrToStr(EventInfo.Handlers[j]));
end; // .for
Unindent;
end; // .for
end; // .with
{!} Debug.ModuleContext.Unlock;
// * * * * * //
FreeAndNil(EventList);
end; // .procedure DumpEventList
procedure DumpPatchList;
var
i: integer;
begin
BinPatching.PatchList.Sort;
with FilesEx.WriteFormattedOutput(GameDir + '\' + DEBUG_PATCH_LIST_PATH) do begin
Line('> Format: [Patch name] (Patch size)');
EmptyLine;
for i := 0 to BinPatching.PatchList.Count - 1 do begin
Line(Format('%s (%d)', [BinPatching.PatchList[i], integer(BinPatching.PatchList.Values[i])]));
end; // .for
end; // .with
end; // .procedure DumpPatchList
procedure DumpModList;
var
{O} MappingsReport: pchar;
begin
MappingsReport := VfsImport.GetMappingsReportA;
Files.WriteFileContents(MappingsReport, GameDir + '\' + DEBUG_MOD_LIST_PATH);
// * * * * * //
VfsImport.MemFree(MappingsReport);
end;
procedure OnGenerateDebugInfo (Event: PEvent); stdcall;
begin
DumpModList;
DumpEventList;
DumpPatchList;
PatchApi.GetPatcher().SaveDump(pchar(GameDir + '\' + DEBUG_X86_PATCH_LIST_PATH));
end;
procedure Init (hDll: integer);
const
RDATA_SECTION_ADDR = Ptr($530000);
RDATA_SECTION_SIZE = $4B000;
var
Buffer: string;
OldProtection: integer;
ModListFilePath: string;
begin
hMe := hDll;
Windows.DisableThreadLibraryCalls(hMe);
Files.ForcePath(GameDir + '\' + DEBUG_DIR);
FireEvent('OnLoadSettings', nil, 0);
// Run VFS
ModListFilePath := CmdApp.GetArg(CMDLINE_ARG_MODLIST);
if ModListFilePath = '' then begin
ModListFilePath := GameDir + '\' + DEFAULT_MOD_LIST_FILE;
end;
VfsImport.MapModsFromListA(pchar(GameDir), pchar(ModsDir), pchar(ModListFilePath));
Log.Write('Core', 'ReportModList', #13#10 + VfsImport.GetMappingsReportA);
if DumpVfsOpt then begin
Log.Write('Core', 'DumpVFS', #13#10 + VfsImport.GetDetailedMappingsReportA);
end;
VfsImport.RunVfs(VfsImport.SORT_FIFO);
VfsImport.RunWatcherA(pchar(GameDir + '\Mods'), 250);
(* Restore default editor constant overwritten by "eramap.dll" *)
Buffer := 'GetSpreadsheet';
ApiJack.WriteAtCode(Length(Buffer) + 1, pointer(Buffer), Ptr($596ED4));
(* GrayFace mapedpatch requires .rdata section to have WRITE flag *)
Windows.VirtualProtect
(
RDATA_SECTION_ADDR,
RDATA_SECTION_SIZE,
Windows.PAGE_EXECUTE_READWRITE,
@OldProtection
);
FireEvent('OnInit', nil, 0);
RegisterHandler(OnGenerateDebugInfo, 'OnGenerateDebugInfo');
LoadPlugins;
BinPatching.ApplyPatches(PATCHES_PATH);
end; // .procedure Init
procedure AsmInit; assembler;
asm
(* Remove ret addr *)
POP ECX
CALL Init
(* Default code *)
PUSH EBP
MOV EBP, ESP
PUSH -1
PUSH $54CA58
PUSH $4EA77C
PUSH [FS:0]
(* Place new ret addr *)
PUSH $4E84EA
// RET
end; // .procedure AsmInit
procedure AssertHandler (const Mes, FileName: string; LineNumber: integer; Address: pointer);
var
CrashMes: string;
begin
CrashMes := StrLib.BuildStr
(
'Assert violation in file "~FileName~" on line ~Line~.'#13#10'Error at address: $~Address~.'#13#10'Message: "~Message~"',
[
'FileName', FileName,
'Line', SysUtils.IntToStr(LineNumber),
'Address', SysUtils.Format('%x', [integer(Address)]),
'Message', Mes
],
'~'
);
Log.Write('Core', 'AssertHandler', CrashMes);
DlgMes.MsgError(CrashMes);
raise EAssertFailure.Create(CrashMes) at Address;
end; // .procedure AssertHandler
begin
AssertErrorProc := AssertHandler;
PluginsList := DataLib.NewStrList(not Utils.OWNS_ITEMS, DataLib.CASE_INSENSITIVE);
Events := DataLib.NewDict(Utils.OWNS_ITEMS, DataLib.CASE_INSENSITIVE);
// Find out path to game directory and force it as current directory
GameDir := StrLib.ExtractDirPathW(WinUtils.GetExePath());
{!} Assert(GameDir <> '', 'Failed to obtain game directory path');
SysUtils.SetCurrentDir(GameDir);
ModsDir := GameDir + '\' + MODS_DIR;
Debug.SetDebugMapsDir(GameDir + '\' + DEBUG_MAPS_DIR);
end.