-
Notifications
You must be signed in to change notification settings - Fork 2
/
Randomize Form Everywhere.pas
187 lines (157 loc) · 5.78 KB
/
Randomize Form Everywhere.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{
Use variate-map.txt to define replacements.
Format is:
oldFormID=newFormID,anotherFormID
Run on something within the target file, will replace any occurence within that file.
}
unit ReplaceFormEverywhere;
uses praUtil;
var
StrReplace: string;
fileToDo, formSearch, formReplace: IInterface;
replaceMap: TStringList;
replaceMapVals: TStringList;
function registerReplacement(key: String; val: String): boolean;
var
i :Integer;
curObj:IInterface;
// obj: TObject;
begin
Result := true;
i := replaceMap.indexOf(key);
if i < 0 then begin
i := replaceMap.add(key);
//replaceMapVals.insert(i, val);
replaceMapVals.insert(i, val);
end else begin
//replaceMapVals[i] := val;
replaceMapVals[i] := val;
//replaceMapVals.Objects[i] := curObj;
end;
end;
function getReplacementElem(key: String): IInterface;
var
i: Integer;
obj: TObject;
begin
result := nil;
i := replaceMap.indexOf(key);
if(i >= 0) then begin
obj := replaceMapVals.objects[i];
Result := ObjectToElement(obj);
end;
end;
// Called before processing
// You can remove it if script doesn't require initialization code
function Initialize: integer;
var
list: TStringList;
curLine: String;
i, j: Integer;
endPos: Integer;
key: String;
val: String;
spacePos: Integer;
begin
Result := 0;
replaceMap := TStringList.create;
replaceMapVals := TStringList.create;
list := TStringList.Create;
list.loadFromFile( ScriptsPath + 'variate-map.txt');
for i:=0 to list.count-1 do begin
curLine := trim(list[i]);
if (length(curLine) > 0) then begin
if (curLine[1] <> ';') then begin
spacePos := -1;
for j:=1 to length(curLine) do begin
if curLine[j] = '=' then begin
spacePos := j;
break;
end;
//AddMessage(curLine[j]);
end;
if spacePos <> -1 then begin
key := trim(copy(curLine, 0, spacePos-1));
val := trim(copy(curLine, spacePos+1, length(curLine)));
if (key <> '') and (val <> '') then begin
if(not registerReplacement(key, val)) then begin
Result := 1;
exit;
end;
end;
end;
end;
end;
end;
end;
// called for every record selected in xEdit
function Process(e: IInterface): integer;
begin
Result := 0;
fileToDo := GetFile(e);
end;
procedure replaceFormIn(container, search, repl: IInterface);
var
i: integer;
child, lt: IInterface;
begin
lt := LinksTo(container);
if(assigned(lt)) then begin
if(IsSameForm(lt, search)) then begin
AddMessage('Found one');
AddMessage(' '+FullPath(container));
setLinksTo(container, repl);
end;
end;
for i := 0 to ElementCount(container)-1 do begin
child := ElementByIndex(container, i);
// AddMessage(prefix+DisplayName(child)+'='+GetEditValue(child));
replaceFormIn(child, search, repl);
end;
end;
// Called after processing
// You can remove it if script doesn't require finalization code
function Finalize: integer;
var
i, j, numRefs, randNr: cardinal;
curRef, formSearch, formReplace: IInterface;
searchEdid, replaceString, replaceEdid: string;
curVariants: TStringList;
begin
Result := 0;
if(not assigned(fileToDo)) then begin
exit;
end;
curVariants := TStringList.create;
for j := 0 to replaceMap.count-1 do begin
searchEdid := replaceMap[j];
AddMessage('== PROCESSING '+searchEdid+' ==');
formSearch := FindObjectByEdid(searchEdid);
replaceString := replaceMapVals[j];
curVariants.clear();
curVariants.Delimiter := ',';
curVariants.DelimitedText := replaceString;
// curVariants.add(searchEdid);
// formReplace := ObjectToElement(replaceMapVals.objects[j]);
for i := ReferencedByCount(formSearch)-1 downto 0 do begin
curRef := ReferencedByIndex(formSearch, i);
if(GetFileName(GetFile(curRef)) = GetFileName(fileToDo)) then begin
//dumpElem(curRef);
randNr := Random(curVariants.count);
replaceEdid := curVariants[randNr];
if(replaceEdid <> searchEdid) then begin
AddMessage('Would replace '+searchEdid+' with '+replaceEdid);
formReplace := findObjectByEdid(replaceEdid);
if(not assigned(formReplace)) then begin
AddMessage('Could not find object '+replaceEdid);
Result := 1;
exit;
end;
replaceFormIn(curRef, formSearch, formReplace);
end;
end;
end;
end;
curVariants.free();
end;
end.