forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prog.Data.pas
318 lines (281 loc) · 10.4 KB
/
Prog.Data.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
unit Prog.Data;
{$include lem_directives.inc}
interface
uses
LCLIntf, {$ifdef windows} Windows, {$endif}
Classes, SysUtils, Contnrs, Zipper, Generics.Collections,
Graphics, //Vcl.Imaging.PngImage,
Base.Utils,
Prog.Types, Prog.Base;
type
TDataType = (
Cursor, // game cursor
Sound, // game sound
Particles, // exploding particles
LemmingData, // generic lemmings data style dependant
LevelGraphics, // specific graphics data or vgaspecdata or metadata binding levelgraphics style dependant
LevelSpecialGraphics, // special graphics (vgaspec)
Level, // LVL only, style dependant
Music // game music, style dependant
);
// quite a complicated bussiness
TData = class sealed
strict private
const CACHE_LIMIT = 80 * 1024 * 1024;
// cached stuff
class var fLoadRequests: Integer;
class var fCacheHits: Integer;
class var fTotalCacheSize: Integer;
class var fCache: TObjectDictionary<string, TBytesStream>;
public
class procedure Init; static; // must be called at startup
class procedure Done; static;
class function CreateDataStream(const aStyleName, aFileName: string; aType: TDataType; preventCaching: Boolean = False): TBytesStream; static;
class function CreatePointer(const aStyleName, aFileName: string; aType: TDataType; out aSize: Integer; preventCaching: Boolean = False): Pointer; static;
class function CreateCursorBitmap(const aStyleName, aFileName: string; preventCaching: Boolean = False): TBitmap; static;
end;
implementation
{ TStreamUnZipper }
type
TStreamUnZipper = class(TUnZipper)
strict private
fInput: TStream;
fOutput: TStream;
procedure DoOpenInputStream(Sender: TObject; var AStream: TStream);
procedure DoCreateStream(Sender: TObject; var AStream: TStream;
AItem: TFullZipFileEntry);
procedure DoDoneStream(Sender: TObject; var AStream: TStream;
AItem: TFullZipFileEntry);
public
constructor Create(var aInput, aOutput: TStream);
end;
constructor TStreamUnZipper.Create(var aInput, aOutput: TStream);
begin
inherited Create;
fInput := TMemoryStream.Create;
fInput.CopyFrom(aInput, aInput.Size);
fOutput := aOutput;
OnOpenInputStream := DoOpenInputStream;
OnCreateStream := DoCreateStream;
OnDoneStream:= DoDoneStream;
end;
procedure TStreamUnZipper.DoOpenInputStream(Sender: TObject; var AStream: TStream);
begin
AStream := fInput;
Astream.Position := 0;
end;
procedure TStreamUnZipper.DoCreateStream(Sender: TObject; var AStream: TStream;
AItem: TFullZipFileEntry);
begin
AStream := TMemoryStream.Create;
end;
procedure TStreamUnZipper.DoDoneStream(Sender: TObject; var AStream: TStream;
AItem: TFullZipFileEntry);
begin
AStream.Position := 0;
fOutput.CopyFrom(AStream, AStream.Size);
AStream.Free;
end;
{ TData }
class procedure TData.Init;
begin
fCache := TObjectDictionary<string, TBytesStream>.Create([doOwnsValues]);
end;
class procedure TData.Done;
begin
fCache.Free;
// we normally have a high hit rate, so caching is nice.
// var s: string := fLoadRequests.ToString + ' / ' + fCacheHits.ToString;
//MessageBox(0, PChar(s), 'requests/cachehits', 0);
end;
class function TData.CreateDataStream(const aStyleName, aFileName: string; aType: TDataType; preventCaching: Boolean = False): TBytesStream;
{-------------------------------------------------------------------------------
Dependent on the datatype, we load a bytesstream.
-------------------------------------------------------------------------------}
const
method = 'CreateDataStream';
var
RealName: string;
cachedStream: TBytesStream;
mapping: Boolean;
procedure LoadFromZipResource(const aResName: string);
var
res: TResourceStream;
input, output: TStream;
zip: TStreamUnZipper;
internalname: string;
begin
internalname := ExtractFileName(RealName); // no pathinfo in zip
res := TResourceStream.Create(HINSTANCE, aResName, RT_RCDATA);
try
input := TMemoryStream.Create;
input.CopyFrom(res, res.Size);
input.Position := 0;
output := TMemoryStream.Create;
zip := TStreamUnZipper.Create(input, output);
try
//if zip.IndexOf(internalname) < 0 then
// Throw('File not found in zip-resource: ' + internalname, method + 'LoadFromZipResource');
zip.UnZipFile(internalname);
output.Position := 0;
Result := TBytesStream.Create;
Result.CopyFrom(output, output.Size);
Result.Position := 0;
finally
zip.Free;
end;
finally
res.Free;
input.Free;
output.Free;
end;
end;
procedure LoadFromNormalResource(const aResName: string);
var
res: TResourceStream;
begin
res := TResourceStream.Create(HINSTANCE, aResName, RT_RCDATA);
try
Result := TBytesStream.Create; // 'global' result
Result.CopyFrom(res, 0);
finally
res.Free;
end;
end;
procedure LoadFromDisk;
begin
if not FileExists(RealName) then
Exit;
Result := TBytesStream.Create; // 'global' result
Result.LoadFromFile(RealName);
end;
var
forceReadFromDisk: Boolean;
info: Consts.TStyleInformation;
begin
Result := nil;
//todo: make a little bit more readable
mapping := False;
forceReadFromDisk :=
((Consts.StyleDef = TStyleDef.User) and (aType in [TDataType.LemmingData, TDataType.LevelGraphics, TDataType.LevelSpecialGraphics, TDataType.Level, TDataType.Music]));
info := Consts.FindStyleInfo(aStyleName);
if (info.UserGraphicsMapping = TLevelGraphicsMapping.Concat) and (aType in [TDataType.LemmingData, TDataType.LevelGraphics]) then begin
mapping := True;
end;
if (info.UserSpecialGraphicsMapping = TLevelSpecialGraphicsMapping.Orig) and (aType = TDataType.LevelSpecialGraphics) then begin
mapping := True;
end;
case aType of
TDataType.Cursor : RealName := Consts.PathToCursors + ExtractFileName(aFileName);
TDataType.LemmingData : RealName := Consts.PathToLemmings[aStylename] + ExtractFileName(aFileName);
TDataType.LevelGraphics : RealName := Consts.PathToLemmings[aStylename] + ExtractFileName(aFileName);
TDataType.LevelSpecialGraphics : RealName := Consts.PathToLemmings[aStylename] + ExtractFileName(aFileName);
TDataType.Level : RealName := Consts.PathToLemmings[aStylename] + ExtractFileName(aFileName);
TDataType.Sound : RealName := Consts.PathToSounds + ExtractFileName(aFileName);
TDataType.Music : RealName := Consts.PathToMusics[aStylename] + ExtractFileName(aFileName);
TDataType.Particles : RealName := Consts.PathToParticles + ExtractFileName(aFileName);
else
Throw('Unhandled resource data type (' + aFileName + ')', 'CreateData');
end;
Inc(fLoadRequests);
if not preventCaching and fCache.TryGetValue(RealName, cachedStream) then begin
Result := TBytesStream.Create;
Result.CopyFrom(cachedStream, 0);
Result.Position := 0;
Inc(fCacheHits);
Exit;
end;
case aType of
TDataType.Cursor:
if not forceReadFromDisk
then LoadFromZipResource(Consts.ResNameZippedCursors)
else LoadFromDisk;
TDataType.LemmingData:
if mapping then LoadFromZipResource(Consts.ResNameCustom)
else if not forceReadFromDisk
then LoadFromZipResource(Consts.ResourceNameZippedLemmings[aStylename])
else LoadFromDisk;
TDataType.LevelGraphics:
if mapping then LoadFromZipResource(Consts.ResNameCustom)
else if not forceReadFromDisk
then LoadFromZipResource(Consts.ResourceNameZippedLemmings[aStylename])
else LoadFromDisk;
TDataType.LevelSpecialGraphics:
if mapping then LoadFromZipResource(Consts.ResNameCustom)
else if not forceReadFromDisk
then LoadFromZipResource(Consts.ResourceNameZippedLemmings[aStylename])
else LoadFromDisk;
TDataType.Level:
if not forceReadFromDisk
then LoadFromZipResource(Consts.ResourceNameZippedLemmings[aStylename])
else LoadFromDisk;
TDataType.Sound:
begin
if not forceReadFromDisk
then LoadFromZipResource(Consts.ResNameZippedSounds)
else LoadFromDisk;
end;
TDataType.Music:
begin
if not forceReadFromDisk
then LoadFromZipResource(Consts.ResourceNameZippedMusics[aStylename])
else LoadFromDisk;
end;
TDataType.Particles:
begin
if not forceReadFromDisk
then LoadFromNormalResource(Consts.ResNameParticles)
else LoadFromDisk;
end;
end;
if Result = nil then
Throw('Unassigned datastream for ' + aFileName + '(' + RealName + ')', method);
// only cache if not getting insane
if not preventCaching and Assigned(Result) and (fTotalCacheSize < CACHE_LIMIT) then begin
Inc(fTotalCacheSize, Result.Size);
cachedStream := TBytesStream.Create;
cachedStream.CopyFrom(Result, 0);
fCache.Add(RealName, cachedStream);
end;
if Assigned(Result) then
Result.Position := 0;
end;
class function TData.CreatePointer(const aStyleName, aFileName: string; aType: TDataType; out aSize: Integer; preventCaching: Boolean = False): Pointer;
// used for sounds
var
stream: TBytesStream;
begin
aSize := 0;
Result := nil;
stream := nil;
try
stream := CreateDataStream(aStyleName, aFileName, aType, preventCaching);
if stream.Size = 0 then
Exit;
GetMem(Result, stream.Size);
Move(stream.Memory^, Result^, stream.Size);
aSize := stream.Size;
finally
stream.Free;
end;
end;
class function TData.CreateCursorBitmap(const aStyleName, aFileName: string; preventCaching: Boolean = False): TBitmap;
var
stream : TBytesStream;
begin
Result := nil;
stream := nil;
try
stream := CreateDataStream(aStyleName, aFileName, TDataType.Cursor, preventCaching);
Result := TBitmap.Create;
try
Result.LoadFromStream(stream);
except
FreeAndNil(Result);
raise;
end;
finally
stream.Free;
end;
end;
end.