1 | UNIT Unit2_functions;
|
---|
2 | INTERFACE
|
---|
3 | USES Classes, Dialogs, SysUtils, StrUtils, Math,
|
---|
4 | Unit3_data, ABSDecUtil, ABSMain, DB;
|
---|
5 |
|
---|
6 | TYPE
|
---|
7 | TExportSet=SET OF (DO_dat,DO_raw,DO_convert,DO_toone);
|
---|
8 |
|
---|
9 | FUNCTION GetFilesList(ext:String; pattern:String; NoEmptyFiles:Boolean):TStringList;
|
---|
10 | FUNCTION GetFilesCount:LongWord;
|
---|
11 | FUNCTION GetExtensionsList:TStringList;
|
---|
12 | FUNCTION GetFileIDByName(name:String):LongWord;
|
---|
13 |
|
---|
14 | FUNCTION HexToLong(hex:String):LongWord;
|
---|
15 | FUNCTION Decode_Int(buffer:Tdata):LongWord;
|
---|
16 | FUNCTION Encode_Int(input:LongWord):Tdata;
|
---|
17 | FUNCTION Decode_Float(buffer:Tdata):Single;
|
---|
18 | FUNCTION Encode_Float(input:Single):Tdata;
|
---|
19 | FUNCTION DataToBin(data:Tdata):String;
|
---|
20 | FUNCTION BinToInt(bin:String):Byte;
|
---|
21 |
|
---|
22 | FUNCTION GetFileInfo(fileid:LongWord):TFileInfo;
|
---|
23 | FUNCTION LoadDatInfos(filename:String):Boolean;
|
---|
24 | PROCEDURE OpenDatabase(FileName:String);
|
---|
25 | PROCEDURE CloseDatabase;
|
---|
26 |
|
---|
27 | FUNCTION LoadDatFile(fileid:LongWord):Tdata;
|
---|
28 | PROCEDURE UpdateDatFile(fileid:LongWord; data:Tdata);
|
---|
29 | FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
|
---|
30 | FUNCTION UpdateDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
|
---|
31 |
|
---|
32 | FUNCTION LoadRawFile(fileid,dat_offset,raw_addr,size:LongWord; loc_sep:Boolean; target:Pointer):Boolean;
|
---|
33 | FUNCTION LoadRawFileByIDOffset(fileid,dat_offset:LongWord; target:Pointer):Boolean;
|
---|
34 | FUNCTION UpdateRawFile(rawinfo:TRawInfo; target:Pointer):Boolean;
|
---|
35 |
|
---|
36 | FUNCTION ExportFile(fileid:LongWord; filename:String; settings:TExportSet; path:String):Integer;
|
---|
37 |
|
---|
38 | FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
|
---|
39 | FUNCTION FormatFileSize(size:LongWord):String;
|
---|
40 | FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
|
---|
41 | FUNCTION GetWinFileName(name:String):String;
|
---|
42 | FUNCTION GetExtractPath:String;
|
---|
43 |
|
---|
44 | FUNCTION Explode(_string:String; delimiter:Char):TStringList;
|
---|
45 |
|
---|
46 |
|
---|
47 | IMPLEMENTATION
|
---|
48 | USES Unit4_Exporters, Unit9_data_structures;
|
---|
49 |
|
---|
50 | VAR
|
---|
51 | Database:TABSDatabase;
|
---|
52 | Query:TABSQuery;
|
---|
53 |
|
---|
54 | TYPE
|
---|
55 | TValueSwitcher=Record
|
---|
56 | CASE IsFloat: Boolean OF
|
---|
57 | True: (ValueFloat:Single);
|
---|
58 | False: (ValueInt:LongWord);
|
---|
59 | END;
|
---|
60 |
|
---|
61 | FUNCTION NormalizeHexString(VAR hex:String):Boolean;
|
---|
62 | VAR
|
---|
63 | i:Byte;
|
---|
64 | BEGIN
|
---|
65 | IF hex[1]='$' THEN BEGIN
|
---|
66 | FOR i:=1 TO Length(hex)-1 DO BEGIN
|
---|
67 | hex[i]:=hex[i+1];
|
---|
68 | END;
|
---|
69 | SetLength(hex, Length(hex)-1);
|
---|
70 | END;
|
---|
71 | IF (hex[1]='0') AND (UpCase(hex[2])='X') THEN BEGIN
|
---|
72 | FOR i:=1 TO Length(hex)-2 DO BEGIN
|
---|
73 | hex[i]:=hex[i+2];
|
---|
74 | END;
|
---|
75 | SetLength(hex, Length(hex)-2);
|
---|
76 | END;
|
---|
77 | IF Length(hex)=0 THEN
|
---|
78 | Result:=False
|
---|
79 | ELSE
|
---|
80 | Result:=True;
|
---|
81 | END;
|
---|
82 |
|
---|
83 | FUNCTION HexToLong(hex:String):LongWord;
|
---|
84 | VAR
|
---|
85 | i:Byte;
|
---|
86 | BEGIN
|
---|
87 | IF NormalizeHexString(hex) THEN BEGIN
|
---|
88 | hex:=UpperCase(hex);
|
---|
89 | Result:=0;
|
---|
90 | FOR i:=1 TO Length(hex) DO BEGIN
|
---|
91 | Result:=Result SHL 4;
|
---|
92 | CASE hex[i] OF
|
---|
93 | '0'..'9': Result:=Result+Ord(hex[i])-48;
|
---|
94 | 'A'..'F': Result:=Result+Ord(hex[i])-55;
|
---|
95 | ELSE
|
---|
96 | Result:=0;
|
---|
97 | Exit;
|
---|
98 | END;
|
---|
99 | END;
|
---|
100 | END ELSE BEGIN
|
---|
101 | Result:=0;
|
---|
102 | END;
|
---|
103 | END;
|
---|
104 |
|
---|
105 | FUNCTION Decode_Int(buffer:Tdata):LongWord;
|
---|
106 | BEGIN
|
---|
107 | Result:=buffer[0]+buffer[1]*256+buffer[2]*256*256+buffer[3]*256*256*256;
|
---|
108 | END;
|
---|
109 | FUNCTION Encode_Int(input:LongWord):Tdata;
|
---|
110 | BEGIN
|
---|
111 | SetLength(Result,4);
|
---|
112 | Result[0]:=input MOD 256;
|
---|
113 | input:=input DIV 256;
|
---|
114 | Result[1]:=input MOD 256;
|
---|
115 | input:=input DIV 256;
|
---|
116 | Result[2]:=input MOD 256;
|
---|
117 | input:=input DIV 256;
|
---|
118 | Result[3]:=input MOD 256;
|
---|
119 | END;
|
---|
120 | FUNCTION Decode_Float(buffer:Tdata):Single;
|
---|
121 | VAR _valueswitcher:TValueSwitcher;
|
---|
122 | BEGIN
|
---|
123 | _valueswitcher.ValueInt:=Decode_Int(buffer);
|
---|
124 | Result:=_valueswitcher.ValueFloat;
|
---|
125 | IF IsNAN(Result) THEN Result:=0.0;
|
---|
126 | END;
|
---|
127 | FUNCTION Encode_Float(input:Single):Tdata;
|
---|
128 | VAR _valueswitcher:TValueSwitcher;
|
---|
129 | BEGIN
|
---|
130 | _valueswitcher.ValueFloat:=input;
|
---|
131 | Result:=Encode_Int(_valueswitcher.ValueInt);
|
---|
132 | END;
|
---|
133 |
|
---|
134 | FUNCTION DataToBin(data:Tdata):String;
|
---|
135 | VAR
|
---|
136 | i,j:Byte;
|
---|
137 | singlebyte:Byte;
|
---|
138 | bytepart:String;
|
---|
139 | BEGIN
|
---|
140 | SetLength(bytepart,8);
|
---|
141 | Result:='';
|
---|
142 | FOR i:=0 TO High(data) DO BEGIN
|
---|
143 | singlebyte:=data[i];
|
---|
144 | FOR j:=7 DOWNTO 0 DO BEGIN
|
---|
145 | bytepart[j+1]:=Char((singlebyte AND $01)+48);
|
---|
146 | singlebyte:=singlebyte SHR 1;
|
---|
147 | END;
|
---|
148 | Result:=Result+bytepart+' ';
|
---|
149 | END;
|
---|
150 | END;
|
---|
151 | FUNCTION BinToInt(bin:String):Byte;
|
---|
152 | VAR
|
---|
153 | Add: Integer;
|
---|
154 | i: Byte;
|
---|
155 | BEGIN
|
---|
156 | Result:=0;
|
---|
157 | IF Length(bin)<>8 THEN Exit;
|
---|
158 | Add:=1;
|
---|
159 | FOR i:=8 DOWNTO 1 DO BEGIN
|
---|
160 | IF NOT (bin[i] IN ['0','1']) THEN Exit;
|
---|
161 | IF bin[i] = '1' THEN Inc(Result,Add);
|
---|
162 | Add:=Add SHL 1;
|
---|
163 | END;
|
---|
164 | END;
|
---|
165 |
|
---|
166 | FUNCTION GetFileInfo(fileid:LongWord):TFileInfo;
|
---|
167 | BEGIN
|
---|
168 | IF opened_state=opened_dat THEN BEGIN
|
---|
169 | Result:=dat_files[fileid];
|
---|
170 | END ELSE BEGIN
|
---|
171 | Query.SQL.Text:='SELECT * FROM datfiles WHERE id='+IntToStr(fileid)+' ORDER BY id ASC;';
|
---|
172 | Query.Open;
|
---|
173 | IF Query.RecordCount=1 THEN BEGIN
|
---|
174 | Query.First;
|
---|
175 | Result.ID:=Query.FieldByName('id').AsInteger;
|
---|
176 | Result.Name:=Query.FieldByName('name').AsString;
|
---|
177 | Result.Extension:=Query.FieldByName('extension').AsString;
|
---|
178 | Result.FileName:=FormatNumber(Result.ID,5,'0')+'-'+Result.Name+'.'+Result.Extension;
|
---|
179 | Result.Size:=Query.FieldByName('size').AsInteger;
|
---|
180 | Result.FileType:=Query.FieldByName('contenttype').AsInteger;
|
---|
181 | Result.DatAddr:=0;
|
---|
182 | Result.opened:=False;
|
---|
183 | END;
|
---|
184 | Query.Close;
|
---|
185 | END;
|
---|
186 | END;
|
---|
187 |
|
---|
188 | FUNCTION GetFileIDByName(name:String):LongWord;
|
---|
189 | BEGIN
|
---|
190 | IF AppSettings.FilenumbersAsHex THEN
|
---|
191 | Result:=HexToLong(MidStr(name,1,4))
|
---|
192 | ELSE
|
---|
193 | Result:=StrToInt(MidStr(name,1,5));
|
---|
194 | END;
|
---|
195 |
|
---|
196 | FUNCTION GetFilesCount:LongWord;
|
---|
197 | BEGIN
|
---|
198 | IF opened_state=opened_dat THEN BEGIN
|
---|
199 | Result:=dat_header.Files;
|
---|
200 | END ELSE BEGIN
|
---|
201 | Query.SQL.Text:='SELECT Count(*) AS cnumber FROM datfiles;';
|
---|
202 | Query.Open;
|
---|
203 | IF Query.RecordCount>0 THEN BEGIN
|
---|
204 | Query.First;
|
---|
205 | Result:=Query.FieldByName('cnumber').AsInteger;
|
---|
206 | END ELSE Result:=0;
|
---|
207 | Query.Close;
|
---|
208 | END;
|
---|
209 | END;
|
---|
210 |
|
---|
211 | FUNCTION GetFilesList(ext:String; pattern:String; NoEmptyFiles:Boolean):TStringList;
|
---|
212 | VAR
|
---|
213 | i:LongWord;
|
---|
214 | where:String;
|
---|
215 | where_ext:String;
|
---|
216 | BEGIN
|
---|
217 | SetLength(Result,0);
|
---|
218 | IF opened_state=opened_dat THEN BEGIN
|
---|
219 | FOR i:=0 TO dat_header.Files-1 DO BEGIN
|
---|
220 | IF ( (Length(ext)=0) OR (Pos(dat_files[i].Extension,ext)>0) ) AND
|
---|
221 | ( (Length(pattern)=0) OR (Pos(UpperCase(pattern),UpperCase(dat_files[i].Name))>0) ) THEN BEGIN
|
---|
222 | IF NoEmptyFiles THEN BEGIN
|
---|
223 | IF (dat_files[i].FileType AND $02)=0 THEN BEGIN
|
---|
224 | SetLength(Result,Length(Result)+1);
|
---|
225 | IF AppSettings.FilenumbersAsHex THEN
|
---|
226 | Result[High(Result)]:=dat_files[i].FileNameHex
|
---|
227 | ELSE
|
---|
228 | Result[High(Result)]:=dat_files[i].FileName;
|
---|
229 | END;
|
---|
230 | END ELSE BEGIN
|
---|
231 | SetLength(Result,Length(Result)+1);
|
---|
232 | IF AppSettings.FilenumbersAsHex THEN
|
---|
233 | Result[High(Result)]:=dat_files[i].FileNameHex
|
---|
234 | ELSE
|
---|
235 | Result[High(Result)]:=dat_files[i].FileName;
|
---|
236 | END;
|
---|
237 | END;
|
---|
238 | END;
|
---|
239 | END ELSE BEGIN
|
---|
240 | where:='';
|
---|
241 | IF Length(ext)>0 THEN BEGIN
|
---|
242 | IF Length(where)>0 THEN where:=where+' AND ';
|
---|
243 | IF Pos(',',ext)>0 THEN BEGIN
|
---|
244 | i:=1;
|
---|
245 | where_ext:='';
|
---|
246 | WHILE i<Length(ext) DO BEGIN
|
---|
247 | IF Length(where_ext)>0 THEN where_ext:=where_ext+' OR ';
|
---|
248 | where_ext:=where_ext+'(extension="'+MidStr(ext,i,4)+'")';
|
---|
249 | i:=i+5;
|
---|
250 | END;
|
---|
251 | where:=where+'('+where_ext+')';
|
---|
252 | END ELSE BEGIN
|
---|
253 | where:=where+'(extension="'+ext+'")';
|
---|
254 | END;
|
---|
255 | END;
|
---|
256 | IF Length(pattern)>0 THEN BEGIN
|
---|
257 | IF Length(where)>0 THEN where:=where+' AND ';
|
---|
258 | where:=where+'(name LIKE "%'+pattern+'%")';
|
---|
259 | END;
|
---|
260 | IF NoEmptyFiles THEN BEGIN
|
---|
261 | IF Length(where)>0 THEN where:=where+' AND ';
|
---|
262 | where:=where+'(contenttype<>2)';
|
---|
263 | END;
|
---|
264 | IF Length(where)>0 THEN where:=' WHERE '+where;
|
---|
265 | Query.SQL.Text:='SELECT id,name,extension FROM datfiles'+where+' ORDER BY id ASC;';
|
---|
266 | Query.Open;
|
---|
267 | IF Query.RecordCount>0 THEN BEGIN
|
---|
268 | Query.First;
|
---|
269 | SetLength(Result,Query.RecordCount);
|
---|
270 | i:=0;
|
---|
271 | REPEAT
|
---|
272 | Result[i]:=FormatNumber(Query.FieldByName('id').AsInteger,5,'0')+'-'+Query.FieldByName('name').AsString+'.'+Query.FieldByName('extension').AsString;
|
---|
273 | Inc(i);
|
---|
274 | Query.Next;
|
---|
275 | UNTIL Query.EOF;
|
---|
276 | END;
|
---|
277 | Query.Close;
|
---|
278 | END;
|
---|
279 | END;
|
---|
280 |
|
---|
281 | FUNCTION GetExtensionsList:TStringList;
|
---|
282 | VAR
|
---|
283 | i:LongWord;
|
---|
284 | BEGIN
|
---|
285 | SetLength(Result,0);
|
---|
286 | IF opened_state=opened_dat THEN BEGIN
|
---|
287 | FOR i:=0 TO dat_header.Extensions-1 DO BEGIN
|
---|
288 | SetLength(Result,Length(Result)+1);
|
---|
289 | WITH dat_extensionsmap[i] DO BEGIN
|
---|
290 | Result[High(Result)]:=Extension[3]+Extension[2]+Extension[1]+Extension[0]+' ('+IntToStr(ExtCount)+')';
|
---|
291 | END;
|
---|
292 | END;
|
---|
293 | END ELSE BEGIN
|
---|
294 | Query.SQL.Text:='SELECT extension,count(extension) AS x FROM datfiles GROUP BY extension ORDER BY extension ASC;';
|
---|
295 | Query.Open;
|
---|
296 | IF Query.RecordCount>0 THEN BEGIN
|
---|
297 | SetLength(Result,Query.RecordCount);
|
---|
298 | i:=0;
|
---|
299 | REPEAT
|
---|
300 | Result[i]:=Query.FieldByName('extension').AsString+' ('+IntToStr(Query.FieldByName('x').AsInteger)+')';
|
---|
301 | Inc(i);
|
---|
302 | Query.Next;
|
---|
303 | UNTIL Query.EOF;
|
---|
304 | END;
|
---|
305 | Query.Close;
|
---|
306 | END;
|
---|
307 | END;
|
---|
308 |
|
---|
309 | FUNCTION LoadDatInfos(filename:String):Boolean;
|
---|
310 | VAR i:LongWord;
|
---|
311 | dat_file:TFileStream;
|
---|
312 | header_pc,header_mac:Boolean;
|
---|
313 | BEGIN
|
---|
314 | Result:=True;
|
---|
315 | opened_state:=opened_dat;
|
---|
316 | dat_filename:=filename;
|
---|
317 | raw_filename:=MidStr(filename,1,Length(filename)-3)+'raw';
|
---|
318 | dat_file:=TFileStream.Create(filename, fmOpenRead);
|
---|
319 | dat_file.Read(dat_header,SizeOf(dat_header));
|
---|
320 | header_pc:=True;
|
---|
321 | header_mac:=True;
|
---|
322 | FOR i:=0 TO High(dat_header.Ident) DO BEGIN
|
---|
323 | IF dat_header.Ident[i]<>header_ident1_pc[i] THEN BEGIN
|
---|
324 | header_pc:=False;
|
---|
325 | END;
|
---|
326 | IF dat_header.Ident[i]<>header_ident1_mac[i] THEN BEGIN
|
---|
327 | header_mac:=False;
|
---|
328 | END;
|
---|
329 | END;
|
---|
330 | IF NOT (header_pc OR header_mac) THEN BEGIN
|
---|
331 | Result:=False;
|
---|
332 | Exit;
|
---|
333 | END ELSE BEGIN
|
---|
334 | IF (header_pc AND NOT header_mac) THEN
|
---|
335 | dat_os_mac:=False
|
---|
336 | ELSE
|
---|
337 | IF (NOT header_pc AND header_mac) THEN
|
---|
338 | dat_os_mac:=True
|
---|
339 | ELSE BEGIN
|
---|
340 | Result:=False;
|
---|
341 | Exit;
|
---|
342 | END;
|
---|
343 | END;
|
---|
344 | SetLength(dat_filesmap,dat_header.Files);
|
---|
345 | SetLength(dat_files,dat_header.Files);
|
---|
346 | FOR i:=0 TO dat_header.Files-1 DO dat_file.Read(dat_filesmap[i],SizeOf(dat_filesmap[i]));
|
---|
347 | FOR i:=0 TO dat_header.Files-1 DO BEGIN
|
---|
348 | dat_files[i].Extension:=dat_filesmap[i].Extension;
|
---|
349 | dat_files[i].Extension:=ReverseString(dat_files[i].Extension);
|
---|
350 | dat_files[i].Size:=dat_filesmap[i].FileSize;
|
---|
351 | dat_files[i].FileType:=dat_filesmap[i].FileType;
|
---|
352 | dat_files[i].DatAddr:=dat_filesmap[i].DataAddr-8+dat_header.DataAddr;
|
---|
353 | IF (dat_filesmap[i].FileType AND $01)=0 THEN BEGIN
|
---|
354 | dat_file.Seek(dat_filesmap[i].NameAddr+dat_header.NamesAddr,soFromBeginning);
|
---|
355 | SetLength(dat_files[i].Name,100);
|
---|
356 | dat_file.Read(dat_files[i].Name[1],100);
|
---|
357 | dat_files[i].Name:=MidStr(dat_files[i].Name,1+4,Pos(#0,dat_files[i].Name)-1-4);
|
---|
358 | END ELSE BEGIN
|
---|
359 | dat_files[i].Name:='';
|
---|
360 | END;
|
---|
361 | dat_files[i].FileName:=FormatNumber(i,5,'0')+'-'+dat_files[i].Name+'.'+dat_files[i].Extension;
|
---|
362 | dat_files[i].FileNameHex:=IntToHex(i,4)+'-'+dat_files[i].Name+'.'+dat_files[i].Extension;
|
---|
363 | END;
|
---|
364 | dat_file.Seek($40+dat_header.Files*$14,soFromBeginning);
|
---|
365 | SetLength(dat_namedfilesmap,dat_header.NamedFiles);
|
---|
366 | FOR i:=0 TO dat_header.NamedFiles-1 DO dat_file.Read(dat_namedfilesmap[i],SizeOf(dat_namedfilesmap[i]));
|
---|
367 |
|
---|
368 | dat_file.Seek($40+dat_header.Files*$14+dat_header.NamedFiles*$8,soFromBeginning);
|
---|
369 | SetLength(dat_extensionsmap,dat_header.Extensions);
|
---|
370 | FOR i:=0 TO dat_header.Extensions-1 DO dat_file.Read(dat_extensionsmap[i],SizeOf(dat_extensionsmap[i]));
|
---|
371 |
|
---|
372 | dat_file.Free;
|
---|
373 | END;
|
---|
374 |
|
---|
375 |
|
---|
376 | FUNCTION LoadDatFile(fileid:LongWord):Tdata;
|
---|
377 | VAR
|
---|
378 | dat_file:TFileStream;
|
---|
379 | mem:TStream;
|
---|
380 | BEGIN
|
---|
381 | IF opened_state=opened_dat THEN BEGIN
|
---|
382 | dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
|
---|
383 | dat_file.Seek(dat_files[fileid].DatAddr,soFromBeginning);
|
---|
384 | SetLength(Result,dat_files[fileid].Size);
|
---|
385 | dat_file.Read(Result[0],dat_files[fileid].Size);
|
---|
386 | dat_file.Free;
|
---|
387 | END ELSE BEGIN
|
---|
388 | Query.SQL.Text:='SELECT data FROM datfiles WHERE id='+IntToStr(fileid)+';';
|
---|
389 | Query.Open;
|
---|
390 | IF Query.RecordCount>0 THEN BEGIN
|
---|
391 | mem:=Query.CreateBlobStream(Query.FieldByName('data'),bmRead);
|
---|
392 | SetLength(Result,mem.Size);
|
---|
393 | mem.Seek(0,soFromBeginning);
|
---|
394 | mem.Read(Result[0],mem.Size);
|
---|
395 | mem.Free;
|
---|
396 | END;
|
---|
397 | Query.Close;
|
---|
398 | END;
|
---|
399 | END;
|
---|
400 | PROCEDURE UpdateDatFile(fileid:LongWord; data:Tdata);
|
---|
401 | VAR
|
---|
402 | dat_file:TFileStream;
|
---|
403 | BEGIN
|
---|
404 | IF opened_state=opened_dat THEN BEGIN
|
---|
405 | dat_file:=TFileStream.Create(dat_filename, fmOpenReadWrite);
|
---|
406 | dat_file.Seek(dat_files[fileid].DatAddr,soFromBeginning);
|
---|
407 | dat_file.Write(data[0],Length(data));
|
---|
408 | dat_file.Free;
|
---|
409 | END ELSE BEGIN
|
---|
410 | END;
|
---|
411 | END;
|
---|
412 |
|
---|
413 | FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
|
---|
414 | VAR
|
---|
415 | dat_file:TFileStream;
|
---|
416 | mem:TStream;
|
---|
417 | BEGIN
|
---|
418 | Result:=False;
|
---|
419 | IF opened_state=opened_dat THEN BEGIN
|
---|
420 | dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
|
---|
421 | Result:=True;
|
---|
422 | dat_file.Seek(dat_files[fileid].DatAddr+offset,soFromBeginning);
|
---|
423 | dat_file.Read(target^,size);
|
---|
424 | dat_file.Free;
|
---|
425 | END ELSE BEGIN
|
---|
426 | Query.SQL.Text:='SELECT data FROM datfiles WHERE id='+IntToStr(fileid)+';';
|
---|
427 | Query.Open;
|
---|
428 | IF Query.RecordCount>0 THEN BEGIN
|
---|
429 | mem:=Query.CreateBlobStream(Query.FieldByName('data'),bmRead);
|
---|
430 | mem.Seek(offset,soFromBeginning);
|
---|
431 | mem.Read(target^,size);
|
---|
432 | mem.Free;
|
---|
433 | END;
|
---|
434 | Query.Close;
|
---|
435 | END;
|
---|
436 | END;
|
---|
437 | FUNCTION UpdateDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
|
---|
438 | VAR
|
---|
439 | dat_file:TFileStream;
|
---|
440 | BEGIN
|
---|
441 | Result:=False;
|
---|
442 | IF opened_state=opened_dat THEN BEGIN
|
---|
443 | dat_file:=TFileStream.Create(dat_filename, fmOpenReadWrite);
|
---|
444 | Result:=True;
|
---|
445 | dat_file.Seek(dat_files[fileid].DatAddr+offset,soFromBeginning);
|
---|
446 | dat_file.Write(target^,size);
|
---|
447 | dat_file.Free;
|
---|
448 | END ELSE BEGIN
|
---|
449 | END;
|
---|
450 | END;
|
---|
451 |
|
---|
452 | FUNCTION LoadRawFile(fileid,dat_offset,raw_addr,size:LongWord; loc_sep:Boolean; target:Pointer):Boolean;
|
---|
453 | VAR
|
---|
454 | filestream:TFileStream;
|
---|
455 | mem:TStream;
|
---|
456 | BEGIN
|
---|
457 | Result:=False;
|
---|
458 | IF opened_state=opened_dat THEN BEGIN
|
---|
459 | Result:=True;
|
---|
460 | IF NOT loc_sep THEN
|
---|
461 | filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.raw'),fmOpenRead)
|
---|
462 | ELSE
|
---|
463 | filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.sep'),fmOpenRead);
|
---|
464 | filestream.Seek(raw_addr,soFromBeginning);
|
---|
465 | filestream.Read(target^,size);
|
---|
466 | filestream.Free;
|
---|
467 | END ELSE BEGIN
|
---|
468 | Query.SQL.Text:='SELECT data FROM rawmap WHERE (src_id='+IntToStr(fileid)+') AND (src_link_offset='+IntToStr(dat_offset)+');';
|
---|
469 | Query.Open;
|
---|
470 | IF Query.RecordCount>0 THEN BEGIN
|
---|
471 | Result:=True;
|
---|
472 | mem:=Query.CreateBlobStream(Query.FieldByName('data'),bmRead);
|
---|
473 | mem.Seek(0,soFromBeginning);
|
---|
474 | mem.Read(target^,size);
|
---|
475 | mem.Free;
|
---|
476 | END;
|
---|
477 | Query.Close;
|
---|
478 | END;
|
---|
479 | END;
|
---|
480 | FUNCTION LoadRawFileByIDOffset(fileid,dat_offset:LongWord; target:Pointer):Boolean;
|
---|
481 | VAR
|
---|
482 | i:Byte;
|
---|
483 | raw_info:TRawInfo;
|
---|
484 | mem:TStream;
|
---|
485 | BEGIN
|
---|
486 | Result:=False;
|
---|
487 | IF opened_state=opened_dat THEN BEGIN
|
---|
488 | Result:=True;
|
---|
489 | raw_info:=GetRawInfo(fileid,dat_offset);
|
---|
490 | LoadRawFile(fileid,dat_offset,raw_info.raw_addr,raw_info.raw_size,raw_info.loc_sep,target);
|
---|
491 | END ELSE BEGIN
|
---|
492 | Query.SQL.Text:='SELECT data FROM rawmap WHERE (src_id='+IntToStr(fileid)+') AND (src_link_offset='+IntToStr(dat_offset)+');';
|
---|
493 | Query.Open;
|
---|
494 | IF Query.RecordCount>0 THEN BEGIN
|
---|
495 | Result:=True;
|
---|
496 | mem:=Query.CreateBlobStream(Query.FieldByName('data'),bmRead);
|
---|
497 | mem.Seek(0,soFromBeginning);
|
---|
498 | mem.Read(target^,mem.size);
|
---|
499 | mem.Free;
|
---|
500 | END;
|
---|
501 | Query.Close;
|
---|
502 | END;
|
---|
503 | END;
|
---|
504 | FUNCTION UpdateRawFile(rawinfo:TRawInfo; target:Pointer):Boolean;
|
---|
505 | VAR
|
---|
506 | filestream:TFileStream;
|
---|
507 | BEGIN
|
---|
508 | Result:=False;
|
---|
509 | IF opened_state=opened_dat THEN BEGIN
|
---|
510 | Result:=True;
|
---|
511 | IF NOT rawinfo.loc_sep THEN
|
---|
512 | filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.raw'),fmOpenReadWrite)
|
---|
513 | ELSE
|
---|
514 | filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.sep'),fmOpenReadWrite);
|
---|
515 | filestream.Seek(rawinfo.raw_addr,soFromBeginning);
|
---|
516 | filestream.Write(target^,rawinfo.raw_size);
|
---|
517 | filestream.Free;
|
---|
518 | END ELSE BEGIN
|
---|
519 | END;
|
---|
520 | END;
|
---|
521 |
|
---|
522 |
|
---|
523 | FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
|
---|
524 | BEGIN
|
---|
525 | Result:=AnsiReplaceStr(Format('%'+IntToStr(width)+'u',[value]),' ',leadingzeros);
|
---|
526 | END;
|
---|
527 |
|
---|
528 | FUNCTION FormatFileSize(size:LongWord):String;
|
---|
529 | BEGIN
|
---|
530 | IF size>=1000*1024*1024 THEN BEGIN
|
---|
531 | Result:=FloatToStrF(size/1024/1024/1024,ffFixed,5,1)+' GB';
|
---|
532 | END ELSE BEGIN
|
---|
533 | IF size>=1000*1024 THEN BEGIN
|
---|
534 | Result:=FloatToStrF(size/1024/1024,ffFixed,5,1)+' MB';
|
---|
535 | END ELSE BEGIN
|
---|
536 | IF size>=1000 THEN BEGIN
|
---|
537 | Result:=FloatToStrF(size/1024,ffFixed,5,1)+' KB';
|
---|
538 | END ELSE BEGIN
|
---|
539 | Result:=IntToStr(size)+' B';
|
---|
540 | END;
|
---|
541 | END;
|
---|
542 | END;
|
---|
543 | END;
|
---|
544 |
|
---|
545 | FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
|
---|
546 | VAR
|
---|
547 | string_build,ascii_version:String;
|
---|
548 | i:LongWord;
|
---|
549 | BEGIN
|
---|
550 | string_build:='';
|
---|
551 | ascii_version:='';
|
---|
552 | FOR i:=0 TO High(data) DO BEGIN
|
---|
553 | IF NOT HexOnly THEN
|
---|
554 | IF (i MOD 16)=0 THEN
|
---|
555 | string_build:=string_build+'0x'+IntToHex(i,6)+' ';
|
---|
556 | string_build:=string_build+IntToHex(data[i],2);
|
---|
557 | IF NOT HexOnly THEN BEGIN
|
---|
558 | IF data[i]>=32 THEN ascii_version:=ascii_version+Chr(data[i])
|
---|
559 | ELSE ascii_version:=ascii_version+'.';
|
---|
560 | IF ((i+1) MOD 2)=0 THEN string_build:=string_build+#32;
|
---|
561 | IF ((i+1) MOD 16)=0 THEN BEGIN
|
---|
562 | string_build:=string_build+#32+ascii_version+CrLf;
|
---|
563 | ascii_version:='';
|
---|
564 | END;
|
---|
565 | END;
|
---|
566 | END;
|
---|
567 | Result:=string_build;
|
---|
568 | END;
|
---|
569 |
|
---|
570 |
|
---|
571 | FUNCTION ExportFile(fileid:LongWord; filename:String; settings:TExportSet; path:String):Integer;
|
---|
572 | VAR
|
---|
573 | i:Byte;
|
---|
574 | extension:String;
|
---|
575 | rawlist:TRawList;
|
---|
576 | BEGIN
|
---|
577 | Result:=export_noerror;
|
---|
578 | extension:=RightStr(filename,4);
|
---|
579 | IF DO_toone IN settings THEN BEGIN
|
---|
580 | ExportDatFile(fileid,path+'\'+GetWinFileName(filename));
|
---|
581 | END ELSE BEGIN
|
---|
582 | IF DO_dat IN settings THEN ExportDatFile(fileid,path+'\'+GetWinFileName(filename));
|
---|
583 | IF DO_raw IN settings THEN BEGIN
|
---|
584 | rawlist:=GetRawList(fileid);
|
---|
585 | IF Length(rawlist)>0 THEN BEGIN
|
---|
586 | FOR i:=0 TO High(rawlist) DO BEGIN
|
---|
587 | ExportRawFile(fileid,rawlist[i].src_offset,path+'\'+GetWinFileName(filename));
|
---|
588 | END;
|
---|
589 | END;
|
---|
590 | END;
|
---|
591 | END;
|
---|
592 | END;
|
---|
593 |
|
---|
594 | FUNCTION Explode(_string:String; delimiter:Char):TStringList;
|
---|
595 | VAR
|
---|
596 | start,len:Word;
|
---|
597 | BEGIN
|
---|
598 | SetLength(Result, 0);
|
---|
599 | start:=1;
|
---|
600 | WHILE PosEx(delimiter,_string,start)>0 DO BEGIN
|
---|
601 | len:=PosEx(delimiter,_string,start)-start;
|
---|
602 | SetLength(Result, Length(Result)+1);
|
---|
603 | Result[High(Result)]:=MidStr(_string,start,len);
|
---|
604 | start:=start+len+1;
|
---|
605 | END;
|
---|
606 | SetLength(Result, Length(Result)+1);
|
---|
607 | Result[High(Result)]:=MidStr(_string,start,Length(_string)-start+1);
|
---|
608 | END;
|
---|
609 |
|
---|
610 | FUNCTION GetWinFileName(name:String):String;
|
---|
611 | BEGIN
|
---|
612 | Result:=name;
|
---|
613 | Result:=AnsiReplaceStr(Result,'\','__');
|
---|
614 | Result:=AnsiReplaceStr(Result,'/','__');
|
---|
615 | Result:=AnsiReplaceStr(Result,'>','__');
|
---|
616 | Result:=AnsiReplaceStr(Result,'<','__');
|
---|
617 | END;
|
---|
618 |
|
---|
619 | FUNCTION GetExtractPath:String;
|
---|
620 | BEGIN
|
---|
621 | Result:=ExtractFilePath(dat_filename)+'\extracted_'+ExtractFileName(dat_filename);
|
---|
622 | END;
|
---|
623 |
|
---|
624 |
|
---|
625 | PROCEDURE OpenDatabase(FileName:String);
|
---|
626 | VAR
|
---|
627 | i:Byte;
|
---|
628 | temps:String;
|
---|
629 | BEGIN
|
---|
630 | IF NOT FileExists(FileName) THEN BEGIN
|
---|
631 | ShowMessage('File doesn''t exist!!!');
|
---|
632 | Exit;
|
---|
633 | END;
|
---|
634 | Database:=TABSDatabase.Create(NIL);
|
---|
635 | Database.DatabaseName:='OLDBcon';
|
---|
636 | Database.DatabaseFileName:=FileName;
|
---|
637 | Database.Open;
|
---|
638 | Query:=TABSQuery.Create(Database);
|
---|
639 | Query.DatabaseName:='OLDBcon';
|
---|
640 | Query.SQL.Text:='SELECT [name],[value] FROM globals ORDER BY [name] ASC';
|
---|
641 | Query.Open;
|
---|
642 | Query.First;
|
---|
643 | REPEAT
|
---|
644 | IF Query.FieldByName('name').AsString='dbversion' THEN BEGIN
|
---|
645 | IF Query.FieldByName('value').AsString<>DBversion THEN BEGIN
|
---|
646 | ShowMessage('Database-file '+CrLf+'"'+FileName+'"'+CrLf+'has wrong version. (Required: '+DBversion+'; found: '+Query.FieldByName('value').AsString+')');
|
---|
647 | Query.Close;
|
---|
648 | CloseDatabase;
|
---|
649 | Exit;
|
---|
650 | END;
|
---|
651 | END;
|
---|
652 | IF Query.FieldByName('name').AsString='lvl' THEN BEGIN
|
---|
653 | database_level:=StrToInt(Query.FieldByName('value').AsString);
|
---|
654 | END;
|
---|
655 | IF Query.FieldByName('name').AsString='ident' THEN BEGIN
|
---|
656 | temps:=Query.FieldByName('value').AsString;
|
---|
657 | FOR i:=0 TO High(database_ident) DO BEGIN
|
---|
658 | CASE temps[(i*2)+1+0] OF
|
---|
659 | '0'..'9': database_ident[i]:=Ord(temps[(i*2)+1+0])-48;
|
---|
660 | 'A'..'F': database_ident[i]:=Ord(temps[(i*2)+1+0])-55;
|
---|
661 | END;
|
---|
662 | database_ident[i]:=database_ident[i]*16;
|
---|
663 | CASE temps[(i*2)+1+1] OF
|
---|
664 | '0'..'9': database_ident[i]:=database_ident[i]+Ord(temps[(i*2)+1+0])-48;
|
---|
665 | 'A'..'F': database_ident[i]:=database_ident[i]+Ord(temps[(i*2)+1+0])-55;
|
---|
666 | END;
|
---|
667 | END;
|
---|
668 | END;
|
---|
669 | Query.Next;
|
---|
670 | UNTIL Query.Eof;
|
---|
671 | Query.Close;
|
---|
672 | opened_state:=opened_db;
|
---|
673 | END;
|
---|
674 | PROCEDURE CloseDatabase;
|
---|
675 | BEGIN
|
---|
676 | Database.Close;
|
---|
677 | Database.Free;
|
---|
678 | opened_state:=opened_nothing;
|
---|
679 | END;
|
---|
680 |
|
---|
681 |
|
---|
682 |
|
---|
683 | END.
|
---|