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

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