source: oup/current/Helper/LevelDB.pas@ 170

Last change on this file since 170 was 167, checked in by alloc, 18 years ago
File size: 24.7 KB
Line 
1unit LevelDB;
2interface
3uses
4 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
5 Dialogs, ComCtrls, StdCtrls, StrUtils;
6
7type
8 TForm_LevelDB = class(TForm)
9 group_progress: TGroupBox;
10 progress: TProgressBar;
11 lbl_progress: TLabel;
12 btn_abortok: TButton;
13 lbl_estimation: TLabel;
14 procedure btn_abortokClick(Sender: TObject);
15 public
16 procedure CreateDatabase(Source, Target: String);
17 procedure CreateLevel(Source, Target: String);
18 end;
19
20
21var
22 Form_LevelDB: TForm_LevelDB;
23
24implementation
25{$R *.dfm}
26uses ABSMain, ABSDecUtil, Main,
27 ConnectionManager, TypeDefs, DataAccess, OniImgClass, Data, RawList,
28 Access_OniArchive;
29
30var
31 Converting: Boolean = False;
32 Abort: Boolean = False;
33
34
35function GetOpenMsg(msg: TStatusMessages): String;
36begin
37 case msg of
38 SM_AlreadyOpened: Result := 'File already opened.';
39 SM_FileNotFound: Result := 'File not found.';
40 SM_UnknownExtension: Result := 'Unknown extension.';
41 SM_IncompatibleFile: Result := 'Incompatible file format.';
42 SM_UnknownError: Result := 'Unknown error.';
43 end;
44end;
45
46
47procedure TForm_LevelDB.CreateLevel(Source, Target: String);
48const
49 EmptyBytes: Array[0..31] of Byte = (
50 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 );
51var
52 DatHeader: THeader;
53 FilesHeader: TFilesMap;
54 NamedFilesHeader: TNamedFilesMap;
55 ExtensionsHeader: TExtensionsMap;
56
57 Stream_Body, Stream_Names: TMemoryStream;
58 Stream_Dat, Stream_Raw, Stream_Sep: TFileStream;
59
60 BeginTime, FileTime: Double;
61 Step: Integer;
62 LevelID: Integer;
63 TimeFormat: TFormatSettings;
64
65 ConID: Integer;
66 Connection: TDataAccess;
67 ConRepMsg: TStatusMessages;
68
69 FileID: Integer;
70
71 Strings: TStrings;
72 i, j: Integer;
73 temps: String;
74 tempi: Integer;
75 tempb: Byte;
76 FileInfo: TFileInfo;
77 DatLinks: TDatLinkList;
78 RawLinks: TRawDataList;
79
80 DatFileStream, RawFileStream: TMemoryStream;
81const
82 Steps: Byte = 3;
83
84
85 procedure DoStep(StepName: String);
86 begin
87 Inc(Step);
88 if StepName <> 'FIN' then
89 group_progress.Caption :=
90 'Creating Dat (Step ' + IntToStr(Step) + '/' + IntToStr(Steps) + ': ' + StepName + ')'
91 else
92 group_progress.Caption := 'Creating Dat (FINISHED)';
93 end;
94
95 procedure StopConvert;
96 begin
97 btn_abortok.Caption := '&Close';
98 btn_abortok.Default := True;
99 converting := False;
100 lbl_estimation.Caption := 'ABORTED';
101 group_progress.Caption := 'Creating Level (ABORTED)';
102
103 Stream_Body.Free;
104 Stream_Names.Free;
105 DatFileStream.Free;
106 RawFileStream.Free;
107
108 Stream_Dat.Free;
109 Stream_Raw.Free;
110 if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
111 Stream_Sep.Free;
112
113 if MessageBox(Self.Handle, PChar('Delete the unfinished level-files?'),
114 PChar('Delete files?'), MB_YESNO) = idYes then
115 begin
116 DeleteFile(target);
117 DeleteFile(AnsiReplaceStr(Target, '.dat', '.raw'));
118 if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
119 DeleteFile(AnsiReplaceStr(Target, '.dat', '.sep'));
120 end;
121 end;
122
123begin
124
125 //
126 // FILE EXISTS CHECK FÜR DAT/RAW/SEP!!!
127 //
128
129 TimeFormat.ShortTimeFormat := 'hh:nn:ss';
130 TimeFormat.LongTimeFormat := 'hh:nn:ss';
131 TimeFormat.TimeSeparator := ':';
132
133 ConID := ConManager.OpenConnection(Source, ConRepMsg);
134 if not (ConRepMsg in [SM_OK, SM_AlreadyOpened]) then
135 begin
136 ShowMessage('Source-file couldn''t be opened! Aborting' + CrLf + GetOpenMsg(ConRepMsg));
137 Exit;
138 end else
139 Connection := ConManager.Connection[ConID];
140
141 ConID := ConManager.FileOpened(Target);
142 if ConID >= 0 then
143 begin
144 if MessageBox(Self.Handle, PChar('Destination-file is opened, close it in ' +
145 'order to proceed conversion?'), PChar('Destination-file opened'),
146 MB_YESNO + MB_ICONQUESTION) = ID_YES then
147 begin
148 if Form_Main.CheckConnectionCloseable(ConID) then
149 if not ConManager.CloseConnection(ConID, ConRepMsg) then
150 begin
151 ShowMessage('Couldn''t close destination-file. Aborting');
152 Exit;
153 end;
154 end else begin
155 ShowMessage('Aborting');
156 Exit;
157 end;
158 end;
159
160 if FileExists(Target) then
161 begin
162 if MessageBox(Self.Handle, PChar('Destination-file exists. ' +
163 'Overwrite it?'), PChar('Destination-file exists'),
164 MB_YESNO + MB_ICONWARNING) = ID_YES then
165 begin
166 if not DeleteFile(Target) then
167 begin
168 ShowMessage('Couldn''t delete file. Aborting');
169 Exit;
170 end;
171 if FileExists(AnsiReplaceStr(Target, '.dat', '.raw')) then
172 if not DeleteFile(AnsiReplaceStr(Target, '.dat', '.raw')) then
173 begin
174 ShowMessage('Couldn''t delete file. Aborting');
175 Exit;
176 end;
177 if FileExists(AnsiReplaceStr(Target, '.dat', '.sep')) then
178 if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
179 if not DeleteFile(AnsiReplaceStr(Target, '.dat', '.sep')) then
180 begin
181 ShowMessage('Couldn''t delete file. Aborting');
182 Exit;
183 end;
184 end else begin
185 ShowMessage('Aborting');
186 Exit;
187 end;
188 end;
189
190 LevelID := Connection.LevelNumber;
191 LevelID := (LevelID * 2) * 256 * 256 * 256 + $01;
192
193 Self.Visible := True;
194 Form_Main.Visible := False;
195 Step := 0;
196 Converting := True;
197 Abort := False;
198 btn_abortok.Caption := '&Abort...';
199 btn_abortok.Default := False;
200 BeginTime := Time;
201
202 Stream_Body := TMemoryStream.Create;
203 Stream_Names := TMemoryStream.Create;
204 Stream_Dat := TFileStream.Create(Target, fmCreate);
205 Stream_Raw := TFileStream.Create(AnsiReplaceStr(Target, '.dat', '.raw'), fmCreate);
206 Stream_Raw.Write(EmptyBytes[0], 32);
207 if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
208 begin
209 Stream_Sep := TFileStream.Create(AnsiReplaceStr(Target, '.dat', '.sep'), fmCreate);
210 Stream_Sep.Write(EmptyBytes[0], 32);
211 end;
212
213
214 DoStep('Creating header');
215 progress.Position := 0;
216 lbl_progress.Caption := '';
217 lbl_estimation.Caption := 'Estimated finishing time: unknown';
218 Application.ProcessMessages;
219
220 SetLength(NamedFilesHeader, 0);
221 Strings := TStringList.Create;
222 Strings := Connection.GetFilesList('', '', False, ST_ExtNameAsc);
223 for i := 0 to Strings.Count - 1 do
224 begin
225 if MidStr(Strings.Strings[i],
226 Pos('-', Strings.Strings[i]) + 1,
227 Length(Strings.Strings[i]) -
228 Pos('.', ReverseString(Strings.Strings[i])) -
229 Pos('-', Strings.Strings[i])
230 ) <> '' then
231 begin
232 SetLength(NamedFilesHeader, Length(NamedFilesHeader) + 1);
233 NamedFilesHeader[High(NamedFilesHeader)].FileNumber := StrToInt(MidStr(Strings.Strings[i], 1, 5));
234 NamedFilesHeader[High(NamedFilesHeader)].blubb := 0;
235 end;
236 end;
237
238 for i := 0 to High(DatHeader.OSIdent) do
239 case Connection.DataOS of
240 DOS_WIN: DatHeader.OSIdent[i] := HeaderOSIdentWin[i];
241 DOS_MAC: DatHeader.OSIdent[i] := HeaderOSIdentMac[i];
242 DOS_MACBETA: DatHeader.OSIdent[i] := HeaderOSIdentMacBeta[i];
243 end;
244 for i := 0 to High(DatHeader.GlobalIdent) do
245 DatHeader.GlobalIdent[i] := HeaderGlobalIdent[i];
246 DatHeader.Files := Connection.GetFileCount;
247 DatHeader.NamedFiles := Length(NamedFilesHeader);
248
249 Strings := Connection.GetExtensionsList(EF_ExtCount);
250
251 DatHeader.Extensions := Strings.Count;
252 DatHeader.DataAddr := 0;
253 DatHeader.DataSize := 0;
254 DatHeader.NamesAddr := 0;
255 DatHeader.NamesSize := 0;
256 for i := 0 to High(DatHeader.Ident2) do
257 DatHeader.Ident2[i] := 0;
258 SetLength(FilesHeader, DatHeader.Files);
259 SetLength(ExtensionsHeader, DatHeader.Extensions);
260
261
262 DoStep('Writing extensions-header');
263 progress.Max := Strings.Count;
264 Application.ProcessMessages;
265 for i := 0 to Strings.Count - 1 do
266 begin
267 temps := Strings.Strings[i];
268 ExtensionsHeader[i].ExtCount := StrToInt( MidStr(
269 temps,
270 Pos('(', temps) + 1,
271 Pos(')', temps) - Pos('(', temps) - 1 ) );
272 temps := MidStr(temps, 1, 4);
273 for j := 0 to 3 do
274 ExtensionsHeader[i].Extension[j] := temps[4-j];
275 for j := 0 to High(FileTypes) do
276 if FileTypes[j].Extension = temps then
277 Break;
278 if j < Length(FileTypes) then
279 begin
280 case Connection.DataOS of
281 DOS_WIN: ExtensionsHeader[i].Ident := FileTypes[j].IdentWin;
282 DOS_WINDEMO: ExtensionsHeader[i].Ident := FileTypes[j].IdentMac;
283 DOS_MAC: ExtensionsHeader[i].Ident := FileTypes[j].IdentMac;
284 DOS_MACBETA: ExtensionsHeader[i].Ident := FileTypes[j].IdentMac;
285 end;
286 end else begin
287 ShowMessage('Unknown Extension: ' + Strings.Strings[i]);
288 Exit;
289 end;
290 progress.Position := i + 1;
291 lbl_progress.Caption := 'Extensions done: ' + IntToStr(i + 1) + '/' +
292 IntToStr(Strings.Count);
293 Application.ProcessMessages;
294 end;
295
296 DoStep('Storing files-data');
297 progress.Position := 0;
298 progress.Max := DatHeader.Files;
299 lbl_progress.Caption := '';
300 lbl_estimation.Caption := 'Estimated finishing time: unknown';
301 Application.ProcessMessages;
302
303 FileTime := Time;
304 for FileID := 0 to DatHeader.Files - 1 do
305 begin
306 FileInfo := Connection.GetFileInfo(FileID);
307 for j := 0 to 3 do
308 FilesHeader[FileID].Extension[j] := FileInfo.Extension[4 - j];
309 if FileInfo.Size > 0 then
310 begin
311 FilesHeader[FileID].DataAddr := Stream_Body.Size + 8;
312 DatFileStream := TMemoryStream.Create;
313 Connection.LoadDatFile(FileID, TStream(DatFileStream));
314 DatFileStream.Seek(0, soFromBeginning);
315 tempi := FileID * 256 + 1;
316 DatFileStream.Write(tempi, 4);
317 DatFileStream.Write(LevelID, 4);
318
319 DatLinks := Connection.GetDatLinks(FileID);
320 if Length(DatLinks) > 0 then
321 begin
322 for i := 0 to High(DatLinks) do
323 begin
324 DatFileStream.Seek(DatLinks[i].SrcOffset, soFromBeginning);
325 if DatLinks[i].DestID < 0 then
326 tempi := 0
327 else
328 tempi := DatLinks[i].DestID * 256 + 1;
329 DatFileStream.Write(tempi, 4);
330 end;
331 end;
332
333 RawLinks := Connection.GetRawList(FileID);
334 if Length(RawLinks) > 0 then
335 begin
336 for i := 0 to High(RawLinks) do
337 begin
338 if RawLinks[i].RawSize > 0 then
339 begin
340 RawFileStream := TMemoryStream.Create;
341 Connection.LoadRawFile(FileID, RawLinks[i].SrcOffset, TStream(RawFileStream));
342 RawFileStream.Seek(0, soFromBeginning);
343 if RawLinks[i].LocSep then
344 begin
345 RawLinks[i].RawAddr := Stream_Sep.Size;
346 Stream_sep.CopyFrom(RawFileStream, RawFileStream.Size);
347 if (Stream_Sep.Size mod 32) > 0 then
348 Stream_Sep.Write(EmptyBytes[0], 32 - (Stream_Sep.Size mod 32));
349 end else begin
350 RawLinks[i].RawAddr := Stream_Raw.Size;
351 Stream_Raw.CopyFrom(RawFileStream, RawFileStream.Size);
352 if (Stream_Raw.Size mod 32) > 0 then
353 Stream_Raw.Write(EmptyBytes[0], 32 - (Stream_Raw.Size mod 32));
354 end;
355 end else
356 RawLinks[i].RawAddr := 0;
357 DatFileStream.Seek(RawLinks[i].SrcOffset, soFromBeginning);
358 DatFileStream.Write(RawLinks[i].RawAddr, 4);
359 end;
360 end;
361 DatFileStream.Seek(0, soFromBeginning);
362 Stream_Body.CopyFrom(DatFileStream, DatFileStream.Size);
363 if (Stream_Body.Size mod 32) > 0 then
364 begin
365 ShowMessage(
366 IntToStr(FileID) + '-' + FileInfo.Name + '.' + FileInfo.Extension + #13#10 +
367 IntToStr(FileInfo.Size) + ' - 0x' + IntToHex(FileInfo.Size, 6) + ' - real: ' + IntToStr(DatFileStream.Size) + ' - 0x' + IntToHex(DatFileStream.Size, 6) + #13#10 +
368 IntToStr(Stream_Body.Size) + ' - 0x' + IntToHex(Stream_Body.Size, 6) );
369 Stream_Body.Write(EmptyBytes[0], 32 - (Stream_Body.Size mod 32));
370 end;
371 end
372 else
373 FilesHeader[FileID].DataAddr := 0;
374 if Length(fileinfo.Name) > 0 then
375 begin
376 FilesHeader[FileID].NameAddr := Stream_Names.Size;
377 temps := fileinfo.Extension + fileinfo.Name + Chr(0);
378 Stream_Names.Write(temps[1], Length(temps));
379 end
380 else
381 FilesHeader[FileID].NameAddr := 0;
382 FilesHeader[FileID].FileSize := fileinfo.Size;
383 FilesHeader[FileID].FileType := fileinfo.FileType;
384
385 if ((FileID mod 10) = 0) and (FileID >= 100) then
386 lbl_estimation.Caption := 'Estimated time left: ' + TimeToStr(
387 (Time - FileTime) / FileID * (progress.Max - FileID + 1) * 1.1, TimeFormat );
388 progress.Position := FileID + 1;
389 lbl_progress.Caption := 'Files done: ' + IntToStr(FileID + 1) + '/' + IntToStr(progress.Max);
390 Application.ProcessMessages;
391 end;
392
393 Stream_Dat.Write(DatHeader, SizeOf(DatHeader));
394 for i := 0 to High(FilesHeader) do
395 Stream_Dat.Write(FilesHeader[i], SizeOf(FilesHeader[i]));
396 for i := 0 to High(NamedFilesHeader) do
397 Stream_Dat.Write(NamedFilesHeader[i], SizeOf(NamedFilesHeader[i]));
398 for i := 0 to High(ExtensionsHeader) do
399 Stream_Dat.Write(ExtensionsHeader[i], SizeOf(ExtensionsHeader[i]));
400
401 if (Stream_Dat.Size mod 32) > 0 then
402 Stream_Dat.Write(EmptyBytes[0], 32 - (Stream_Dat.Size mod 32));
403
404 DatHeader.DataSize := Stream_Body.Size;
405 DatHeader.NamesSize := Stream_Names.Size;
406 DatHeader.DataAddr := Stream_Dat.Size;
407
408 Stream_Body.Seek(0, soFromBeginning);
409 Stream_Dat.CopyFrom(Stream_Body, Stream_Body.Size);
410
411 if (Stream_Dat.Size mod 32) > 0 then
412 Stream_Dat.Write(EmptyBytes[0], 32 - (Stream_Dat.Size mod 32));
413
414 DatHeader.NamesAddr := Stream_Dat.Size;
415 Stream_Names.Seek(0, soFromBeginning);
416 Stream_Dat.CopyFrom(Stream_Names, Stream_Names.Size);
417
418 Stream_Dat.Seek(0, soFromBeginning);
419 Stream_Dat.Write(DatHeader, SizeOf(DatHeader));
420
421 Stream_Dat.Free;
422 Stream_Body.Free;
423 Stream_Names.Free;
424 Stream_Raw.Free;
425
426 if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
427 Stream_Sep.Free;
428
429 progress.Position := progress.Max;
430 lbl_progress.Caption := 'Files done: ' + IntToStr(progress.Max) + '/' +
431 IntToStr(progress.Max);
432 lbl_estimation.Caption := 'FINISHED (duration: ' + TimeToStr(Time - Begintime, TimeFormat) + ')';
433
434 DoStep('FIN');
435 btn_abortok.Caption := '&OK';
436 btn_abortok.Default := True;
437
438 converting := False;
439
440// CloseDataConnection(DataConnections[conIndex]);
441end;
442
443
444
445
446procedure TForm_LevelDB.CreateDatabase(Source, target: String);
447var
448 DataBase: TABSDatabase;
449 Query: TABSQuery;
450 MimeCoder: TStringFormat_MIME64;
451
452 BeginTime, FileTime: Double;
453 Step: Integer;
454 TimeFormat: TFormatSettings;
455
456 ConID: Integer;
457 Connection: TDataAccess;
458 ConRepMsg: TStatusMessages;
459
460 FileID: Integer;
461
462 i: Integer;
463 temps: String;
464 tempdata: TByteData;
465 FileInfo: TFileInfo;
466 DatLinks: TDatLinkList;
467 RawLinks: TRawDataList;
468const
469 steps: Byte = 2;
470
471 procedure DoStep(stepname: String);
472 begin
473 Inc(step);
474 if stepname <> 'FIN' then
475 group_progress.Caption :=
476 'Creating DB (Step ' + IntToStr(step) + '/' + IntToStr(steps) + ': ' + stepname + ')'
477 else
478 group_progress.Caption := 'Creating DB (FINISHED)';
479 end;
480
481 procedure StopConvert;
482 begin
483 btn_abortok.Caption := '&Close';
484 btn_abortok.Default := True;
485 converting := False;
486 lbl_estimation.Caption := 'ABORTED';
487 group_progress.Caption := 'Creating DB (ABORTED)';
488 DataBase.Close;
489 if MessageBox(Self.Handle, PChar('Delete the unfinished DB-file?'),
490 PChar('Delete file?'), MB_YESNO) = idYes then
491 begin
492 DeleteFile(target);
493 end;
494end;
495
496
497
498begin
499
500 //
501 // FILE EXISTS CHECK FÜR DAT/RAW/SEP!!!
502 //
503
504 TimeFormat.ShortTimeFormat := 'hh:nn:ss';
505 TimeFormat.LongTimeFormat := 'hh:nn:ss';
506 TimeFormat.TimeSeparator := ':';
507
508 ConID := ConManager.OpenConnection(Source, ConRepMsg);
509 if not (ConRepMsg in [SM_OK, SM_AlreadyOpened]) then
510 begin
511 ShowMessage('Source-file couldn''t be opened! Aborting' + CrLf + GetOpenMsg(ConRepMsg));
512 Exit;
513 end else
514 Connection := ConManager.Connection[ConID];
515
516 ConID := ConManager.FileOpened(Target);
517 if ConID >= 0 then
518 begin
519 if MessageBox(Self.Handle, PChar('Destination-file is opened, close it in ' +
520 'order to proceed conversion?'), PChar('Destination-file opened'),
521 MB_YESNO + MB_ICONQUESTION) = ID_YES then
522 begin
523 if Form_Main.CheckConnectionCloseable(ConID) then
524 if not ConManager.CloseConnection(ConID, ConRepMsg) then
525 begin
526 ShowMessage('Couldn''t close destination-file. Aborting');
527 Exit;
528 end;
529 end else begin
530 ShowMessage('Aborting');
531 Exit;
532 end;
533 end;
534
535 if FileExists(Target) then
536 begin
537 if MessageBox(Self.Handle, PChar('Destination-file exists. ' +
538 'Overwrite it?'), PChar('Destination-file exists'),
539 MB_YESNO + MB_ICONWARNING) = ID_YES then
540 begin
541 if not DeleteFile(Target) then
542 begin
543 ShowMessage('Couldn''t delete file. Aborting');
544 Exit;
545 end;
546 end else begin
547 ShowMessage('Aborting');
548 Exit;
549 end;
550 end;
551
552 Self.Visible := True;
553 Form_Main.Visible := False;
554 step := 0;
555 converting := True;
556 abort := False;
557 btn_abortok.Caption := '&Abort...';
558 btn_abortok.Default := False;
559
560 BeginTime := Time;
561
562 DataBase := TABSDatabase.Create(Self);
563 DataBase.MaxConnections := 1;
564 DataBase.PageSize := 8112;
565 DataBase.PageCountInExtent := 8;
566
567 DataBase.DatabaseName := 'OLDB';
568 DataBase.DatabaseFileName := target;
569 DataBase.CreateDatabase;
570
571 DoStep('Creating tables');
572 progress.Position := 0;
573 lbl_progress.Caption := '';
574 lbl_estimation.Caption := 'Estimated finishing time: unknown';
575 Application.ProcessMessages;
576
577 Query := TABSQuery.Create(Self);
578 Query.DatabaseName := 'OLDB';
579 Query.SQL.Text :=
580 'CREATE TABLE globals ( id AUTOINC PRIMARY KEY, name STRING(128), ' +
581 'value STRING(128) );';
582 Query.ExecSQL;
583 Query.SQL.Text :=
584 'CREATE TABLE linkmap ( id AUTOINC PRIMARY KEY, src_id INTEGER, ' +
585 'src_link_offset INTEGER, target_id INTEGER);';
586 Query.ExecSQL;
587 Query.SQL.Text := 'CREATE INDEX idsrcid ON linkmap (src_id);';
588 Query.ExecSQL;
589 Query.SQL.Text := 'CREATE INDEX idtargetid ON linkmap (target_id);';
590 Query.ExecSQL;
591 Query.SQL.Text :=
592 'CREATE TABLE rawmap ( id AUTOINC PRIMARY KEY, src_id INTEGER, ' +
593 'src_link_offset INTEGER, sep BOOLEAN, size INTEGER, ' +
594 'data BLOB BlobCompressionMode 9 BlobBlockSize 1024 BlobCompressionAlgorithm ZLib);';
595 // Query.SQL.Text:='CREATE TABLE rawmap ( id AUTOINC PRIMARY KEY, src_id INTEGER, src_link_offset INTEGER, size INTEGER, data BLOB BlobCompressionAlgorithm None );';
596 Query.ExecSQL;
597 Query.SQL.Text := 'CREATE INDEX idsrcid ON rawmap (src_id);';
598 Query.ExecSQL;
599 Query.SQL.Text :=
600 'CREATE TABLE datfiles ( id INTEGER PRIMARY KEY, extension CHAR(4), ' +
601 'name STRING(128), contenttype INTEGER, size INTEGER, ' +
602 'data BLOB BlobCompressionMode 9 BlobBlockSize 1024 BlobCompressionAlgorithm ZLib );';
603 // Query.SQL.Text:='CREATE TABLE datfiles ( id INTEGER PRIMARY KEY, extension CHAR(4), name STRING(128), contenttype INTEGER, size INTEGER, data BLOB BlobCompressionAlgorithm None );';
604 Query.ExecSQL;
605// Query.SQL.Text :=
606// 'CREATE TABLE extlist ( id AUTOINC PRIMARY KEY, ext CHAR(4), ident CHAR(16) );';
607// Query.ExecSQL;
608
609 Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("dbversion","' +
610 dbversion + '");';
611 Query.ExecSQL;
612
613 Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("lvl","' +
614 IntToStr(Connection.LevelNumber) + '");';
615 Query.ExecSQL;
616 case Connection.DataOS of
617 DOS_WIN: temps := 'WIN';
618 DOS_WINDEMO: temps := 'WINDEMO';
619 DOS_MAC: temps := 'MAC';
620 DOS_MACBETA: temps := 'MACBETA';
621 end;
622 Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("os","' + temps + '");';
623 Query.ExecSQL;
624
625 progress.Position := 0;
626 lbl_progress.Caption := 'Files done: ' + IntToStr(0) + '/' + IntToStr(
627 Connection.GetFileCount);
628 lbl_estimation.Caption := 'Estimated finishing time: unknown';
629
630 progress.Max := Connection.GetFileCount;
631 begintime := Time;
632 DoStep('Writing .dat-fileslist');
633 Application.ProcessMessages;
634
635 TAccess_OniArchive(Connection).UnloadWhenUnused := False;
636
637 FileTime := Time;
638 Database.StartTransaction;
639 for FileID := 0 to Connection.GetFileCount - 1 do
640 begin
641 fileinfo := Connection.GetFileInfo(FileID);
642 if (fileinfo.FileType and $02) = 0 then
643 begin
644 mimecoder := TStringFormat_MIME64.Create;
645 Connection.LoadDatFile(FileID, tempdata);
646 Query.SQL.Text :=
647 'INSERT INTO datfiles (id,extension,name,contenttype,size,data) VALUES (' +
648 IntToStr(FileID) + ',"' + fileinfo.Extension + '","' + fileinfo.Name + '","' + IntToHex(
649 fileinfo.FileType, 8) + '",' + IntToStr(fileinfo.Size) + ',MimeToBin("' +
650 MimeCoder.StrTo(@tempdata[0], Length(tempdata)) + '") );';
651 Query.ExecSQL;
652 mimecoder.Free;
653
654 RawLinks := Connection.GetRawList(FileID);
655 if Length(RawLinks) > 0 then
656 begin
657 for i := 0 to High(RawLinks) do
658 begin
659 if RawLinks[i].RawSize > 0 then
660 begin
661 SetLength(tempdata, RawLinks[i].RawSize);
662 Connection.LoadRawFile(FileID, RawLinks[i].SrcOffset, tempdata);
663 mimecoder := TStringFormat_MIME64.Create;
664 Query.SQL.Text :=
665 'INSERT INTO rawmap (src_id,src_link_offset,sep,size,data) VALUES (' +
666 IntToStr(FileID) + ', ' + IntToStr(RawLinks[i].SrcOffset) + ',' +
667 BoolToStr(RawLinks[i].LocSep) + ', ' +
668 IntToStr(RawLinks[i].RawSize) + ', ' +
669 'MimeToBin("' + MimeCoder.StrTo(@tempdata[0], RawLinks[i].RawSize) + '") );';
670 Query.ExecSQL;
671 mimecoder.Free;
672 end
673 else
674 begin
675 Query.SQL.Text :=
676 'INSERT INTO rawmap (src_id,src_link_offset,sep,size) VALUES (' +
677 IntToStr(FileID) + ', ' + IntToStr(RawLinks[i].SrcOffset) + ', ' +
678 BoolToStr(RawLinks[i].LocSep) + ', 0);';
679 Query.ExecSQL;
680 end;
681 end;
682 end;
683
684 DatLinks := Connection.GetDatLinks(FileID);
685 if Length(DatLinks) > 0 then
686 begin
687 for i := 0 to High(DatLinks) do
688 begin
689 Query.SQL.Text :=
690 'INSERT INTO linkmap (src_id, src_link_offset, target_id) VALUES (' +
691 IntToStr(FileID) + ', ' + IntToStr(DatLinks[i].SrcOffset) + ', ' +
692 IntToStr(DatLinks[i].DestID) + ');';
693 Query.ExecSQL;
694 end;
695 end;
696 end
697 else
698 begin
699 Query.SQL.Text :=
700 'INSERT INTO datfiles (id,extension,name,contenttype,size) VALUES (' +
701 IntToStr(FileID) + ', "' + fileinfo.Extension + '", ' +
702 '"' + fileinfo.Name + '", "' + IntToHex(fileinfo.FileType, 8) + '", 0);';
703 Query.ExecSQL;
704 end;
705 if ((FileID mod 100) = 0) and (FileID > 0) then
706 begin
707 Database.Commit(False);
708 Database.StartTransaction;
709 end;
710 if ((FileID mod 10) = 0) and (FileID >= 100) then
711 lbl_estimation.Caption := 'Estimated time left: ' + TimeToStr(
712 (Time - FileTime) / FileID * (progress.Max - FileID + 1) * 1.1, timeformat );
713 progress.Position := FileID;
714 lbl_progress.Caption := 'Files done: ' + IntToStr(FileID) + '/' + IntToStr(progress.Max);
715 Application.ProcessMessages;
716 if abort then
717 begin
718 StopConvert;
719 Exit;
720 end;
721 end;
722 Database.Commit(False);
723 progress.Position := progress.Max;
724 lbl_progress.Caption := 'Files done: ' + IntToStr(progress.Max) + '/' +
725 IntToStr(progress.Max);
726
727 lbl_estimation.Caption := 'FINISHED (duration: ' + TimeToStr(Time - BeginTime, timeformat) + ')';
728
729 DoStep('FIN');
730 btn_abortok.Caption := '&OK';
731 btn_abortok.Default := True;
732
733 converting := False;
734 TAccess_OniArchive(Connection).UnloadWhenUnused := True;
735
736 Query.Close;
737 Query.Free;
738 DataBase.Close;
739 DataBase.Free;
740end;
741
742
743
744
745procedure TForm_LevelDB.btn_abortokClick(Sender: TObject);
746begin
747 if converting then
748 begin
749 if MessageBox(Self.Handle,
750 PChar('Do you really want to cancel the convert-progress?'),
751 PChar('Warning: Converting'), MB_YESNO) = idYes then
752 abort := True;
753 end
754 else
755 begin
756 Self.Visible := False;
757 Form_Main.Visible := True;
758 end;
759end;
760
761
762end.
Note: See TracBrowser for help on using the repository browser.