Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit FindSwapsWithMissingMaterials;
- var
- slAssets, slMaterialsList: TwbFastStringList;
- f: IInterface;
- function Initialize: integer;
- var
- slContainers: TStringList;
- i: integer;
- begin
- f := AddNewFile;
- if Assigned(f) then begin
- AddMasterIfMissing(f, GetFileName(FileByIndex(0)));
- slContainers := TStringList.Create;
- slAssets := TwbFastStringList.Create;
- ResourceContainerList(slContainers);
- for i := 0 to Pred(slContainers.Count) do
- ResourceList(slContainers[i], slAssets);
- slAssets.Sort;
- wbRemoveDuplicateStrings(slAssets);
- slMaterialsList := TwbFastStringList.Create;
- for i := 0 to Pred(slAssets.Count) do
- if SameText(ExtractFileExt(slAssets[i]), '.BGSM') or SameText(ExtractFileExt(slAssets[i]), '.BGEM') then
- slMaterialsList.Add(slAssets[i]);
- slAssets.Free;
- AddMessage('Assets List Ready.');
- end
- else exit;
- end;
- Function FindMaterialFile(filename: string): string;
- var
- i: integer;
- Material: string;
- begin
- Material := '';
- for i := 0 to Pred(slMaterialsList.Count) do
- if SameText(ExtractFileName(slMaterialsList[i]), filename) then begin
- Material := slMaterialsList[i];
- break;
- end;
- Result := StringReplace(Material, 'materials\', '', [rfIgnoreCase]);
- end;
- procedure SetFlag(elem: IInterface; flagName: string; flagValue: boolean);
- var
- sl: TStringList;
- i: Integer;
- f, f2: Cardinal;
- begin
- sl := TStringList.Create;
- sl.Text := FlagValues(elem);
- f := GetNativeValue(elem);
- for i := 0 to Pred(sl.Count) do
- if SameText(sl[i], flagName) then begin
- if flagValue then
- f2 := f or (1 shl i)
- else
- f2 := f and not (1 shl i);
- if f <> f2 then SetNativeValue(elem, f2);
- Break;
- end;
- sl.Free;
- end;
- procedure RemoveMSWPFromReferenced(MSWP: IInterface);
- var
- i, j: integer;
- referencedOverride, referenced, el: IInterface;
- sl: TStringList;
- begin
- for i := Pred(ReferencedByCount(MSWP)) downto 0 do begin
- referenced := ReferencedByIndex(MSWP, i);
- AddMasterIfMissing(f, GetFileName(GetFile(referenced)));
- referencedOverride := wbCopyElementToFile(referenced, GetFile(MSWP), False, True);
- if ElementExists(referencedOverride, 'XMSP') then
- RemoveElement(referencedOverride, ElementByPath(referencedOverride, 'XMSP'));
- if ElementExists(referencedOverride, 'Model\MODS - Material Swap') then
- Remove(ElementByPath(referencedOverride, 'Model\MODS - Material Swap'));
- end;
- end;
- function Process(e: IInterface): integer;
- var
- MSWPOverride, OverrideMaterialSubstitutions, OverrideSubstitution, MaterialSubstitutions: IInterface;
- MaterialFile, BNAM, SNAM, BNAMPath, SNAMPath: string;
- i: integer;
- BNAMFound, SNAMFound: boolean;
- begin
- if IsMaster(e) then begin
- if Signature(e) = 'MSWP' then begin
- AddMasterIfMissing(f, GetFileName(GetFile(e)));
- MSWPOverride := wbCopyElementToFile(e, f, False, True);
- MaterialSubstitutions := ElementByPath(e, 'Material Substitutions');
- if Assigned(MaterialSubstitutions) then begin
- OverrideMaterialSubstitutions := ElementByPath(MSWPOverride, 'Material Substitutions');
- for i := 0 to Pred(ElementCount(OverrideMaterialSubstitutions)) do begin
- OverrideSubstitution := ElementByIndex(OverrideMaterialSubstitutions, i);
- BNAM := GetElementEditValues(OverrideSubstitution, 'BNAM');
- SNAM := GetElementEditValues(OverrideSubstitution, 'SNAM');
- If Assigned(BNAM) then if Assigned(SNAM) then begin
- BNAMPath := 'Materials\' + BNAM;
- SNAMPath := 'Materials\' + SNAM;
- BNAMFound := true;
- SNAMFound := true;
- if not ContainsText(BNAM, '*') then begin
- if ResourceExists(BNAMPath) = false then begin
- BNAMFound := false;
- //AddMessage('BNAM Missing Material: ' + BNAM);
- MaterialFile := FindMaterialFile(ExtractFileName(BNAMPath));
- if MaterialFile <> '' then begin
- BNAMFound := true;
- //AddMessage('Found material file at: ' + MaterialFile);
- SetElementEditValues(OverrideSubstitution, 'BNAM', MaterialFile);
- end;
- end;
- end;
- if not ContainsText(SNAM, '*') then begin
- if ResourceExists(SNAMPath) = false then begin
- SNAMFound := false;
- //AddMessage('SNAM Missing Material: ' + SNAM);
- MaterialFile := FindMaterialFile(ExtractFileName(SNAMPath));
- if MaterialFile <> '' then begin
- SNAMFound := true;
- //AddMessage('Found material file at: ' + MaterialFile);
- SetElementEditValues(OverrideSubstitution, 'SNAM', MaterialFile);
- end;
- end;
- end;
- if SNAMFound = false or BNAMFound = false then begin
- RemoveElement(OverrideMaterialSubstitutions, OverrideSubstitution);
- end;
- end;
- Inc(i);
- end;
- for i := ElementCount(OverrideMaterialSubstitutions) downto 0 do begin
- OverrideSubstitution := ElementByIndex(OverrideMaterialSubstitutions, i);
- BNAM := GetElementEditValues(OverrideSubstitution, 'BNAM');
- SNAM := GetElementEditValues(OverrideSubstitution, 'SNAM');
- If Assigned(BNAM) then if Assigned(SNAM) then begin
- BNAMPath := 'Materials\' + BNAM;
- SNAMPath := 'Materials\' + SNAM;
- BNAMFound := true;
- SNAMFound := true;
- if not ContainsText(BNAM, '*') then begin
- if ResourceExists(BNAMPath) = false then begin
- BNAMFound := false;
- //AddMessage('BNAM Missing Material: ' + BNAM);
- MaterialFile := FindMaterialFile(ExtractFileName(BNAMPath));
- if MaterialFile <> '' then begin
- BNAMFound := true;
- //AddMessage('Found material file at: ' + MaterialFile);
- SetElementEditValues(OverrideSubstitution, 'BNAM', MaterialFile);
- end;
- end;
- end;
- if not ContainsText(SNAM, '*') then begin
- if ResourceExists(SNAMPath) = false then begin
- SNAMFound := false;
- //AddMessage('SNAM Missing Material: ' + SNAM);
- MaterialFile := FindMaterialFile(ExtractFileName(SNAMPath));
- if MaterialFile <> '' then begin
- SNAMFound := true;
- //AddMessage('Found material file at: ' + MaterialFile);
- SetElementEditValues(OverrideSubstitution, 'SNAM', MaterialFile);
- end;
- end;
- end;
- if SNAMFound = false or BNAMFound = false then begin
- RemoveElement(OverrideMaterialSubstitutions, OverrideSubstitution);
- end;
- end;
- Inc(i);
- end;
- for i := ElementCount(OverrideMaterialSubstitutions) downto 0 do begin
- OverrideSubstitution := ElementByIndex(OverrideMaterialSubstitutions, i);
- BNAM := GetElementEditValues(OverrideSubstitution, 'BNAM');
- SNAM := GetElementEditValues(OverrideSubstitution, 'SNAM');
- If not Assigned(BNAM) or not Assigned(SNAM) then begin
- RemoveElement(OverrideMaterialSubstitutions, OverrideSubstitution);
- end;
- end;
- if ConflictAllForElements(MSWPOverride, e, False, False) <= caNoConflict then begin
- Remove(MSWPOverride);
- end
- else begin
- if ElementCount(OverrideMaterialSubstitutions) = 0 then begin
- RemoveMSWPFromReferenced(MSWPOverride);
- SetFlag(ElementByPath(MSWPOverride, 'Record Header\Record Flags'), 'Deleted', true);
- end;
- end;
- end
- else begin
- RemoveMSWPFromReferenced(MSWPOverride);
- SetFlag(ElementByPath(MSWPOverride, 'Record Header\Record Flags'), 'Deleted', true);
- end;
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement