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

Last change on this file since 244 was 241, checked in by alloc, 17 years ago
File size: 25.2 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,
[166]27 ConnectionManager, TypeDefs, DataAccess, OniImgClass, Data, RawList,
28 Access_OniArchive;
[93]29
30var
[112]31 Converting: Boolean = False;
32 Abort: Boolean = False;
[93]33
[112]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);
[143]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 );
[93]51var
[112]52 DatHeader: THeader;
53 FilesHeader: TFilesMap;
[93]54 NamedFilesHeader: TNamedFilesMap;
55 ExtensionsHeader: TExtensionsMap;
[112]56
57 Stream_Body, Stream_Names: TMemoryStream;
[93]58 Stream_Dat, Stream_Raw, Stream_Sep: TFileStream;
59
[112]60 BeginTime, FileTime: Double;
61 Step: Integer;
62 LevelID: Integer;
63 TimeFormat: TFormatSettings;
[93]64
[112]65 ConID: Integer;
66 Connection: TDataAccess;
67 ConRepMsg: TStatusMessages;
[93]68
[120]69 FileID: Integer;
70
[112]71 Strings: TStrings;
72 i, j: Integer;
[113]73 temps: String;
[120]74 tempi: Integer;
75 tempb: Byte;
[113]76 FileInfo: TFileInfo;
[120]77 DatLinks: TDatLinkList;
78 RawLinks: TRawDataList;
[113]79
80 DatFileStream, RawFileStream: TMemoryStream;
[93]81const
[112]82 Steps: Byte = 3;
[93]83
84
[112]85 procedure DoStep(StepName: String);
[93]86 begin
[112]87 Inc(Step);
88 if StepName <> 'FIN' then
[93]89 group_progress.Caption :=
[112]90 'Creating Dat (Step ' + IntToStr(Step) + '/' + IntToStr(Steps) + ': ' + StepName + ')'
[93]91 else
92 group_progress.Caption := 'Creating Dat (FINISHED)';
93 end;
94
[138]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
[93]123begin
124
125 //
126 // FILE EXISTS CHECK FÜR DAT/RAW/SEP!!!
127 //
128
[112]129 TimeFormat.ShortTimeFormat := 'hh:nn:ss';
130 TimeFormat.LongTimeFormat := 'hh:nn:ss';
131 TimeFormat.TimeSeparator := ':';
[93]132
[112]133 ConID := ConManager.OpenConnection(Source, ConRepMsg);
134 if not (ConRepMsg in [SM_OK, SM_AlreadyOpened]) then
[93]135 begin
[112]136 ShowMessage('Source-file couldn''t be opened! Aborting' + CrLf + GetOpenMsg(ConRepMsg));
[93]137 Exit;
[112]138 end else
139 Connection := ConManager.Connection[ConID];
[93]140
[112]141 ConID := ConManager.FileOpened(Target);
142 if ConID >= 0 then
[93]143 begin
[112]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;
[93]158 end;
159
[112]160 if FileExists(Target) then
[93]161 begin
[112]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;
[131]170 end;
171 if FileExists(AnsiReplaceStr(Target, '.dat', '.raw')) then
172 if not DeleteFile(AnsiReplaceStr(Target, '.dat', '.raw')) then
[130]173 begin
174 ShowMessage('Couldn''t delete file. Aborting');
175 Exit;
176 end;
[131]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;
[112]184 end else begin
185 ShowMessage('Aborting');
186 Exit;
187 end;
[93]188 end;
[112]189
190 LevelID := Connection.LevelNumber;
191 LevelID := (LevelID * 2) * 256 * 256 * 256 + $01;
[93]192
193 Self.Visible := True;
194 Form_Main.Visible := False;
[112]195 Step := 0;
196 Converting := True;
197 Abort := False;
[93]198 btn_abortok.Caption := '&Abort...';
199 btn_abortok.Default := False;
[112]200 BeginTime := Time;
[93]201
202 Stream_Body := TMemoryStream.Create;
203 Stream_Names := TMemoryStream.Create;
[112]204 Stream_Dat := TFileStream.Create(Target, fmCreate);
205 Stream_Raw := TFileStream.Create(AnsiReplaceStr(Target, '.dat', '.raw'), fmCreate);
[154]206 Stream_Raw.Write(EmptyBytes[0], 32);
[112]207 if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
[154]208 begin
[112]209 Stream_Sep := TFileStream.Create(AnsiReplaceStr(Target, '.dat', '.sep'), fmCreate);
[154]210 Stream_Sep.Write(EmptyBytes[0], 32);
211 end;
[93]212
[154]213
[93]214 DoStep('Creating header');
215 progress.Position := 0;
216 lbl_progress.Caption := '';
217 lbl_estimation.Caption := 'Estimated finishing time: unknown';
218 Application.ProcessMessages;
219
[112]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
[113]238 for i := 0 to High(DatHeader.OSIdent) do
239 case Connection.DataOS of
240 DOS_WIN: DatHeader.OSIdent[i] := HeaderOSIdentWin[i];
[173]241 DOS_MAC, DOS_WINDEMO: DatHeader.OSIdent[i] := HeaderOSIdentMac[i];
[113]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;
[93]247 DatHeader.NamedFiles := Length(NamedFilesHeader);
[113]248
249 Strings := Connection.GetExtensionsList(EF_ExtCount);
250
251 DatHeader.Extensions := Strings.Count;
[93]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
[112]261
[93]262 DoStep('Writing extensions-header');
[113]263 progress.Max := Strings.Count;
[93]264 Application.ProcessMessages;
[113]265 for i := 0 to Strings.Count - 1 do
[93]266 begin
[113]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);
[93]273 for j := 0 to 3 do
[113]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;
[173]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;
[113]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;
[93]294 progress.Position := i + 1;
295 lbl_progress.Caption := 'Extensions done: ' + IntToStr(i + 1) + '/' +
[113]296 IntToStr(Strings.Count);
[93]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
[113]307 FileTime := Time;
[120]308 for FileID := 0 to DatHeader.Files - 1 do
[93]309 begin
[120]310 FileInfo := Connection.GetFileInfo(FileID);
[93]311 for j := 0 to 3 do
[120]312 FilesHeader[FileID].Extension[j] := FileInfo.Extension[4 - j];
[113]313 if FileInfo.Size > 0 then
[93]314 begin
[120]315 FilesHeader[FileID].DataAddr := Stream_Body.Size + 8;
[113]316 DatFileStream := TMemoryStream.Create;
[120]317 Connection.LoadDatFile(FileID, TStream(DatFileStream));
[133]318 DatFileStream.Seek(0, soFromBeginning);
[137]319 tempi := FileID * 256 + 1;
320 DatFileStream.Write(tempi, 4);
[113]321 DatFileStream.Write(LevelID, 4);
[93]322
[120]323 DatLinks := Connection.GetDatLinks(FileID);
324 if Length(DatLinks) > 0 then
[93]325 begin
[120]326 for i := 0 to High(DatLinks) do
[93]327 begin
[120]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
[93]343 begin
[120]344 RawFileStream := TMemoryStream.Create;
[127]345 Connection.LoadRawFile(FileID, RawLinks[i].SrcOffset, TStream(RawFileStream));
346 RawFileStream.Seek(0, soFromBeginning);
347 if RawLinks[i].LocSep then
[120]348 begin
[127]349 RawLinks[i].RawAddr := Stream_Sep.Size;
350 Stream_sep.CopyFrom(RawFileStream, RawFileStream.Size);
[143]351 if (Stream_Sep.Size mod 32) > 0 then
352 Stream_Sep.Write(EmptyBytes[0], 32 - (Stream_Sep.Size mod 32));
[120]353 end else begin
[127]354 RawLinks[i].RawAddr := Stream_Raw.Size;
355 Stream_Raw.CopyFrom(RawFileStream, RawFileStream.Size);
[143]356 if (Stream_Raw.Size mod 32) > 0 then
357 Stream_Raw.Write(EmptyBytes[0], 32 - (Stream_Raw.Size mod 32));
[120]358 end;
359 end else
360 RawLinks[i].RawAddr := 0;
[129]361 DatFileStream.Seek(RawLinks[i].SrcOffset, soFromBeginning);
362 DatFileStream.Write(RawLinks[i].RawAddr, 4);
[120]363 end;
364 end;
[129]365 DatFileStream.Seek(0, soFromBeginning);
366 Stream_Body.CopyFrom(DatFileStream, DatFileStream.Size);
[143]367 if (Stream_Body.Size mod 32) > 0 then
[150]368 begin
369 ShowMessage(
370 IntToStr(FileID) + '-' + FileInfo.Name + '.' + FileInfo.Extension + #13#10 +
[152]371 IntToStr(FileInfo.Size) + ' - 0x' + IntToHex(FileInfo.Size, 6) + ' - real: ' + IntToStr(DatFileStream.Size) + ' - 0x' + IntToHex(DatFileStream.Size, 6) + #13#10 +
[151]372 IntToStr(Stream_Body.Size) + ' - 0x' + IntToHex(Stream_Body.Size, 6) );
[143]373 Stream_Body.Write(EmptyBytes[0], 32 - (Stream_Body.Size mod 32));
[150]374 end;
[93]375 end
376 else
[134]377 FilesHeader[FileID].DataAddr := 0;
[93]378 if Length(fileinfo.Name) > 0 then
379 begin
[134]380 FilesHeader[FileID].NameAddr := Stream_Names.Size;
[93]381 temps := fileinfo.Extension + fileinfo.Name + Chr(0);
382 Stream_Names.Write(temps[1], Length(temps));
383 end
384 else
[134]385 FilesHeader[FileID].NameAddr := 0;
386 FilesHeader[FileID].FileSize := fileinfo.Size;
387 FilesHeader[FileID].FileType := fileinfo.FileType;
[93]388
[130]389 if ((FileID mod 10) = 0) and (FileID >= 100) then
[93]390 lbl_estimation.Caption := 'Estimated time left: ' + TimeToStr(
[130]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);
[93]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
[143]405 if (Stream_Dat.Size mod 32) > 0 then
406 Stream_Dat.Write(EmptyBytes[0], 32 - (Stream_Dat.Size mod 32));
407
[93]408 DatHeader.DataSize := Stream_Body.Size;
409 DatHeader.NamesSize := Stream_Names.Size;
410 DatHeader.DataAddr := Stream_Dat.Size;
[133]411
[93]412 Stream_Body.Seek(0, soFromBeginning);
413 Stream_Dat.CopyFrom(Stream_Body, Stream_Body.Size);
[143]414
415 if (Stream_Dat.Size mod 32) > 0 then
416 Stream_Dat.Write(EmptyBytes[0], 32 - (Stream_Dat.Size mod 32));
417
[93]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;
[129]429
430 if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
[93]431 Stream_Sep.Free;
432
433 progress.Position := progress.Max;
434 lbl_progress.Caption := 'Files done: ' + IntToStr(progress.Max) + '/' +
435 IntToStr(progress.Max);
[113]436 lbl_estimation.Caption := 'FINISHED (duration: ' + TimeToStr(Time - Begintime, TimeFormat) + ')';
[93]437
438 DoStep('FIN');
439 btn_abortok.Caption := '&OK';
440 btn_abortok.Default := True;
441
442 converting := False;
443
[129]444// CloseDataConnection(DataConnections[conIndex]);
[93]445end;
446
447
448
449
450procedure TForm_LevelDB.CreateDatabase(Source, target: String);
[134]451var
452 DataBase: TABSDatabase;
[138]453 Query: TABSQuery;
454 MimeCoder: TStringFormat_MIME64;
[112]455
[138]456 BeginTime, FileTime: Double;
457 Step: Integer;
458 TimeFormat: TFormatSettings;
[93]459
[138]460 ConID: Integer;
461 Connection: TDataAccess;
462 ConRepMsg: TStatusMessages;
[93]463
[138]464 FileID: Integer;
[93]465
[138]466 i: Integer;
467 temps: String;
468 tempdata: TByteData;
469 FileInfo: TFileInfo;
470 DatLinks: TDatLinkList;
471 RawLinks: TRawDataList;
472const
473 steps: Byte = 2;
[93]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
[138]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;
[173]498 end;
[138]499
500
501
[93]502begin
[138]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
[93]514 begin
[138]515 ShowMessage('Source-file couldn''t be opened! Aborting' + CrLf + GetOpenMsg(ConRepMsg));
[93]516 Exit;
[138]517 end else
518 Connection := ConManager.Connection[ConID];
519
520 ConID := ConManager.FileOpened(Target);
521 if ConID >= 0 then
[93]522 begin
[138]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;
[93]537 end;
538
[138]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;
[93]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
[138]564 BeginTime := Time;
[93]565
566 DataBase := TABSDatabase.Create(Self);
[134]567 DataBase.MaxConnections := 1;
[136]568 DataBase.PageSize := 8112;
569 DataBase.PageCountInExtent := 8;
[138]570
[93]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 :=
[136]584 'CREATE TABLE globals ( id AUTOINC PRIMARY KEY, name STRING(128), ' +
585 'value STRING(128) );';
[93]586 Query.ExecSQL;
587 Query.SQL.Text :=
[136]588 'CREATE TABLE linkmap ( id AUTOINC PRIMARY KEY, src_id INTEGER, ' +
[141]589 'src_link_offset INTEGER, target_id INTEGER);';
[93]590 Query.ExecSQL;
[141]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;
[93]595 Query.SQL.Text :=
[241]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, ' +
[141]598 'data BLOB BlobCompressionMode 9 BlobBlockSize 1024 BlobCompressionAlgorithm ZLib);';
[93]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;
[141]601 Query.SQL.Text := 'CREATE INDEX idsrcid ON rawmap (src_id);';
602 Query.ExecSQL;
[241]603 Query.SQL.Text := 'CREATE INDEX idtype ON rawmap (type);';
604 Query.ExecSQL;
[93]605 Query.SQL.Text :=
[136]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 );';
[93]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;
[136]611// Query.SQL.Text :=
612// 'CREATE TABLE extlist ( id AUTOINC PRIMARY KEY, ext CHAR(4), ident CHAR(16) );';
613// Query.ExecSQL;
[93]614
615 Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("dbversion","' +
616 dbversion + '");';
617 Query.ExecSQL;
[138]618
[93]619 Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("lvl","' +
[138]620 IntToStr(Connection.LevelNumber) + '");';
[93]621 Query.ExecSQL;
[138]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 + '");';
[93]629 Query.ExecSQL;
630
631 progress.Position := 0;
632 lbl_progress.Caption := 'Files done: ' + IntToStr(0) + '/' + IntToStr(
[138]633 Connection.GetFileCount);
[93]634 lbl_estimation.Caption := 'Estimated finishing time: unknown';
635
[138]636 progress.Max := Connection.GetFileCount;
[93]637 begintime := Time;
638 DoStep('Writing .dat-fileslist');
639 Application.ProcessMessages;
640
[166]641 TAccess_OniArchive(Connection).UnloadWhenUnused := False;
642
[142]643 FileTime := Time;
[93]644 Database.StartTransaction;
[138]645 for FileID := 0 to Connection.GetFileCount - 1 do
[93]646 begin
[138]647 fileinfo := Connection.GetFileInfo(FileID);
[93]648 if (fileinfo.FileType and $02) = 0 then
649 begin
650 mimecoder := TStringFormat_MIME64.Create;
[138]651 Connection.LoadDatFile(FileID, tempdata);
[93]652 Query.SQL.Text :=
653 'INSERT INTO datfiles (id,extension,name,contenttype,size,data) VALUES (' +
[138]654 IntToStr(FileID) + ',"' + fileinfo.Extension + '","' + fileinfo.Name + '","' + IntToHex(
[93]655 fileinfo.FileType, 8) + '",' + IntToStr(fileinfo.Size) + ',MimeToBin("' +
[138]656 MimeCoder.StrTo(@tempdata[0], Length(tempdata)) + '") );';
[93]657 Query.ExecSQL;
658 mimecoder.Free;
659
[138]660 RawLinks := Connection.GetRawList(FileID);
661 if Length(RawLinks) > 0 then
[93]662 begin
[138]663 for i := 0 to High(RawLinks) do
[93]664 begin
[138]665 if RawLinks[i].RawSize > 0 then
[93]666 begin
[138]667 SetLength(tempdata, RawLinks[i].RawSize);
668 Connection.LoadRawFile(FileID, RawLinks[i].SrcOffset, tempdata);
[93]669 mimecoder := TStringFormat_MIME64.Create;
670 Query.SQL.Text :=
[241]671 'INSERT INTO rawmap (name,src_id,src_link_offset,sep,type,size,data) VALUES (' +
672 '"' + RawLinks[i].Name + '", ' +
[138]673 IntToStr(FileID) + ', ' + IntToStr(RawLinks[i].SrcOffset) + ',' +
674 BoolToStr(RawLinks[i].LocSep) + ', ' +
[241]675 '"' + RawLinks[i].RawType + '", ' +
[138]676 IntToStr(RawLinks[i].RawSize) + ', ' +
677 'MimeToBin("' + MimeCoder.StrTo(@tempdata[0], RawLinks[i].RawSize) + '") );';
[93]678 Query.ExecSQL;
679 mimecoder.Free;
680 end
681 else
682 begin
683 Query.SQL.Text :=
[241]684 'INSERT INTO rawmap (name,src_id,src_link_offset,sep,type,size) VALUES (' +
685 '"' + RawLinks[i].Name + '", ' +
[138]686 IntToStr(FileID) + ', ' + IntToStr(RawLinks[i].SrcOffset) + ', ' +
[241]687 BoolToStr(RawLinks[i].LocSep) + ', ' +
688 '"' + RawLinks[i].RawType + '", ' +
689 '0);';
[93]690 Query.ExecSQL;
691 end;
692 end;
693 end;
694
[138]695 DatLinks := Connection.GetDatLinks(FileID);
696 if Length(DatLinks) > 0 then
697 begin
698 for i := 0 to High(DatLinks) do
699 begin
700 Query.SQL.Text :=
701 'INSERT INTO linkmap (src_id, src_link_offset, target_id) VALUES (' +
702 IntToStr(FileID) + ', ' + IntToStr(DatLinks[i].SrcOffset) + ', ' +
703 IntToStr(DatLinks[i].DestID) + ');';
704 Query.ExecSQL;
705 end;
706 end;
[93]707 end
708 else
709 begin
710 Query.SQL.Text :=
711 'INSERT INTO datfiles (id,extension,name,contenttype,size) VALUES (' +
[138]712 IntToStr(FileID) + ', "' + fileinfo.Extension + '", ' +
713 '"' + fileinfo.Name + '", "' + IntToHex(fileinfo.FileType, 8) + '", 0);';
[93]714 Query.ExecSQL;
715 end;
[138]716 if ((FileID mod 100) = 0) and (FileID > 0) then
[93]717 begin
718 Database.Commit(False);
719 Database.StartTransaction;
720 end;
[138]721 if ((FileID mod 10) = 0) and (FileID >= 100) then
[93]722 lbl_estimation.Caption := 'Estimated time left: ' + TimeToStr(
[142]723 (Time - FileTime) / FileID * (progress.Max - FileID + 1) * 1.1, timeformat );
[138]724 progress.Position := FileID;
725 lbl_progress.Caption := 'Files done: ' + IntToStr(FileID) + '/' + IntToStr(progress.Max);
[93]726 Application.ProcessMessages;
727 if abort then
728 begin
[138]729 StopConvert;
[93]730 Exit;
731 end;
732 end;
733 Database.Commit(False);
734 progress.Position := progress.Max;
735 lbl_progress.Caption := 'Files done: ' + IntToStr(progress.Max) + '/' +
736 IntToStr(progress.Max);
737
[138]738 lbl_estimation.Caption := 'FINISHED (duration: ' + TimeToStr(Time - BeginTime, timeformat) + ')';
[93]739
740 DoStep('FIN');
741 btn_abortok.Caption := '&OK';
742 btn_abortok.Default := True;
743
744 converting := False;
[167]745 TAccess_OniArchive(Connection).UnloadWhenUnused := True;
[93]746
[138]747 Query.Close;
748 Query.Free;
749 DataBase.Close;
750 DataBase.Free;
[93]751end;
752
753
754
755
756procedure TForm_LevelDB.btn_abortokClick(Sender: TObject);
757begin
758 if converting then
759 begin
760 if MessageBox(Self.Handle,
761 PChar('Do you really want to cancel the convert-progress?'),
762 PChar('Warning: Converting'), MB_YESNO) = idYes then
763 abort := True;
764 end
765 else
766 begin
767 Self.Visible := False;
768 Form_Main.Visible := True;
769 end;
770end;
771
772
[138]773end.
Note: See TracBrowser for help on using the repository browser.