-
Notifications
You must be signed in to change notification settings - Fork 2
/
FixXPRI.pas
459 lines (365 loc) · 14.8 KB
/
FixXPRI.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
{
to be run on cells in the target file only
}
unit userscript;
uses praUtil;
const
configFile = ScriptsPath + 'FixXPRI.cfg';
var
settingWorkshopString: string;
settingTargetLayer: string;
settingLinkToWorkshop: boolean;
settingScraplinkToNew: boolean;
settingMoveToLayer: boolean;
targetWorkshop: IInterface;
targetLayer: IInterface;
WorkshopStackedItemParentKEYWORD: IInterface;
WorkshopLinkContainer: IInterface;
procedure loadConfig();
var
i, j, breakPos: integer;
curLine, curKey, curVal: string;
lines : TStringList;
begin
// default
settingLinkToWorkshop := false;
settingScraplinkToNew := false;
settingMoveToLayer := false;
settingTargetLayer := '';
settingWorkshopString := '';
if(not FileExists(configFile)) then begin
exit;
end;
lines := TStringList.create;
lines.LoadFromFile(configFile);
for i:=0 to lines.count-1 do begin
curLine := lines[i];
breakPos := -1;
for j:=1 to length(curLine) do begin
if(curLine[j] = '=') then begin
breakPos := j;
break;
end;
end;
if breakPos <> -1 then begin
curKey := trim(copy(curLine, 0, breakPos-1));
curVal := trim(copy(curLine, breakPos+1, length(curLine)));
if(curKey = 'WorkshopString') then begin
settingWorkshopString := curVal;
end else if(curKey = 'LinkToWorkshop') then begin
settingLinkToWorkshop := StrToBool(curVal);
end else if(curKey = 'MoveToLayer') then begin
settingMoveToLayer := StrToBool(curVal);
end else if(curKey = 'ScraplinkToNew') then begin
settingScraplinkToNew := StrToBool(curVal);
end else if(curKey = 'TargetLayer') then begin
settingTargetLayer := curVal;
end;
end;
end;
lines.free();
end;
procedure saveConfig();
var
lines : TStringList;
begin
lines := TStringList.create;
lines.add('WorkshopString='+settingWorkshopString);
lines.add('TargetLayer='+settingTargetLayer);
lines.add('LinkToWorkshop='+BoolToStr(settingLinkToWorkshop));
lines.add('ScraplinkToNew='+BoolToStr(settingScraplinkToNew));
lines.add('MoveToLayer='+BoolToStr(settingMoveToLayer));
lines.saveToFile(configFile);
lines.free();
end;
function showConfigDialog(): boolean;
var
frm: TForm;
cbScraplinkToNew, cbLinkToWorkshop, cbMoveToLayer: TCheckBox;
editLayer, editWorkshopStr: TEdit;
resultCode: cardinal;
btnOkay, btnCancel: TButton;
yOffset: integer;
begin
//loadConfig();
Result := false;
frm := CreateDialog('Fix XPRI Settings', 350, 300);
cbScraplinkToNew := CreateCheckbox(frm, 10, 10, 'Scraplink to new ref');
if(settingScraplinkToNew) then begin
cbScraplinkToNew.checked := true;
end;
yOffset := 20;
cbLinkToWorkshop := CreateCheckbox(frm, 10, yOffset+ 10, 'Link containers to Workshop');
if(settingLinkToWorkshop) then begin
cbLinkToWorkshop.checked := true;
end;
yOffset := yOffset + 20;
CreateLabel(frm, 10, yOffset + 20, 'Workshop ref to use:');
editWorkshopStr := CreateInput(frm, 10, yOffset + 40, settingWorkshopString);
editWorkshopStr.width := 260;
yOffset := yOffset + 80;
cbMoveToLayer := CreateCheckbox(frm, 10, yOffset, 'Move deleted to layer');
cbMoveToLayer.checked := settingMoveToLayer;
CreateLabel(frm, 10, yOffset + 23, 'Layer:');
editLayer := CreateInput(frm, 10, yOffset + 40, settingTargetLayer);
editLayer.width := 260;
yOffset := yOffset + 80;
btnOkay := CreateButton(frm, 90, yOffset, ' OK ');
btnOkay.ModalResult := mrYes;
btnOkay.Default := true;
btnCancel := CreateButton(frm, 180, yOffset, 'Cancel');
btnCancel.ModalResult := mrCancel;
resultCode := frm.showModal();
if(resultCode = mrYes) then begin
settingScraplinkToNew := cbScraplinkToNew.checked;
settingLinkToWorkshop := cbLinkToWorkshop.checked;
settingMoveToLayer := cbMoveToLayer.checked;
settingWorkshopString := trim(editWorkshopStr.text);
settingTargetLayer := trim(editLayer.text);
saveConfig();
Result := true;
if(settingLinkToWorkshop) then begin
if(settingWorkshopString <> '') then begin
targetWorkshop := findFormByString(settingWorkshopString);
if(not assigned(targetWorkshop)) then begin
AddMessage('Failed to find workshop "'+settingWorkshopString+'"');
Result := false;
end else begin
if(Signature(targetWorkshop) <> 'REFR') then begin
AddMessage('Form "'+settingWorkshopString+'" is not a REFR');
Result := false;
end;
end;
end;
end;
if(settingMoveToLayer) then begin
if(settingTargetLayer <> '') then begin
targetLayer := FindObjectByEdidAndSignature(settingTargetLayer, 'LAYR');
if(not assigned(targetLayer)) then begin
AddMessage('Failed to find layer "'+targetLayer+'"');
Result := false;
end;
end;
end;
end;
frm.free();
end;
// Called before processing
// You can remove it if script doesn't require initialization code
function Initialize: integer;
begin
Result := 1;
WorkshopLinkContainer := AbsStrToForm('Fallout4.esm:0002682F');
if(not assigned(WorkshopLinkContainer)) then begin
AddMessage('Failed to find WorkshopLinkContainer');
exit;
end;
WorkshopStackedItemParentKEYWORD := AbsStrToForm('Fallout4.esm:001C5EDD');
if(not assigned(WorkshopStackedItemParentKEYWORD)) then begin
AddMessage('Failed to find WorkshopStackedItemParentKEYWORD');
exit;
end;
Result := 0;
loadConfig();
if( not showConfigDialog()) then begin
Result := 1;
exit;
end;
end;
procedure replaceFormIn(container, search, repl, targetFile: IInterface);
var
i: integer;
child, lt: IInterface;
begin
lt := LinksTo(container);
if(assigned(lt)) then begin
//lt := getOrCreateElementOverride(lt, targetFile);
if(IsSameForm(lt, search)) then begin
//AddMessage('Found one');
//AddMessage(' '+FullPath(container));
setLinksTo(container, repl);
end;
end;
for i := 0 to ElementCount(container)-1 do begin
child := ElementByIndex(container, i);
// AddMessage(prefix+DisplayName(child)+'='+GetEditValue(child));
replaceFormIn(child, search, repl, targetFile);
end;
end;
procedure replaceFormUsages(formSearch, formReplace, targetFile: IInterface);
var
i, j, numRefs: cardinal;
curRef, curRefOverride: IInterface;
searchEdid: string;
begin
AddMessage('trying to replace '+Name(formSearch)+' with '+Name(formReplace));
for i := ReferencedByCount(formSearch)-1 downto 0 do begin
curRef := ReferencedByIndex(formSearch, i);
if(Signature(curRef) = 'TES4') then continue;
AddMessage(' -> trying to do it in '+Name(curRef));
curRefOverride := getOrCreateElementOverride(curRef, targetFile);
replaceFormIn(curRefOverride, formSearch, formReplace, targetFile);
end;
end;
procedure markAsDeleted(e: IInterface);
var
baseForm: IInterface;
baseFormSig: string;
prevOverride, xesp: IInterface;
prevZpos: integer;
isRefMaster, inDummyPrecomb, canDummyPrecomb, newPersistence: boolean;
begin
prevOverride := nil;
isRefMaster := isMaster(e);
if(not isRefMaster) then begin
prevOverride := getWinningOverrideBefore(e, GetFile(e));
end;
RemoveElement(e, 'Enable Parent');
RemoveElement(e, 'XTEL');
// ... remove anything else here
// linked refs maybe
RemoveElement(e, 'Linked References');
// just in case
RemoveElement(e, 'XLRL');
RemoveElement(e, 'XLRT');
//if(shouldChangePersist) then begin
// maybe unpersist
if(isRefMaster) then begin
// yes
newPersistence := false;
// SetIsPersistent(e, False);
end else if (assigned(prevOverride)) then begin
// apply the state of the prev one, just to be safe
newPersistence := GetIsPersistent(prevOverride);
//SetIsPersistent(e, GetIsPersistent(prevOverride));
end;
//end;
// set to disabled
SetIsInitiallyDisabled(e, True);
// set layer
baseForm := pathLinksTo(e, 'NAME');
SetIsPersistent(e, newPersistence);
// add enabled opposite of player (true - silent)
xesp := Add(e, 'XESP', True);
if Assigned(xesp) then begin
SetElementNativeValues(xesp, 'Reference', $14); // Player ref
SetElementNativeValues(xesp, 'Flags', 1); // opposite of parent flag
end;
end;
procedure setLinkedRef(fromRef, toRef, keyword: IInterface);
var
linkedRefs, lnk, curKw: IInterface;
i: integer;
begin
linkedRefs := ElementByPath(fromRef, 'Linked References');
if(not assigned(linkedRefs)) then begin
linkedRefs := Add(fromRef, 'Linked References', True);
// get the freebie
lnk := ElementByIndex(linkedRefs, 0);
end else begin
for i:=0 to ElementCount(linkedRefs)-1 do begin
lnk := ElementByIndex(linkedRefs, i);
curKw := pathLinksTo(lnk, 'Keyword/Ref');
if(not assigned(keyword)) then begin
if(not assigned(curKw)) then begin
// relink this
setPathLinksTo(lnk, 'Ref', toRef);
exit;
end;
end else begin
if (isSameForm(curKw, keyword)) then begin
// relink this
setPathLinksTo(lnk, 'Ref', toRef);
exit;
end;
end;
end;
// otherwise, add a new one
lnk := ElementAssign(linkedRefs, HighInteger, nil, False);
end;
if(assigned(keyword)) then begin
setPathLinksTo(lnk, 'Keyword/Ref', keyword);
end;
setPathLinksTo(lnk, 'Ref', toRef);
end;
procedure fixXpriEntry(e, targetFile: IInterface);
var
targetOverride, newRef, baseForm: IInterface;
begin
targetOverride := getOrCreateElementOverride(e, targetFile);
if(isConsideredDeleted(targetOverride)) then begin
AddMessage(' -> Ref deleted already');
exit;
end;
newRef := wbCopyElementToFile(targetOverride, targetFile, True, True);
replaceFormUsages(MasterOrSelf(e), newRef, targetFile);
markAsDeleted(targetOverride);
if(assigned(targetLayer) and settingMoveToLayer) then begin
setPathLinksTo(targetOverride, 'XLYR', targetLayer);
end;
if(settingScraplinkToNew) then begin
// - link through scriptlink KW to the new ref.. which requires the new ref to be persistent?
SetIsPersistent(newRef, true);
setLinkedRef(targetOverride, newRef, WorkshopStackedItemParentKEYWORD);
end;
baseForm := pathLinksTo(targetOverride, 'NAME');
if (Signature(baseForm) = 'CONT') then begin
if (settingLinkToWorkshop) and (assigned(targetWorkshop)) then begin
setLinkedRef(targetOverride, targetWorkshop, WorkshopLinkContainer);
end;
end;
// now, stuff
// - link to Workshop using WorkshopLinkContainer?
end;
procedure processCellXpri(e, targetFile: IInterface);
var
j: integer;
xrpi, curRef: IInterface;
begin
AddMessage('Checking XPRI from '+GetFileName(GetFile(e)));
xrpi := ElementByPath(e, 'XPRI');
if(assigned(xrpi)) then begin
for j:=0 to ElementCount(xrpi)-1 do begin
curRef := linksTo(ElementByIndex(xrpi, j));
AddMessage('curRef: '+Name(curRef));
fixXpriEntry(curRef, targetFile);
end;
end;
end;
// called for every record selected in xEdit
function Process(e: IInterface): integer;
var
targetFile: IInterface;
xrpi, cellMaster, curOverride, curRef: IInterface;
i, j, numOverrides: integer;
begin
Result := 0;
if (Signature(e) <> 'CELL') then exit;
if (GetIsPersistent(e)) then exit;
xrpi := ElementByPath(e, 'XPRI');
if(assigned(xrpi)) then begin
exit;
end;
targetFile := getFile(e);
cellMaster := MasterOrSelf(e);
if(isSameFile(GetFile(cellMaster), targetFile)) then begin
exit;
end;
AddMessage('Relevant cell: ' + Name(e));
processCellXpri(cellMaster, targetFile);
numOverrides := OverrideCount(cellMaster);
for i:=0 to numOverrides-1 do begin
curOverride := OverrideByIndex(cellMaster, i);
if(isSameFile(GetFile(curOverride), targetFile)) then begin
break;
end;
processCellXpri(curOverride, targetFile);
end;
end;
// Called after processing
// You can remove it if script doesn't require finalization code
function Finalize: integer;
begin
Result := 0;
end;
end.