Advertisement
Samatter

Enchant-Cleaner-Patcher.pas

Apr 22nd, 2019 (edited)
179
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.96 KB | None
  1. unit userscript;
  2.  
  3. uses 'lib\mxpf';
  4.  
  5. function Initialize: integer;
  6.  
  7. var
  8.     badlist_A, badlist_W, goodlist_A, goodlist_W: IInterface;
  9.     badrec, goodrec, badforms_A, badforms_W, newbadform, goodforms_A, goodforms_W, newgoodform: IInterface;
  10.     KWDA, VMAD: IInterface;
  11.     index, P0, P1, P2, object_index, N: integer;
  12.     item_edid, item_name: string;
  13.     ArmorScriptExceptions, WeaponScriptExceptions: TStringList;
  14.     badobject, goodobject: TObject;
  15.     badobject_list, goodobject_list: TList;
  16.     ArmorIsPlayable, ArmorHasEnch, ArmorHasKeyword, ArmorIsInList: boolean;
  17.     WeaponIsPlayable, WeaponHasEnch, WeaponHasKeyword, WeaponIsInList: boolean;
  18.     NewObject: boolean;
  19.  
  20. begin
  21.     InitializeMXPF;
  22.     DefaultOptionsMXPF;
  23.     PatchFileByName('EnchantCleaner.esp');
  24.    
  25. //----------------------------------------------------------------// SCRIPT EXCEPTIONS. DON'T EDIT ANYTHING ABOVE.
  26.     ArmorScriptExceptions := TStringList.Create;
  27.     //  ArmorScriptExceptions.Add('???');
  28.     //  ArmorScriptExceptions.Add('???');
  29.     //  ArmorScriptExceptions.Add('???');
  30.     //  ArmorScriptExceptions.Add('???');
  31.    
  32.     WeaponScriptExceptions := TStringList.Create;
  33.         WeaponScriptExceptions.Add('SilverSwordScript');
  34.         WeaponScriptExceptions.Add('DLC1EnhancedCrossBowAddPerkScript');
  35.     //  WeaponScriptExceptions.Add('???');
  36.     //  WeaponScriptExceptions.Add('???');
  37. //----------------------------------------------------------------// SCRIPT EXCEPTIONS. DON'T EDIT ANYTHING BELOW.
  38.    
  39.     badlist_A := MainRecordByEditorID(GroupBySignature(mxPatchFile, 'FLST'), 'EC_ArmorList');
  40.     badforms_A := Add(badlist_A, 'FormIDs', true);
  41.     Remove(ElementByIP(badlist_A, 'FormIDs\[0]'));
  42.    
  43.     goodlist_A := MainRecordByEditorID(GroupBySignature(mxPatchFile, 'FLST'), 'EC_ArmorList_NoEnch');
  44.     goodforms_A := Add(goodlist_A, 'FormIDs', true);
  45.     Remove(ElementByIP(goodlist_A, 'FormIDs\[0]'));
  46.    
  47.     badlist_W := MainRecordByEditorID(GroupBySignature(mxPatchFile, 'FLST'), 'EC_WeaponList');
  48.     badforms_W := Add(badlist_W, 'FormIDs', true);
  49.     Remove(ElementByIP(badlist_W, 'FormIDs\[0]'));
  50.    
  51.     goodlist_W := MainRecordByEditorID(GroupBySignature(mxPatchFile, 'FLST'), 'EC_WeaponList_NoEnch');
  52.     goodforms_W := Add(goodlist_W, 'FormIDs', true);
  53.     Remove(ElementByIP(goodlist_W, 'FormIDs\[0]'));
  54.    
  55.     badobject_list := TList.Create;
  56.     goodobject_list := TList.Create;
  57.    
  58.     LoadRecords('ARMO');
  59.     for index := 0 to MaxRecordIndex do begin
  60.         badrec := GetRecord(index);
  61.        
  62.         ArmorIsPlayable := false;
  63.         ArmorHasEnch := false;
  64.         ArmorHasKeyword := false;
  65.        
  66.         P0 := StrToInt('0' + GetElementEditValues(badrec, 'Record Header\Record Flags\Non-Playable'));
  67.         P1 := StrToInt('0' + GetElementEditValues(badrec, 'BODT\General Flags\(ARMO)Non-Playable'));
  68.         P2 := StrToInt('0' + GetElementEditValues(badrec, 'BOD2\General Flags\(ARMO)Non-Playable'));
  69.         if (P0 + P1 + P2 = 0) then ArmorIsPlayable := true;
  70.         if (GetElementEditValues(badrec, 'EITM') <> '') then ArmorHasEnch := true;
  71.         if (HasKeyword(badrec, 'MagicDisallowEnchanting') = true) then ArmorHasKeyword := true;
  72.        
  73.         ArmorIsInList := false;
  74.         if (ArmorIsPlayable = true) and ((ArmorHasEnch = true) or (ArmorHasKeyword = true)) then ArmorIsInList := true;
  75.        
  76.         if (ArmorIsInList = true) then begin
  77.             item_edid := GetElementEditValues(badrec, 'EDID');
  78.             item_name := Name(badrec);
  79.            
  80.             AddRequiredMasters(badrec, mxPatchFile);
  81.             newbadform := ElementAssign(badforms_A, HighInteger, nil, false);
  82.             SetEditValue(newbadform, item_name);
  83.            
  84.             while (GetElementEditValues(badrec, 'TNAM') <> '') do begin
  85.                 badrec := LinksTo(ElementByPath(badrec, 'TNAM'));
  86.                 badrec := HighestOverrideOrSelf(badrec, 4096);
  87.                 AddRequiredMasters(badrec, mxPatchFile);
  88.             end;
  89.            
  90.             if (HasKeyword(badrec, 'MagicDisallowEnchanting') = false) and (GetElementEditValues(badrec, 'EITM') = '')
  91.             then goodrec := badrec
  92.             else begin
  93.                 badobject := TObject(badrec);
  94.                 object_index := badobject_list.IndexOf(badobject);
  95.                 if (object_index = -1)
  96.                 then begin
  97.                     goodrec := wbCopyElementToFile(badrec, mxPatchFile, true, true);
  98.                     goodobject := TObject(goodrec);
  99.                     badobject_list.Add(badobject);
  100.                     goodobject_list.Add(goodobject);
  101.                     NewObject := true;
  102.                 end
  103.                 else begin
  104.                     goodobject := goodobject_list[object_index];
  105.                     goodrec := ObjectToElement(goodobject);
  106.                     NewObject := false;
  107.                 end;
  108.                
  109.                 if (NewObject = true) then begin
  110.                     SetElementEditValues(goodrec, 'DESC', '');
  111.                     Remove(ElementByIP(goodrec, 'EITM'));
  112.                    
  113.                     KWDA := ElementByPath(goodrec, 'KWDA');
  114.                     for N := Pred(ElementCount(KWDA)) downto 0 do if (GetElementEditValues(LinksTo(ElementByIndex(KWDA, N)), 'EDID') = 'MagicDisallowEnchanting') then Remove(ElementByIndex(KWDA, N));
  115.                     if (ElementCount(KWDA) = 0) then Remove(ElementByPath(goodrec, 'KWDA'));
  116.                    
  117.                     VMAD := ElementByPath(goodrec, 'VMAD\Scripts');
  118.                     for N := Pred(ElementCount(VMAD)) downto 0 do if (ArmorScriptExceptions.IndexOf(GetElementEditValues(goodrec, 'VMAD\Scripts\[' + IntToStr(N) + ']\ScriptName')) = -1) then Remove(ElementByPath(goodrec, 'VMAD\Scripts\[' + IntToStr(N) + ']'));
  119.                     if (ElementCount(VMAD) = 0) then Remove(ElementByPath(goodrec, 'VMAD'));
  120.                    
  121.                     SetElementEditValues(goodrec, 'EDID', GetElementEditValues(goodrec, 'EDID') + '_ECDup');
  122.                 end;
  123.             end;
  124.            
  125.             newgoodform := ElementAssign(goodforms_A, HighInteger, nil, false);
  126.             SetEditValue(newgoodform, Name(goodrec));
  127.            
  128.             AddMessage(item_edid + ' -->-- ' + GetElementEditValues(goodrec, 'EDID'));
  129.         end;
  130.     end;
  131.     for index := MaxRecordIndex downto 0 do RemoveRecord(index);
  132.     badobject_list.Clear;
  133.     goodobject_list.Clear;
  134.    
  135.     LoadRecords('WEAP');
  136.     for index := 0 to MaxRecordIndex do begin
  137.         badrec := GetRecord(index);
  138.        
  139.         WeaponIsPlayable := false;
  140.         WeaponHasEnch := false;
  141.         WeaponHasKeyword := false;
  142.        
  143.         P0 := StrToInt('0' + GetElementEditValues(badrec, 'Record Header\Record Flags\Non-Playable'));
  144.         P1 := StrToInt('0' + GetElementEditValues(badrec, 'DNAM\Flags\Non-playable'));
  145.         if (P0 + P1 = 0) then WeaponIsPlayable := true;
  146.         if (GetElementEditValues(badrec, 'EITM') <> '') then WeaponHasEnch := true;
  147.         if (HasKeyword(badrec, 'MagicDisallowEnchanting') = true) then WeaponHasKeyword := true;
  148.        
  149.         WeaponIsInList := false;
  150.         if (WeaponIsPlayable = true) and ((WeaponHasEnch = true) or (WeaponHasKeyword = true)) then WeaponIsInList := true;
  151.        
  152.         if (WeaponIsInList = true) then begin
  153.             item_edid := GetElementEditValues(badrec, 'EDID');
  154.             item_name := Name(badrec);
  155.            
  156.             AddRequiredMasters(badrec, mxPatchFile);
  157.             newbadform := ElementAssign(badforms_W, HighInteger, nil, false);
  158.             SetEditValue(newbadform, item_name);
  159.            
  160.             while (GetElementEditValues(badrec, 'CNAM') <> '') do begin
  161.                 badrec := LinksTo(ElementByPath(badrec, 'CNAM'));
  162.                 badrec := HighestOverrideOrSelf(badrec, 4096);
  163.                 AddRequiredMasters(badrec, mxPatchFile);
  164.             end;
  165.            
  166.             if (HasKeyword(badrec, 'MagicDisallowEnchanting') = false) and (GetElementEditValues(badrec, 'EITM') = '')
  167.             then goodrec := badrec
  168.             else begin
  169.                 badobject := TObject(badrec);
  170.                 object_index := badobject_list.IndexOf(badobject);
  171.                 if (object_index = -1)
  172.                 then begin
  173.                     goodrec := wbCopyElementToFile(badrec, mxPatchFile, true, true);
  174.                     goodobject := TObject(goodrec);
  175.                     badobject_list.Add(badobject);
  176.                     goodobject_list.Add(goodobject);
  177.                     NewObject := true;
  178.                 end
  179.                 else begin
  180.                     goodobject := goodobject_list[object_index];
  181.                     goodrec := ObjectToElement(goodobject);
  182.                     NewObject := false;
  183.                 end;
  184.                
  185.                 if (NewObject = true) then begin
  186.                     SetElementEditValues(goodrec, 'DESC', '');
  187.                     Remove(ElementByIP(goodrec, 'EITM'));
  188.                    
  189.                     KWDA := ElementByPath(goodrec, 'KWDA');
  190.                     for N := Pred(ElementCount(KWDA)) downto 0 do if (GetElementEditValues(LinksTo(ElementByIndex(KWDA, N)), 'EDID') = 'MagicDisallowEnchanting') then Remove(ElementByIndex(KWDA, N));
  191.                     if (ElementCount(KWDA) = 0) then Remove(ElementByPath(goodrec, 'KWDA'));
  192.                    
  193.                     VMAD := ElementByPath(goodrec, 'VMAD\Scripts');
  194.                     for N := Pred(ElementCount(VMAD)) downto 0 do if (WeaponScriptExceptions.IndexOf(GetElementEditValues(goodrec, 'VMAD\Scripts\[' + IntToStr(N) + ']\ScriptName')) = -1) then Remove(ElementByPath(goodrec, 'VMAD\Scripts\[' + IntToStr(N) + ']'));
  195.                     if (ElementCount(VMAD) = 0) then Remove(ElementByPath(goodrec, 'VMAD'));
  196.                    
  197.                     SetElementEditValues(goodrec, 'EDID', GetElementEditValues(goodrec, 'EDID') + '_ECDup');
  198.                 end;
  199.             end;
  200.            
  201.             newgoodform := ElementAssign(goodforms_W, HighInteger, nil, false);
  202.             SetEditValue(newgoodform, Name(goodrec));
  203.            
  204.             AddMessage(item_edid + ' -->-- ' + GetElementEditValues(goodrec, 'EDID'));
  205.         end;
  206.     end;
  207.     for index := MaxRecordIndex downto 0 do RemoveRecord(index);
  208.     badobject_list.Clear;
  209.     goodobject_list.Clear;
  210.    
  211.     FinalizeMXPF;
  212. end;
  213.  
  214. procedure AddRequiredMasters(rec: IInterface; target: IwbFile);
  215. var
  216.     masters: TStringList;
  217. begin
  218.     masters := TStringList.Create;
  219.     AddMastersToList(GetFile(rec), masters);
  220.     AddMastersToFile(target, masters, true);
  221.     SortMasters(target);
  222.     masters.Clear;
  223. end;
  224.  
  225. end.
  226.  
Advertisement
RAW Paste Data Copied
Advertisement