-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUMake_Configuration.pas
executable file
·396 lines (308 loc) · 10.4 KB
/
UMake_Configuration.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
unit UMake_Configuration;
interface
uses
Classes, SysUtils, SysTools, FileCtrl, Hashes;
(*****************************************************************************)
(* TConfiguration
(*****************************************************************************)
type
EConfiguration = class(Exception);
EConfigurationGameDirInvalid = class(EConfiguration);
EConfigurationGameDirNotFound = class(EConfiguration);
EConfigurationGameIniNotFound = class(EConfiguration);
EConfigurationPackageDirInvalid = class(EConfiguration);
EConfigurationPackageDirNotFound = class(EConfiguration);
TConfiguration = class
private
HashFilePackage: TStringHash;
TextDirCacheRecord: string;
TextDirGame: string;
TextDirPackage: string;
TextFileIniGame: string;
TextFileIniPackage: string;
TextNamePackage: string;
function FindIniGame: Boolean;
function FindIniPackage: Boolean;
procedure StringListPathsChange(Sender: TObject);
public
StringListPaths: TStringList;
StringListPackages: TStringList;
constructor Create(ATextNamePackage: string; ATextDirGame: string);
destructor Destroy; override;
procedure Read;
procedure Write;
function FindFilePackage(TextPackage: string): string;
property Package: string read TextNamePackage;
property DirGame: string read TextDirGame;
property DirPackage: string read TextDirPackage;
end;
implementation
(*****************************************************************************)
(* TConfiguration
(*****************************************************************************)
constructor TConfiguration.Create(ATextNamePackage: string; ATextDirGame: string);
begin
TextNamePackage := ATextNamePackage;
TextDirGame := ATextDirGame;
if not DirectoryExists(TextDirGame) then
raise EConfigurationGameDirNotFound.Create('Game directory not found');
if not FileExists(IncludeTrailingBackslash(TextDirGame) + 'System\ucc.exe') then
raise EConfigurationGameDirInvalid.Create('Game directory does not contain valid "System" subdirectory');
TextDirPackage := IncludeTrailingBackslash(TextDirGame) + TextNamePackage;
if not DirectoryExists(TextDirPackage) then
raise EConfigurationPackageDirNotFound.Create('Package directory not found');
if not DirectoryExists(IncludeTrailingBackslash(TextDirPackage) + 'Classes') then
raise EConfigurationPackageDirInvalid.Create('Package directory does not contain "Classes" subdirectory');
HashFilePackage := TStringHash.Create;
StringListPaths := TStringList.Create;
StringListPackages := TStringList.Create;
StringListPaths.OnChange := StringListPathsChange;
end;
destructor TConfiguration.Destroy;
begin
HashFilePackage.Free;
StringListPaths.Free;
StringListPackages.Free;
inherited;
end;
function TConfiguration.FindIniGame: Boolean;
var
FileIni: TextFile;
TextFileIni: string;
TextLineIni: string;
ResultFind: Integer;
SearchRecIni: TSearchRec;
begin
Result := False;
ResultFind := FindFirst(IncludeTrailingBackslash(TextDirGame) + 'System\*.ini', faAnyFile, SearchRecIni);
while ResultFind = 0 do
begin
if not AnsiSameText(SearchRecIni.Name, 'Default.ini') then
begin
TextFileIni := IncludeTrailingBackslash(TextDirGame) + 'System\' + SearchRecIni.Name;
try
AssignFile(FileIni, TextFileIni);
Reset(FileIni);
Readln(FileIni, TextLineIni);
CloseFile(FileIni);
if AnsiSameText(TextLineIni, '[url]') then
begin
Result := True;
TextFileIniGame := TextFileIni;
Break;
end;
except
on EInOutError do;
end;
end;
ResultFind := FindNext(SearchRecIni);
end;
FindClose(SearchRecIni);
end;
function TConfiguration.FindIniPackage: Boolean;
begin
TextFileIniPackage := IncludeTrailingBackslash(TextDirPackage) + 'make.ini';
Result := FileExists(TextFileIniPackage);
end;
procedure TConfiguration.Read;
var
FileIni: TextFile;
IndexCharSeparator: Integer;
TextLine: string;
TextLineName: string;
TextLineValue: string;
TextSection: string;
procedure ReadIni(TextFileIni: string);
begin
StringListPaths.Clear;
StringListPackages.Clear;
AssignFile(FileIni, TextFileIni);
Reset(FileIni);
while not Eof(FileIni) do
begin
Readln(FileIni, TextLine);
TextLine := Trim(TextLine);
if (Length(TextLine) = 0) or (TextLine[1] = ';') then
Continue;
if TextLine[1] = '[' then
begin
TextSection := Copy(TextLine, 2, Length(TextLine) - 2);
end
else begin
IndexCharSeparator := Pos('=', TextLine);
if IndexCharSeparator = 0 then
Continue;
TextLineName := Copy(TextLine, 1, IndexCharSeparator - 1);
TextLineValue := Copy(TextLine, IndexCharSeparator + 1, Length(TextLine));
if AnsiSameText(TextSection, 'Core.System') then
begin
if AnsiSameText(TextLineName, 'Paths') then
StringListPaths.Add(TextLineValue);
if AnsiSameText(TextLineName, 'CacheRecordPath') then
TextDirCacheRecord := TextLineValue;
end
else if AnsiSameText(TextSection, 'Editor.EditorEngine') then
begin
if AnsiSameText(TextLineName, 'EditPackages') then
StringListPackages.Add(TextLineValue);
end;
end;
end;
CloseFile(FileIni);
end;
var
FlagFound: Boolean;
IndexPackage: Integer;
begin
if not FindIniGame then
raise EConfigurationGameIniNotFound.Create('Game configuration file not found');
ReadIni(TextFileIniGame);
if FindIniPackage then
ReadIni(TextFileIniPackage);
FlagFound := False;
for IndexPackage := 0 to StringListPackages.Count - 1 do
begin
if AnsiSameText(StringListPackages[IndexPackage], TextNamePackage) then
begin
FlagFound := True;
Break;
end;
end;
if not FlagFound then
StringListPackages.Add(TextNamePackage);
end;
procedure TConfiguration.Write;
var
StringListIni: TStringList;
procedure InsertSettings(TextSection: string; TextName: string; StringListSettings: TStringList);
var
IndexLine: Integer;
IndexLineSection: Integer;
IndexLineSetting: Integer;
IndexSetting: Integer;
TextLine: string;
begin
TextSection := '[' + TextSection + ']';
TextName := TextName + '=';
IndexLineSection := 0;
while (IndexLineSection < StringListIni.Count) and not AnsiSameText(Trim(StringListIni[IndexLineSection]), TextSection) do
Inc(IndexLineSection);
if IndexLineSection < StringListIni.Count then
begin
IndexLineSetting := -1;
IndexLine := IndexLineSection + 1;
while IndexLine < StringListIni.Count do
begin
TextLine := Trim(StringListIni[IndexLine]);
if (Length(TextLine) > 0) and (TextLine[1] = '[') then
begin
Break;
end
else if AnsiSameText(Copy(TextLine, 1, Length(TextName)), TextName) then
begin
if IndexLineSetting < 0 then
IndexLineSetting := IndexLine;
StringListIni.Delete(IndexLine);
end
else begin
Inc(IndexLine);
end;
end;
if IndexLineSetting < 0 then
begin
IndexLineSetting := IndexLine;
while Length(Trim(StringListIni[IndexLineSetting - 1])) = 0 do
Dec(IndexLineSetting);
end;
for IndexSetting := StringListSettings.Count - 1 downto 0 do
StringListIni.Insert(IndexLineSetting, TextName + StringListSettings[IndexSetting]);
end
else begin
StringListIni.Add('');
StringListIni.Add(TextSection);
for IndexSetting := 0 to StringListSettings.Count - 1 do
StringListIni.Add(TextName + StringListSettings[IndexSetting]);
end;
end;
procedure InsertSetting(TextSection: string; TextName: string; TextSetting: string);
var
StringListSetting: TStringList;
begin
StringListSetting := TStringList.Create;
StringListSetting.Add(TextSetting);
InsertSettings(TextSection, TextName, StringListSetting);
StringListSetting.Free;
end;
var
FlagChanged: Boolean;
IndexLine: Integer;
StringListIniOriginal: TStringList;
begin
StringListIni := TStringList.Create;
StringListIniOriginal := TStringList.Create;
FindIniPackage;
if FileExists(TextFileIniPackage) then
begin
StringListIni.LoadFromFile(TextFileIniPackage);
StringListIniOriginal.Assign(StringListIni);
end
else begin
StringListIni.Add('; Generated by UMake');
StringListIni.Add('');
StringListIni.Add('[Engine.Engine]');
StringListIni.Add('EditorEngine=Editor.EditorEngine');
StringListIni.Add('');
StringListIni.Add('[Editor.EditorEngine]');
StringListIni.Add('CacheSizeMegs=32');
end;
InsertSettings('Core.System', 'Paths', StringListPaths);
InsertSettings('Editor.EditorEngine', 'EditPackages', StringListPackages);
if Length(TextDirCacheRecord) > 0 then
InsertSetting ('Core.System', 'CacheRecordPath', TextDirCacheRecord);
if StringListIni.Count <> StringListIniOriginal.Count then
begin
FlagChanged := True;
end
else begin
FlagChanged := False;
for IndexLine := 0 to StringListIni.Count - 1 do
begin
if not AnsiSameText(StringListIni[IndexLine], StringListIniOriginal[IndexLine]) then
begin
FlagChanged := True;
Break;
end;
end;
end;
if FlagChanged then
StringListIni.SaveToFile(TextFileIniPackage);
StringListIni.Free;
end;
procedure TConfiguration.StringListPathsChange(Sender: TObject);
begin
HashFilePackage.Clear;
end;
function TConfiguration.FindFilePackage(TextPackage: string): string;
var
IndexPath: Integer;
TextFilePackage: string;
begin
if not HashFilePackage.Exists(LowerCase(TextPackage)) then
begin
for IndexPath := 0 to StringListPaths.Count - 1 do
begin
TextFilePackage := StringReplace(StringListPaths[IndexPath], '/', '\', [rfReplaceAll]);
TextFilePackage := GetAbsolutePath(TextFilePackage, IncludeTrailingBackslash(TextDirGame) + 'System\');
TextFilePackage := StringReplace(TextFilePackage, '*', TextPackage, [rfReplaceAll]);
if FileExists(TextFilePackage) then
begin
HashFilePackage[LowerCase(TextPackage)] := TextFilePackage;
Break;
end;
end;
if not HashFilePackage.Exists(LowerCase(TextPackage)) then
HashFilePackage[LowerCase(TextPackage)] := '';
end;
Result := HashFilePackage[LowerCase(TextPackage)];
end;
end.