Repository navigation
Expand file tree
/
Copy pathKM_ScriptingTypes.pas
More file actions
364 lines (287 loc) · 9.54 KB
/
Copy pathKM_ScriptingTypes.pas
File metadata and controls
364 lines (287 loc) · 9.54 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
unit KM_ScriptingTypes;
interface
uses
System.Classes, System.Math, System.SysUtils, System.Types, System.Generics.Collections, System.Generics.Defaults, System.StrUtils,
KM_DocumenterTypes,
KM_ScriptingCommon, KM_ScriptingType;
type
// List of types
// Documenter > Scripting > Types
TKMScriptingTypes = class(TKMScriptingCommon)
private
fList: TObjectList<TKMScriptType>;
procedure LoadFromFile(const aSourceMask: string);
procedure AssignSortOrder;
procedure Clear;
function ExportWikiBody: string;
function ExportWikiLinks: string;
procedure LoadFromFileInt(const aInputFile: string);
procedure SortByName(aSortBy: TKMSortType);
public
constructor Create(aGame: TKMParsingGame; aArea: TKMScriptingArea; aOnLog: TProc<string>); override;
destructor Destroy; override;
procedure GenerateCode(const aSourceFile, aFilename1, aFilename2: string); override;
procedure GenerateWiki(const aSourceFile, aTemplateFile, aOutputFile: string); override;
end;
implementation
uses
System.IOUtils,
KM_StringUtils;
{ TKMScriptingTypes }
constructor TKMScriptingTypes.Create(aGame: TKMParsingGame; aArea: TKMScriptingArea; aOnLog: TProc<string>);
begin
inherited;
fList := TObjectList<TKMScriptType>.Create(
TComparer<TKMScriptType>.Construct(
function (const A, B: TKMScriptType): Integer
begin
Result := CompareValue(A.SortPriority, B.SortPriority);
if Result = 0 then
// Case-sensitive compare, since we use CamelCase and it looks nicer that way
Result := CompareText(A.Name, B.Name);
end));
end;
destructor TKMScriptingTypes.Destroy;
begin
FreeAndNil(fList);
inherited;
end;
procedure TKMScriptingTypes.Clear;
begin
fList.Clear;
end;
// Scans source contents and puts it all in proper formatting for most wikis.
procedure TKMScriptingTypes.LoadFromFileInt(const aInputFile: string);
var
slSource: TStringList;
I: Integer;
srcLine: string;
sl: TStringList;
sectionStarted: Boolean;
recordStarted: Boolean;
begin
slSource := TStringList.Create;
try
slSource.LoadFromFile(aInputFile);
// Assemble method sections 1 by 1
{
//* This is an Enum
TKMSomeType = (stNone,
//
stSomething);
//* This is a Record
// ignore this comment
TKMSomeType = record
A,B: Integer;
function Some: Byte;
end;
//* This is an Array of
TKMSomeType = array of TKMSomething;
//* This is a Set of
TKMSomeType = set of TKMSomething;
}
var areaStarted := False;
sectionStarted := False;
recordStarted := False;
sl := TStringList.Create;
for I := 0 to slSource.Count - 1 do
begin
srcLine := Trim(slSource[I]);
// Skip special areas
if StartsStr(DOC_TAG_AREA, srcLine) then
if not areaStarted then
areaStarted := True
else
begin
areaStarted := False;
// Skip this closing line too
Continue;
end;
if areaStarted then
Continue;
if not sectionStarted and StartsStr(DOC_TAG, srcLine) then
begin
sectionStarted := True;
sl.Clear;
end;
if sectionStarted then
if not StartsStr('//', srcLine) or StartsStr(DOC_TAG, srcLine) then
sl.Append(srcLine);
if sectionStarted and not StartsStr('//', srcLine) and (Pos('record', srcLine) > 0) then
recordStarted := True;
if sectionStarted and not recordStarted and (StartsStr('procedure', srcLine) or StartsStr('function', srcLine)) then
sectionStarted := False;
if sectionStarted and recordStarted and (Pos('end;', srcLine) > 0) then
recordStarted := False;
if sectionStarted and not recordStarted and not StartsStr(DOC_TAG, srcLine) and (Pos(';', srcLine) > 0) then
begin
sectionStarted := False;
fList.Add(TKMScriptType.Create);
fList.Last.LoadFromStringList(sl);
end;
end;
sl.Free;
finally
slSource.Free;
end;
end;
procedure TKMScriptingTypes.LoadFromFile(const aSourceMask: string);
var
s: TStringDynArray;
I: Integer;
begin
Clear;
// Get all files matching the mask
s := TDirectory.GetFiles(ExtractFilePath(aSourceMask), ExtractFileName(aSourceMask), TSearchOption.soAllDirectories);
for I := Low(s) to High(s) do
LoadFromFileInt(s[I]);
fOnLog(Format('%d %s parsed', [fList.Count, SCRIPTING_AREA_SPEC[paTypes].Name]));
end;
function TKMScriptingTypes.ExportWikiBody: string;
var
I: Integer;
begin
Result := '';
for I := 0 to fList.Count - 1 do
Result := Result + IfThen(I > 0, sLineBreak) + fList[I].ExportWikiBody;
end;
function TKMScriptingTypes.ExportWikiLinks: string;
var
I: Integer;
begin
Result := '';
for I := 0 to fList.Count - 1 do
Result := Result + IfThen(I > 0, sLineBreak) + fList[I].ExportWikiLink;
end;
procedure TKMScriptingTypes.AssignSortOrder;
function FindType(aName: string): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to fList.Count - 1 do
if fList[I].Name = aName then
Exit(I);
end;
var
I, K: Integer;
use: array of TList<Integer>;
order: array of Integer;
id: Integer;
s: string;
orderLoop: Integer;
needsAnotherLoop: Boolean;
begin
SetLength(use, fList.Count);
for I := 0 to fList.Count - 1 do
use[I] := TList<Integer>.Create;
SetLength(order, fList.Count);
for I := 0 to fList.Count - 1 do
order[I] := -1;
for I := 0 to fList.Count - 1 do
case fList[I].Typ of
ttRecord: begin
for K := 0 to fList[I].Elements.List.Count - 1 do
begin
s := RightStrAfter(fList[I].Elements.List[K].Name, ': ');
id := FindType(s);
if id <> -1 then
use[I].Add(id);
end;
end;
ttArray: begin
s := ReplaceStr(fList[I].Elements.List[0].Name, 'array of ', '');
id := FindType(s);
if id <> -1 then
use[I].Add(id);
end;
ttSetOfType: begin
s := ReplaceStr(fList[I].Elements.List[0].Name, 'set of ', '');
id := FindType(s);
if id <> -1 then
use[I].Add(id);
end;
end;
orderLoop := 0;
repeat
// Demark items without dependencies
for I := 0 to fList.Count - 1 do
if (order[I] = -1) and (use[I].Count = 0) then
order[I] := orderLoop;
// Trim demarked items
for I := 0 to fList.Count - 1 do
for K := use[I].Count - 1 downto 0 do
if order[use[I][K]] = 0 then
use[I].Delete(K);
// Check if all items are demarked
needsAnotherLoop := False;
for I := 0 to fList.Count - 1 do
if (order[I] = -1) then
needsAnotherLoop := True;
Inc(orderLoop);
until not needsAnotherLoop or (orderLoop = 9);
for I := 0 to fList.Count - 1 do
fList[I].SortPriority := order[I];
end;
procedure TKMScriptingTypes.SortByName(aSortBy: TKMSortType);
begin
case aSortBy of
stByAlphabet: ; // Already sorted by default
stByDependancy: AssignSortOrder;
end;
fList.Sort;
end;
procedure TKMScriptingTypes.GenerateCode(const aSourceFile, aFilename1, aFilename2: string);
begin
Assert(aFilename2 = '');
if not FileExists(aFilename1) then Exit;
LoadFromFile(aSourceFile);
SortByName(stByDependancy);
var sl := TStringList.Create;
try
sl.LoadFromFile(aFilename1);
var lineFrom, lineTo, padLevel: Integer;
FindRegionBounds(sl, SCRIPTING_AREA_SPEC[paTypes].RegTag, lineFrom, lineTo, padLevel);
if lineFrom <> -1 then
begin
// Remove old code
for var I := lineTo downto lineFrom do
sl.Delete(I);
// Insert new code
for var I := fList.Count - 1 downto 0 do
begin
sl.Insert(lineFrom, DupeString(' ', padLevel) + fList[I].ExportCode);
if (I > 0) and (fList[I].SortPriority <> fList[I-1].SortPriority) then
begin
sl.Insert(lineFrom, '');
sl.Insert(lineFrom + 1, DupeString(' ', padLevel) + Format('// Level %d types depend on preceeding types of level %d', [fList[I].SortPriority, fList[I].SortPriority - 1]));
end;
end;
end;
sl.SaveToFile(aFilename1);
finally
sl.Free;
end;
fOnLog(Format('Written %d items of %s into Code', [fList.Count, SCRIPTING_AREA_SPEC[paTypes].Name]));
end;
procedure TKMScriptingTypes.GenerateWiki(const aSourceFile, aTemplateFile, aOutputFile: string);
var
sl: TStringList;
exportPath: string;
begin
// Without template we cant generate output
if aTemplateFile = '' then Exit;
LoadFromFile(aSourceFile);
SortByName(stByAlphabet);
sl := TStringList.Create;
sl.LoadFromFile(aTemplateFile);
sl.Text := StringReplace(sl.Text, '{LINKS}', ExportWikiLinks, []);
sl.Text := StringReplace(sl.Text, '{BODY}', ExportWikiBody, []);
exportPath := ExpandFileName(ExtractFilePath(ParamStr(0)) + aOutputFile);
if not DirectoryExists(ExtractFileDir(exportPath)) then
ForceDirectories(ExtractFileDir(exportPath));
sl.SaveToFile(aOutputFile);
sl.Free;
fOnLog(Format('Written %d items of %s into Wiki', [fList.Count, SCRIPTING_AREA_SPEC[paTypes].Name]));
end;
end.