enderalse/_build/Find respawning items.pas
2024-05-07 12:02:13 +02:00

113 lines
2.7 KiB
ObjectPascal

{
Create patch plugin with "No Respawn" flag on references.
}
unit FindRespawningItems;
var
plugin: IInterface;
bNewFile: bool;
bSkipSection: bool;
sFormType: string;
function Initialize: Integer;
begin
if not InputQuery('Filter By Form', 'WEAP, ARMO, etc.', sFormType) then
Exit;
if sFormType = '' then
Exit;
bNewFile := False;
if MessageDlg('Create a new plugin [YES] or set flags, modifying source files [NO]?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
bNewFile := True;
end;
function HasScript(e: IInterface; aScript: string): Boolean;
var
i: integer;
begin
if Name(e) = 'scriptName' then begin
Result := SameText(GetEditValue(e), aScript);
bSkipSection := True;
end else
for i := 0 to Pred(ElementCount(e)) do begin
Result := HasScript(ElementByIndex(e, i), aScript);
if bSkipSection then begin
bSkipSection := False;
Exit;
end;
if Result then Exit;
end;
end;
function Process(e: IInterface): Integer;
var
sFormID: string;
r: IInterface;
begin
// process only references
if Signature(e) <> 'REFR' then
Exit;
// only master references
if not IsMaster(e) then
Exit;
// but work with the current winning override
e := WinningOverride(e);
// references of activator only
if Signature(BaseRecord(e)) <> sFormType then
Exit;
if GetElementEditValues(e, 'Record Header\Record Flags\No Respawn') = '1' then
exit;
if bNewFile then begin
if not Assigned(plugin) then begin
plugin := AddNewFile;
if not Assigned(plugin) then begin
Result := 1;
Exit;
end;
end;
end;
try
if Assigned(plugin) then begin
// add masters before copying as override for parent CELL
AddRequiredElementMasters(LinksTo(ElementByIndex(e, 0)), plugin, False);
// and REFR itself
AddRequiredElementMasters(e, plugin, False);
// winning cell override
r := WinningOverride(LinksTo(ElementByName(e, 'Cell')));
if GetFile(r) <> plugin then begin
AddRequiredElementMasters(r, plugin, False);
wbCopyElementToFile(r, plugin, False, True);
end;
// copy reference as override
e := wbCopyElementToFile(e, plugin, False, True);
end;
// set No Respawn flag
SetElementNativeValues(e, 'Record Header\Record Flags\No Respawn', 1);
except
on Ex: Exception do begin
AddMessage('Failed to copy: ' + FullPath(e));
AddMessage(' reason: ' + Ex.Message);
end;
end;
end;
function Finalize: integer;
begin
if Assigned(plugin) then
SortMasters(plugin);
end;
end.