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

Last change on this file since 1085 was 248, checked in by alloc, 17 years ago
File size: 25.0 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, DOS_WINDEMO: 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:
283 if FileTypes[j].Extension = 'SNDD' then
284 ExtensionsHeader[i].Ident := FileTypes[j].IdentWin
285 else
286 ExtensionsHeader[i].Ident := FileTypes[j].IdentMac;
287 DOS_MAC: ExtensionsHeader[i].Ident := FileTypes[j].IdentMac;
288 DOS_MACBETA: ExtensionsHeader[i].Ident := FileTypes[j].IdentMac;
289 end;
290 end else begin
291 ShowMessage('Unknown Extension: ' + Strings.Strings[i]);
292 Exit;
293 end;
294 progress.Position := i + 1;
295 lbl_progress.Caption := 'Extensions done: ' + IntToStr(i + 1) + '/' +
296 IntToStr(Strings.Count);
297 Application.ProcessMessages;
298 end;
299
300 DoStep('Storing files-data');
301 progress.Position := 0;
302 progress.Max := DatHeader.Files;
303 lbl_progress.Caption := '';
304 lbl_estimation.Caption := 'Estimated finishing time: unknown';
305 Application.ProcessMessages;
306
307 FileTime := Time;
308 for FileID := 0 to DatHeader.Files - 1 do
309 begin
310 FileInfo := Connection.GetFileInfo(FileID);
311 for j := 0 to 3 do
312 FilesHeader[FileID].Extension[j] := FileInfo.Extension[4 - j];
313 if FileInfo.Size > 0 then
314 begin
315 FilesHeader[FileID].DataAddr := Stream_Body.Size + 8;
316 DatFileStream := TMemoryStream.Create;
317 Connection.LoadDatFile(FileID, TStream(DatFileStream));
318 DatFileStream.Seek(0, soFromBeginning);
319 tempi := FileID * 256 + 1;
320 DatFileStream.Write(tempi, 4);
321 DatFileStream.Write(LevelID, 4);
322
323 DatLinks := Connection.GetDatLinks(FileID);
324 if Length(DatLinks) > 0 then
325 begin
326 for i := 0 to High(DatLinks) do
327 begin
328 DatFileStream.Seek(DatLinks[i].SrcOffset, soFromBeginning);
329 if DatLinks[i].DestID < 0 then
330 tempi := 0
331 else
332 tempi := DatLinks[i].DestID * 256 + 1;
333 DatFileStream.Write(tempi, 4);
334 end;
335 end;
336
337 RawLinks := Connection.GetRawList(FileID);
338 if Length(RawLinks) > 0 then
339 begin
340 for i := 0 to High(RawLinks) do
341 begin
342 if RawLinks[i].RawSize > 0 then
343 begin
344 RawFileStream := TMemoryStream.Create;
345 Connection.LoadRawFile(FileID, RawLinks[i].SrcOffset, TStream(RawFileStream));
346 RawFileStream.Seek(0, soFromBeginning);
347 if RawLinks[i].LocSep then
348 begin
349 RawLinks[i].RawAddr := Stream_Sep.Size;
350 Stream_sep.CopyFrom(RawFileStream, RawFileStream.Size);
351 if (Stream_Sep.Size mod 32) > 0 then
352 Stream_Sep.Write(EmptyBytes[0], 32 - (Stream_Sep.Size mod 32));
353 end else begin
354 RawLinks[i].RawAddr := Stream_Raw.Size;
355 Stream_Raw.CopyFrom(RawFileStream, RawFileStream.Size);
356 if (Stream_Raw.Size mod 32) > 0 then
357 Stream_Raw.Write(EmptyBytes[0], 32 - (Stream_Raw.Size mod 32));
358 end;
359 end else
360 RawLinks[i].RawAddr := 0;
361 DatFileStream.Seek(RawLinks[i].SrcOffset, soFromBeginning);
362 DatFileStream.Write(RawLinks[i].RawAddr, 4);
363 end;
364 end;
365 DatFileStream.Seek(0, soFromBeginning);
366 Stream_Body.CopyFrom(DatFileStream, DatFileStream.Size);
367 if (Stream_Body.Size mod 32) > 0 then
368 begin
369 ShowMessage(
370 IntToStr(FileID) + '-' + FileInfo.Name + '.' + FileInfo.Extension + #13#10 +
371 IntToStr(FileInfo.Size) + ' - 0x' + IntToHex(FileInfo.Size, 6) + ' - real: ' + IntToStr(DatFileStream.Size) + ' - 0x' + IntToHex(DatFileStream.Size, 6) + #13#10 +
372 IntToStr(Stream_Body.Size) + ' - 0x' + IntToHex(Stream_Body.Size, 6) );
373 Stream_Body.Write(EmptyBytes[0], 32 - (Stream_Body.Size mod 32));
374 end;
375 end
376 else
377 FilesHeader[FileID].DataAddr := 0;
378 if Length(fileinfo.Name) > 0 then
379 begin
380 FilesHeader[FileID].NameAddr := Stream_Names.Size;
381 temps := fileinfo.Extension + fileinfo.Name + Chr(0);
382 Stream_Names.Write(temps[1], Length(temps));
383 end
384 else
385 FilesHeader[FileID].NameAddr := 0;
386 FilesHeader[FileID].FileSize := fileinfo.Size;
387 FilesHeader[FileID].FileType := fileinfo.FileType;
388
389 if ((FileID mod 10) = 0) and (FileID >= 100) then
390 lbl_estimation.Caption := 'Estimated time left: ' + TimeToStr(
391 (Time - FileTime) / FileID * (progress.Max - FileID + 1) * 1.1, TimeFormat );
392 progress.Position := FileID + 1;
393 lbl_progress.Caption := 'Files done: ' + IntToStr(FileID + 1) + '/' + IntToStr(progress.Max);
394 Application.ProcessMessages;
395 end;
396
397 Stream_Dat.Write(DatHeader, SizeOf(DatHeader));
398 for i := 0 to High(FilesHeader) do
399 Stream_Dat.Write(FilesHeader[i], SizeOf(FilesHeader[i]));
400 for i := 0 to High(NamedFilesHeader) do
401 Stream_Dat.Write(NamedFilesHeader[i], SizeOf(NamedFilesHeader[i]));
402 for i := 0 to High(ExtensionsHeader) do
403 Stream_Dat.Write(ExtensionsHeader[i], SizeOf(ExtensionsHeader[i]));
404
405 if (Stream_Dat.Size mod 32) > 0 then
406 Stream_Dat.Write(EmptyBytes[0], 32 - (Stream_Dat.Size mod 32));
407
408 DatHeader.DataSize := Stream_Body.Size;
409 DatHeader.NamesSize := Stream_Names.Size;
410 DatHeader.DataAddr := Stream_Dat.Size;
411
412 Stream_Body.Seek(0, soFromBeginning);
413 Stream_Dat.CopyFrom(Stream_Body, Stream_Body.Size);
414
415 if (Stream_Dat.Size mod 32) > 0 then
416 Stream_Dat.Write(EmptyBytes[0], 32 - (Stream_Dat.Size mod 32));
417
418 DatHeader.NamesAddr := Stream_Dat.Size;
419 Stream_Names.Seek(0, soFromBeginning);
420 Stream_Dat.CopyFrom(Stream_Names, Stream_Names.Size);
421
422 Stream_Dat.Seek(0, soFromBeginning);
423 Stream_Dat.Write(DatHeader, SizeOf(DatHeader));
424
425 Stream_Dat.Free;
426 Stream_Body.Free;
427 Stream_Names.Free;
428 Stream_Raw.Free;
429
430 if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
431 Stream_Sep.Free;
432
433 progress.Position := progress.Max;
434 lbl_progress.Caption := 'Files done: ' + IntToStr(progress.Max) + '/' +
435 IntToStr(progress.Max);
436 lbl_estimation.Caption := 'FINISHED (duration: ' + TimeToStr(Time - Begintime, TimeFormat) + ')';
437
438 DoStep('FIN');
439 btn_abortok.Caption := '&OK';
440 btn_abortok.Default := True;
441
442 converting := False;
443
444// CloseDataConnection(DataConnections[conIndex]);
445end;
446
447
448
449
450procedure TForm_LevelDB.CreateDatabase(Source, target: String);
451var
452 DataBase: TABSDatabase;
453 Query: TABSQuery;
454 MimeCoder: TStringFormat_MIME64;
455
456 BeginTime, FileTime: Double;
457 Step: Integer;
458 TimeFormat: TFormatSettings;
459
460 ConID: Integer;
461 Connection: TDataAccess;
462 ConRepMsg: TStatusMessages;
463
464 FileID: Integer;
465
466 i: Integer;
467 temps: String;
468 tempdata: TByteData;
469 FileInfo: TFileInfo;
470 DatLinks: TDatLinkList;
471 RawLinks: TRawDataList;
472const
473 steps: Byte = 2;
474
475 procedure DoStep(stepname: String);
476 begin
477 Inc(step);
478 if stepname <> 'FIN' then
479 group_progress.Caption :=
480 'Creating DB (Step ' + IntToStr(step) + '/' + IntToStr(steps) + ': ' + stepname + ')'
481 else
482 group_progress.Caption := 'Creating DB (FINISHED)';
483 end;
484
485 procedure StopConvert;
486 begin
487 btn_abortok.Caption := '&Close';
488 btn_abortok.Default := True;
489 converting := False;
490 lbl_estimation.Caption := 'ABORTED';
491 group_progress.Caption := 'Creating DB (ABORTED)';
492 DataBase.Close;
493 if MessageBox(Self.Handle, PChar('Delete the unfinished DB-file?'),
494 PChar('Delete file?'), MB_YESNO) = idYes then
495 begin
496 DeleteFile(target);
497 end;
498 end;
499
500
501
502begin
503
504 //
505 // FILE EXISTS CHECK FÜR DAT/RAW/SEP!!!
506 //
507
508 TimeFormat.ShortTimeFormat := 'hh:nn:ss';
509 TimeFormat.LongTimeFormat := 'hh:nn:ss';
510 TimeFormat.TimeSeparator := ':';
511
512 ConID := ConManager.OpenConnection(Source, ConRepMsg);
513 if not (ConRepMsg in [SM_OK, SM_AlreadyOpened]) then
514 begin
515 ShowMessage('Source-file couldn''t be opened! Aborting' + CrLf + GetOpenMsg(ConRepMsg));
516 Exit;
517 end else
518 Connection := ConManager.Connection[ConID];
519
520 ConID := ConManager.FileOpened(Target);
521 if ConID >= 0 then
522 begin
523 if MessageBox(Self.Handle, PChar('Destination-file is opened, close it in ' +
524 'order to proceed conversion?'), PChar('Destination-file opened'),
525 MB_YESNO + MB_ICONQUESTION) = ID_YES then
526 begin
527 if Form_Main.CheckConnectionCloseable(ConID) then
528 if not ConManager.CloseConnection(ConID, ConRepMsg) then
529 begin
530 ShowMessage('Couldn''t close destination-file. Aborting');
531 Exit;
532 end;
533 end else begin
534 ShowMessage('Aborting');
535 Exit;
536 end;
537 end;
538
539 if FileExists(Target) then
540 begin
541 if MessageBox(Self.Handle, PChar('Destination-file exists. ' +
542 'Overwrite it?'), PChar('Destination-file exists'),
543 MB_YESNO + MB_ICONWARNING) = ID_YES then
544 begin
545 if not DeleteFile(Target) then
546 begin
547 ShowMessage('Couldn''t delete file. Aborting');
548 Exit;
549 end;
550 end else begin
551 ShowMessage('Aborting');
552 Exit;
553 end;
554 end;
555
556 Self.Visible := True;
557 Form_Main.Visible := False;
558 step := 0;
559 converting := True;
560 abort := False;
561 btn_abortok.Caption := '&Abort...';
562 btn_abortok.Default := False;
563
564 BeginTime := Time;
565
566 DataBase := TABSDatabase.Create(Self);
567 DataBase.MaxConnections := 1;
568 DataBase.PageSize := 8112;
569 DataBase.PageCountInExtent := 8;
570
571 DataBase.DatabaseName := 'OLDB';
572 DataBase.DatabaseFileName := target;
573 DataBase.CreateDatabase;
574
575 DoStep('Creating tables');
576 progress.Position := 0;
577 lbl_progress.Caption := '';
578 lbl_estimation.Caption := 'Estimated finishing time: unknown';
579 Application.ProcessMessages;
580
581 Query := TABSQuery.Create(Self);
582 Query.DatabaseName := 'OLDB';
583 Query.SQL.Text :=
584 'CREATE TABLE globals ( id AUTOINC PRIMARY KEY, name STRING(128), ' +
585 'value STRING(128) );';
586 Query.ExecSQL;
587 Query.SQL.Text :=
588 'CREATE TABLE linkmap ( id AUTOINC PRIMARY KEY, src_id INTEGER, ' +
589 'src_link_offset INTEGER, target_id INTEGER);';
590 Query.ExecSQL;
591 Query.SQL.Text := 'CREATE INDEX idsrcid ON linkmap (src_id);';
592 Query.ExecSQL;
593 Query.SQL.Text := 'CREATE INDEX idtargetid ON linkmap (target_id);';
594 Query.ExecSQL;
595 Query.SQL.Text :=
596 'CREATE TABLE rawmap ( id AUTOINC PRIMARY KEY, name STRING(32), src_id INTEGER, ' +
597 'src_link_offset INTEGER, sep BOOLEAN, type STRING(8), size INTEGER, ' +
598 'data BLOB BlobCompressionMode 9 BlobBlockSize 1024 BlobCompressionAlgorithm ZLib);';
599 // Query.SQL.Text:='CREATE TABLE rawmap ( id AUTOINC PRIMARY KEY, src_id INTEGER, src_link_offset INTEGER, size INTEGER, data BLOB BlobCompressionAlgorithm None );';
600 Query.ExecSQL;
601 Query.SQL.Text := 'CREATE INDEX idsrcid ON rawmap (src_id);';
602 Query.ExecSQL;
603 Query.SQL.Text := 'CREATE INDEX idtype ON rawmap (type);';
604 Query.ExecSQL;
605 Query.SQL.Text :=
606 'CREATE TABLE datfiles ( id INTEGER PRIMARY KEY, extension CHAR(4), ' +
607 'name STRING(128), contenttype INTEGER, size INTEGER, ' +
608 'data BLOB BlobCompressionMode 9 BlobBlockSize 1024 BlobCompressionAlgorithm ZLib );';
609 // Query.SQL.Text:='CREATE TABLE datfiles ( id INTEGER PRIMARY KEY, extension CHAR(4), name STRING(128), contenttype INTEGER, size INTEGER, data BLOB BlobCompressionAlgorithm None );';
610 Query.ExecSQL;
611// Query.SQL.Text :=
612// 'CREATE TABLE extlist ( id AUTOINC PRIMARY KEY, ext CHAR(4), ident CHAR(16) );';
613// Query.ExecSQL;
614
615 Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("dbversion","' +
616 dbversion + '");';
617 Query.ExecSQL;
618
619 Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("lvl","' +
620 IntToStr(Connection.LevelNumber) + '");';
621 Query.ExecSQL;
622 case Connection.DataOS of
623 DOS_WIN: temps := 'WIN';
624 DOS_WINDEMO: temps := 'WINDEMO';
625 DOS_MAC: temps := 'MAC';
626 DOS_MACBETA: temps := 'MACBETA';
627 end;
628 Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("os","' + temps + '");';
629 Query.ExecSQL;
630
631 progress.Position := 0;
632 lbl_progress.Caption := 'Files done: ' + IntToStr(0) + '/' + IntToStr(
633 Connection.GetFileCount);
634 lbl_estimation.Caption := 'Estimated finishing time: unknown';
635
636 progress.Max := Connection.GetFileCount;
637 begintime := Time;
638 DoStep('Writing .dat-fileslist');
639 Application.ProcessMessages;
640
641 FileTime := Time;
642 Database.StartTransaction;
643 for FileID := 0 to Connection.GetFileCount - 1 do
644 begin
645 fileinfo := Connection.GetFileInfo(FileID);
646 if (fileinfo.FileType and $02) = 0 then
647 begin
648 mimecoder := TStringFormat_MIME64.Create;
649 Connection.LoadDatFile(FileID, tempdata);
650 Query.SQL.Text :=
651 'INSERT INTO datfiles (id,extension,name,contenttype,size,data) VALUES (' +
652 IntToStr(FileID) + ',"' + fileinfo.Extension + '","' + fileinfo.Name + '","' + IntToHex(
653 fileinfo.FileType, 8) + '",' + IntToStr(fileinfo.Size) + ',MimeToBin("' +
654 MimeCoder.StrTo(@tempdata[0], Length(tempdata)) + '") );';
655 Query.ExecSQL;
656 mimecoder.Free;
657
658 RawLinks := Connection.GetRawList(FileID);
659 if Length(RawLinks) > 0 then
660 begin
661 for i := 0 to High(RawLinks) do
662 begin
663 if RawLinks[i].RawSize > 0 then
664 begin
665 SetLength(tempdata, RawLinks[i].RawSize);
666 Connection.LoadRawFile(FileID, RawLinks[i].SrcOffset, tempdata);
667 mimecoder := TStringFormat_MIME64.Create;
668 Query.SQL.Text :=
669 'INSERT INTO rawmap (name,src_id,src_link_offset,sep,type,size,data) VALUES (' +
670 '"' + RawLinks[i].Name + '", ' +
671 IntToStr(FileID) + ', ' + IntToStr(RawLinks[i].SrcOffset) + ',' +
672 BoolToStr(RawLinks[i].LocSep) + ', ' +
673 '"' + RawLinks[i].RawType + '", ' +
674 IntToStr(RawLinks[i].RawSize) + ', ' +
675 'MimeToBin("' + MimeCoder.StrTo(@tempdata[0], RawLinks[i].RawSize) + '") );';
676 Query.ExecSQL;
677 mimecoder.Free;
678 end
679 else
680 begin
681 Query.SQL.Text :=
682 'INSERT INTO rawmap (name,src_id,src_link_offset,sep,type,size) VALUES (' +
683 '"' + RawLinks[i].Name + '", ' +
684 IntToStr(FileID) + ', ' + IntToStr(RawLinks[i].SrcOffset) + ', ' +
685 BoolToStr(RawLinks[i].LocSep) + ', ' +
686 '"' + RawLinks[i].RawType + '", ' +
687 '0);';
688 Query.ExecSQL;
689 end;
690 end;
691 end;
692
693 DatLinks := Connection.GetDatLinks(FileID);
694 if Length(DatLinks) > 0 then
695 begin
696 for i := 0 to High(DatLinks) do
697 begin
698 Query.SQL.Text :=
699 'INSERT INTO linkmap (src_id, src_link_offset, target_id) VALUES (' +
700 IntToStr(FileID) + ', ' + IntToStr(DatLinks[i].SrcOffset) + ', ' +
701 IntToStr(DatLinks[i].DestID) + ');';
702 Query.ExecSQL;
703 end;
704 end;
705 end
706 else
707 begin
708 Query.SQL.Text :=
709 'INSERT INTO datfiles (id,extension,name,contenttype,size) VALUES (' +
710 IntToStr(FileID) + ', "' + fileinfo.Extension + '", ' +
711 '"' + fileinfo.Name + '", "' + IntToHex(fileinfo.FileType, 8) + '", 0);';
712 Query.ExecSQL;
713 end;
714 if ((FileID mod 100) = 0) and (FileID > 0) then
715 begin
716 Database.Commit(False);
717 Database.StartTransaction;
718 end;
719 if ((FileID mod 10) = 0) and (FileID >= 100) then
720 lbl_estimation.Caption := 'Estimated time left: ' + TimeToStr(
721 (Time - FileTime) / FileID * (progress.Max - FileID + 1) * 1.1, timeformat );
722 progress.Position := FileID;
723 lbl_progress.Caption := 'Files done: ' + IntToStr(FileID) + '/' + IntToStr(progress.Max);
724 Application.ProcessMessages;
725 if abort then
726 begin
727 StopConvert;
728 Exit;
729 end;
730 end;
731 Database.Commit(False);
732 progress.Position := progress.Max;
733 lbl_progress.Caption := 'Files done: ' + IntToStr(progress.Max) + '/' +
734 IntToStr(progress.Max);
735
736 lbl_estimation.Caption := 'FINISHED (duration: ' + TimeToStr(Time - BeginTime, timeformat) + ')';
737
738 DoStep('FIN');
739 btn_abortok.Caption := '&OK';
740 btn_abortok.Default := True;
741
742 converting := False;
743
744 Query.Close;
745 Query.Free;
746 DataBase.Close;
747 DataBase.Free;
748end;
749
750
751
752
753procedure TForm_LevelDB.btn_abortokClick(Sender: TObject);
754begin
755 if converting then
756 begin
757 if MessageBox(Self.Handle,
758 PChar('Do you really want to cancel the convert-progress?'),
759 PChar('Warning: Converting'), MB_YESNO) = idYes then
760 abort := True;
761 end
762 else
763 begin
764 Self.Visible := False;
765 Form_Main.Visible := True;
766 end;
767end;
768
769
770end.
Note: See TracBrowser for help on using the repository browser.