-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mark as partial if possible.pas
511 lines (436 loc) · 16 KB
/
Mark as partial if possible.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
{
Attempts to figure out if overrides of Worldspaces or Cells are safe to mark as partial,
and, if yes, marks them as partial and deletes some data.
Loosely based on a script by E (01000101)
The rules are:
- Worldspaces:
- never (apparently it isn't actually safe)
- Cells:
- Interiors:
- Must be an ITM
- Must not contain navmeshes
- Must contain the "Persistent" subgroup with at least 1 child
- Must not contain precombine-breaking overrides
- Exteriors:
- Never
- Worldspace persistent cells:
- Always
(Terms like "exists" and "contains" refer to the target plugin only)
}
unit MakePartialForm;
function FilesEqual(file1, file2: IwbFile): boolean;
begin
// Should be faster than comparing the filenames
Result := (GetLoadOrder(file1) = GetLoadOrder(file2));
end;
function getExistingElementOverride(sourceElem: IInterface; targetFile: IwbFile): IInterface;
var
masterElem, curOverride: IINterface;
numOverrides, i: integer;
targetFileName: string;
begin
Result := nil;
masterElem := MasterOrSelf(sourceElem);
targetFileName := GetFileName(targetFile);
// important failsafe
if(FilesEqual(targetFile, GetFile(masterElem))) then begin
Result := sourceElem;
exit;
end;
numOverrides := OverrideCount(masterElem);
for i:=0 to numOverrides-1 do begin
curOverride := OverrideByIndex(masterElem, i);
if (FilesEqual(GetFile(curOverride), targetFile)) then begin
Result := curOverride;
exit;
end;
end;
end;
function elementsDeepEqual(e1, e2: IInterface; prevPath: string): boolean;
var
i: integer;
c1, c2, l1, l2: IInterface;
n1, n2: string;
begin
Result := false;
n1 := DisplayName(e1);
n2 := DisplayName(e2);
if(n1 <> n2) then begin
//AddMessage('DisplayName: '+n1 +' != '+ n2);
exit;
end;
if(ElementCount(e1) <> ElementCount(e2)) then begin
//AddMessage('ElementCount: '+n1+': '+ElementCount(e1) +' != '+n2+': ' + ElementCount(e2));
exit;
end;
//AddMessage('prevPath '+prevPath);
if(prevPath = 'Record Header') then begin
if(n1 <> 'Signature') and (n1 <> 'Record Flags') then begin
Result := true;
exit;
end;
end;
l1 := LinksTo(e1);
l2 := LinksTo(e2);
if(assigned(l1) or assigned(l2)) then begin
if(assigned(l1) and assigned(l2)) then begin
if(not Equals(MasterOrSelf(l1), MasterOrSelf(l2))) then begin
//AddMessage('LinksTo 1: '+n1 +' != '+ n2);
exit;
end;
end else begin
//AddMessage('LinksTo 2: '+n1 +' != '+ n2);
exit;
end;
end else begin
if(GetEditValue(e1) <> GetEditValue(e2)) then begin
//AddMessage('GetEditValue: '+n1+': '+GetEditValue(e1) +' != '+n2+': '+ GetEditValue(e2));
exit;
end;
for i:=0 to ElementCount(e1)-1 do begin
c1 := ElementByIndex(e1, i);
c2 := ElementByIndex(e2, i);
if(not elementsDeepEqual(c1, c2, n1)) then begin
//AddMessage('Not equal at '+n1 +' != '+ n2);
exit;
end;
end;
end;
Result := true;
end;
function hasPartialFlag(e: IInterface): boolean;
begin
Result := (GetElementNativeValues( e, 'Record Header\Record Flags' ) and $4000) <> 0;
end;
procedure addPartialFlag(e: IInterface);
begin
SetElementNativeValues( e, 'Record Header\Record Flags', GetElementNativeValues( e, 'Record Header\Record Flags' ) or $4000 );
end;
function getPrevOverride(e: IInterface): IInterface;
var
i, numOverrides: integer;
prevOverride, elemMaster, curOverride, elemFile: IInterface;
begin
elemMaster := Master(e);
numOverrides := OverrideCount(elemMaster);
// only check overrides from masters of current file
elemFile := GetFile(e);
Result := nil;
if(HasMaster(elemFile, GetFileName(GetFile(elemMaster)))) then begin
Result := elemMaster;
end;
Result := elemMaster;
if(numOverrides <= 1) then begin
// the given element is the only override anyway
exit;
end;
// prevOverride := elemMaster;
for i:=0 to numOverrides-1 do begin
curOverride := OverrideByIndex(elemMaster, i);
if(hasPartialFlag(curOverride)) then continue;
if(not HasMaster(elemFile, GetFileName(GetFile(curOverride)))) then continue; // skip non-master override
if(Equals(curOverride, e)) then begin
// AddMessage('found '+GetFileName(GetFile(prevOverride)));
Result := prevOverride;
exit;
end;
prevOverride := curOverride;
end;
Result := elemMaster;
end;
function isITM(e: IInterface): boolean;
var
prevOverride: IInterface;
begin
prevOverride := getPrevOverride(e);
Result := elementsDeepEqual(e, prevOverride, '');
end;
function isInterior(cell: IInterface): boolean;
var
dataFlags: cardinal;
begin
// interior flag: 1
dataFlags := GetElementNativeValues(cell, 'DATA');
Result := (dataFlags and 1) <> 0;
end;
function scanCellForLandAndNavmesh(cell: IInterface): boolean;
var
tempCell, r: IInterface;
i: integer;
sig: string;
begin
Result := false;
// 9 is temporary
tempCell := FindChildGroup(ChildGroup(cell), 9, cell);
for i := 0 to ElementCount(tempCell)-1 do begin
r := ElementByIndex(tempCell, i);
sig := Signature(r);
if (sig = 'NAVM') or (sig = 'LAND') then begin
AddMessage(' -> Found a '+sig +' record');
exit;
end;
end;
Result := true;
end;
function scanCellForPrecombreakers(cellPrevOverride, modFile: IInterface): boolean;
var
tempCell, xpri, xcri, r, refOverride: IInterface;
i: integer;
begin
Result := false;
xpri := ElementByPath(cellPrevOverride, 'XPRI');
xcri := ElementByPath(cellPrevOverride, 'XCRI');
if((not assigned(xpri)) and (not assigned(xcri) )) then begin
// disregard precombine-less cells
Result := true;
exit;
end;
//hasPrecombines
// 9 is temporary
tempCell := FindChildGroup(ChildGroup(cellPrevOverride), 9, cellPrevOverride);
for i := 0 to ElementCount(tempCell)-1 do begin
r := ElementByIndex(tempCell, i);
if(HasPrecombinedMesh(r)) then begin
refOverride := getExistingElementOverride(r, modFile);
if(assigned(refOverride)) then begin
if(elementsDeepEqual(r, refOverride, '')) then begin
AddMessage(' WARNING: found ITM ref: '+FullPath(refOverride));
end else begin
AddMessage(' -> Found precombine breaker: '+FullPath(refOverride));
exit;
end;
end;
end;
end;
Result := true;
end;
function scanCell(cellPrevOverride, cell: IInterface): boolean;
begin
Result := false;
if(not scanCellForLandAndNavmesh(cell)) then begin
exit;
end;
if(not scanCellForPrecombreakers(cellPrevOverride, GetFile(cell))) then begin
exit;
end;
Result := true;
end;
function checkInteriorCell(cell: IInterface): boolean;
var
persistentCell, prevCellOverride: IInterface;
begin
Result := false;
// must have persistent subgroup
// 9 is temporary
// 8 seems to be persistent
persistentCell := FindChildGroup(ChildGroup(cell), 8, cell);
// must exist in the same file. does it also have to have at least one thing in there? probably yes
if(not assigned(persistentCell)) then begin
AddMessage(' -> has no persistent subgroup');
exit;
end;
if(ElementCount(persistentCell) <= 0) then begin
AddMessage(' -> has no persistent refs');
exit;
end;
prevCellOverride := getPrevOverride(cell);
// must be ITM
if(not elementsDeepEqual(cell, prevCellOverride, '')) then begin
AddMessage(' -> is not an ITM');
exit;
end;
if(not scanCell(prevCellOverride, cell)) then begin
exit;
end;
Result := true;
// cell must not contain NAVM
// cell must not break precombines
end;
function findPersistentCell(worldspace: IInterface): IInterface;
var
wrldgrup, block: IInterface;
i: integer;
flags: cardinal;
begin
Result := nil;
wrldgrup := ChildGroup(worldspace);
for i := 0 to ElementCount(wrldgrup)-1 do begin
block := ElementByIndex(wrldgrup, i);
if(Signature(block) = 'CELL') then begin
flags := GetElementNativeValues(block, 'Record Header\Record Flags');
// persistent = 1024
if((flags and 1024) <> 0) then begin
Result := block;
exit;
end;
end;
end;
end;
function checkExteriorCell(cell: IInterfaec): boolean;
var
flags: cardinal;
prevCellOverride, parentWs, wsPersistentCell, wspcGroup: IInterface;
begin
Result := false;
// if this is the persistent cell: true
flags := GetElementNativeValues(cell, 'Record Header\Record Flags');
// persistent = 1024
if((flags and 1024) <> 0) then begin
Result := true;
exit
end;
// if this is NOT the persistent cell:
// I don't know under which circumstances this is actually safe.
// So far it seems the answer is "never".
exit;
prevCellOverride := getPrevOverride(cell);
// - parent worldspace's persistent cell must be in current file
parentWs := LinksTo(ElementByPath(cell, 'Worldspace'));
// how do I find the persistent cell?
wsPersistentCell := findPersistentCell(parentWs);
if(not assigned(wsPersistentCell)) then begin
AddMessage(' -> parent worldspace has no persistent cell in the plugin');
exit;
end;
// this isn't actually relevant
{
wspcGroup := FindChildGroup(ChildGroup(wsPersistentCell), 8, wsPersistentCell);
if(ElementCount(wspcGroup) < 1) then begin
AddMessage(' -> persistent cell of parent worldspace is empty');
exit;
end;
}
// must be ITM
if(not elementsDeepEqual(cell, prevCellOverride, '')) then begin
AddMessage(' -> is not an ITM');
exit;
end;
// - cell must not contain LAND
// - cell must not contain NAVM
// - must not break precombines
if(not scanCell(prevCellOverride, cell)) then begin
exit;
end;
Result := true;
end;
procedure truncateCell(e: IInterface);
begin
RemoveElement( e, 'DATA' );
RemoveElement( e, 'VISI' );
RemoveElement( e, 'RVIS' );
RemoveElement( e, 'PCMB' );
RemoveElement( e, 'XCLC' );
RemoveElement( e, 'XCLL' );
RemoveElement( e, 'CNAM' );
RemoveElement( e, 'ZNAM' );
RemoveElement( e, 'TVDT' );
RemoveElement( e, 'MHDT' );
RemoveElement( e, 'LTMP' );
RemoveElement( e, 'XCLW' );
RemoveElement( e, 'XCLR' );
RemoveElement( e, 'XLCN' );
RemoveElement( e, 'XWCN' );
RemoveElement( e, 'XWCU' );
RemoveElement( e, 'XCWT' );
RemoveElement( e, 'XOWN' );
RemoveElement( e, 'XRNK' );
RemoveElement( e, 'XILL' );
RemoveElement( e, 'XILW' );
RemoveElement( e, 'XWEM' );
RemoveElement( e, 'XCCM' );
RemoveElement( e, 'XCAS' );
RemoveElement( e, 'XEZN' );
RemoveElement( e, 'XCMO' );
RemoveElement( e, 'XCIM' );
RemoveElement( e, 'XGDR' );
RemoveElement( e, 'XPRI' );
RemoveElement( e, 'XCRI' );
end;
procedure truncateWorldspace(e: IInterface);
begin
RemoveElement( e, 'RNAM' );
RemoveElement( e, 'MHDT' );
RemoveElement( e, 'FULL' );
RemoveElement( e, 'WCTR' );
RemoveElement( e, 'LTMP' );
RemoveElement( e, 'XEZN' );
RemoveElement( e, 'XLCN' );
RemoveElement( e, 'Parent' );
RemoveElement( e, 'CNAM' );
RemoveElement( e, 'NAM2' );
RemoveElement( e, 'NAM3' );
RemoveElement( e, 'NAM4' );
RemoveElement( e, 'DNAM' );
RemoveElement( e, 'ICON' );
RemoveElement( e, 'Cloud Model' );
RemoveElement( e, 'MNAM' );
RemoveElement( e, 'ONAM' );
RemoveElement( e, 'NAMA' );
RemoveElement( e, 'DATA' );
RemoveElement( e, 'Object Bounds' );
RemoveElement( e, 'ZNAM' );
RemoveElement( e, 'NNAM' );
RemoveElement( e, 'XWEM' );
RemoveElement( e, 'TNAM' );
RemoveElement( e, 'UNAM' );
RemoveElement( e, 'World Default Level Data' );
RemoveElement( e, 'OFST' );
RemoveElement( e, 'CLSZ' );
end;
function checkWorldSpace(worldspace: IInterface): boolean;
begin
// must be ITM
Result := IsITM(worldspace);
if(not Result) then begin
AddMessage(' -> not an ITM');
end;
end;
function Process(e: IInterface): integer;
var
s, curHash, prevHash: string;
f: integer;
prevOverride: IInterface;
begin
s := Signature(e);
if( s <> 'CELL' ) and ( s <> 'WRLD' ) then exit;
AddMessage('Checking '+FullPath(e));
if (IsMaster( e )) then begin
AddMessage(' -> not an override, skipping');
Exit; // Can't make a master record partial - durr
end;
if (hasPartialFlag(e)) then begin
AddMessage(' -> has partial flag already');
// done already
exit;
end;
{
// check stuff
if(s = 'WRLD') then begin
if(not checkWorldSpace(e)) then begin
AddMessage(' => SKIPPING');
exit;
end;
AddMessage(' => PROCESSING');
addPartialFlag(e);
truncateWorldspace(e);
exit;
end;
}
if(s = 'CELL' ) then begin
if(isInterior(e)) then begin
if(not checkInteriorCell(e)) then begin
AddMessage(' => SKIPPING');
exit;
end;
end else begin
if(not checkExteriorCell(e)) then begin
AddMessage(' => SKIPPING');
exit;
end;
end;
AddMessage(' => PROCESSING');
addPartialFlag(e);
truncateCell(e);
end;
end;
end.