|
| 1 | +{ |
| 2 | + Find and Replace References for Interactive Clutter SSE |
| 3 | +
|
| 4 | + Author: fireundubh <fireundubh@gmail.com> |
| 5 | + Version: 1.0 |
| 6 | +} |
| 7 | + |
| 8 | +unit UserScript; |
| 9 | + |
| 10 | +const |
| 11 | + ICSSE_Name = 'Interactive Clutter SSE'; |
| 12 | + ICSSE_FileName = 'Interactive Clutter SSE.esp'; |
| 13 | + |
| 14 | +var |
| 15 | + WorkingFile : IwbFile; |
| 16 | + |
| 17 | + |
| 18 | +function Initialize: integer; |
| 19 | +begin |
| 20 | + WorkingFile := nil; |
| 21 | +end; |
| 22 | + |
| 23 | + |
| 24 | +function Process(e: IwbElement): integer; |
| 25 | +begin |
| 26 | + if not Assigned(WorkingFile) then |
| 27 | + try |
| 28 | + WorkingFile := AddNewFileName(ICSSE_Name + ' - ' + GetFileName(GetFile(e)), False); |
| 29 | + except |
| 30 | + WorkingFile := FileByName(ICSSE_Name + ' - ' + GetFileName(GetFile(e))); |
| 31 | + end; |
| 32 | + |
| 33 | + if Assigned(WorkingFile) then |
| 34 | + AddMasterIfMissing(WorkingFile, GetFileName(GetFile(e)), True); |
| 35 | + |
| 36 | + Exit; |
| 37 | +end; |
| 38 | + |
| 39 | + |
| 40 | +function Finalize: integer; |
| 41 | +var |
| 42 | + f : IwbFile; |
| 43 | + |
| 44 | + Statics : IwbMainRecord; |
| 45 | + MiscObjects : IwbMainRecord; |
| 46 | + |
| 47 | + StaticForms : IwbElement; |
| 48 | + MiscObjectForms : IwbElement; |
| 49 | + |
| 50 | + StaticRecord : IwbMainRecord; |
| 51 | + |
| 52 | + i : integer; |
| 53 | +begin |
| 54 | + if not Assigned(WorkingFile) then |
| 55 | + Exit; |
| 56 | + |
| 57 | + f := FileByName(ICSSE_FileName); |
| 58 | + |
| 59 | + if not Assigned(f) then |
| 60 | + Exit; |
| 61 | + |
| 62 | + AddMasterIfMissing(WorkingFile, ICSSE_FileName, True); |
| 63 | + |
| 64 | + Statics := RecordByFormID(f, $0600085C, False); |
| 65 | + MiscObjects := RecordByFormID(f, $0600085D, False); |
| 66 | + |
| 67 | + StaticForms := ElementByName(Statics, 'FormIDs'); |
| 68 | + MiscObjectForms := ElementByName(MiscObjects, 'FormIDs'); |
| 69 | + |
| 70 | + for i := 0 to Pred(ElementCount(StaticForms)) do |
| 71 | + begin |
| 72 | + StaticRecord := MasterOrSelf(LinksTo(ElementByIndex(StaticForms, i))); |
| 73 | + TryToReplaceReference(StaticRecord, MiscObjectForms, i); |
| 74 | + end; |
| 75 | +end; |
| 76 | + |
| 77 | + |
| 78 | +procedure AddMastersIfMissing(AFile: IwbFile; ARecord: IwbMainRecord); |
| 79 | +var |
| 80 | + f : IwbFile; |
| 81 | + i : integer; |
| 82 | +begin |
| 83 | + f := GetFile(ARecord); |
| 84 | + AddMasterIfMissing(AFile, GetFileName(f), True); |
| 85 | + for i := 0 to Pred(MasterCount(f)) do |
| 86 | + AddMasterIfMissing(AFile, GetFileName(MasterByIndex(f, i)), True); |
| 87 | +end; |
| 88 | + |
| 89 | + |
| 90 | +procedure TryToReplaceReference(AParent: IwbMainRecord; AForms: IwbElement; AIndex: integer); |
| 91 | +var |
| 92 | + r : IwbMainRecord; |
| 93 | + o : IwbMainRecord; |
| 94 | + MiscObjectRecord : IwbMainRecord; |
| 95 | + WorkingRecord : IwbMainRecord; |
| 96 | + i : integer; |
| 97 | +begin |
| 98 | + AddMessage('Processing: ' + Name(AParent)); |
| 99 | + |
| 100 | + MiscObjectRecord := LinksTo(ElementByIndex(AForms, AIndex)); |
| 101 | + |
| 102 | + AddMastersIfMissing(WorkingFile, MiscObjectRecord); |
| 103 | + |
| 104 | + for i := 0 to Pred(ReferencedByCount(AParent)) do begin |
| 105 | + r := ReferencedByIndex(AParent, i); |
| 106 | + |
| 107 | + // skip references in main plugin |
| 108 | + if GetFileName(GetFile(r)) = 'Interactive Clutter SSE.esp' then |
| 109 | + Continue; |
| 110 | + |
| 111 | + o := WinningOverride(r); |
| 112 | + |
| 113 | + if not ContainsStr('REFR', Signature(o)) then |
| 114 | + Continue; |
| 115 | + |
| 116 | + if not ContainsStr('FURN MSTT STAT', Signature(BaseRecord(o))) then |
| 117 | + Continue; |
| 118 | + |
| 119 | + try |
| 120 | + AddMastersIfMissing(WorkingFile, o); |
| 121 | + WorkingRecord := wbCopyElementToFile(o, WorkingFile, False, True); |
| 122 | + except |
| 123 | + on E: Exception do |
| 124 | + AddMessage(Name(o) + ' cannot be copied because: ' + E.Message); |
| 125 | + Continue; |
| 126 | + end; |
| 127 | + |
| 128 | + SetElementNativeValues(WorkingRecord, 'NAME', GetLoadOrderFormID(MiscObjectRecord)); |
| 129 | + end; |
| 130 | +end; |
| 131 | + |
| 132 | + |
| 133 | +function FileByName(AFileName: string): IwbFile; |
| 134 | +var |
| 135 | + f : IwbFile; |
| 136 | + i : integer; |
| 137 | +begin |
| 138 | + Result := nil; |
| 139 | + |
| 140 | + for i := 0 to Pred(FileCount) do |
| 141 | + begin |
| 142 | + f := FileByIndex(i); |
| 143 | + |
| 144 | + if SameText(AFileName, GetFileName(f)) then |
| 145 | + begin |
| 146 | + Result := f; |
| 147 | + Break; |
| 148 | + end; |
| 149 | + end; |
| 150 | +end; |
| 151 | + |
| 152 | +end. |
0 commit comments