Repository navigation
Expand file tree
/
Copy pathKM_ModdingTypes.pas
More file actions
218 lines (160 loc) · 5.35 KB
/
Copy pathKM_ModdingTypes.pas
File metadata and controls
218 lines (160 loc) · 5.35 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
unit KM_ModdingTypes;
interface
uses
System.Classes, System.Math, System.SysUtils, System.Types, System.Generics.Collections, System.Generics.Defaults, System.StrUtils,
KM_DocumenterTypes,
KM_ModdingType;
type
// List of types
// Documenter > Modding > Types
TKMModdingTypes = class
private
fOnLog: TProc<string>;
fList: TObjectList<TKMModdingNode>;
procedure LoadFromFile(const aSourceMask: string);
procedure Clear;
procedure ConnectCrossReferences;
function ExportWikiBody: string;
function ExportWikiLinks: string;
procedure LoadFromFileInt(const aInputFile: string);
procedure SortByName(aSortBy: TKMSortType);
function ExportListing: string;
public
constructor Create(aOnLog: TProc<string>);
destructor Destroy; override;
procedure GenerateWiki(const aSourceFile, aTemplateFile, aOutputFile: string);
procedure LintCode(const aSourceFile: string);
end;
implementation
uses
System.IOUtils,
KM_StringUtils;
{ TKMModdingTypes }
constructor TKMModdingTypes.Create(aOnLog: TProc<string>);
begin
inherited Create;
fOnLog := aOnLog;
fList := TObjectList<TKMModdingNode>.Create(
TComparer<TKMModdingNode>.Construct(
function (const A, B: TKMModdingNode): Integer
begin
// Case-sensitive compare, since we use CamelCase and it looks nicer that way
Result := CompareText(A.Caption, B.Caption);
end));
end;
destructor TKMModdingTypes.Destroy;
begin
FreeAndNil(fList);
inherited;
end;
procedure TKMModdingTypes.Clear;
begin
fList.Clear;
end;
procedure TKMModdingTypes.ConnectCrossReferences;
begin
// Let the fields know about each other if they need references
// O(n^2) is definitely stupid, but it is KISS
for var I := 0 to fList.Count - 1 do
for var K := 0 to fList.Count - 1 do
if I <> K then
fList[I].CrossLinkWith(fList[K]);
end;
// Scans source contents and puts it all in proper formatting for most wikis.
procedure TKMModdingTypes.LoadFromFileInt(const aInputFile: string);
begin
var slSource := TStringList.Create;
try
slSource.LoadFromFile(aInputFile);
// Look for areas denoted as Modding specifications
{
//*Area-Modding-Definition*//
...
//*Area-Modding-Definition*//
}
var areaStarted := False;
var slArea := TStringList.Create;
for var I := 0 to slSource.Count - 1 do
begin
var srcLine := Trim(slSource[I]);
// New area starts
if not areaStarted and StartsStr(DOC_TAG_AREA_MODDING_DEFINITION, srcLine) then
begin
areaStarted := True;
slArea.Clear;
Continue;
end;
// Area ends
if areaStarted and StartsStr(DOC_TAG_AREA_MODDING_DEFINITION, srcLine) then
begin
var newType := TKMModdingFactory.NewTypeFromStringList(slArea);
fList.Add(newType);
areaStarted := False;
Continue;
end;
if areaStarted then
slArea.Append(srcLine);
end;
slArea.Free;
finally
slSource.Free;
end;
end;
procedure TKMModdingTypes.LoadFromFile(const aSourceMask: string);
begin
Clear;
// Get all files matching the mask
var sa := TDirectory.GetFiles(ExtractFilePath(aSourceMask), ExtractFileName(aSourceMask), TSearchOption.soAllDirectories);
for var I := Low(sa) to High(sa) do
LoadFromFileInt(sa[I]);
ConnectCrossReferences;
fOnLog(Format('%d %s parsed', [fList.Count, SCRIPTING_AREA_SPEC[paTypes].Name]));
end;
function TKMModdingTypes.ExportListing: string;
begin
Result := '';
for var I := 0 to fList.Count - 1 do
Result := Result + fList[I].ExportListing(IntToStr(I) + '. ') + sLineBreak;
end;
function TKMModdingTypes.ExportWikiBody: string;
begin
Result := '';
for var I := 0 to fList.Count - 1 do
if fList[I].IsRoot then
Result := Result + fList[I].ExportWikiBody + sLineBreak + '---' + sLineBreak;
end;
function TKMModdingTypes.ExportWikiLinks: string;
begin
Result := '### Root types' + sLineBreak + sLineBreak;
for var I := 0 to fList.Count - 1 do
if fList[I].IsRoot then
Result := Result + fList[I].ExportWikiLink + sLineBreak;
end;
procedure TKMModdingTypes.SortByName(aSortBy: TKMSortType);
begin
fList.Sort;
end;
procedure TKMModdingTypes.GenerateWiki(const aSourceFile, aTemplateFile, aOutputFile: string);
begin
// Without template we cant generate output
if aTemplateFile = '' then Exit;
LoadFromFile(aSourceFile);
SortByName(stByAlphabet);
var sl := TStringList.Create;
sl.LoadFromFile(aTemplateFile);
sl.Text := StringReplace(sl.Text, '{LINKS}', ExportWikiLinks, []);
sl.Text := StringReplace(sl.Text, '{BODY}', ExportWikiBody, []);
//sl.Text := sl.Text + sLineBreak + ExportListing;
var 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;
procedure TKMModdingTypes.LintCode(const aSourceFile: string);
begin
// Following things could be linted:
// - matching names between XML attribute name and field name
end;
end.