forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProg.Cache.pas
764 lines (684 loc) · 24.1 KB
/
Prog.Cache.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
unit Prog.Cache;
{$include lem_directives.inc}
interface
uses
Classes, Contnrs, SysUtils, FileUtil, Generics.Collections, Character,
Dos.Structures,
Base.Utils,
Level.Hash,
Styles.Base, Styles.Factory,
Prog.Types, Prog.Base;
type
// the style cache builds lists of all styles with levelhashes + titles.
// this is needed to be able to quickly find levelhashes, levelcodes, replayfiles
IStyleCacheFeedback = interface
procedure LoadingFeedback(const s: string); virtual; abstract;
end;
TStyleCache = class
public
const
MAX_STYLENAME_LENGTH = 64;
CACHE_ID = 'LCACHE';
CACHE_VERSION = 8;
LEVEL_ID = 'L';
type
// on disk
THeaderRec = packed record
Id: array[1..6] of Char; // = CACHE_ID; version 1 = 'LEMMIX' version 2 = 'LCACHE'
LemmixVersion: Consts.TVersionRecord;
CacheVersion: Integer;
StyleName: array[1..MAX_STYLENAME_LENGTH] of Char;
Family: TStyleFamily;
FileSize: Integer;
LevelCount: Integer; // total levelinfo records
LastWriteTimeInSourceFolder: TDateTime;
LastWriteTimeOfConfigFile: TDateTime;
procedure Clear; inline;
function GetStyleName: string;
procedure SetStyleName(const s: string);
end;
TStatics = packed record
ReleaseRate : Byte;
LemmingsCount : Byte;
RescueCount : Byte;
TimeLimit : Byte;
ClimberCount : Byte;
FloaterCount : Byte;
BomberCount : Byte;
BlockerCount : Byte;
BuilderCount : Byte;
BasherCount : Byte;
MinerCount : Byte;
DiggerCount : Byte;
GraphicSet : Byte;
GraphicSetEx : Byte;
SuperLemming : Boolean;
ObjectCount : Byte;
TerrainCount : word;
EntranceCount : Byte;
ExitCount : Byte;
end;
// on disk
TLevelRec = packed record
Id: Char; // = LEVEL_ID; = 'L'
SectionIndex: Integer;
LevelIndex: Integer;
LevelHash: UInt64;
Statics: TStatics;
LevelTitle: TLVLTitle;
SourceFile: TArray<Char>;
procedure Clear; inline;
function GetTitle: string;
function GetSourceFile: string;
procedure SetSourceFile(const aValue: string);
end;
const
LEVEL_REC_SIZE_UNTIL_SOURCE_FILE = SizeOf(Char) + SizeOf(Integer) * 2 + SizeOf(UInt64) + SizeOf(TStatics) + SizeOf(TLVLTitle);
// Id section + level hash statics title
type
// in memory
TLevelCacheItem = class
private
fStyleName: string;
//Family: TStyleFamily; todo
fSectionIndex: Integer;
fLevelIndex: Integer;
fLevelHash: UInt64;
fLevelCode: string;
fLevelTitle: TLVLTitle;
fStatics: TStatics;
fSourceFile: string;
public
constructor Create(const aStyleName: string; const aLevelRec: TLevelRec);
function GetTitleAsString: string;
function MatchesWithLevelLoadingInformation(info: TLevelLoadingInformation): Boolean;
property StyleName: string read fStyleName;
property SectionIndex: Integer read fSectionIndex;
property LevelIndex: Integer read fLevelIndex;
property LevelHash: UInt64 read fLevelHash;
property LevelCode: string read fLevelCode;
property LevelTitle: TLVLTitle read fLevelTitle;
property Statics: TStatics read fStatics;
property SourceFile: string read fSourceFile;
end;
TLevelCacheList = TFastObjectList<TLevelCacheItem>;
// TExactEntry = record
// Style: string;
// Section: Integer;
// Level: Integer;
// constructor Create(const aStyle: string; aSection, aLevel: Integer);
// end;
private
fFlatList: TFastObjectList<TLevelCacheItem>; // todo: make this one the ultimate owner
fCache: TObjectDictionary<string, TLevelCacheList>; // key=stylename, value=levels
fHashCache: TObjectDictionary<UInt64, TLevelCacheList>; // key=levelhash, value=levels which have this hashcode
fCodeCache: TObjectDictionary<string, TLevelCacheList>; // key=levelcode, value=levels which have this levelcode
fTitleCache: TObjectDictionary<TLVLTitle, TLevelCacheList>; // key=leveltitle, value=levels which have this title
// fExactCache : TObjectDictionary<TExactEntry, TLevelCacheItem>;
fOnFeedback: IStyleCacheFeedback; // simple feedback during load
fLastState: string;
procedure Feedback(const state: string);
function ReadHeader(s: TStream; var header: THeaderRec): Boolean;
function ReadLevelInfo(s: TStream; var info: TLevelRec): Boolean;
procedure WriteLevelInfo(s: TStream; const info: TLevelRec);
function RebuildNeeded(const aStyleName: string): Boolean;
procedure BuildStyleCache(const aStyle: TStyle; aTargetList: TLevelCacheList);
procedure LoadStyleCache(const aStyleName: string; aTargetlist: TLevelCacheList);
public
constructor Create;
destructor Destroy; override;
procedure Load(const aFeedbackProc: IStyleCacheFeedback);
function GetCacheFilenames(fullName: Boolean = True): TArray<string>;
function FindLevelsByHash(const aHash: UInt64): TArray<TLevelCacheItem>;
function FindLevelsByCode(const aLevelCode: string): TArray<TLevelCacheItem>;
function FindLevelsByTitle(const aLevelTitle: TLVLTitle): TArray<TLevelCacheItem>;
// function FindExactLevel(const aStyleName: string; aSectionIndex, aLevelIndex: Integer): TLevelCacheItem;
function GetLevelCount(const aStyleName: string): Integer;
function GetTotalLevelCount: Integer;
procedure ToCsv(const aFilename: string);
property FlatList: TFastObjectList<TLevelCacheItem> read fFlatList;
end;
implementation
{ TStyleCache.THeaderRec }
procedure TStyleCache.THeaderRec.Clear;
begin
FillChar(Self, SizeOf(Self), 0);
end;
function TStyleCache.THeaderRec.GetStyleName: string;
var
C: Char;
i: Integer;
begin
Result := '';
for i := 1 to MAX_STYLENAME_LENGTH do begin
C := StyleName[i];
if C = #0 then
Break;
Result := Result + C;
end;
end;
procedure TStyleCache.THeaderRec.SetStyleName(const s: string);
var
i: Integer;
begin
for i := 1 to Length(s) do
StyleName[i] := s[i];
end;
{ TStyleCache.TLevelRec }
function TStyleCache.TLevelRec.GetTitle: string;
var
C: AnsiChar;
i: Integer;
begin
SetLength(Result, 31);
for i := 0 to 31 do begin
C := LevelTitle[i];
Result[i + 1] := Char(C);
end;
end;
procedure TStyleCache.TLevelRec.Clear;
begin
SetLength(SourceFile, 0);
FillChar(Self, SizeOf(Self), 0);
end;
function TStyleCache.TLevelRec.GetSourceFile: string;
var
i: Integer;
begin
SetLength(Result, Length(SourceFile));
for i := 0 to Length(SourceFile) - 1 do
Result[i + 1] := SourceFile[i];
end;
procedure TStyleCache.TLevelRec.SetSourceFile(const aValue: string);
var
i: Integer;
begin
SetLength(SourceFile, Length(aValue));
for i := 1 to Length(aValue) do
SourceFile[i - 1] := aValue[i];
end;
{ TStyleCache.TLevelCacheItem }
constructor TStyleCache.TLevelCacheItem.Create(const aStyleName: string; const aLevelRec: TLevelRec);
begin
fStylename := aStyleName;
fSectionIndex := aLevelRec.SectionIndex;
fLevelIndex := aLevelRec.LevelIndex;
fLevelHash := aLevelRec.LevelHash;
fLevelTitle := aLevelRec.LevelTitle;
fLevelCode := TLevelHasher.GetLevelCode(fLevelHash);
fStatics := aLevelRec.Statics;
fSourceFile := aLevelRec.GetSourceFile;
end;
function TStyleCache.TLevelCacheItem.GetTitleAsString: string;
var
C: AnsiChar;
i: Integer;
begin
SetLength(Result, 31);
for i := 0 to 31 do begin
C := fLevelTitle[i];
Result[i + 1] := Char(C);
end;
end;
function TStyleCache.TLevelCacheItem.MatchesWithLevelLoadingInformation(info: TLevelLoadingInformation): Boolean;
begin
Result :=
Assigned(info) and
(fStyleName = info.Style.Name) and
(fSectionIndex = info.SectionIndex) and
(fLevelIndex = info.LevelIndex) and
(fSourceFile = info.SourceFileName);
end;
//
//{ TStyleCache.TExactEntry }
//
//constructor TStyleCache.TExactEntry.Create(const aStyle: string; aSection, aLevel: Integer);
//begin
// Style := aStyle;
// Section := aSection;
// Level := aLevel;
//end;
{ TStyleCache }
constructor TStyleCache.Create;
begin
fCache := TObjectDictionary<string, TLevelCacheList>.Create([doOwnsValues]);
fHashCache := TObjectDictionary<UInt64, TLevelCacheList>.Create([doOwnsValues]);
fCodeCache := TObjectDictionary<string, TLevelCacheList>.Create([doOwnsValues]);
fTitleCache := TObjectDictionary<TLVLTitle, TLevelCacheList>.Create([doOwnsValues]);
fFlatList := TFastObjectList<TLevelCacheItem>.Create(False);
// fExactCache := TObjectDictionary<TExactEntry, TLevelCacheItem>.Create;
end;
destructor TStyleCache.Destroy;
begin
fCache.Free;
fHashCache.Free;
fCodeCache.Free;
fTitleCache.Free;
//fExactCache.Free;
fFlatList.Free;
inherited;
end;
procedure TStyleCache.Feedback(const state: string);
begin
if fLastState <> state then begin
fLastState := state;
if Assigned(fOnFeedback) then
fOnFeedback.LoadingFeedback(state);
end;
end;
function TStyleCache.ReadHeader(s: TStream; var header: THeaderRec): Boolean;
// returns false if some error or version conflict
begin
s.Read(header, SizeOf(header));
if header.Id <> CACHE_ID then
Exit(False);
if header.CacheVersion <> CACHE_VERSION then
Exit(False);
Result := True;
end;
function TStyleCache.ReadLevelInfo(s: TStream; var info: TLevelRec): Boolean;
var
len: Integer;
begin
// if s.ReadData(info) <> SizeOf(info) then
// Exit(False);
// if info.Id <> 'L' then
// Exit(False);
info.Clear;
s.Read(info, LEVEL_REC_SIZE_UNTIL_SOURCE_FILE);
if info.Id <> 'L' then
Exit(False);
len := 0;
s.Read(len, SizeOf(len));
if (len < 0) or (len > 255) then
Throw('Invalid sourcefilename length encountered (' + len.ToString + ')', 'ReadLevelInfo');
if len > 0 then begin
SetLength(info.SourceFile, len);
s.ReadBuffer(info.SourceFile[0], len * SizeOf(Char));
end;
Result := True;
end;
procedure TStyleCache.WriteLevelInfo(s: TStream; const info: TLevelRec);
var
len: Integer;
begin
s.Write(info, LEVEL_REC_SIZE_UNTIL_SOURCE_FILE);
len := Length(info.SourceFile);
if (len < 0) or (len > 255) then
Throw('Invalid sourcefilename length encountered (' + len.ToString + ')', 'WriteLevelInfo');
s.Write(len, SizeOf(len));
if len > 0 then
s.WriteBuffer(info.SourceFile[0], len * SizeOf(Char));
end;
function TStyleCache.RebuildNeeded(const aStyleName: string): Boolean;
var
filename, configfilename: string;
sourcePath: string;
header: THeaderRec;
stream: TBufferedFileStream;
isCustom: Boolean;
begin
filename := Consts.PathToCache + aStyleName + '.cache';
configfilename := Consts.PathToStyle[aStyleName] + 'Style.config';
if not FileExists(filename) then
Exit(True);
isCustom := Consts.IsUserStyle(aStylename);
sourcePath := Consts.PathToStyles + aStyleName;
stream := TBufferedFileStream.Create(filename, fmOpenRead);
try
// check header
if not ReadHeader(stream, header) then
Exit(True);
if isCustom then begin
// check folder changes
if FileAge(sourcePath) <> header.LastWriteTimeInSourceFolder then
Exit(True);
// check config file change
if FileExists(configfilename) then begin
if header.LastWriteTimeOfConfigFile <> FileAge(configfilename) then
Exit(True);
end
else begin
if header.LastWriteTimeOfConfigFile = 0 then
Exit(True);
end;
end;
Result := False;
finally
stream.Free;
end;
end;
procedure TStyleCache.BuildStyleCache(const aStyle: TStyle; aTargetList: TLevelCacheList);
// write all levelinfo to file
var
filename, configfilename: string;
sourcePath: string;
stream: TBytesStream;
header: THeaderRec;
ix: Integer;
C: Char;
rec: TLevelRec;
lev: TLevelLoadingInformation;
item: TLevelCacheItem;
LVL: PLVLRec;
entranceCount, exitCount: Integer;
begin
sourcePath := Consts.PathToStyle[aStyle.Name];
filename := Consts.PathToCache + aStyle.Name + '.cache';
configfilename := Consts.PathToStyle[aStyle.Name] + 'Style.config';
if not ForceDir(filename) then
Throw('Cannot create cache directory');
// fill header
header.Clear;
header.Id := CACHE_ID;
header.LemmixVersion := Consts.LemmixVersionRecord;
header.CacheVersion := CACHE_VERSION;
ix := 1;
for C in aStyle.Name do begin
header.StyleName[ix] := C;
Inc(ix);
if ix > Length(header.StyleName) then
Break;
end;
if DirectoryExists(sourcePath) then
header.LastWriteTimeInSourceFolder := FileAge(sourcePath)
else
header.LastWriteTimeInSourceFolder := 0;
if FileExists(configfilename) then
header.LastWriteTimeOfConfigFile := FileAge(configfilename);
stream := TBytesStream.Create;
try
// write header
header.FileSize := SizeOf(header);
stream.Write(header, SizeOf(header));
// write levels
lev := aStyle.LevelSystem.FirstLevelOrDefault;
while lev <> nil do begin
rec.Clear;
rec.Id := 'L';
rec.SectionIndex := lev.SectionIndex;
rec.LevelIndex := lev.LevelIndex;
rec.LevelHash := lev.GetLevelHash; // cachelvl triggered
Assert(lev.IsCached);
LVL := lev.CachedLVL;
rec.Statics.ReleaseRate := LVL^.SwapProp(LVL^.ReleaseRate);
rec.Statics.LemmingsCount := LVL^.SwapProp(LVL^.LemmingsCount);
rec.Statics.RescueCount := LVL^.SwapProp(LVL^.RescueCount);
rec.Statics.TimeLimit := LVL^.SwapProp(LVL^.TimeLimit);
rec.Statics.ClimberCount := LVL^.SwapProp(LVL^.ClimberCount);
rec.Statics.FloaterCount := LVL^.SwapProp(LVL^.FloaterCount);
rec.Statics.BomberCount := LVL^.SwapProp(LVL^.BomberCount);
rec.Statics.BlockerCount := LVL^.SwapProp(LVL^.BlockerCount);
rec.Statics.BuilderCount := LVL^.SwapProp(LVL^.BuilderCount);
rec.Statics.BasherCount := LVL^.SwapProp(LVL^.BasherCount);
rec.Statics.MinerCount := LVL^.SwapProp(LVL^.MinerCount);
rec.Statics.DiggerCount := Byte(LVL^.SwapProp(LVL^.DiggerCount));
rec.Statics.GraphicSet := Byte(LVL^.SwapProp(LVL^.GraphicSet));
rec.Statics.GraphicSetEx := LVL^.SwapProp(LVL^.GraphicSetEx);
rec.Statics.SuperLemming := LVL^.IsSuperLemming;
rec.Statics.ObjectCount := Byte(LVL^.GetObjectCount(entranceCount, exitCount));
rec.Statics.TerrainCount := Word(LVL^.GetTerrainCount);
rec.Statics.EntranceCount := Byte(entranceCount);
rec.Statics.ExitCount := Byte(exitCount);
rec.LevelTitle := lev.GetRawLVLTitle;
rec.SetSourceFile(lev.SourceFileName);
inc(header.LevelCount);
inc(header.FileSize, SizeOf(rec));
item := TLevelCacheItem.Create(aStyle.Name, rec);
aTargetList.Add(item);
fFlatList.Add(item);
WriteLevelInfo(stream, Rec);
//stream.WriteData(rec);
lev := lev.Next;
end;
stream.Position := 0;
stream.Write(header, SizeOf(header));
stream.SaveToFile(filename);
finally
stream.Free;
end;
end;
procedure TStyleCache.LoadStyleCache(const aStyleName: string; aTargetlist: TLevelCacheList);
// load all levelinfo from file
var
filename: string;
stream: TBufferedFileStream;
header: THeaderRec;
item: TLevelCacheItem;
rec: TLevelRec;
begin
filename := Consts.PathToCache + aStyleName + '.cache';
if not FileExists(filename) then
Throw('Cannot find cache: ' + filename);
stream := TBufferedFileStream.Create(filename, fmOpenRead);
try
if not ReadHeader(stream, header) then
Throw('Invalid cache: ' + filename + sLineBreak + 'Please delete this file and restart'); // todo: rewrite cache is needed
while ReadLevelInfo(stream, rec) do begin
item := TLevelCacheItem.Create(aStyleName, rec);
aTargetList.Add(item);
fFlatList.Add(item);
end;
finally
stream.Free;
end;
end;
procedure TStyleCache.Load(const aFeedbackProc: IStyleCacheFeedback);
const
method = 'Load';
var
oldStyleName: string;
cacheFiles: TStringList;
cachefile, check: string;
styleinfo: Consts.TStyleInformation;
style: TStyle;
list: TLevelCacheList;
key: string;
info: TLevelCacheItem;
begin
oldStyleName := Consts.StyleName;
fOnFeedback := aFeedbackProc;
try
Feedback('Loading');
// remove user style caches which are obsolete (not found in Styles folder)
if DirectoryExists(Consts.PathToCache) then begin
cacheFiles := FindAllFiles(Consts.PathToCache, '*.cache');
for cachefile in cacheFiles do begin
check := ReplaceFileExt(ExtractFileName(cachefile), '');
if Consts.IsUserStyle(check) and not DirectoryExists(Consts.PathToStyles + check) then
DeleteFile(cachefile);
end;
end;
// build
for styleinfo in Consts.StyleInformationlist do begin
list := TLevelCacheList.Create;
fCache.Add(styleinfo.Name, list);
// load if cache is up to date
if not RebuildNeeded(styleinfo.Name) then
LoadStyleCache(styleinfo.Name, list)
// otherwise build and load
else begin
Feedback('Cache ' + styleinfo.Name);
// only one style can be loaded in the program so here we trick the system
Consts.SetStyleName(styleinfo.Name);
style := TStyleFactory.CreateStyle(True);
try
BuildStyleCache(style, list);
finally
style.Free;
end;
end
end;
Feedback('Loading');
// now cache levelhash + levelcodes + leveltitles in dictionaries for fast searching
for key in fCache.Keys do begin
if not fCache.TryGetValue(key, list) or (list = nil) then
Throw('Fatal error dictionary key not found (' + key + ') or unassigned list during loading', method);
for info in list do begin
// hash
list := nil;
if not fHashCache.TryGetValue(info.LevelHash, list) then begin
list := TLevelCacheList.Create(False); // this list does not own the items. ref only
list.Add(info);
fHashCache.Add(info.LevelHash, list);
end
else begin
list.Add(info);
end;
// code
list := nil;
if not fCodeCache.TryGetValue(info.LevelCode, list) then begin
list := TLevelCacheList.Create(False); // this list does not own the items. ref only
list.Add(info);
fCodeCache.Add(info.LevelCode, list);
end
else begin
list.Add(info);
end;
// title
list := nil;
if not fTitleCache.TryGetValue(info.LevelTitle, list) then begin
list := TLevelCacheList.Create(False); // this list does not own the items. ref only
list.Add(info);
fTitleCache.Add(info.levelTitle, list);
end
else begin
list.Add(info);
end;
// exact
//fExactCache.Add(TExactEntry.Create(key, info.SectionIndex, info.LevelIndex), info);
// if not Assigned(FindExactLevel(key, info.SectionIndex, info.LevelIndex)) then
// dlg('kut');
end;
end;
finally
fOnFeedback := nil;
Consts.SetStyleName(oldStyleName); // restore
end;
end;
function TStyleCache.FindLevelsByHash(const aHash: UInt64): TArray<TLevelCacheItem>;
var
list: TLevelCacheList;
begin
Result := nil;
if fHashCache.TryGetValue(aHash, list) then
Result := list.ToArray;
end;
//function TStyleCache.FindExactLevel(const aStyleName: string; aSectionIndex, aLevelIndex: Integer): TLevelCacheItem;
//begin
// //fExactCache.TryGetValue(TExactEntry.Create(aStyleName, aSectionIndex, aLevelIndex), Result);
//end;
function TStyleCache.FindLevelsByCode(const aLevelCode: string): TArray<TLevelCacheItem>;
var
list: TLevelCacheList;
begin
Result := nil;
if fCodeCache.TryGetValue(aLevelCode, list) then
Result := list.ToArray;
end;
function TStyleCache.FindLevelsByTitle(const aLevelTitle: TLVLTitle): TArray<TLevelCacheItem>;
var
list: TLevelCacheList;
begin
Result := nil;
if fTitleCache.TryGetValue(aLevelTitle, list) then
Result := list.ToArray;
end;
function TStyleCache.GetLevelCount(const aStyleName: string): Integer;
var
list: TLevelCacheList;
begin
if fCache.TryGetValue(aStyleName, list) then
Result := list.Count
else
Result := 0;
end;
function TStyleCache.GetTotalLevelCount: Integer;
begin
Result := fFlatList.Count;
end;
procedure TStyleCache.ToCsv(const aFilename: string);
const
tab = Chr(9);
var
l: TStringList;
ix: Integer;
info: TLevelCacheItem;
tmp: TFastObjectList<TLevelCacheItem>;
hashdups, titledups: Integer;
begin
// showmessage('titlecachecount = ' + fTitleCache.Count.ToString + sLineBreak + 'codecache count = ' + fCodeCache.Count.ToString);
//
l := tstringlist.Create;
l.add('INDEX' + tab + 'STYLE' + tab + 'SECTION' + tab + 'LEVEL' + tab + 'HASH' + tab + 'HASHDUPS' + tab + 'LEVELCODE' + tab + 'TITLE' + tab +
'TITLEDUPS' + tab + 'SOURCE' + tab +
'RELEASERATE' + tab +
'LEMMINGSCOUNT' + tab +
'RESCUECOUNT' + tab +
'TIMELIMIT' + tab +
'CLIMBERCOUNT' + tab +
'FLOATERCOUNT' + tab +
'BOMBERCOUNT' + tab +
'BLOCKERCOUNT' + tab +
'BUILDERCOUNT' + tab +
'BASHERCOUNT' + tab +
'MINERCOUNT' + tab +
'DIGGERCOUNT' + tab +
'GRAPHICSET' + tab +
'GRAPHICSETEX' + tab +
'SUPERLEMMING' + tab +
'TERRAINCOUNT' + tab +
'OBJECTCOUNT' + tab +
'ENTRANCECOUNT' + tab +
'EXITCOUNT'
);
ix := 0;
for info in fflatList do begin
hashdups := 0;
titledups := 0;
if fHashCache.TryGetValue(info.fLevelHash, tmp) then
hashdups := tmp.Count;
if fTitleCache.TryGetValue(info.fLevelTitle, tmp) then
titledups := tmp.Count;
l.add(ix.tostring + tab + info.StyleName + tab + info.SectionIndex.ToString + tab + info.LevelIndex.ToString + tab + IntToHex(info.LevelHash, 16) + tab +
hashdups.tostring + tab + info.LevelCode + tab + '[' + info.GetTitleAsString + ']' + tab + titledups.ToString + tab + info.fSourceFile + tab +
info.fStatics.ReleaseRate.ToString + tab +
info.fStatics.LemmingsCount.ToString + tab +
info.fStatics.RescueCount.ToString + tab +
info.fStatics.TimeLimit.ToString + tab +
info.fStatics.ClimberCount.ToString + tab +
info.fStatics.FloaterCount.ToString + tab +
info.fStatics.BomberCount.ToString + tab +
info.fStatics.BlockerCount.ToString + tab +
info.fStatics.BuilderCount.ToString + tab +
info.fStatics.BasherCount.ToString + tab +
info.fStatics.MinerCount.ToString + tab +
info.fStatics.DiggerCount.ToString + tab +
info.fStatics.GraphicSet.ToString + tab +
info.fStatics.GraphicSetEx.ToString + tab +
Byte(info.fStatics.SuperLemming).ToString + tab +
info.fStatics.TerrainCount.ToString + tab +
info.fStatics.ObjectCount.ToString + tab +
info.fStatics.EntranceCount.ToString + tab +
info.fStatics.ExitCount.ToString);
Inc(ix);
end;
l.SaveToFile(ReplaceFileExt(aFileName, '.csv'));
l.Free;
end;
function TStyleCache.GetCacheFilenames(fullName: Boolean): TArray<string>;
var
ix: Integer;
key: string;
begin
SetLength(Result, fCache.Count);
ix := 0;
for key in fCache.Keys do begin
if fullName then
Result[ix] := Consts.PathToCache + key + '.cache'
else
Result[ix] := key + '.cache';
Inc(ix);
end;
end;
end.