forked from TES5Edit/TES5Edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwbLoadOrder.pas
412 lines (356 loc) · 10.5 KB
/
wbLoadOrder.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
{******************************************************************************
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
*******************************************************************************}
unit wbLoadOrder;
{$I wbDefines.inc}
interface
uses
wbInit,
wbInterface;
type
TwbModuleExtension = (
meUnknown,
meESM,
meESL,
meESP
);
TwbModuleFlag = (
mfInvalid,
mfMastersMissing,
mfHasESMFlag,
mfHasESLFlag,
mfIsESM,
mfActiveInPluginsTxt,
mfActive,
mfTaken,
mfLoaded,
mfLoading
);
TwbModuleFlags = set of TwbModuleFlag;
PwbModuleInfo = ^TwbModuleInfo;
TwbModuleInfo = record
miName : string;
miUnghostedName : string;
miDateTime : TDateTime;
miExtension : TwbModuleExtension;
miIsGhost : Boolean;
miMasterNames : TDynStrings;
miMasters : array of PwbModuleInfo;
miFlags : TwbModuleFlags;
miPluginsIndex : Integer;
miOfficialIndex : Integer;
miCCIndex : Integer;
function IsValid: Boolean;
function IsActive: Boolean;
procedure ActivateMasters(aRecursive: Boolean);
procedure Activate(aActivateMasters: Boolean = False);
end;
TwbModuleInfos = array of PwbModuleInfo;
TwbModuleInfosHelper = record helper for TwbModuleInfos
function ToStrings: TDynStrings;
procedure DeactivateAll;
procedure ActivateMasters;
function SimulateLoad: TwbModuleInfos;
end;
procedure wbLoadModules;
function wbModuleByName(const aName: string): PwbModuleInfo;
function wbModulesByLoadOrder: TwbModuleInfos;
implementation
uses
System.Types,
System.Classes,
System.SysUtils,
System.IOUtils,
wbImplementation,
wbSort;
type
TwbDynModuleInfos = array of TwbModuleInfo;
var
_Modules : TwbDynModuleInfos;
_ModulesByName : TStringList;
_InvalidModule : TwbModuleInfo = (miFlags: [mfInvalid]);
_ModulesLoadOrder : TwbModuleInfos;
const
csDotGhost = '.ghost';
csDotEsm = '.esm';
csDotEsl = '.esl';
csDotEsp = '.esp';
function wbModuleByName(const aName: string): PwbModuleInfo;
var
i: Integer;
begin
wbLoadModules;
if _ModulesByName.Find(aName, i) then
Result := Pointer(_ModulesByName.Objects[i])
else
Result := @_InvalidModule;
end;
function _ModulesLoadOrderCompare(Item1, Item2: Pointer): Integer;
var
a, b: PwbModuleInfo;
begin
if Item1 = Item2 then
Exit(0);
a := Item1;
b := Item2;
Result := CmpI32(a.miOfficialIndex, b.miOfficialIndex);
if Result = 0 then begin
Result := CmpI32(a.miCCIndex, b.miCCIndex);
if Result = 0 then begin
if (mfIsESM in a.miFlags) = (mfIsESM in b.miFlags) then begin
Result := CmpI32(a.miPluginsIndex, b.miPluginsIndex);
if Result = 0 then begin
Result := CmpDouble(a.miDateTime, b.miDateTime);
if Result = 0 then begin
Result := CompareText(a.miName, b.miName);
if Result = 0 then
Result := CmpPtr(Item1, Item2);
end;
end;
end else
if mfIsESM in a.miFlags then
Result := -1
else
Result := 1;
end;
end;
end;
procedure wbLoadModules;
var
Files : TStringDynArray;
i, j, k : Integer;
s : string;
IsESM ,
IsESL : Boolean;
lIsActive : Boolean;
sl : TStringList;
begin
if Assigned(_ModulesByName) then {already loaded}
Exit;
Files := TDirectory.GetFiles(wbDataPath);
SetLength(_Modules, Length(Files));
j := 0;
for i := Low(Files) to High(Files) do
with _Modules[j] do begin
miFlags := [];
miName := ExtractFileName(Files[i]);
miIsGhost := miName.EndsWith('.ghost', True);
if miIsGhost then
miUnghostedName := Copy(miName, 1, Length(miName) - Length(csDotGhost))
else
miUnghostedName := miName;
miExtension := meUnknown;
if miUnghostedName.EndsWith(csDotEsm) then
miExtension := meESM
else if miUnghostedName.EndsWith(csDotEsp) then
miExtension := meESP
else if miUnghostedName.EndsWith(csDotEsl) and wbIsEslSupported then
miExtension := meESL;
if miExtension = meUnknown then
Continue;
if miExtension in [meESM, meESL] then
Include(miFlags, mfIsESM);
miDateTime := TFile.GetLastWriteTime(wbDataPath + miName);
if not wbMastersForFile(wbDataPath+miName, miMasterNames, @IsESM, @IsESL) then
Continue;
if IsESM then begin
Include(miFlags, mfHasESMFlag);
if (wbToolMode in [tmMasterUpdate, tmMasterRestore]) and wbIsFallout3 then
{ignore header flag for load order, only extension counts}
else
Include(miFlags, mfIsESM);
end;
if IsESL then
Include(miFlags, mfHasESLFlag);
Inc(j);
end;
SetLength(_Modules, j);
{do NOT perform SetLength on _Modules after this, it could invalidate pointer into the array}
_ModulesByName := TStringList.Create;
for i := Low(_Modules) to High(_Modules) do
_ModulesByName.AddObject(_Modules[i].miName, @_Modules[i]);
_ModulesByName.Sorted := True;
SetLength(_ModulesLoadOrder, Length(_Modules));
for i := Low(_Modules) to High(_Modules) do
with _Modules[i] do begin
_ModulesLoadOrder[i] := @_Modules[i];
SetLength(miMasters, Length(miMasterNames));
for j := Low(miMasterNames) to High(miMasterNames) do
if _ModulesByName.Find(miMasterNames[j], k) then
miMasters[j] := Pointer(_ModulesByName.Objects[k])
else
Include(miFlags, mfMastersMissing);
miPluginsIndex := High(Integer);
miOfficialIndex := High(Integer);
miCCIndex := High(Integer);
end;
if Length(_Modules) < 1 then
Exit;
sl := TStringList.Create;
try
sl.LoadFromFile(wbPluginsFileName);
for i := 0 to Pred(sl.Count) do begin
s := sl[i];
j := Pos('#', s);
if j > 0 then
Delete(s, j, High(Integer));
s := Trim(s);
lIsActive := wbGameMode in wbSimplePluginsTxt;
if not lIsActive then begin
lIsActive := s.StartsWith('*');
if lIsActive then
Delete(s, 1, 1);
s := Trim(s);
end;
with wbModuleByName(s)^ do
if IsValid then begin
if not (wbGameMode in wbSimplePluginsTxt) then
miPluginsIndex := i;
if lIsActive then begin
Include(miFlags, mfActiveInPluginsTxt);
Include(miFlags, mfActive);
end;
end;
end;
finally
sl.Free;
end;
with wbModuleByName(wbGameName + csDotEsm)^ do
if IsValid then begin
miOfficialIndex := Low(Integer);
Include(miFlags, mfActive);
end;
if wbIsSkyrim then
with wbModuleByName('Update.esm')^ do
if IsValid then begin
miOfficialIndex := -1;
Include(miFlags, mfActive);
end;
for i := Low(wbOfficialDLC) to High(wbOfficialDLC) do
with wbModuleByName(wbOfficialDLC[i])^ do
if IsValid then begin
miOfficialIndex := i;
Include(miFlags, mfActive);
end;
for i := Low(wbCreationClubContent) to High(wbCreationClubContent) do
with wbModuleByName(wbCreationClubContent[i])^ do
if IsValid then begin
miCCIndex := Succ(i);
Include(miFlags, mfActive);
end;
i := Length(_ModulesLoadOrder);
if i > 1 then
wbMergeSort(@_ModulesLoadOrder[0], i, _ModulesLoadOrderCompare);
end;
function wbModulesByLoadOrder: TwbModuleInfos;
begin
wbLoadModules;
Result := Copy(_ModulesLoadOrder);
end;
{ TwbModuleInfo }
procedure TwbModuleInfo.Activate(aActivateMasters: Boolean);
begin
Include(miFlags, mfActive);
if aActivateMasters then
ActivateMasters(True);
end;
procedure TwbModuleInfo.ActivateMasters(aRecursive: Boolean);
var
i: Integer;
begin
for i := High(miMasters) downto Low(miMasters) do
if Assigned(miMasters[i]) then
with miMasters[i]^ do
if not (mfActive in miFlags) then
Activate(aRecursive);
end;
function TwbModuleInfo.IsActive: Boolean;
begin
Result := IsValid and (mfActive in miFlags);
end;
function TwbModuleInfo.IsValid: Boolean;
begin
Result := not ((mfInvalid in miFlags) or (@Self = @_InvalidModule));
end;
{ TwbModuleInfosHelper }
procedure TwbModuleInfosHelper.ActivateMasters;
var
i: Integer;
begin
for i := Low(Self) to High(Self) do
with Self[i]^ do
if mfActive in miFlags then
ActivateMasters(True);
end;
procedure TwbModuleInfosHelper.DeactivateAll;
var
i: Integer;
begin
for i := Low(Self) to High(Self) do
with Self[i]^ do
Exclude(miFlags, mfActive);
end;
function TwbModuleInfosHelper.SimulateLoad: TwbModuleInfos;
var
NewLoadOrder : TwbModuleInfos;
NewLoadOrderCount : Integer;
procedure Load(aModule: PwbModuleInfo);
var
i: Integer;
begin
with aModule^ do begin
if mfLoaded in miFlags then
Exit;
if mfLoading in miFlags then
raise Exception.Create('Modules contain circular references. Can''t load "'+miName+'"');
Include(miFlags, mfLoading);
try
for i := Low(miMasters) to High(miMasters) do
if Assigned(miMasters[i]) then
Load(miMasters[i])
else
raise Exception.Create('Module "'+miName+'" requires master "'+miMasterNames[i]+'" which can not be found');
Include(miFlags, mfLoaded);
NewLoadOrder[NewLoadOrderCount] := aModule;
Inc(NewLoadOrderCount);
finally
Exclude(miFlags, mfLoading);
end;
end;
end;
var
i: Integer;
begin
for i := Low(_Modules) to High(_Modules) do
with _Modules[i] do begin
Exclude(miFlags, mfLoaded);
Exclude(miFlags, mfLoading);
end;
SetLength(NewLoadOrder, Length(_Modules));
NewLoadOrderCount := 0;
for i := Low(Self) to High(Self) do
with Self[i]^ do
if mfActive in miFlags then
Load(Self[i]);
SetLength(NewLoadOrder, NewLoadOrderCount);
Result := NewLoadOrder;
end;
function TwbModuleInfosHelper.ToStrings: TDynStrings;
var
i: Integer;
begin
SetLength(Result ,Length(Self));
for i := Low(Self) to High(Self) do
Result[i] := Self[i].miName;
end;
initialization
finalization
FreeAndNil(_ModulesByName);
end.