1 | unit LevelDB;
|
---|
2 | interface
|
---|
3 | uses
|
---|
4 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
5 | Dialogs, ComCtrls, StdCtrls, StrUtils;
|
---|
6 |
|
---|
7 | type
|
---|
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 |
|
---|
21 | var
|
---|
22 | Form_LevelDB: TForm_LevelDB;
|
---|
23 |
|
---|
24 | implementation
|
---|
25 | {$R *.dfm}
|
---|
26 | uses ABSMain, ABSDecUtil, Main,
|
---|
27 | ConnectionManager, TypeDefs, DataAccess, OniImgClass, Data, RawList;
|
---|
28 |
|
---|
29 | var
|
---|
30 | Converting: Boolean = False;
|
---|
31 | Abort: Boolean = False;
|
---|
32 |
|
---|
33 |
|
---|
34 | function GetOpenMsg(msg: TStatusMessages): String;
|
---|
35 | begin
|
---|
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;
|
---|
43 | end;
|
---|
44 |
|
---|
45 |
|
---|
46 | procedure TForm_LevelDB.CreateLevel(Source, Target: String);
|
---|
47 | const
|
---|
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 );
|
---|
50 | var
|
---|
51 | DatHeader: THeader;
|
---|
52 | FilesHeader: TFilesMap;
|
---|
53 | NamedFilesHeader: TNamedFilesMap;
|
---|
54 | ExtensionsHeader: TExtensionsMap;
|
---|
55 |
|
---|
56 | Stream_Body, Stream_Names: TMemoryStream;
|
---|
57 | Stream_Dat, Stream_Raw, Stream_Sep: TFileStream;
|
---|
58 |
|
---|
59 | BeginTime, FileTime: Double;
|
---|
60 | Step: Integer;
|
---|
61 | LevelID: Integer;
|
---|
62 | TimeFormat: TFormatSettings;
|
---|
63 |
|
---|
64 | ConID: Integer;
|
---|
65 | Connection: TDataAccess;
|
---|
66 | ConRepMsg: TStatusMessages;
|
---|
67 |
|
---|
68 | FileID: Integer;
|
---|
69 |
|
---|
70 | Strings: TStrings;
|
---|
71 | i, j: Integer;
|
---|
72 | temps: String;
|
---|
73 | tempi: Integer;
|
---|
74 | tempb: Byte;
|
---|
75 | FileInfo: TFileInfo;
|
---|
76 | DatLinks: TDatLinkList;
|
---|
77 | RawLinks: TRawDataList;
|
---|
78 |
|
---|
79 | DatFileStream, RawFileStream: TMemoryStream;
|
---|
80 | const
|
---|
81 | Steps: Byte = 3;
|
---|
82 |
|
---|
83 |
|
---|
84 | procedure DoStep(StepName: String);
|
---|
85 | begin
|
---|
86 | Inc(Step);
|
---|
87 | if StepName <> 'FIN' then
|
---|
88 | group_progress.Caption :=
|
---|
89 | 'Creating Dat (Step ' + IntToStr(Step) + '/' + IntToStr(Steps) + ': ' + StepName + ')'
|
---|
90 | else
|
---|
91 | group_progress.Caption := 'Creating Dat (FINISHED)';
|
---|
92 | end;
|
---|
93 |
|
---|
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 |
|
---|
122 | begin
|
---|
123 |
|
---|
124 | //
|
---|
125 | // FILE EXISTS CHECK FÜR DAT/RAW/SEP!!!
|
---|
126 | //
|
---|
127 |
|
---|
128 | TimeFormat.ShortTimeFormat := 'hh:nn:ss';
|
---|
129 | TimeFormat.LongTimeFormat := 'hh:nn:ss';
|
---|
130 | TimeFormat.TimeSeparator := ':';
|
---|
131 |
|
---|
132 | ConID := ConManager.OpenConnection(Source, ConRepMsg);
|
---|
133 | if not (ConRepMsg in [SM_OK, SM_AlreadyOpened]) then
|
---|
134 | begin
|
---|
135 | ShowMessage('Source-file couldn''t be opened! Aborting' + CrLf + GetOpenMsg(ConRepMsg));
|
---|
136 | Exit;
|
---|
137 | end else
|
---|
138 | Connection := ConManager.Connection[ConID];
|
---|
139 |
|
---|
140 | ConID := ConManager.FileOpened(Target);
|
---|
141 | if ConID >= 0 then
|
---|
142 | begin
|
---|
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;
|
---|
157 | end;
|
---|
158 |
|
---|
159 | if FileExists(Target) then
|
---|
160 | begin
|
---|
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;
|
---|
169 | end;
|
---|
170 | if FileExists(AnsiReplaceStr(Target, '.dat', '.raw')) then
|
---|
171 | if not DeleteFile(AnsiReplaceStr(Target, '.dat', '.raw')) then
|
---|
172 | begin
|
---|
173 | ShowMessage('Couldn''t delete file. Aborting');
|
---|
174 | Exit;
|
---|
175 | end;
|
---|
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;
|
---|
183 | end else begin
|
---|
184 | ShowMessage('Aborting');
|
---|
185 | Exit;
|
---|
186 | end;
|
---|
187 | end;
|
---|
188 |
|
---|
189 | LevelID := Connection.LevelNumber;
|
---|
190 | LevelID := (LevelID * 2) * 256 * 256 * 256 + $01;
|
---|
191 |
|
---|
192 | Self.Visible := True;
|
---|
193 | Form_Main.Visible := False;
|
---|
194 | Step := 0;
|
---|
195 | Converting := True;
|
---|
196 | Abort := False;
|
---|
197 | btn_abortok.Caption := '&Abort...';
|
---|
198 | btn_abortok.Default := False;
|
---|
199 | BeginTime := Time;
|
---|
200 |
|
---|
201 | Stream_Body := TMemoryStream.Create;
|
---|
202 | Stream_Names := TMemoryStream.Create;
|
---|
203 | Stream_Dat := TFileStream.Create(Target, fmCreate);
|
---|
204 | Stream_Raw := TFileStream.Create(AnsiReplaceStr(Target, '.dat', '.raw'), fmCreate);
|
---|
205 | Stream_Raw.Write(EmptyBytes[0], 32);
|
---|
206 | if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
|
---|
207 | begin
|
---|
208 | Stream_Sep := TFileStream.Create(AnsiReplaceStr(Target, '.dat', '.sep'), fmCreate);
|
---|
209 | Stream_Sep.Write(EmptyBytes[0], 32);
|
---|
210 | end;
|
---|
211 |
|
---|
212 |
|
---|
213 | DoStep('Creating header');
|
---|
214 | progress.Position := 0;
|
---|
215 | lbl_progress.Caption := '';
|
---|
216 | lbl_estimation.Caption := 'Estimated finishing time: unknown';
|
---|
217 | Application.ProcessMessages;
|
---|
218 |
|
---|
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 |
|
---|
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;
|
---|
246 | DatHeader.NamedFiles := Length(NamedFilesHeader);
|
---|
247 |
|
---|
248 | Strings := Connection.GetExtensionsList(EF_ExtCount);
|
---|
249 |
|
---|
250 | DatHeader.Extensions := Strings.Count;
|
---|
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 |
|
---|
260 |
|
---|
261 | DoStep('Writing extensions-header');
|
---|
262 | progress.Max := Strings.Count;
|
---|
263 | Application.ProcessMessages;
|
---|
264 | for i := 0 to Strings.Count - 1 do
|
---|
265 | begin
|
---|
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);
|
---|
272 | for j := 0 to 3 do
|
---|
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;
|
---|
289 | progress.Position := i + 1;
|
---|
290 | lbl_progress.Caption := 'Extensions done: ' + IntToStr(i + 1) + '/' +
|
---|
291 | IntToStr(Strings.Count);
|
---|
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 |
|
---|
302 | FileTime := Time;
|
---|
303 | for FileID := 0 to DatHeader.Files - 1 do
|
---|
304 | begin
|
---|
305 | FileInfo := Connection.GetFileInfo(FileID);
|
---|
306 | for j := 0 to 3 do
|
---|
307 | FilesHeader[FileID].Extension[j] := FileInfo.Extension[4 - j];
|
---|
308 | if FileInfo.Size > 0 then
|
---|
309 | begin
|
---|
310 | FilesHeader[FileID].DataAddr := Stream_Body.Size + 8;
|
---|
311 | DatFileStream := TMemoryStream.Create;
|
---|
312 | Connection.LoadDatFile(FileID, TStream(DatFileStream));
|
---|
313 | DatFileStream.Seek(0, soFromBeginning);
|
---|
314 | tempi := FileID * 256 + 1;
|
---|
315 | DatFileStream.Write(tempi, 4);
|
---|
316 | DatFileStream.Write(LevelID, 4);
|
---|
317 |
|
---|
318 | DatLinks := Connection.GetDatLinks(FileID);
|
---|
319 | if Length(DatLinks) > 0 then
|
---|
320 | begin
|
---|
321 | for i := 0 to High(DatLinks) do
|
---|
322 | begin
|
---|
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
|
---|
338 | begin
|
---|
339 | RawFileStream := TMemoryStream.Create;
|
---|
340 | Connection.LoadRawFile(FileID, RawLinks[i].SrcOffset, TStream(RawFileStream));
|
---|
341 | RawFileStream.Seek(0, soFromBeginning);
|
---|
342 | if RawLinks[i].LocSep then
|
---|
343 | begin
|
---|
344 | RawLinks[i].RawAddr := Stream_Sep.Size;
|
---|
345 | Stream_sep.CopyFrom(RawFileStream, RawFileStream.Size);
|
---|
346 | if (Stream_Sep.Size mod 32) > 0 then
|
---|
347 | Stream_Sep.Write(EmptyBytes[0], 32 - (Stream_Sep.Size mod 32));
|
---|
348 | end else begin
|
---|
349 | RawLinks[i].RawAddr := Stream_Raw.Size;
|
---|
350 | Stream_Raw.CopyFrom(RawFileStream, RawFileStream.Size);
|
---|
351 | if (Stream_Raw.Size mod 32) > 0 then
|
---|
352 | Stream_Raw.Write(EmptyBytes[0], 32 - (Stream_Raw.Size mod 32));
|
---|
353 | end;
|
---|
354 | end else
|
---|
355 | RawLinks[i].RawAddr := 0;
|
---|
356 | DatFileStream.Seek(RawLinks[i].SrcOffset, soFromBeginning);
|
---|
357 | DatFileStream.Write(RawLinks[i].RawAddr, 4);
|
---|
358 | end;
|
---|
359 | end;
|
---|
360 | DatFileStream.Seek(0, soFromBeginning);
|
---|
361 | Stream_Body.CopyFrom(DatFileStream, DatFileStream.Size);
|
---|
362 | if (Stream_Body.Size mod 32) > 0 then
|
---|
363 | begin
|
---|
364 | ShowMessage(
|
---|
365 | IntToStr(FileID) + '-' + FileInfo.Name + '.' + FileInfo.Extension + #13#10 +
|
---|
366 | IntToStr(FileInfo.Size) + ' - 0x' + IntToHex(FileInfo.Size, 6) + ' - real: ' + IntToStr(DatFileStream.Size) + ' - 0x' + IntToHex(DatFileStream.Size, 6) + #13#10 +
|
---|
367 | IntToStr(Stream_Body.Size) + ' - 0x' + IntToHex(Stream_Body.Size, 6) );
|
---|
368 | Stream_Body.Write(EmptyBytes[0], 32 - (Stream_Body.Size mod 32));
|
---|
369 | end;
|
---|
370 | end
|
---|
371 | else
|
---|
372 | FilesHeader[FileID].DataAddr := 0;
|
---|
373 | if Length(fileinfo.Name) > 0 then
|
---|
374 | begin
|
---|
375 | FilesHeader[FileID].NameAddr := Stream_Names.Size;
|
---|
376 | temps := fileinfo.Extension + fileinfo.Name + Chr(0);
|
---|
377 | Stream_Names.Write(temps[1], Length(temps));
|
---|
378 | end
|
---|
379 | else
|
---|
380 | FilesHeader[FileID].NameAddr := 0;
|
---|
381 | FilesHeader[FileID].FileSize := fileinfo.Size;
|
---|
382 | FilesHeader[FileID].FileType := fileinfo.FileType;
|
---|
383 |
|
---|
384 | if ((FileID mod 10) = 0) and (FileID >= 100) then
|
---|
385 | lbl_estimation.Caption := 'Estimated time left: ' + TimeToStr(
|
---|
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);
|
---|
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 |
|
---|
400 | if (Stream_Dat.Size mod 32) > 0 then
|
---|
401 | Stream_Dat.Write(EmptyBytes[0], 32 - (Stream_Dat.Size mod 32));
|
---|
402 |
|
---|
403 | DatHeader.DataSize := Stream_Body.Size;
|
---|
404 | DatHeader.NamesSize := Stream_Names.Size;
|
---|
405 | DatHeader.DataAddr := Stream_Dat.Size;
|
---|
406 |
|
---|
407 | Stream_Body.Seek(0, soFromBeginning);
|
---|
408 | Stream_Dat.CopyFrom(Stream_Body, Stream_Body.Size);
|
---|
409 |
|
---|
410 | if (Stream_Dat.Size mod 32) > 0 then
|
---|
411 | Stream_Dat.Write(EmptyBytes[0], 32 - (Stream_Dat.Size mod 32));
|
---|
412 |
|
---|
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;
|
---|
424 |
|
---|
425 | if Connection.DataOS in [DOS_WINDEMO, DOS_MAC, DOS_MACBETA] then
|
---|
426 | Stream_Sep.Free;
|
---|
427 |
|
---|
428 | progress.Position := progress.Max;
|
---|
429 | lbl_progress.Caption := 'Files done: ' + IntToStr(progress.Max) + '/' +
|
---|
430 | IntToStr(progress.Max);
|
---|
431 | lbl_estimation.Caption := 'FINISHED (duration: ' + TimeToStr(Time - Begintime, TimeFormat) + ')';
|
---|
432 |
|
---|
433 | DoStep('FIN');
|
---|
434 | btn_abortok.Caption := '&OK';
|
---|
435 | btn_abortok.Default := True;
|
---|
436 |
|
---|
437 | converting := False;
|
---|
438 |
|
---|
439 | // CloseDataConnection(DataConnections[conIndex]);
|
---|
440 | end;
|
---|
441 |
|
---|
442 |
|
---|
443 |
|
---|
444 |
|
---|
445 | procedure TForm_LevelDB.CreateDatabase(Source, target: String);
|
---|
446 | var
|
---|
447 | DataBase: TABSDatabase;
|
---|
448 | Query: TABSQuery;
|
---|
449 | MimeCoder: TStringFormat_MIME64;
|
---|
450 |
|
---|
451 | BeginTime, FileTime: Double;
|
---|
452 | Step: Integer;
|
---|
453 | TimeFormat: TFormatSettings;
|
---|
454 |
|
---|
455 | ConID: Integer;
|
---|
456 | Connection: TDataAccess;
|
---|
457 | ConRepMsg: TStatusMessages;
|
---|
458 |
|
---|
459 | FileID: Integer;
|
---|
460 |
|
---|
461 | i: Integer;
|
---|
462 | temps: String;
|
---|
463 | tempdata: TByteData;
|
---|
464 | FileInfo: TFileInfo;
|
---|
465 | DatLinks: TDatLinkList;
|
---|
466 | RawLinks: TRawDataList;
|
---|
467 | const
|
---|
468 | steps: Byte = 2;
|
---|
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 |
|
---|
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;
|
---|
493 | end;
|
---|
494 |
|
---|
495 |
|
---|
496 |
|
---|
497 | begin
|
---|
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
|
---|
509 | begin
|
---|
510 | ShowMessage('Source-file couldn''t be opened! Aborting' + CrLf + GetOpenMsg(ConRepMsg));
|
---|
511 | Exit;
|
---|
512 | end else
|
---|
513 | Connection := ConManager.Connection[ConID];
|
---|
514 |
|
---|
515 | ConID := ConManager.FileOpened(Target);
|
---|
516 | if ConID >= 0 then
|
---|
517 | begin
|
---|
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;
|
---|
532 | end;
|
---|
533 |
|
---|
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;
|
---|
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 |
|
---|
559 | BeginTime := Time;
|
---|
560 |
|
---|
561 | DataBase := TABSDatabase.Create(Self);
|
---|
562 | DataBase.MaxConnections := 1;
|
---|
563 | DataBase.PageSize := 8112;
|
---|
564 | DataBase.PageCountInExtent := 8;
|
---|
565 |
|
---|
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 :=
|
---|
579 | 'CREATE TABLE globals ( id AUTOINC PRIMARY KEY, name STRING(128), ' +
|
---|
580 | 'value STRING(128) );';
|
---|
581 | Query.ExecSQL;
|
---|
582 | Query.SQL.Text :=
|
---|
583 | 'CREATE TABLE linkmap ( id AUTOINC PRIMARY KEY, src_id INTEGER, ' +
|
---|
584 | 'src_link_offset INTEGER, target_id INTEGER);';
|
---|
585 | Query.ExecSQL;
|
---|
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;
|
---|
590 | Query.SQL.Text :=
|
---|
591 | 'CREATE TABLE rawmap ( id AUTOINC PRIMARY KEY, src_id INTEGER, ' +
|
---|
592 | 'src_link_offset INTEGER, sep BOOLEAN, size INTEGER, ' +
|
---|
593 | 'data BLOB BlobCompressionMode 9 BlobBlockSize 1024 BlobCompressionAlgorithm ZLib);';
|
---|
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;
|
---|
596 | Query.SQL.Text := 'CREATE INDEX idsrcid ON rawmap (src_id);';
|
---|
597 | Query.ExecSQL;
|
---|
598 | Query.SQL.Text :=
|
---|
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 );';
|
---|
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;
|
---|
604 | // Query.SQL.Text :=
|
---|
605 | // 'CREATE TABLE extlist ( id AUTOINC PRIMARY KEY, ext CHAR(4), ident CHAR(16) );';
|
---|
606 | // Query.ExecSQL;
|
---|
607 |
|
---|
608 | Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("dbversion","' +
|
---|
609 | dbversion + '");';
|
---|
610 | Query.ExecSQL;
|
---|
611 |
|
---|
612 | Query.SQL.Text := 'INSERT INTO globals (name,value) VALUES ("lvl","' +
|
---|
613 | IntToStr(Connection.LevelNumber) + '");';
|
---|
614 | Query.ExecSQL;
|
---|
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 + '");';
|
---|
622 | Query.ExecSQL;
|
---|
623 |
|
---|
624 | progress.Position := 0;
|
---|
625 | lbl_progress.Caption := 'Files done: ' + IntToStr(0) + '/' + IntToStr(
|
---|
626 | Connection.GetFileCount);
|
---|
627 | lbl_estimation.Caption := 'Estimated finishing time: unknown';
|
---|
628 |
|
---|
629 | progress.Max := Connection.GetFileCount;
|
---|
630 | begintime := Time;
|
---|
631 | DoStep('Writing .dat-fileslist');
|
---|
632 | Application.ProcessMessages;
|
---|
633 |
|
---|
634 | FileTime := Time;
|
---|
635 | Database.StartTransaction;
|
---|
636 | for FileID := 0 to Connection.GetFileCount - 1 do
|
---|
637 | begin
|
---|
638 | fileinfo := Connection.GetFileInfo(FileID);
|
---|
639 | if (fileinfo.FileType and $02) = 0 then
|
---|
640 | begin
|
---|
641 | mimecoder := TStringFormat_MIME64.Create;
|
---|
642 | Connection.LoadDatFile(FileID, tempdata);
|
---|
643 | Query.SQL.Text :=
|
---|
644 | 'INSERT INTO datfiles (id,extension,name,contenttype,size,data) VALUES (' +
|
---|
645 | IntToStr(FileID) + ',"' + fileinfo.Extension + '","' + fileinfo.Name + '","' + IntToHex(
|
---|
646 | fileinfo.FileType, 8) + '",' + IntToStr(fileinfo.Size) + ',MimeToBin("' +
|
---|
647 | MimeCoder.StrTo(@tempdata[0], Length(tempdata)) + '") );';
|
---|
648 | Query.ExecSQL;
|
---|
649 | mimecoder.Free;
|
---|
650 |
|
---|
651 | RawLinks := Connection.GetRawList(FileID);
|
---|
652 | if Length(RawLinks) > 0 then
|
---|
653 | begin
|
---|
654 | for i := 0 to High(RawLinks) do
|
---|
655 | begin
|
---|
656 | if RawLinks[i].RawSize > 0 then
|
---|
657 | begin
|
---|
658 | SetLength(tempdata, RawLinks[i].RawSize);
|
---|
659 | Connection.LoadRawFile(FileID, RawLinks[i].SrcOffset, tempdata);
|
---|
660 | mimecoder := TStringFormat_MIME64.Create;
|
---|
661 | Query.SQL.Text :=
|
---|
662 | 'INSERT INTO rawmap (src_id,src_link_offset,sep,size,data) VALUES (' +
|
---|
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) + '") );';
|
---|
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 (' +
|
---|
674 | IntToStr(FileID) + ', ' + IntToStr(RawLinks[i].SrcOffset) + ', ' +
|
---|
675 | BoolToStr(RawLinks[i].LocSep) + ', 0);';
|
---|
676 | Query.ExecSQL;
|
---|
677 | end;
|
---|
678 | end;
|
---|
679 | end;
|
---|
680 |
|
---|
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;
|
---|
693 | end
|
---|
694 | else
|
---|
695 | begin
|
---|
696 | Query.SQL.Text :=
|
---|
697 | 'INSERT INTO datfiles (id,extension,name,contenttype,size) VALUES (' +
|
---|
698 | IntToStr(FileID) + ', "' + fileinfo.Extension + '", ' +
|
---|
699 | '"' + fileinfo.Name + '", "' + IntToHex(fileinfo.FileType, 8) + '", 0);';
|
---|
700 | Query.ExecSQL;
|
---|
701 | end;
|
---|
702 | if ((FileID mod 100) = 0) and (FileID > 0) then
|
---|
703 | begin
|
---|
704 | Database.Commit(False);
|
---|
705 | Database.StartTransaction;
|
---|
706 | end;
|
---|
707 | if ((FileID mod 10) = 0) and (FileID >= 100) then
|
---|
708 | lbl_estimation.Caption := 'Estimated time left: ' + TimeToStr(
|
---|
709 | (Time - FileTime) / FileID * (progress.Max - FileID + 1) * 1.1, timeformat );
|
---|
710 | progress.Position := FileID;
|
---|
711 | lbl_progress.Caption := 'Files done: ' + IntToStr(FileID) + '/' + IntToStr(progress.Max);
|
---|
712 | Application.ProcessMessages;
|
---|
713 | if abort then
|
---|
714 | begin
|
---|
715 | StopConvert;
|
---|
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 |
|
---|
724 | lbl_estimation.Caption := 'FINISHED (duration: ' + TimeToStr(Time - BeginTime, timeformat) + ')';
|
---|
725 |
|
---|
726 | DoStep('FIN');
|
---|
727 | btn_abortok.Caption := '&OK';
|
---|
728 | btn_abortok.Default := True;
|
---|
729 |
|
---|
730 | converting := False;
|
---|
731 |
|
---|
732 | Query.Close;
|
---|
733 | Query.Free;
|
---|
734 | DataBase.Close;
|
---|
735 | DataBase.Free;
|
---|
736 | end;
|
---|
737 |
|
---|
738 |
|
---|
739 |
|
---|
740 |
|
---|
741 | procedure TForm_LevelDB.btn_abortokClick(Sender: TObject);
|
---|
742 | begin
|
---|
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;
|
---|
755 | end;
|
---|
756 |
|
---|
757 |
|
---|
758 | end.
|
---|