Mator

[Pascal] [xEdit] Find and modify matching elements

Jul 28th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.64 KB | None | 0 0
  1. {
  2.   Find and modify matching elements
  3.   created by matortheeternal
  4. }
  5.  
  6. unit UserScript;
  7.  
  8. uses mteFunctions;
  9.  
  10. const
  11.   // if print is true the script will print its log to the xEdit console
  12.   print = true;
  13.   // if save is true the script will save its log to the xEdit root directory
  14.   save = true;
  15.  
  16. var
  17.   log: TStringList;
  18.  
  19. procedure ModifyElementValue(var e: IInterface);
  20. const
  21.   // path to modify
  22.   path = 'DATA\Position\Z';
  23.   modifier = -1.0;
  24. var
  25.   oldValue, newValue: real;
  26. begin
  27.   oldValue := genv(e, path);
  28.   newValue := oldValue + modifier;
  29.   log.Add(Format('Changed %0.2f to %0.2f on %s', [oldValue, newValue, Name(e)]));
  30.   senv(e, path, newValue);
  31. end;
  32.  
  33. procedure FindElements(var g: IInterface; var lst: TList; s: string);
  34. var
  35.   i: Integer;
  36.   e: IInterface;
  37. begin
  38.   for i := 0 to ElementCount(g) - 1 do begin
  39.     e := ElementByIndex(g, i);
  40.     if Signature(e) = s then
  41.       lst.Add(TObject(e))
  42.     else if ElementCount(e) > 0 then
  43.       FindElements(e, lst, s);
  44.   end;
  45. end;
  46.  
  47. function Initialize: Integer;
  48. var
  49.   i, j: Integer;
  50.   filename: string;
  51.   UserFile, group, e: IInterface;
  52.   lst: TList;
  53.   oldValue, newValue: real;
  54.   GroupSignatures, ElementSignatures:  TStringList;
  55. begin
  56.   // initialize
  57.   log := TStringList.Create;
  58.   GroupSignatures := TStringList.Create;
  59.   ElementSignatures := TStringList.Create;
  60.   lst := TList.Create;
  61.  
  62.   // let user specify file to use
  63.   UserFile := FileSelect('Select a file');
  64.   if not Assigned(UserFile) then begin
  65.     AddMessage('No user file specified, terminating.');
  66.     exit;
  67.   end;
  68.   AddMessage('Script is using '+GetFileName(UserFile));
  69.  
  70.   // load groups and records
  71.   GroupSignatures.Add('WRLD');
  72.   ElementSignatures.Add('REFR');
  73.  
  74.   // find elements
  75.   for i := 0 to Pred(GroupSignatures.Count) do begin
  76.     AddMessage('Finding elements in group '+GroupSignatures[i]);
  77.     group := GroupBySignature(UserFile, GroupSignatures[i]);
  78.     for j := 0 to Pred(ElementSignatures.Count) do begin
  79.       AddMessage('  Finding elements with signature '+ElementSignatures[j]);
  80.       FindElements(group, lst, ElementSignatures[j]);
  81.     end;
  82.   end;
  83.  
  84.   // loop through elements
  85.   for i := 0 to Pred(lst.Count) do begin
  86.     e := ObjectToElement(lst[i]);
  87.     ModifyElementValue(e);
  88.   end;
  89.  
  90.   // print/save
  91.   if print then begin
  92.     AddMessage(' ');
  93.     AddMessage(log.Text);
  94.   end;
  95.   if save then begin
  96.     filename := 'FindElements_'+StringReplace(GroupSignatures.Text, #13#10, '-', [rfReplaceAll])+'.txt';
  97.     log.SavetoFile(filename);
  98.   end;
  99.  
  100.   // clean up
  101.   log.Free;
  102.   lst.Free;
  103.   GroupSignatures.Free;
  104.   ElementSignatures.Free;
  105. end;
  106.  
  107. end.
Advertisement
Add Comment
Please, Sign In to add comment