-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuHighscoreEngine.pas
More file actions
343 lines (308 loc) · 9.81 KB
/
uHighscoreEngine.pas
File metadata and controls
343 lines (308 loc) · 9.81 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
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
(******************************************************************************)
(* Highscore Engine ??.??.???? *)
(* *)
(* Version : 0.01 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : Provides a simple highscore engine *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* *)
(******************************************************************************)
Unit uHighscoreEngine;
Interface
Uses
SysUtils, // inttostr, Fileexists
Classes // TFilestream
;
Const
HighscoreEngineVersion: byte = 001;
Type
TItem = Record
Name: String;
Points: integer;
End;
TItemList = Array Of TItem;
THighscoreEngine = Class
private
FPassword: Array Of byte;
FPasswordPointer: integer;
FMaxItems: integer;
FFilename: String;
Fdata: TItemlist;
FChanged: boolean;
Procedure QuickSortAsc(Var Value: TItemList; li, re: integer);
Procedure QuickSortDesc(Var Value: TItemList; li, re: integer);
Procedure WriteString(Const Stream: TStream; Value: String);
Procedure WriteInteger(Const Stream: TStream; Value: integer);
Procedure WriteByte(Const Stream: TStream; Value: byte);
Function ReadString(Const Stream: TStream): String;
Function ReadInteger(Const Stream: TStream): integer;
Function ReadByte(Const Stream: TStream): byte;
public
InsertAlways: boolean; // Wenn dieser Wert = True ist dann wird ein Neuer Item im zweifel als Letzer eingefügt.
Property Changed: boolean read FChanged;
Constructor Create(Filename, Password: String; MaxItems: integer);
Destructor Destroy; override;
Procedure LoadFromStream(Const Stream: Tstream);
Procedure SaveToStream(Const Stream: Tstream);
Procedure Clear;
Procedure Save;
Procedure Add(Name: String; Points: integer);
Function Show(Ascending: boolean): TItemlist;
End;
Implementation
{ THighscoreEngine }
Constructor THighscoreEngine.Create(Filename, Password: String; MaxItems: integer);
Var
f: TFileStream;
m: TMemoryStream;
i: integer;
Begin
Inherited Create;
InsertAlways := False;
FMaxItems := MaxItems;
FFilename := Filename;
setlength(FPassword, length(Password));
For i := 1 To Length(Password) Do
Fpassword[i - 1] := Ord(password[i]);
Clear;
FChanged := False;
If FileExists(Filename) Then Begin
f := TFileStream.Create(FFilename, fmOpenRead);
m := TMemoryStream.Create;
m.CopyFrom(f, f.size);
f.Free;
m.position := 0;
LoadFromStream(m);
m.Free;
End;
End;
Destructor THighscoreEngine.Destroy;
Begin
Clear;
End;
Procedure THighscoreEngine.Add(Name: String; Points: integer);
Begin
FChanged := True;
If InsertAlways Then Begin
If high(Fdata) >= FMaxItems - 1 Then Begin
QuickSortDesc(fdata, 0, high(fdata));
If fdata[high(fdata)].Points >= Points Then Begin
fdata[high(fdata)].Name := Name;
fdata[high(fdata)].Points := Points;
End
Else Begin
setlength(fdata, high(fdata) + 2);
fdata[high(fdata)].Name := Name;
fdata[high(fdata)].Points := Points;
QuickSortDesc(fdata, 0, high(fdata));
setlength(fdata, FMaxItems);
End;
End
Else Begin
setlength(fdata, high(fdata) + 2);
fdata[high(fdata)].Name := Name;
fdata[high(fdata)].Points := Points;
End;
End
Else Begin
setlength(fdata, high(fdata) + 2);
fdata[high(fdata)].Name := Name;
fdata[high(fdata)].Points := Points;
If high(Fdata) >= FMaxItems - 1 Then Begin
QuickSortDesc(fdata, 0, high(fdata));
setlength(fdata, FMaxItems);
End;
End;
End;
Procedure THighscoreEngine.Clear;
Begin
SetLength(fdata, 0);
FChanged := True;
End;
Procedure THighscoreEngine.Save;
Var
f: Tfilestream;
m: TMemorystream;
Begin
m := TMemoryStream.Create;
SaveToStream(m);
f := TFileStream.Create(ffilename, fmcreate Or fmopenwrite);
m.position := 0;
f.CopyFrom(m, m.size);
m.Free;
f.Free;
End;
Function THighscoreEngine.Show(Ascending: boolean): TItemlist;
Var
i: integer;
Begin
result := Nil;
setlength(Result, high(Fdata) + 1);
For i := 0 To high(Fdata) Do
Result[i] := Fdata[i];
If Ascending Then
QuickSortAsc(Result, 0, high(Result))
Else
QuickSortDesc(Result, 0, high(Result));
End;
Procedure THighscoreEngine.LoadFromStream(Const Stream: Tstream);
Var
dummy: byte;
i: integer;
Begin
FChanged := False;
FPasswordPointer := 0;
dummy := ReadByte(Stream);
If dummy <> HighscoreEngineVersion Then
Raise Exception.Create('Error : Invalid file version.');
i := ReadInteger(stream);
setlength(fdata, i);
For i := 0 To High(Fdata) Do Begin
Fdata[i].Name := ReadString(Stream);
Fdata[i].Points := ReadInteger(Stream);
End;
End;
Procedure THighscoreEngine.SaveToStream(Const Stream: Tstream);
Var
i: integer;
Begin
FChanged := False;
FPasswordPointer := 0;
WriteByte(stream, HighscoreEngineVersion);
i := High(Fdata) + 1;
writeInteger(Stream, i);
For i := 0 To High(Fdata) Do Begin
WriteString(Stream, Fdata[i].Name);
WriteInteger(Stream, Fdata[i].Points);
End;
End;
Function THighscoreEngine.ReadByte(Const Stream: TStream): byte;
Var
b: byte;
Begin
b := 0;
stream.Read(b, sizeof(b));
Result := b Xor fpassword[fpasswordpointer];
fpasswordpointer := (fpasswordpointer + 1) Mod (high(fpassword) + 1);
End;
Function THighscoreEngine.ReadInteger(Const Stream: TStream): integer;
Var
b: byte;
Begin
b := ReadByte(Stream);
Result := b;
b := ReadByte(Stream);
Result := Result Or (b Shl 8);
b := ReadByte(Stream);
Result := Result Or (b Shl 16);
b := ReadByte(Stream);
Result := Result Or (b Shl 24);
End;
Function THighscoreEngine.ReadString(Const Stream: TStream): String;
Var
i: integer;
b: byte;
Begin
result := '';
i := ReadInteger(stream);
setlength(Result, i);
For i := 1 To Length(Result) Do Begin
b := ReadByte(stream);
Result[i] := chr(b);
End;
End;
Procedure THighscoreEngine.WriteByte(Const Stream: TStream; Value: byte);
Var
b: byte;
Begin
b := Value Xor fpassword[fpasswordpointer];
stream.Write(b, sizeof(b));
fpasswordpointer := (fpasswordpointer + 1) Mod (high(fpassword) + 1);
End;
Procedure THighscoreEngine.WriteInteger(Const Stream: TStream; Value: integer);
Begin
WriteByte(stream, byte(Value));
WriteByte(stream, byte(Value Shr 8));
WriteByte(stream, byte(Value Shr 16));
WriteByte(stream, byte(Value Shr 24));
End;
Procedure THighscoreEngine.WriteString(Const Stream: TStream; Value: String);
Var
i: integer;
Begin
i := length(Value);
WriteInteger(stream, i);
For i := 1 To length(Value) Do
WriteByte(stream, Ord(Value[i]));
End;
Procedure THighscoreEngine.QuickSortAsc(Var Value: TItemList; li, re: integer);
Var
h: TItem;
l, r, p: integer;
Begin
If Li < Re Then Begin
p := Value[Trunc((li + re) / 2)].Points; // Auslesen des Pivo Elementes
l := Li;
r := re;
While l < r Do Begin
While Value[l].Points < p Do
Inc(l);
While Value[r].Points > p Do
Dec(r);
If L <= R Then Begin
h := Value[l];
Value[l] := Value[r];
Value[r] := h;
Inc(l);
Dec(r);
End;
End;
QuickSortAsc(Value, li, r);
QuickSortAsc(Value, l, re);
End;
End;
Procedure THighscoreEngine.QuickSortDesc(Var Value: TItemList; li, re: integer);
Var
h: TItem;
l, r, p: integer;
Begin
If Li < Re Then Begin
p := Value[Trunc((li + re) / 2)].Points; // Auslesen des Pivo Elementes
l := Li;
r := re;
While l < r Do Begin
While Value[l].Points > p Do
Inc(l);
While Value[r].Points < p Do
Dec(r);
If L <= R Then Begin
h := Value[l];
Value[l] := Value[r];
Value[r] := h;
Inc(l);
Dec(r);
End;
End;
QuickSortDesc(Value, li, r);
QuickSortDesc(Value, l, re);
End;
End;
End.