source: oup/releases/0.30a/Unit15_Classes.pas@ 1065

Last change on this file since 1065 was 33, checked in by alloc, 18 years ago
File size: 28.6 KB
RevLine 
[33]1unit Unit15_Classes;
2interface
3uses Unit3_data, Unit9_data_structures, Classes, SysUtils, StrUtils, Dialogs, ABSDecUtil, ABSMain, DB;
4
5
6type
7 TOniData = class
8 private
9 FFileName:String;
10 FLevelInfo:TLevelInfo;
11 FBackend:Integer;
12 Fos_mac:Boolean;
13 protected
14 public
15 property FileName:String read FFileName write FFileName;
16 property Backend:Integer read FBackend write FBackend;
17 property OSisMac:Boolean read Fos_mac write Fos_mac;
18
19 constructor Create(filename:String; var Result:Boolean); virtual; abstract;
20 procedure Close; virtual; abstract;
21
22 function GetFileInfo(fileid:LongWord):TFileInfo; virtual; abstract;
23 function GetFilesList(ext:String; pattern:String; NoEmptyFiles:Boolean):TStringArray; virtual; abstract;
24 function GetFilesCount:LongWord; virtual; abstract;
25 function GetExtensionsList:TStringArray; virtual; abstract;
26 function GetFileIDByName(name:String):LongWord;
27
28 function LoadDatFile(fileid:LongWord):Tdata; virtual; abstract;
29 procedure UpdateDatFile(fileid:LongWord; data:Tdata); virtual; abstract;
30 procedure LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer); virtual; abstract;
31 procedure UpdateDatFilePart(fileid,offset,size:LongWord; target:Pointer); virtual; abstract;
32
33 function GetRawList(fileid:LongWord):TRawList; virtual; abstract;
34 function GetRawInfo(fileid,dat_offset:LongWord):TRawInfo;
35 procedure LoadRawFile(fileid,dat_offset:LongWord; target:Pointer); virtual; abstract;
36 procedure UpdateRawFile(fileid,dat_offset:LongWord; size:LongWord; target:Pointer); virtual; abstract;
37 procedure LoadRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer); virtual; abstract;
38 procedure UpdateRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer); virtual; abstract;
39 function AppendRawFile(loc_sep:Boolean; size:LongWord; target:Pointer):LongWord; virtual; abstract;//Returns new Address
40 published
41 end;
42
43 TOniDataDat = class(TOniData)
44 private
45 Fdat_header:THeader;
46 Fdat_filesmap:TFilesMap;
47 Fdat_files:TFiles;
48 Fdat_namedfilesmap:TNamedFilesMap;
49 Fdat_extensionsmap:TExtensionsMap;
50 protected
51 public
52 constructor Create(DatFilename:String; var Result:Boolean); override;
53 procedure Close; override;
54
55 function GetFileInfo(fileid:LongWord):TFileInfo; override;
56 function GetFilesList(ext:String; pattern:String; NoEmptyFiles:Boolean):TStringArray; override;
57 function GetFilesCount:LongWord; override;
58 function GetExtensionsList:TStringArray; override;
59
60 function LoadDatFile(fileid:LongWord):Tdata; override;
61 procedure UpdateDatFile(fileid:LongWord; data:Tdata); override;
62 procedure LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer); override;
63 procedure UpdateDatFilePart(fileid,offset,size:LongWord; target:Pointer); override;
64
65 procedure LoadRawOffset(loc_sep:Boolean; raw_addr,size:LongWord; target:Pointer);
66 function GetRawList(fileid:LongWord):TRawList; override;
67 procedure LoadRawFile(fileid,dat_offset:LongWord; target:Pointer); override;
68 procedure UpdateRawFile(fileid,dat_offset:LongWord; size:LongWord; target:Pointer); override;
69 procedure LoadRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer); override;
70 procedure UpdateRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer); override;
71 function AppendRawFile(loc_sep:Boolean; size:LongWord; target:Pointer):LongWord; override;//Returns new Address
72 published
73 end;
74
75 TOniDataADB = class(TOniData)
76 private
77 FDatabase:TABSDatabase;
78 FQuery:TABSQuery;
79 protected
80 public
81 constructor Create(OLDBFilename:String; var Result:Boolean); override;
82 procedure Close; override;
83
84 function GetFileInfo(fileid:LongWord):TFileInfo; override;
85 function GetFilesList(ext:String; pattern:String; NoEmptyFiles:Boolean):TStringArray; override;
86 function GetFilesCount:LongWord; override;
87 function GetExtensionsList:TStringArray; override;
88
89 function LoadDatFile(fileid:LongWord):Tdata; override;
90 procedure UpdateDatFile(fileid:LongWord; data:Tdata); override;
91 procedure LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer); override;
92 procedure UpdateDatFilePart(fileid,offset,size:LongWord; target:Pointer); override;
93
94 function GetRawList(fileid:LongWord):TRawList; override;
95 procedure LoadRawFile(fileid,dat_offset:LongWord; target:Pointer); override;
96 procedure UpdateRawFile(fileid,dat_offset:LongWord; size:LongWord; target:Pointer); override;
97 procedure LoadRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer); override;
98 procedure UpdateRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer); override;
99 published
100 end;
101
102
103const
104 ODB_None=-1;
105 ODB_Dat=0;
106 ODB_ADB=1;
107
108var
109 OniDataConnection:TOniData;
110
111function CreateDataConnection(filename:String; backend:Integer):Boolean;
112procedure CloseDataConnection;
113
114
115
116
117implementation
118uses Unit2_Functions;
119
120
121
122(*
123 Implementation of TOniData
124*)
125
126function TOniData.GetFileIDByName(name:String):LongWord;
127 begin
128 if AppSettings.FilenumbersAsHex then
129 Result:=HexToLong(MidStr(name,1,4))
130 else
131 Result:=StrToInt(MidStr(name,1,5));
132 end;
133
134function TOniData.GetRawInfo(fileid,dat_offset:LongWord):TRawInfo;
135 var
136 i:LongWord;
137 raw_list:TRawList;
138 begin
139 raw_list:=Self.GetRawList(fileid);
140 Result.src_id:=0;
141 Result.src_offset:=0;
142 Result.raw_addr:=0;
143 Result.raw_size:=0;
144 for i:=0 to High(raw_list) do begin
145 if raw_list[i].src_offset=dat_offset then begin
146 Result.src_id:=fileid;
147 Result.src_offset:=raw_list[i].src_offset;
148 Result.raw_addr:=raw_list[i].raw_addr;
149 Result.raw_size:=raw_list[i].raw_size;
150 Result.loc_sep:=raw_list[i].loc_sep;
151 Break;
152 end;
153 end;
154 end;
155
156
157
158
159
160
161
162(*
163================================================================================
164 Implementation of TOniDataDat
165*)
166
167constructor TOniDataDat.Create(DatFilename:String; var Result:Boolean);
168 const
169 header_ident1_pc:Array[0..$13] of Byte=
170 ($1F,$27,$DC,$33,$DF,$BC,$03,$00,$31,$33,$52,$56,$40,$00,$14,$00,$10,$00,$08,$00);
171 header_ident1_mac:Array[0..$13] of Byte=
172 ($61,$30,$C1,$23,$DF,$BC,$03,$00,$31,$33,$52,$56,$40,$00,$14,$00,$10,$00,$08,$00);
173 header_ident2:Array[0..$F] of Byte=
174 ($99,$CF,$40,$00,$90,$4F,$63,$00,$F4,$55,$5F,$00,$90,$4F,$63,$00);
175 var
176 i:LongWord;
177 dat_file:TFileStream;
178 header_pc,header_mac:Boolean;
179 begin
180 if not FileExists(DatFilename) then begin
181 ShowMessage('File doesn''t exist!!!');
182 Result:=False;
183 Exit;
184 end;
185 FFileName:=DatFilename;
186 dat_file:=TFileStream.Create(FFileName, fmOpenRead);
187 dat_file.Read(Fdat_header,SizeOf(Fdat_header));
188 header_pc:=True;
189 header_mac:=True;
190 for i:=0 to High(Fdat_header.Ident) do begin
191 FLevelInfo.Ident[i]:=Fdat_header.Ident[i];
192 if Fdat_header.Ident[i]<>header_ident1_pc[i] then begin
193 header_pc:=False;
194 end;
195 if Fdat_header.Ident[i]<>header_ident1_mac[i] then begin
196 header_mac:=False;
197 end;
198 end;
199 if not (header_pc xor header_mac) then begin
200 Result:=False;
201 Exit;
202 end else begin
203 if (header_pc and not header_mac) then
204 Fos_mac:=False
205 else
206 Fos_mac:=True;
207 end;
208 SetLength(Fdat_filesmap,Fdat_header.Files);
209 SetLength(Fdat_files,Fdat_header.Files);
210 for i:=0 to Fdat_header.Files-1 do
211 dat_file.Read(Fdat_filesmap[i],SizeOf(Fdat_filesmap[i]));
212 for i:=0 to Fdat_header.Files-1 do begin
213 Fdat_files[i].Extension:=Fdat_filesmap[i].Extension;
214 Fdat_files[i].Extension:=ReverseString(Fdat_files[i].Extension);
215 Fdat_files[i].Size:=Fdat_filesmap[i].FileSize;
216 Fdat_files[i].FileType:=Fdat_filesmap[i].FileType;
217 Fdat_files[i].DatAddr:=Fdat_filesmap[i].DataAddr-8+Fdat_header.DataAddr;
218 if (Fdat_filesmap[i].FileType and $01)=0 then begin
219 dat_file.Seek(Fdat_filesmap[i].NameAddr+Fdat_header.NamesAddr,soFromBeginning);
220 SetLength(Fdat_files[i].Name,100);
221 dat_file.Read(Fdat_files[i].Name[1],100);
222 Fdat_files[i].Name:=MidStr(Fdat_files[i].Name,1+4,Pos(#0,Fdat_files[i].Name)-1-4);
223 end else begin
224 Fdat_files[i].Name:='';
225 end;
226 Fdat_files[i].FileName:=FormatNumber(i,5,'0')+'-'+Fdat_files[i].Name+'.'+Fdat_files[i].Extension;
227 Fdat_files[i].FileNameHex:=IntToHex(i,4)+'-'+Fdat_files[i].Name+'.'+Fdat_files[i].Extension;
228 end;
229 dat_file.Seek($40+Fdat_header.Files*$14,soFromBeginning);
230 SetLength(Fdat_namedfilesmap,Fdat_header.NamedFiles);
231 for i:=0 to Fdat_header.NamedFiles-1 do
232 dat_file.Read(Fdat_namedfilesmap[i],SizeOf(Fdat_namedfilesmap[i]));
233
234 dat_file.Seek($40+Fdat_header.Files*$14+Fdat_header.NamedFiles*$8,soFromBeginning);
235 SetLength(Fdat_extensionsmap,Fdat_header.Extensions);
236 for i:=0 to Fdat_header.Extensions-1 do
237 dat_file.Read(Fdat_extensionsmap[i],SizeOf(Fdat_extensionsmap[i]));
238
239 dat_file.Seek(Fdat_files[0].DatAddr+7,soFromBeginning);
240 dat_file.Read(FLevelInfo.LevelNumber,1);
241 FLevelInfo.LevelNumber:=FLevelInfo.LevelNumber DIV 2;
242
243 dat_file.Free;
244
245 Result:=True;
246 FBackend:=ODB_Dat;
247 end;
248
249procedure TOniDataDat.Close;
250 begin
251 Self.Free;
252 end;
253
254
255
256function TOniDataDat.GetFileInfo(fileid:LongWord):TFileInfo;
257 begin
258 if fileid<Self.GetFilesCount then
259 Result:=Fdat_files[fileid]
260 else
261 Result.ID:=-1;
262 end;
263
264function TOniDataDat.GetFilesList(ext:String; pattern:String; NoEmptyFiles:Boolean):TStringArray;
265 var
266 i:LongWord;
267 begin
268 SetLength(Result,0);
269 for i:=0 to Fdat_header.Files-1 do begin
270 if ( (Length(ext)=0) or (Pos(Fdat_files[i].Extension,ext)>0) ) and
271 ( (Length(pattern)=0) or (Pos(UpperCase(pattern),UpperCase(Fdat_files[i].Name))>0) ) then begin
272 if (NoEmptyFiles=false) or ((Fdat_files[i].FileType and $02)=0) then begin
273 SetLength(Result,Length(Result)+1);
274 if AppSettings.FilenumbersAsHex then
275 Result[High(Result)]:=Fdat_files[i].FileNameHex
276 else
277 Result[High(Result)]:=Fdat_files[i].FileName;
278 end;
279 end;
280 end;
281 end;
282
283function TOniDataDat.GetFilesCount:LongWord;
284 begin
285 Result:=Fdat_header.Files;
286 end;
287
288function TOniDataDat.GetExtensionsList:TStringArray;
289 var
290 i:LongWord;
291 begin
292 SetLength(Result,0);
293 for i:=0 to Fdat_header.Extensions-1 do begin
294 SetLength(Result,Length(Result)+1);
295 with Fdat_extensionsmap[i] do begin
296 Result[High(Result)]:=Extension[3]+Extension[2]+Extension[1]+Extension[0]+' ('+IntToStr(ExtCount)+')';
297 end;
298 end;
299 end;
300
301
302
303function TOniDataDat.LoadDatFile(fileid:LongWord):Tdata;
304 var
305 dat_file:TFileStream;
306 begin
307 if fileid<Self.GetFilesCount then begin
308 dat_file:=TFileStream.Create(FFileName, fmOpenRead);
309 dat_file.Seek(Fdat_files[fileid].DatAddr,soFromBeginning);
310 SetLength(Result,Fdat_files[fileid].Size);
311 dat_file.Read(Result[0],Fdat_files[fileid].Size);
312 dat_file.Free;
313 end;
314 end;
315
316procedure TOniDataDat.UpdateDatFile(fileid:LongWord; data:Tdata);
317 var
318 dat_file:TFileStream;
319 begin
320 if fileid<Self.GetFilesCount then begin
321 dat_file:=TFileStream.Create(FFileName, fmOpenReadWrite);
322 dat_file.Seek(Fdat_files[fileid].DatAddr,soFromBeginning);
323 dat_file.Write(data[0],Length(data));
324 dat_file.Free;
325 end;
326 end;
327
328procedure TOniDataDat.LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer);
329 var
330 dat_file:TFileStream;
331 begin
332 if fileid<Self.GetFilesCount then begin
333 dat_file:=TFileStream.Create(FFileName, fmOpenRead);
334 dat_file.Seek(Fdat_files[fileid].DatAddr+offset,soFromBeginning);
335 dat_file.Read(target^,size);
336 dat_file.Free;
337 end;
338 end;
339
340procedure TOniDataDat.UpdateDatFilePart(fileid,offset,size:LongWord; target:Pointer);
341 var
342 dat_file:TFileStream;
343 begin
344 if fileid<Self.GetFilesCount then begin
345 dat_file:=TFileStream.Create(FFileName, fmOpenReadWrite);
346 dat_file.Seek(Fdat_files[fileid].DatAddr+offset,soFromBeginning);
347 dat_file.Write(target^,size);
348 dat_file.Free;
349 end;
350 end;
351
352
353
354function TOniDataDat.GetRawList(fileid:LongWord):TRawList;
355 var
356 i:LongWord;
357 begin
358 SetLength(Result,0);
359 for i:=0 to High(RawListHandlers) do
360 if UpperCase(RawListHandlers[i].Ext)=UpperCase(Fdat_files[fileid].extension) then
361 if RawListHandlers[i].needed then begin
362 Result:=RawListHandlers[i].Handler(fileid);
363 Break;
364 end else
365 Break;
366 end;
367
368procedure TOniDataDat.LoadRawOffset(loc_sep:Boolean; raw_addr,size:LongWord; target:Pointer);
369 var
370 filestream:TFileStream;
371 begin
372 if not loc_sep then
373 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.raw'),fmOpenRead)
374 else
375 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.sep'),fmOpenRead);
376 if raw_addr<=filestream.Size then begin
377 filestream.Seek(raw_addr,soFromBeginning);
378 filestream.Read(target^,size);
379 end;
380 filestream.Free;
381 end;
382
383procedure TOniDataDat.LoadRawFile(fileid,dat_offset:LongWord; target:Pointer);
384 var
385 raw_info:TRawInfo;
386 filestream:TFileStream;
387 begin
388 if fileid<Self.GetFilesCount then begin
389 raw_info:=Self.GetRawInfo(fileid,dat_offset);
390 if not raw_info.loc_sep then
391 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.raw'),fmOpenRead)
392 else
393 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.sep'),fmOpenRead);
394 filestream.Seek(raw_info.raw_addr,soFromBeginning);
395 filestream.Read(target^,raw_info.raw_size);
396 filestream.Free;
397 end;
398 end;
399
400procedure TOniDataDat.UpdateRawFile(fileid,dat_offset:LongWord; size:LongWord; target:Pointer);
401 var
402 raw_info:TRawInfo;
403 filestream:TFileStream;
404 begin
405 if fileid<Self.GetFilesCount then begin
406 raw_info:=Self.GetRawInfo(fileid,dat_offset);
407 if not raw_info.loc_sep then
408 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.raw'),fmOpenReadWrite)
409 else
410 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.sep'),fmOpenReadWrite);
411 filestream.Seek(raw_info.raw_addr,soFromBeginning);
412 filestream.Write(target^,size);
413 filestream.Free;
414 end;
415 end;
416
417procedure TOniDataDat.LoadRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer);
418 var
419 raw_info:TRawInfo;
420 data:Tdata;
421 mem:TMemoryStream;
422 begin
423 if fileid<Self.GetFilesCount then begin
424 raw_info:=Self.GetRawInfo(fileid,dat_offset);
425 SetLength(data, raw_info.raw_size);
426 Self.LoadRawFile(fileid,dat_offset,@data[0]);
427 mem:=TMemoryStream.Create;
428 mem.Write(data[offset],size);
429 mem.Read(target^,size);
430 mem.Free;
431 end;
432 end;
433
434procedure TOniDataDat.UpdateRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer);
435 var
436 raw_info:TRawInfo;
437 filestream:TFileStream;
438 begin
439 if fileid<Self.GetFilesCount then begin
440 raw_info:=Self.GetRawInfo(fileid,dat_offset);
441 if not raw_info.loc_sep then
442 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.raw'),fmOpenReadWrite)
443 else
444 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.sep'),fmOpenReadWrite);
445 filestream.Seek(raw_info.raw_addr+offset,soFromBeginning);
446 filestream.Write(target^,size);
447 filestream.Free;
448 end;
449 end;
450
451function TOniDataDat.AppendRawFile(loc_sep:Boolean; size:LongWord; target:Pointer):LongWord; //Returns new Address
452 var
453 filestream:TFileStream;
454 begin
455 if not loc_sep then
456 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.raw'),fmOpenReadWrite)
457 else
458 filestream:=TFileStream.Create(AnsiReplaceStr(FFileName,'.dat','.sep'),fmOpenReadWrite);
459 Result:=filestream.Size;
460 filestream.Seek(0,soFromEnd);
461 filestream.Write(target^,size);
462 filestream.Free;
463 end;
464
465
466
467
468
469
470
471
472
473
474
475(*
476================================================================================
477 Implementation of TOniDataADB
478*)
479
480constructor TOniDataADB.Create(OLDBFilename:String; var Result:Boolean);
481 var
482 i,j:Byte;
483 temps:String;
484 begin
485 if not FileExists(OLDBFilename) then begin
486 ShowMessage('File doesn''t exist!!!');
487 Result:=False;
488 Exit;
489 end;
490 FFileName:=OLDBFilename;
491 FDatabase:=TABSDatabase.Create(nil);
492 FDatabase.DatabaseName:='OLDBcon';
493 FDatabase.DatabaseFileName:=OLDBFilename;
494 FDatabase.Open;
495 FQuery:=TABSQuery.Create(FDatabase);
496 FQuery.DatabaseName:='OLDBcon';
497 FQuery.SQL.Text:='SELECT [name],[value] FROM globals ORDER BY [name] ASC';
498 FQuery.Open;
499 FQuery.First;
500 repeat
501 if FQuery.FieldByName('name').AsString='dbversion' then begin
502 if FQuery.FieldByName('value').AsString<>DBversion then begin
503 ShowMessage('Database-file '+#13+#10+
504 '"'+OLDBFilename+'"'+#13+#10+
505 'has wrong version. (Required: '+DBversion+'; found: '+
506 FQuery.FieldByName('value').AsString+')');
507 FQuery.Close;
508 Result:=False;
509 Exit;
510 end;
511 end;
512 if FQuery.FieldByName('name').AsString='lvl' then begin
513 FLevelInfo.LevelNumber:=StrToInt(FQuery.FieldByName('value').AsString);
514 end;
515 if FQuery.FieldByName('name').AsString='ident' then begin
516 temps:=FQuery.FieldByName('value').AsString;
517 for i:=0 to High(FLevelInfo.Ident) do begin
518 j:=i*2+1;
519 case temps[j] of
520 '0'..'9': FLevelInfo.Ident[i]:=Ord(temps[j])-48;
521 'A'..'F': FLevelInfo.Ident[i]:=Ord(temps[j])-55;
522 end;
523 FLevelInfo.Ident[i]:=FLevelInfo.Ident[i]*16;
524 case temps[j+1] of
525 '0'..'9': FLevelInfo.Ident[i]:=FLevelInfo.Ident[i]+Ord(temps[j+1])-48;
526 'A'..'F': FLevelInfo.Ident[i]:=FLevelInfo.Ident[i]+Ord(temps[j+1])-55;
527 end;
528 end;
529 end;
530 if FQuery.FieldByName('name').AsString='ident' then begin
531 temps:=FQuery.FieldByName('value').AsString;
532 Fos_mac:=temps='MAC';
533 end;
534 FQuery.Next;
535 until FQuery.EoF;
536 FQuery.Close;
537
538 Result:=True;
539 FBackend:=ODB_ADB;
540 end;
541
542procedure TOniDataADB.Close;
543 begin
544 FDatabase.Close;
545 FDatabase.Free;
546 Self.Free;
547 end;
548
549
550
551function TOniDataADB.GetFileInfo(fileid:LongWord):TFileInfo;
552 begin
553 if fileid<Self.GetFilesCount then begin
554 FQuery.SQL.Text:='SELECT * FROM datfiles WHERE id='+IntToStr(fileid)+' ORDER BY id ASC;';
555 FQuery.Open;
556 if FQuery.RecordCount=1 then begin
557 FQuery.First;
558 Result.ID:=FQuery.FieldByName('id').AsInteger;
559 Result.Name:=FQuery.FieldByName('name').AsString;
560 Result.Extension:=FQuery.FieldByName('extension').AsString;
561 Result.FileName:=FormatNumber(Result.ID,5,'0')+'-'+Result.Name+'.'+Result.Extension;
562 Result.Size:=FQuery.FieldByName('size').AsInteger;
563 Result.FileType:=FQuery.FieldByName('contenttype').AsInteger;
564 Result.DatAddr:=0;
565 Result.opened:=False;
566 end;
567 FQuery.Close;
568 end else begin
569 Result.ID:=-1;
570 end;
571 end;
572
573function TOniDataADB.GetFilesList(ext:String; pattern:String; NoEmptyFiles:Boolean):TStringArray;
574 var
575 i:LongWord;
576 where:String;
577 where_ext:String;
578 begin
579 where:='';
580 if Length(ext)>0 then begin
581 if Length(where)>0 then
582 where:=where+' AND ';
583 if Pos(',',ext)>0 then begin
584 i:=1;
585 where_ext:='';
586 while i<Length(ext) do begin
587 if Length(where_ext)>0 then
588 where_ext:=where_ext+' OR ';
589 where_ext:=where_ext+'(extension="'+MidStr(ext,i,4)+'")';
590 i:=i+5;
591 end;
592 where:=where+'('+where_ext+')';
593 end else begin
594 where:=where+'(extension="'+ext+'")';
595 end;
596 end;
597 if Length(pattern)>0 then begin
598 if Length(where)>0 then
599 where:=where+' AND ';
600 where:=where+'(name LIKE "%'+pattern+'%")';
601 end;
602 if NoEmptyFiles then begin
603 if Length(where)>0 then
604 where:=where+' AND ';
605 where:=where+'(contenttype<>2)';
606 end;
607 if Length(where)>0 then
608 where:=' WHERE '+where;
609 FQuery.SQL.Text:='SELECT id,name,extension FROM datfiles'+where+' ORDER BY id ASC;';
610 FQuery.Open;
611 if FQuery.RecordCount>0 then begin
612 FQuery.First;
613 SetLength(Result,FQuery.RecordCount);
614 i:=0;
615 repeat
616 Result[i]:=FormatNumber(FQuery.FieldByName('id').AsInteger,5,'0')+'-'+FQuery.FieldByName('name').AsString+'.'+FQuery.FieldByName('extension').AsString;
617 Inc(i);
618 FQuery.Next;
619 until FQuery.EOF;
620 end;
621 FQuery.Close;
622 end;
623
624function TOniDataADB.GetFilesCount:LongWord;
625 begin
626 FQuery.SQL.Text:='SELECT Count(*) AS cnumber FROM datfiles;';
627 FQuery.Open;
628 if FQuery.RecordCount>0 then begin
629 FQuery.First;
630 Result:=FQuery.FieldByName('cnumber').AsInteger;
631 end else Result:=0;
632 FQuery.Close;
633 end;
634
635function TOniDataADB.GetExtensionsList:TStringArray;
636 var
637 i:LongWord;
638 begin
639 SetLength(Result,0);
640 FQuery.SQL.Text:='SELECT extension,count(extension) AS x FROM datfiles GROUP BY extension ORDER BY extension ASC;';
641 FQuery.Open;
642 if FQuery.RecordCount>0 then begin
643 SetLength(Result,FQuery.RecordCount);
644 i:=0;
645 repeat
646 Result[i]:=FQuery.FieldByName('extension').AsString+' ('+IntToStr(FQuery.FieldByName('x').AsInteger)+')';
647 Inc(i);
648 FQuery.Next;
649 until FQuery.EOF;
650 end;
651 FQuery.Close;
652 end;
653
654
655
656function TOniDataADB.LoadDatFile(fileid:LongWord):Tdata;
657 var
658 mem:TStream;
659 begin
660 if fileid<Self.GetFilesCount then begin
661 FQuery.SQL.Text:='SELECT data FROM datfiles WHERE id='+IntToStr(fileid)+';';
662 FQuery.Open;
663 if FQuery.RecordCount>0 then begin
664 mem:=FQuery.CreateBlobStream(FQuery.FieldByName('data'),bmRead);
665 SetLength(Result,mem.Size);
666 mem.Seek(0,soFromBeginning);
667 mem.Read(Result[0],mem.Size);
668 mem.Free;
669 end;
670 FQuery.Close;
671 end;
672 end;
673
674procedure TOniDataADB.UpdateDatFile(fileid:LongWord; data:Tdata);
675 var
676 MimeCoder: TStringFormat_MIME64;
677 mem:TMemoryStream;
678 begin
679 if fileid<Self.GetFilesCount then begin
680 mimecoder:=TStringFormat_MIME64.Create;
681 mem:=TMemoryStream.Create;
682 mem.Write(data[0],Length(data));
683 mem.Seek(0,soFromBeginning);
684 FQuery.SQL.Text:='UPDATE datfiles SET data=MimeToBin("'+MimeCoder.StrTo(mem.Memory, mem.Size)+'") WHERE id='+IntToStr(fileid)+';';
685 FQuery.ExecSQL;
686 mem.Free;
687 mimecoder.Free;
688 end;
689 end;
690
691procedure TOniDataADB.LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer);
692 var
693 mem:TStream;
694 begin
695 if fileid<Self.GetFilesCount then begin
696 FQuery.SQL.Text:='SELECT data FROM datfiles WHERE id='+IntToStr(fileid)+';';
697 FQuery.Open;
698 IF FQuery.RecordCount>0 THEN BEGIN
699 mem:=FQuery.CreateBlobStream(FQuery.FieldByName('data'),bmRead);
700 mem.Seek(offset,soFromBeginning);
701 mem.Read(target^,size);
702 mem.Free;
703 END;
704 FQuery.Close;
705 END;
706 END;
707
708procedure TOniDataADB.UpdateDatFilePart(fileid,offset,size:LongWord; target:Pointer);
709 var
710 MimeCoder: TStringFormat_MIME64;
711 mem:TMemoryStream;
712 data:Tdata;
713 begin
714 if fileid<Self.GetFilesCount then begin
715 data:=Self.LoadDatFile(fileid);
716 mimecoder:=TStringFormat_MIME64.Create;
717 mem:=TMemoryStream.Create;
718 mem.Write(data[0],Length(data));
719 mem.Seek(offset,soFromBeginning);
720 mem.Write(target^,size);
721 mem.Seek(0,soFromBeginning);
722 FQuery.SQL.Text:='UPDATE datfiles SET data=MimeToBin("'+MimeCoder.StrTo(mem.Memory, mem.Size)+'") WHERE id='+IntToStr(fileid)+';';
723 FQuery.ExecSQL;
724 mem.Free;
725 mimecoder.Free;
726 end;
727 end;
728
729
730
731function TOniDataADB.GetRawList(fileid:LongWord):TRawList;
732 var
733 i:LongWord;
734 Query:TABSQuery;
735 begin
736 SetLength(Result,0);
737 FQuery.SQL.Text:='SELECT [src_link_offset],[size] FROM rawmap WHERE [src_id]='+IntToStr(fileid)+' ORDER BY src_link_offset ASC;';
738 FQuery.Open;
739 if FQuery.RecordCount>0 then begin
740 FQuery.First;
741 SetLength(Result,FQuery.RecordCount);
742 i:=0;
743 repeat
744 Result[i].src_id:=fileid;
745 Result[i].src_offset:=FQuery.FieldByName('src_link_offset').AsInteger;
746 Result[i].raw_addr:=0;
747 Result[i].raw_size:=FQuery.FieldByName('size').AsInteger;
748 Inc(i);
749 FQuery.Next;
750 until FQuery.EOF;
751 end;
752 FQuery.Close;
753 end;
754
755procedure TOniDataADB.LoadRawFile(fileid,dat_offset:LongWord; target:Pointer);
756 var
757 mem:TStream;
758 begin
759 if fileid<Self.GetFilesCount then begin
760 FQuery.SQL.Text:='SELECT data FROM rawmap WHERE (src_id='+IntToStr(fileid)+') AND (src_link_offset='+IntToStr(dat_offset)+');';
761 FQuery.Open;
762 if FQuery.RecordCount>0 then begin
763 mem:=FQuery.CreateBlobStream(FQuery.FieldByName('data'),bmRead);
764 mem.Seek(0,soFromBeginning);
765 mem.Read(target^,mem.size);
766 mem.Free;
767 end;
768 FQuery.Close;
769 end;
770 end;
771
772procedure TOniDataADB.UpdateRawFile(fileid,dat_offset:LongWord; size:LongWord; target:Pointer);
773 var
774 MimeCoder: TStringFormat_MIME64;
775 mem:TMemoryStream;
776 begin
777 if fileid<Self.GetFilesCount then begin
778 mimecoder:=TStringFormat_MIME64.Create;
779 mem:=TMemoryStream.Create;
780 mem.Write(target^,size);
781 mem.Seek(0,soFromBeginning);
782 FQuery.SQL.Text:='UPDATE rawmap SET data=MimeToBin("'+MimeCoder.StrTo(mem.Memory, mem.Size)+'") WHERE (src_id='+IntToStr(fileid)+') AND (src_link_offset='+IntToStr(dat_offset)+');';
783 FQuery.ExecSQL;
784 mem.Free;
785 mimecoder.Free;
786 end;
787 end;
788
789procedure TOniDataADB.LoadRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer);
790 var
791 data:Tdata;
792 mem:TMemoryStream;
793 begin
794 if fileid<Self.GetFilesCount then begin
795 SetLength(data, Self.GetRawInfo(fileid,dat_offset).raw_size);
796 Self.LoadRawFile(fileid,dat_offset,@data[0]);
797 mem:=TMemoryStream.Create;
798 mem.Write(data[offset],size);
799 mem.Read(target^,size);
800 mem.Free;
801 end;
802 end;
803
804procedure TOniDataADB.UpdateRawFilePart(fileid,dat_offset:LongWord; offset,size:LongWord; target:Pointer);
805 var
806 MimeCoder: TStringFormat_MIME64;
807 mem:TMemoryStream;
808 data:Tdata;
809 begin
810 if fileid<Self.GetFilesCount then begin
811 SetLength(data, Self.GetRawInfo(fileid,offset).raw_size);
812 Self.LoadRawFile(fileid,offset,@data[0]);
813 mimecoder:=TStringFormat_MIME64.Create;
814 mem:=TMemoryStream.Create;
815 mem.Write(data[0],Length(data));
816 mem.Seek(offset,soFromBeginning);
817 mem.Write(target^,size);
818 mem.Seek(0,soFromBeginning);
819 FQuery.SQL.Text:='UPDATE rawmap SET data=MimeToBin("'+MimeCoder.StrTo(mem.Memory, mem.Size)+'") WHERE (src_id='+IntToStr(fileid)+') AND (src_link_offset='+IntToStr(dat_offset)+');';
820 FQuery.ExecSQL;
821 mem.Free;
822 mimecoder.Free;
823 end;
824 end;
825
826
827
828
829
830
831
832
833
834
835
836function CreateDataConnection(filename:String; backend:Integer):Boolean;
837 var
838 answer:Boolean;
839 begin
840 if Assigned(OniDataConnection) then begin
841 OniDataConnection.Close;
842 OniDataConnection.Free;
843 OniDataConnection:=Nil;
844 end;
845 case backend of
846 ODB_Dat: OniDataConnection:=TOniDataDat.Create(filename, answer);
847 ODB_ADB: OniDataConnection:=TOniDataADB.Create(filename, answer);
848 else
849 ShowMessage('Unknown Backend');
850 Result:=False;
851 Exit;
852 end;
853
854 if answer then begin
855// ShowMessage('file loaded');
856// ShowMessage('Files: '+IntToStr(OniDataConnection.GetFilesCount));
857 Result:=True;
858 end else begin
859 ShowMessage('File not loaded');
860 OniDataConnection.Close;
861 OniDataConnection.Free;
862 Result:=False;
863 end;
864 end;
865
866procedure CloseDataConnection;
867 begin
868 if Assigned(OniDataConnection) then begin
869 OniDataConnection.Close;
870 OniDataConnection:=Nil;
871 end;
872 end;
873
874end.
Note: See TracBrowser for help on using the repository browser.