Repository navigation
Expand file tree
/
Copy pathKM_ScriptingMethods.pas
More file actions
317 lines (248 loc) · 9.48 KB
/
Copy pathKM_ScriptingMethods.pas
File metadata and controls
317 lines (248 loc) · 9.48 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
unit KM_ScriptingMethods;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections,
KM_DocumenterTypes,
KM_ScriptingCommon, KM_ScriptingMethod;
type
// List of methods
// Documenter > Scripting > Methods
TKMScriptingMethods = class(TKMScriptingCommon)
private
fList: TObjectList<TKMMethodInfo>;
procedure LoadFromFile(const aSourceFile: string);
function ExportWikiBody: string;
function ExportWikiLinks: string;
function ExportCodeSectionCheck(aSL: TStringList): Boolean;
function ExportCodeSectionReg(aSL: TStringList): Boolean;
procedure SortByName;
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;
procedure LintLogMessages(const aSourceFile: string); override;
end;
implementation
uses
System.StrUtils, System.Generics.Defaults,
KM_StringUtils;
{ TKMScriptingMethods }
constructor TKMScriptingMethods.Create(aGame: TKMParsingGame; aArea: TKMScriptingArea; aOnLog: TProc<string>);
begin
inherited;
fList := TObjectList<TKMMethodInfo>.Create(
TComparer<TKMMethodInfo>.Construct(
function (const A, B: TKMMethodInfo): Integer
begin
// Case-sensitive compare, since we use CamelCase and it looks nicer that way
Result := CompareText(A.Name, B.Name);
// Changed methods have same names, but need to be sorted from old to new
if Result = 0 then
Result := Ord(B.Status) - Ord(A.Status);
end));
end;
destructor TKMScriptingMethods.Destroy;
begin
FreeAndNil(fList);
inherited;
end;
// Scans source contents and puts it all in proper formatting for most wikis.
procedure TKMScriptingMethods.LoadFromFile(const aSourceFile: string);
var
slSource: TStringList;
I: Integer;
srcLine: string;
slMethodDeclaration: TStringList;
sectionStarted, sectionTailEnded: Boolean;
lastSectionStart: Integer;
begin
fList.Clear;
if not FileExists(aSourceFile) then Exit;
slSource := TStringList.Create;
try
slSource.LoadFromFile(aSourceFile);
// Assemble method sections 1 by 1
{
//* Version: 1234
//* Status: Deprecated/Removed/Changed [optional]
//* Replacement: name of the replacement method [optional]
//* Large description of the method [optional]
//* empty line
//* Another paragraph with even more description of the method [optional]
//* aX: Small optional description of parameter
//* aY: Small optional description of parameter
//* Result: Small optional description of returned value
function Something(something, something, something
something): something
}
sectionStarted := False;
sectionTailEnded := True;
lastSectionStart := -1;
slMethodDeclaration := TStringList.Create;
for I := 0 to slSource.Count - 1 do
begin
srcLine := slSource[I];
if not sectionStarted and StartsStr(DOC_TAG, srcLine) then
begin
sectionStarted := True;
sectionTailEnded := True;
slMethodDeclaration.Clear;
lastSectionStart := I;
end;
if sectionStarted then
slMethodDeclaration.Append(slSource[I]);
if sectionStarted and (StartsStr('procedure', srcLine) or StartsStr('function', srcLine) or not sectionTailEnded) then
begin
if (Pos('(', srcLine) > 0) then
sectionTailEnded := False;
if (Pos(')', srcLine) > 0) then
sectionTailEnded := True;
if sectionTailEnded then
begin
sectionTailEnded := True;
sectionStarted := False;
fList.Add(TKMMethodInfo.Create);
fList.Last.LoadFromStringList(slMethodDeclaration, lastSectionStart, fArea = paEvents);
lastSectionStart := -1;
end;
end;
end;
slMethodDeclaration.Free;
finally
slSource.Free;
end;
SortByName;
fOnLog(Format('%d %s parsed', [fList.Count, SCRIPTING_AREA_SPEC[fArea].Name]));
end;
function TKMScriptingMethods.ExportWikiBody: string;
begin
Result := '';
for var I := 0 to fList.Count - 1 do
begin
fList[I].Parameters.AdjoinPairs;
Result := Result + IfThen(I > 0, sLineBreak) + fList[I].ExportWikiBody(SCRIPTING_AREA_SPEC[fArea].NeedsReturn);
end;
end;
function TKMScriptingMethods.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 TKMScriptingMethods.SortByName;
begin
fList.Sort;
end;
function TKMScriptingMethods.ExportCodeSectionCheck(aSL: TStringList): Boolean;
begin
var lineFrom, lineTo, padLevel: Integer;
FindRegionBounds(aSL, SCRIPTING_AREA_SPEC[fArea].CheckTag, lineFrom, lineTo, padLevel);
if lineFrom = -1 then Exit(False);
for var I := lineTo downto lineFrom do
aSL.Delete(I);
// Insert in reverse so we could skip "removed" methods
for var I := fList.Count - 1 downto 0 do
if fList[I].Status in [msOk, msDeprecated] then
case fArea of
paActions,
paStates,
paUtils: begin
// We can write more compact code with AdjoinPairs
fList[I].Parameters.AdjoinPairs;
aSL.Insert(lineFrom, DupeString(' ', padLevel) + 'RegisterMethodCheck(c, '#39 + fList[I].ExportCodeSignature + #39');');
end;
paEvents: // Can not use AdjoinPairs here. All vars must be separate
aSL.Insert(lineFrom, DupeString(' ', padLevel) + fList[I].ExportCodeSignatureEvent(fGame, I = fList.Count-1));
end;
Result := True;
fOnLog(Format('%d %s exported into Code checks', [fList.Count, SCRIPTING_AREA_SPEC[fArea].Name]));
end;
function TKMScriptingMethods.ExportCodeSectionReg(aSL: TStringList): Boolean;
const
AREA_REG_CLASS: array [TKMParsingGame, TKMScriptingArea] of string = (
('TKMScriptActions', '', 'TKMScriptStates', 'TKMScriptUtils', ''), // KMR
('TKMScriptingActions', '', 'TKMScriptingStates', 'TKMScriptingUtils', '') // KP
);
begin
var lineFrom, lineTo, padLevel: Integer;
FindRegionBounds(aSL, SCRIPTING_AREA_SPEC[fArea].RegTag, lineFrom, lineTo, padLevel);
if lineFrom = -1 then Exit(False);
for var I := lineTo downto lineFrom do
aSL.Delete(I);
// Insert in reverse so we could skip "removed" methods
for var I := fList.Count - 1 downto 0 do
if fList[I].Status in [msOk, msDeprecated] then
case fArea of
paActions,
paStates,
paUtils: aSL.Insert(lineFrom, DupeString(' ', padLevel) + 'RegisterMethod(@' + AREA_REG_CLASS[fGame, fArea] + '.' + fList[I].ExportCodeNameRegistration + ');');
paEvents: aSL.Insert(lineFrom, DupeString(' ', padLevel) + fList[I].ExportCodeNameRegistrationEvent(fGame, I = fList.Count - 1));
end;
Result := True;
fOnLog(Format('%d %s exported into Code regs', [fList.Count, SCRIPTING_AREA_SPEC[fArea].Name]));
end;
procedure TKMScriptingMethods.GenerateCode(const aSourceFile, aFilename1, aFilename2: string);
begin
LoadFromFile(aSourceFile);
var checkFound := False;
var regFound := False;
// It is inefficient, but simple to just always process 2 files
if FileExists(aFilename1) then
begin
var sl := TStringList.Create;
sl.LoadFromFile(aFilename1);
checkFound := ExportCodeSectionCheck(sl);
regFound := ExportCodeSectionReg(sl);
sl.SaveToFile(aFilename1);
sl.Free;
end;
if FileExists(aFilename2) then
begin
var sl := TStringList.Create;
sl.LoadFromFile(aFilename2);
checkFound := checkFound or ExportCodeSectionCheck(sl);
regFound := regFound or ExportCodeSectionReg(sl);
sl.SaveToFile(aFilename2);
sl.Free;
end;
if not checkFound then
fOnLog(Format('%s tag not found', [SCRIPTING_AREA_SPEC[fArea].CheckTag]));
if not regFound then
fOnLog(Format('%s tag not found', [SCRIPTING_AREA_SPEC[fArea].RegTag]));
end;
procedure TKMScriptingMethods.GenerateWiki(const aSourceFile, aTemplateFile, aOutputFile: string);
var
sl: TStringList;
exportPath: string;
begin
// Without template we cant generate output
if aTemplateFile = '' then Exit;
LoadFromFile(aSourceFile);
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('%d %s exported into Wiki', [fList.Count, SCRIPTING_AREA_SPEC[fArea].Name]));
end;
procedure TKMScriptingMethods.LintLogMessages(const aSourceFile: string);
begin
LoadFromFile(aSourceFile);
var slSourceCode := TStringList.Create;
slSourceCode.LoadFromFile(aSourceFile);
for var I := 0 to fList.Count - 1 do
begin
var res := fList[I].LintLogMessages(slSourceCode, LOG_MESSAGE_NAME[fGame]);
if res <> '' then
fOnLog(res);
end;
slSourceCode.Free;
end;
end.