-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLodman.pas
517 lines (411 loc) · 14.4 KB
/
Lodman.pas
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
unit Lodman;
(*
Description: LOD archives manager. Includes resource redirection support.
Author: Alexander Shostak aka Berserker
Based on: "Lods" plugin by Sav, WoG Sources by ZVS
*)
(***) interface (***)
uses
Math,
SysUtils,
Windows,
ApiJack,
AssocArrays,
DataLib,
Debug,
DlgMes,
Files,
Lists,
StrUtils,
TypeWrappers,
Utils,
EventMan,
GameExt,
Heroes,
Json,
Log,
Stores;
const
MAX_NUM_LODS = 100;
DEF_NUM_LODS = 8;
LODREDIR_SAVE_SECTION = 'Era.ResourceRedirections';
type
(* IMPORT *)
TString = TypeWrappers.TString;
TGameVersion = Heroes.ROE..Heroes.SOD_AND_AB;
TLodType = (LOD_SPRITE = 1, LOD_BITMAP = 2, LOD_WAV = 3);
PLodTable = ^TLodTable;
TLodTable = array [0..MAX_NUM_LODS - 1] of Heroes.TLod;
TZvsAddLodToList = function (LodInd: integer): integer; cdecl;
PIndexes = ^TIndexes;
TIndexes = array [0..MAX_NUM_LODS - 1] of integer;
PLodIndexes = ^TLodIndexes;
TLodIndexes = packed record
NumLods: integer;
Indexes: PIndexes;
end;
PLodTypes = ^TLodTypes;
TLodTypes = packed record
Table: array [TLodType, TGameVersion] of TLodIndexes;
Indexes: array [TLodType, TGameVersion] of TIndexes;
end;
TFindRedirectionFlag = (FRF_EXCLUDE_FALLBACKS);
TFindRedirectionFlags = set of TFindRedirectionFlag;
const
ZvsAddLodToList: TZvsAddLodToList = Ptr($75605B);
ZvsLodTable: PLodTable = Ptr($28077D0);
ZvsLodTypes: PLodTypes = Ptr($79EFE0);
procedure RedirectFile (const OldFileName, NewFileName: string);
procedure GlobalRedirectFile (const OldFileName, NewFileName: string);
function FindFileLod (const FileName: string; out LodPath: string): boolean;
function FileIsInLod (const FileName: string; Lod: Heroes.PLod): boolean;
function FindRedirection (const FileName: string; var {out} Redirected: string; Flags: TFindRedirectionFlags = []): boolean;
function GetRedirectedName (const FileName: string; Flags: TFindRedirectionFlags = []): string;
(***) implementation (***)
uses SndVid;
const
GLOBAL_REDIRECTIONS_CONFIG_DIR = 'Data\Redirections';
GLOBAL_MISSING_REDIRECTIONS_CONFIG_DIR = GLOBAL_REDIRECTIONS_CONFIG_DIR + '\Missing';
MUSIC_DIR = 'Mp3';
REDIRECT_ONLY_MISSING = true;
REDIRECT_MISSING_AND_EXISTING = not REDIRECT_ONLY_MISSING;
var
{O} GlobalLodRedirs: {O} AssocArrays.TAssocArray {of TString};
{O} LodRedirs: {O} AssocArrays.TAssocArray {of TString};
{O} FallbackLodRedirs: {O} AssocArrays.TAssocArray {of TString};
{O} LodList: Lists.TStringList;
NumLods: integer = DEF_NUM_LODS;
RedirCritSection: Windows.TRTLCriticalSection;
{O} PostponedMissingMediaRedirs: {O} Lists.TStringList {of TString};
procedure UnregisterLod (LodInd: integer);
var
{U} Table: PLodIndexes;
{U} Indexes: PIndexes;
LodType: TLodType;
GameVersion: TGameVersion;
LocalNumLods: integer;
LeftInd: integer;
i: integer;
begin
{!} Assert(Math.InRange(LodInd, 0, NumLods - 1), 'Lod index is out of allowed range: ' + IntToStr(LodInd));
Table := nil;
Indexes := nil;
// * * * * * //
for LodType := Low(TLodType) to High(TLodType) do begin
for GameVersion := Low(TGameVersion) to High(TGameVersion) do begin
Table := @ZvsLodTypes.Table[LodType, GameVersion];
Indexes := Table.Indexes;
LocalNumLods := Table.NumLods;
LeftInd := 0;
i := 0;
while i < LocalNumLods do begin
if Indexes[i] <> LodInd then begin
Indexes[LeftInd] := Indexes[i];
Inc(LeftInd);
end;
Inc(i);
end;
Table.NumLods := LeftInd;
end; // .for
end; // .for
Dec(NumLods);
end; // .procedure UnregisterLod
procedure UnregisterDeadLods;
begin
if not SysUtils.FileExists('Data\h3abp_sp.lod') then begin
UnregisterLod(7);
end;
if not SysUtils.FileExists('Data\h3abp_bm.lod') then begin
UnregisterLod(6);
end;
if not SysUtils.FileExists('Data\h3psprit.lod') then begin
UnregisterLod(5);
end;
if not SysUtils.FileExists('Data\h3pbitma.lod') then begin
UnregisterLod(4);
end;
if not SysUtils.FileExists('Data\h3ab_spr.lod') then begin
UnregisterLod(3);
end;
if not SysUtils.FileExists('Data\h3ab_bmp.lod') then begin
UnregisterLod(2);
end;
if not SysUtils.FileExists('Data\h3sprite.lod') then begin
UnregisterLod(1);
end;
if not SysUtils.FileExists('Data\h3bitmap.lod') then begin
UnregisterLod(0);
end;
end; // .procedure UnregisterDeadLods
function FileIsInLod (const FileName: string; Lod: Heroes.PLod): boolean;
begin
{!} Assert(Lod <> nil);
result := false;
if FileName <> '' then begin
asm
MOV ECX, Lod
ADD ECX, 4
PUSH FileName
MOV EAX, $4FB100
CALL EAX
MOV result, AL
end; // .asm
end;
end; // .function FileIsInLod
function FindFileLod (const FileName: string; out LodPath: string): boolean;
var
Lod: Heroes.PLod;
i: integer;
begin
Lod := @ZvsLodTable[NumLods - 1];
// * * * * * //
result := false;
i := NumLods - 1;
while not result and (i >= 0) do begin
result := FileIsInLod(FileName, Lod);
if not result then begin
Dec(Lod);
Dec(i);
end;
end;
if result then begin
LodPath := pchar(integer(Lod) + 8);
end;
end; // .function FindFileLod
function FileIsInLods (const FileName: string): boolean;
var
FoundLod: string;
begin
result := FindFileLod(FileName, FoundLod);
end;
function FindRedirection (const FileName: string; var {out} Redirected: string; Flags: TFindRedirectionFlags = []): boolean;
var
{U} Redirection: TString;
begin
{!} Windows.EnterCriticalSection(RedirCritSection);
Redirection := LodRedirs[FileName];
result := false;
if Redirection = nil then begin
Redirection := GlobalLodRedirs[FileName];
end;
if (Redirection = nil) and not (FRF_EXCLUDE_FALLBACKS in Flags) then begin
Redirection := FallbackLodRedirs[FileName];
end;
if Redirection <> nil then begin
Redirected := Redirection.Value;
result := true;
end;
{!} Windows.LeaveCriticalSection(RedirCritSection);
end; // .function FindRedirection
function GetRedirectedName (const FileName: string; Flags: TFindRedirectionFlags = []): string;
begin
if not FindRedirection(FileName, result, Flags) then begin
result := FileName;
end;
end;
(* Loads global redirection rules from json configs *)
procedure LoadGlobalRedirectionConfig (const ConfigDir: string; RedirectOnlyMissing: boolean);
var
{O} Config: TlkJsonObject;
ResourceName: string;
WillBeRedirected: boolean;
ConfigFileContents: string;
i: integer;
begin
Config := nil;
// * * * * * //
with Files.Locate(ConfigDir + '\*.json', Files.ONLY_FILES) do begin
while FindNext do begin
if Files.ReadFileContents(ConfigDir + '\' + FoundName, ConfigFileContents) then begin
Utils.CastOrFree(TlkJson.ParseText(ConfigFileContents), TlkJsonObject, Config);
if Config <> nil then begin
for i := 0 to Config.Count - 1 do begin
ResourceName := Config.NameOf[i];
if GlobalLodRedirs[ResourceName] = nil then begin
WillBeRedirected := not RedirectOnlyMissing;
if RedirectOnlyMissing then begin
if AnsiEndsText('.mp3', ResourceName) then begin
WillBeRedirected := not FileExists(MUSIC_DIR + '\' + ResourceName);
end else if AnsiEndsText('.wav', ResourceName) or AnsiEndsText('.smk', ResourceName) or AnsiEndsText('.bik', ResourceName) then begin
PostponedMissingMediaRedirs.AddObj(ResourceName, TString.Create(Config.GetString(i)));
end else begin
WillBeRedirected := not FileIsInLods(ResourceName);
end;
end;
if WillBeRedirected then begin
if RedirectOnlyMissing then begin
FallbackLodRedirs[ResourceName] := TString.Create(Config.GetString(i));
end else begin
GlobalLodRedirs[ResourceName] := TString.Create(Config.GetString(i));
end;
end;
end; // .if
end; // .for
end else begin
Debug.NotifyError('Invalid json config: "' + ConfigDir + '\' + FoundName + '"');
end; // .else
end; // .if
end; // .while
end; // .with
// * * * * * //
FreeAndNil(Config);
end; // .procedure LoadGlobalRedirectionConfig
function Hook_FindFileInLod (Context: ApiJack.PHookContext): longbool; stdcall;
var
Redirected: string;
begin
if FindRedirection(ppchar(Context.EBP + $8)^, Redirected) then begin
ppchar(Context.EBP + $8)^ := pchar(Redirected);
end;
result := true;
end;
function Hook_LoadLods (Context: ApiJack.PHookContext): longbool; stdcall;
var
i: integer;
begin
UnregisterDeadLods;
with Files.Locate('Data\*.pac', Files.ONLY_FILES) do begin
while FindNext do begin
LodList.Add(FoundName);
end;
end;
for i := LodList.Count - 1 downto 0 do begin
Heroes.LoadLod(LodList[i], @ZvsLodTable[NumLods]);
ZvsAddLodToList(NumLods);
Inc(NumLods);
end;
result := true;
end;
function Hook_AfterLoadLods (Context: ApiJack.PHookContext): longbool; stdcall;
begin
LoadGlobalRedirectionConfig(GLOBAL_MISSING_REDIRECTIONS_CONFIG_DIR, REDIRECT_ONLY_MISSING);
(* Begin lods files redirection *)
ApiJack.Hook(Ptr($4FB106), @Hook_FindFileInLod);
ApiJack.Hook(Ptr($4FACA6), @Hook_FindFileInLod); // A0_Lod_FindResource_sub_4FACA0
EventMan.GetInstance().Fire('OnAfterLoadLods');
result := true;
end;
function Hook_AfterLoadMedia (Context: ApiJack.PHookContext): longbool; stdcall;
var
ResourceName: string;
WillBeRedirected: boolean;
i: integer;
begin
(* Apply postponed missing media redirections *)
for i := 0 to PostponedMissingMediaRedirs.Count - 1 do begin
ResourceName := PostponedMissingMediaRedirs[i];
if AnsiEndsText('.wav', ResourceName) then begin
WillBeRedirected := not SndVid.HasSoundReal(ResourceName);
end else begin
WillBeRedirected := not SndVid.HasVideoReal(ResourceName);
end;
if WillBeRedirected then begin
GlobalRedirectFile(ResourceName, TString(PostponedMissingMediaRedirs.Values[i]).Value);
end;
end;
PostponedMissingMediaRedirs.Clear;
EventMan.GetInstance().Fire('OnAfterLoadMedia');
result := true;
end; // .function Hook_AfterLoadMedia
procedure RedirectFile (const OldFileName, NewFileName: string);
var
Redirection: TString;
begin
{!} Windows.EnterCriticalSection(RedirCritSection);
if NewFileName = '' then begin
if OldFileName = '' then begin
LodRedirs.Clear;
end else begin
LodRedirs.DeleteItem(OldFileName);
end;
end else begin
Redirection := LodRedirs[OldFileName];
if Redirection = nil then begin
LodRedirs[OldFileName] := TString.Create(NewFileName);
end else begin
Redirection.Value := NewFileName;
end;
end; // .else
{!} Windows.LeaveCriticalSection(RedirCritSection);
end; // .procedure RedirectFile
procedure GlobalRedirectFile (const OldFileName, NewFileName: string);
var
Redirection: TString;
begin
{!} Windows.EnterCriticalSection(RedirCritSection);
if NewFileName = '' then begin
if OldFileName = '' then begin
GlobalLodRedirs.Clear;
end else begin
GlobalLodRedirs.DeleteItem(OldFileName);
end;
end else begin
Redirection := GlobalLodRedirs[OldFileName];
if Redirection = nil then begin
GlobalLodRedirs[OldFileName] := TString.Create(NewFileName);
end else begin
Redirection.Value := NewFileName;
end;
end; // .else
{!} Windows.LeaveCriticalSection(RedirCritSection);
end; // .procedure GlobalRedirectFile
procedure OnBeforeErmInstructions (Event: PEvent); stdcall;
begin
LodRedirs.Clear;
end;
procedure OnSavegameWrite (Event: PEvent); stdcall;
begin
with Stores.NewRider(LODREDIR_SAVE_SECTION) do begin
WriteInt(LodRedirs.ItemCount);
with DataLib.IterateDict(LodRedirs) do begin
while IterNext do begin
WriteStr(IterKey);
WriteStr(TString(IterValue).Value);
end;
end;
end;
end;
procedure OnSavegameRead (Event: PEvent); stdcall;
var
OldFileName: string;
NewFileName: string;
i: integer;
begin
{!} Windows.EnterCriticalSection(RedirCritSection);
LodRedirs.Clear;
with Stores.NewRider(LODREDIR_SAVE_SECTION) do begin
for i := 0 to ReadInt - 1 do begin
OldFileName := ReadStr;
NewFileName := ReadStr;
LodRedirs[OldFileName] := TString.Create(NewFileName);
end;
end;
{!} Windows.LeaveCriticalSection(RedirCritSection);
end;
procedure OnBeforeWoG (Event: PEvent); stdcall;
begin
(* Remove WoG h3custom and h3wog lods registration *)
PWORD($7015E5)^ := $38EB;
(* Lead lods loading/reordering *)
ApiJack.Hook(Ptr($559408), @Hook_LoadLods);
(* Implement OnAfterLoadLods event and missing resources redirection *)
ApiJack.Hook(Ptr($4EDD65), @Hook_AfterLoadLods);
ApiJack.Hook(Ptr($4EE0CB), @Hook_AfterLoadMedia);
end;
procedure OnAfterWoG (Event: PEvent); stdcall;
begin
LoadGlobalRedirectionConfig(GLOBAL_REDIRECTIONS_CONFIG_DIR, REDIRECT_MISSING_AND_EXISTING);
end;
begin
Windows.InitializeCriticalSection(RedirCritSection);
GlobalLodRedirs := AssocArrays.NewStrictAssocArr(TString);
LodRedirs := AssocArrays.NewStrictAssocArr(TString);
FallbackLodRedirs := AssocArrays.NewStrictAssocArr(TString);
LodList := Lists.NewSimpleStrList;
PostponedMissingMediaRedirs := DataLib.NewStrList(Utils.OWNS_ITEMS, DataLib.CASE_SENSITIVE);
EventMan.GetInstance.On('OnBeforeWoG', OnBeforeWoG);
EventMan.GetInstance.On('OnAfterWoG', OnAfterWoG);
EventMan.GetInstance.On('OnBeforeErmInstructions', OnBeforeErmInstructions);
EventMan.GetInstance.On('OnSavegameWrite', OnSavegameWrite);
EventMan.GetInstance.On('OnSavegameRead', OnSavegameRead);
end.