Advertisement
Aezay

xEdit Script: Cellnames With Underscores Fix

Jun 1st, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.44 KB | None | 0 0
  1. {
  2.     Script for dealing with underscores in cellnames, which breaks savegames in SSE.
  3.     Details on bug: https://redd.it/5fvul2
  4.  
  5.     Has two modes, one for just filtering the cells, and another to fix the namnes by removing the underscore.
  6.  
  7.     Author      Aezay
  8.     Created     2018.05.31
  9.     Modificed   2018.06.01
  10. }
  11.  
  12. unit CellNamesWithUnderscoresFix;
  13.  
  14. const
  15.     { method IDs }
  16.     ID_NONE     = 0;
  17.     ID_FILTER   = 1;
  18.     ID_FIX      = 2;
  19. var
  20.     methodID: Integer;
  21.     log: TStringList;
  22.     editorID, fullName: string;     // used in Process, Filter
  23.     fid: DWORD;                     // used in Process, Filter
  24.  
  25. // add found cell to our log and xEdit log
  26. procedure LogMatch(e: IInterface; const EditorID: string);
  27. begin
  28.     fullName := GetElementNativeValues(e,'FULL');
  29.     //fid := GetLoadOrderFormID(e);
  30.     fid := FormID(e);
  31.  
  32.     AddMessage(Format('* [%.8x] %s  (%s)',[fid,EditorID,fullName]));
  33.     log.Add(Format('[%.8x] %-40s %s',[fid,EditorID,fullName]));
  34. end;
  35.  
  36. // regular processing
  37. function Process(e: IInterface):Integer;
  38. begin
  39.     if (Signature(e) <> 'CELL') then begin
  40.         Result := 0;
  41.         Exit;
  42.     end;
  43.  
  44.     editorID := GetElementNativeValues(e,'EDID');
  45.     if (editorID = '') or (Pos('_',editorID) = 0) then begin
  46.         Result := 0;
  47.         Exit;
  48.     end;
  49.  
  50.     LogMatch(e,editorID);
  51.  
  52.     editorID := StringReplace(editorID,'_','',[rfReplaceAll]);
  53.     SetElementNativeValues(e,'EDID',editorID);
  54. end;
  55.  
  56. // filter processing
  57. function Filter(e: IInterface):Boolean;
  58. begin
  59.     if (Signature(e) <> 'CELL') then begin
  60.         Result := false;
  61.         Exit;
  62.     end;
  63.  
  64.     editorID := GetElementNativeValues(e,'EDID');
  65.     if (editorID = '') or (Pos('_',editorID) = 0) then begin
  66.         Result := false;
  67.         Exit;
  68.     end;
  69.  
  70.     LogMatch(e,editorID);
  71.  
  72.     Result := true;
  73. end;
  74.  
  75. // method button
  76. procedure Method_Button_Click(Sender: TObject);
  77. begin
  78.     methodID := TButton(Sender).Tag;
  79. end;
  80.  
  81. function _CreateMethodButton(Owner: TWinControl; MethodID: Integer; const Caption: string):TButton;
  82. begin
  83.     Result := TButton.Create(Owner);
  84.     Result.Parent := Owner;
  85.     Result.Caption := Caption;
  86.     Result.Tag := MethodID;
  87.     Result.ModalResult := mrOk;
  88.     Result.OnClick := Method_Button_Click;
  89. end;
  90.  
  91. // prompts for method to use
  92. function MethodPrompt():Integer;
  93. const
  94.     MARGIN = 8;
  95.     BTN_HEIGHT = 32;
  96. var
  97.     form: TForm;
  98.     btn1, btn2: TButton;
  99. begin
  100.     { form }
  101.     form := TForm.Create(nil);
  102.     form.Caption := 'Underscore cell script';
  103.     form.SetBounds(0,0,440,120);
  104.     form.Position := poScreenCenter;
  105.     form.BorderWidth := MARGIN;
  106.  
  107.     { btn }
  108.     btn1 := _CreateMethodButton(form,ID_FILTER,'<INFO> &Apply global filter to show cells including underscores...');
  109.     btn1.SetBounds(0,0,form.ClientWidth,BTN_HEIGHT);
  110.  
  111.     btn2 := _CreateMethodButton(form,ID_FIX,'<FIX> &Remove underscores from cells in selected elements...');
  112.     btn2.SetBounds(0,BTN_HEIGHT + MARGIN,form.ClientWidth,BTN_HEIGHT);
  113.  
  114.     form.ClientHeight := btn2.BoundsRect.Bottom;
  115.  
  116.     { display }
  117.     if (form.ShowModal() = mrOk) then
  118.         Result := methodID
  119.     else
  120.         Result := ID_NONE;
  121.     form.Free();
  122. end;
  123.  
  124. // displays the content of the log strings
  125. procedure DisplayReport();
  126. var
  127.     form: TForm;
  128.     memo: TMemo;
  129. begin
  130.     if (log.Count = 0) then begin
  131.         Exit;
  132.     end;
  133.  
  134.     { form }
  135.     form := TForm.Create(nil);
  136.     form.Caption := Format('Report of cells with underscores (%d cells)',[log.Count]);
  137.     form.SetBounds(0,0,680,680);
  138.     form.Position := poScreenCenter;
  139.     form.BorderWidth := 0;
  140.  
  141.     { memo }
  142.     memo := TMemo.Create(form);
  143.     memo.Parent := form;
  144.     memo.Align := alClient;
  145.     memo.ReadOnly := true;
  146.     memo.ScrollBars := ssBoth;
  147.     memo.Font.Name := 'Courier New';
  148.     memo.Font.Size := 8;
  149.     memo.Lines.Text := log.Text;
  150.  
  151.     { display }
  152.     form.ShowModal();
  153.     form.Free();
  154. end;
  155.  
  156. // init for filter method
  157. function InitFilter():Integer;
  158. begin
  159.     FilterConflictAll := False;
  160.     FilterConflictThis := False;
  161.     FilterByInjectStatus := False;
  162.     FilterInjectStatus := False;
  163.     FilterByNotReachableStatus := False;
  164.     FilterNotReachableStatus := False;
  165.     FilterByReferencesInjectedStatus := False;
  166.     FilterReferencesInjectedStatus := False;
  167.     FilterByEditorID := False;
  168.     FilterEditorID := '';
  169.     FilterByName := False;
  170.     FilterName := '';
  171.     FilterByBaseEditorID := False;
  172.     FilterBaseEditorID := '';
  173.     FilterByBaseName := False;
  174.     FilterBaseName := '';
  175.     FilterScaledActors := False;
  176.     FilterByPersistent := False;
  177.     FilterPersistent := False;
  178.     FilterUnnecessaryPersistent := False;
  179.     FilterMasterIsTemporary := False;
  180.     FilterIsMaster := False;
  181.     FilterPersistentPosChanged := False;
  182.     FilterDeleted := False;
  183.     FilterByVWD := False;
  184.     FilterVWD := False;
  185.     FilterByHasVWDMesh := False;
  186.     FilterHasVWDMesh := False;
  187.     FilterBySignature := False;
  188.     FilterSignatures := '';
  189.     FilterByBaseSignature := False;
  190.     FilterBaseSignatures := '';
  191.     FlattenBlocks := False;
  192.     FlattenCellChilds := False;
  193.     AssignPersWrldChild := False;
  194.     InheritConflictByParent := True; // color conflicts
  195.     FilterScripted := True; // use custom Filter() function
  196.  
  197.     ApplyFilter();
  198.     Finalize();     // we have to call this manually here, since we return 1
  199.  
  200.     Result := 1;
  201. end;
  202.  
  203. // init for fix method
  204. function InitFix():Integer;
  205. begin
  206.     Result := 0;
  207. end;
  208.  
  209. // main init
  210. function Initialize():Integer;
  211. begin
  212.     log := TStringList.Create();
  213.  
  214.     case MethodPrompt() of
  215.         ID_FILTER:  Result := InitFilter();
  216.         ID_FIX:     Result := InitFix();
  217.     else
  218.         Result := 1;
  219.     end
  220. end;
  221.  
  222. // main finalize
  223. function Finalize():Integer;
  224. begin
  225.     AddMessage(Format('Found %d cells containing underscores!',[log.Count]));
  226.     DisplayReport();
  227.     log.Free();
  228. end;
  229.  
  230. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement