source: oup/releases/0.19a/Unit2_functions.pas@ 1016

Last change on this file since 1016 was 21, checked in by alloc, 18 years ago
File size: 10.7 KB
Line 
1UNIT Unit2_functions;
2INTERFACE
3USES Classes, Dialogs, SysUtils, StrUtils, Math, SQLiteTable3,
4 Unit3_data, Unit4_Exporters;
5
6FUNCTION GetFilesList(ext:String; pattern:String):TStringList;
7FUNCTION Decode_Int(buffer:Tdata):LongWord;
8FUNCTION Encode_Int(input:LongWord):Tdata;
9FUNCTION Decode_Float(buffer:Tdata):Single;
10FUNCTION Encode_Float(input:Single):Tdata;
11FUNCTION LoadDatInfos(filename:String):Boolean;
12FUNCTION LoadDatFile(fileid:LongWord):Tdata;
13PROCEDURE SaveDatFile(fileid:LongWord; data:Tdata);
14FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
15FUNCTION LoadRawFilePart(address,size:LongWord; target:Pointer):Boolean;
16FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
17FUNCTION FormatFileSize(size:LongWord):String;
18FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
19FUNCTION ExportFile(fileid:LongWord; convert:Boolean):Integer;
20FUNCTION GetWinFileName(fileid:LongWord; substring:String):String;
21FUNCTION GetExtractPath:String;
22PROCEDURE OpenDatabase(FileName:String);
23
24
25IMPLEMENTATION
26
27TYPE
28 TValueSwitcher=Record
29 CASE IsFloat: Boolean OF
30 True: (ValueFloat:Single);
31 False: (ValueInt:LongWord);
32 END;
33
34
35FUNCTION Decode_Int(buffer:Tdata):LongWord;
36 BEGIN
37 Result:=buffer[0]+buffer[1]*256+buffer[2]*256*256+buffer[3]*256*256*256;
38 END;
39FUNCTION Encode_Int(input:LongWord):Tdata;
40 BEGIN
41 Result[0]:=input MOD 256;
42 input:=input DIV 256;
43 Result[1]:=input MOD 256;
44 input:=input DIV 256;
45 Result[2]:=input MOD 256;
46 input:=input DIV 256;
47 Result[3]:=input MOD 256;
48 END;
49FUNCTION Decode_Float(buffer:Tdata):Single;
50 VAR _valueswitcher:TValueSwitcher;
51 BEGIN
52 _valueswitcher.ValueInt:=Decode_Int(buffer);
53 Result:=_valueswitcher.ValueFloat;
54 END;
55FUNCTION Encode_Float(input:Single):Tdata;
56 VAR _valueswitcher:TValueSwitcher;
57 BEGIN
58 _valueswitcher.ValueFloat:=input;
59 Result:=Encode_Int(_valueswitcher.ValueInt);
60 END;
61
62
63FUNCTION GetFilesList(ext:String; pattern:String):TStringList;
64 VAR
65 i:LongWord;
66 BEGIN
67 SetLength(Result,0);
68 IF opened_state=opened_dat THEN BEGIN
69 FOR i:=0 TO dat_header.Files-1 DO BEGIN
70 IF (Length(ext)=0 OR dat_files[i].Extension=ext) AND
71 (Length(pattern)=0 OR Pos(pattern,dat_files[i].FileName)>0) THEN BEGIN
72 SetLength(Result,Length(Result)+1);
73 Result[High(Result)]:=dat_files[i].FileName;
74 END;
75 END;
76 END ELSE BEGIN
77 END;
78 END;
79
80FUNCTION LoadDatInfos(filename:String):Boolean;
81 VAR i:LongWord;
82 dat_file:TFileStream;
83 BEGIN
84 Result:=True;
85 opened_state:=opened_dat;
86 dat_filename:=filename;
87 raw_filename:=MidStr(filename,1,Length(filename)-3)+'raw';
88 dat_file:=TFileStream.Create(filename, fmOpenRead);
89 dat_file.Read(dat_header,SizeOf(dat_header));
90 FOR i:=0 TO High(dat_header.Ident) DO
91 IF dat_header.Ident[i]<>header_ident1[i] THEN BEGIN
92 Result:=False;
93 Exit;
94 END;
95{ FOR i:=0 TO High(dat_header.Ident2) DO
96 IF dat_header.Ident2[i]<>header_ident2[i] THEN BEGIN
97 Result:=False;
98 Exit;
99 END;
100}
101 SetLength(dat_filesmap,dat_header.Files);
102 SetLength(dat_files,dat_header.Files);
103 FOR i:=0 TO dat_header.Files-1 DO dat_file.Read(dat_filesmap[i],SizeOf(dat_filesmap[i]));
104 FOR i:=0 TO dat_header.Files-1 DO BEGIN
105 dat_files[i].Extension:=dat_filesmap[i].Extension;
106 dat_files[i].Extension:=ReverseString(dat_files[i].Extension);
107 dat_files[i].Size:=dat_filesmap[i].FileSize;
108 dat_files[i].FileType:=dat_filesmap[i].FileType;
109 dat_files[i].DatAddr:=dat_filesmap[i].DataAddr-8+dat_header.DataAddr;
110 IF (dat_filesmap[i].FileType AND $01)=0 THEN BEGIN
111 dat_file.Seek(dat_filesmap[i].NameAddr+dat_header.NamesAddr,soFromBeginning);
112 SetLength(dat_files[i].Name,100);
113 dat_file.Read(dat_files[i].Name[1],100);
114 dat_files[i].Name:=MidStr(dat_files[i].Name,1+4,Pos(#0,dat_files[i].Name)-1-4);
115 END ELSE BEGIN
116 dat_files[i].Name:='';
117 END;
118 dat_files[i].FileName:=FormatNumber(i,5,'0')+'-'+dat_files[i].Name+'.'+dat_files[i].Extension;
119 END;
120 dat_file.Seek($40+dat_header.Files*$14,soFromBeginning);
121 SetLength(dat_namedfilesmap,dat_header.NamedFiles);
122 FOR i:=0 TO dat_header.NamedFiles-1 DO dat_file.Read(dat_namedfilesmap[i],SizeOf(dat_namedfilesmap[i]));
123
124 dat_file.Seek($40+dat_header.Files*$14+dat_header.NamedFiles*$8,soFromBeginning);
125 SetLength(dat_extensionsmap,dat_header.Extensions);
126 FOR i:=0 TO dat_header.Extensions-1 DO dat_file.Read(dat_extensionsmap[i],SizeOf(dat_extensionsmap[i]));
127
128 dat_file.Free;
129 END;
130
131
132FUNCTION LoadDatFile(fileid:LongWord):Tdata;
133 VAR dat_file:TFileStream;
134 BEGIN
135 IF opened_state=opened_dat THEN BEGIN
136 dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
137 dat_file.Seek(dat_files[fileid].DatAddr,soFromBeginning);
138 SetLength(Result,dat_files[fileid].Size);
139 dat_file.Read(Result[0],dat_files[fileid].Size);
140 dat_file.Free;
141 END;
142 END;
143
144
145PROCEDURE SaveDatFile(fileid:LongWord; data:Tdata);
146 VAR dat_file:TFileStream;
147 BEGIN
148 IF opened_state=opened_dat THEN BEGIN
149 dat_file:=TFileStream.Create(dat_filename, fmOpenReadWrite);
150 dat_file.Seek(dat_files[fileid].DatAddr,soFromBeginning);
151 dat_file.Write(data[0],Length(data));
152 dat_file.Free;
153 END;
154 END;
155
156
157FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
158 VAR dat_file:TFileStream;
159 BEGIN
160 IF opened_state=opened_dat THEN BEGIN
161 dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
162 Result:=True;
163 dat_file.Seek(dat_files[fileid].DatAddr+offset,soFromBeginning);
164 dat_file.Read(target^,size);
165 dat_file.Free;
166 END;
167 END;
168
169
170FUNCTION LoadRawFilePart(address,size:LongWord; target:Pointer):Boolean;
171 VAR
172 filestream:TFileStream;
173 BEGIN
174 IF opened_state=opened_dat THEN BEGIN
175 Result:=True;
176 filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.raw'),fmOpenRead);
177 filestream.Seek(address,soFromBeginning);
178 filestream.Read(target^,size);
179 filestream.Free;
180 END;
181 END;
182
183
184FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
185 BEGIN
186 Result:=AnsiReplaceStr(Format('%'+IntToStr(width)+'u',[value]),' ',leadingzeros);
187 END;
188
189
190FUNCTION FormatFileSize(size:LongWord):String;
191 BEGIN
192 IF size>=1000*1024*1024 THEN BEGIN
193 Result:=FloatToStrF(size/1024/1024/1024,ffFixed,5,1)+' GB';
194 END ELSE BEGIN
195 IF size>=1000*1024 THEN BEGIN
196 Result:=FloatToStrF(size/1024/1024,ffFixed,5,1)+' MB';
197 END ELSE BEGIN
198 IF size>=1000 THEN BEGIN
199 Result:=FloatToStrF(size/1024,ffFixed,5,1)+' KB';
200 END ELSE BEGIN
201 Result:=IntToStr(size)+' B';
202 END;
203 END;
204 END;
205 END;
206
207
208FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
209 VAR
210 string_build,ascii_version:String;
211 i:LongWord;
212 BEGIN
213 string_build:='';
214 ascii_version:='';
215 FOR i:=0 TO High(data) DO BEGIN
216 IF NOT HexOnly THEN
217 IF (i MOD 16)=0 THEN
218 string_build:=string_build+'0x'+IntToHex(i,6)+' ';
219 string_build:=string_build+IntToHex(data[i],2);
220 IF NOT HexOnly THEN BEGIN
221 IF data[i]>=32 THEN ascii_version:=ascii_version+Chr(data[i])
222 ELSE ascii_version:=ascii_version+'.';
223 IF ((i+1) MOD 2)=0 THEN string_build:=string_build+#32;
224 IF ((i+1) MOD 16)=0 THEN BEGIN
225 string_build:=string_build+#32+ascii_version+CrLf;
226 ascii_version:='';
227 END;
228 END;
229 END;
230 Result:=string_build;
231 END;
232
233
234FUNCTION ExportFile(fileid:LongWord; convert:Boolean):Integer;
235 VAR
236 i:Byte;
237 BEGIN
238 IF opened_state=opened_dat THEN BEGIN
239 Result:=export_noerror;
240 //ExportDefFileHeader(fileid);
241 IF (dat_files[fileid].FileType AND $02)=0 THEN BEGIN
242 //ExportDatFile(fileid);
243 FOR i:=1 TO Length(ExportHandlers)+1 DO BEGIN
244 IF i<=Length(ExportHandlers) THEN BEGIN
245 IF ExportHandlers[i].Ext=dat_files[fileid].Extension THEN BEGIN
246 IF ExportHandlers[i].needed THEN BEGIN
247 CASE ExportHandlers[i].Handler(fileid,convert) OF
248 0: Result:=0;
249 ELSE
250 Result:=export_handlererror;
251 END;
252 END;
253 Break;
254 END;
255 END ELSE BEGIN
256 Result:=export_nohandler;
257 END;
258 END;
259 END;
260 END;
261 END;
262
263
264FUNCTION GetWinFileName(fileid:LongWord; substring:String):String;
265 VAR
266 name:String;
267 BEGIN
268 name:=dat_files[fileid].Name;
269 name:=AnsiReplaceStr(name,'\','__');
270 name:=AnsiReplaceStr(name,'/','__');
271 name:=AnsiReplaceStr(name,'>','__');
272 name:=AnsiReplaceStr(name,'<','__');
273 Result:=FormatNumber(fileid,5,'0')+'-'+name+'.'+substring+'.'+dat_files[fileid].Extension;
274 END;
275
276FUNCTION GetExtractPath:String;
277 BEGIN
278 Result:=ExtractFilePath(dat_filename)+'\extracted_'+ExtractFileName(dat_filename);
279 END;
280
281
282PROCEDURE OpenDatabase(FileName:String);
283 VAR
284 i:Byte;
285 data:Tdata;
286 temps:String;
287 Tbl: TSQLiteTable;
288 BEGIN
289 IF NOT FileExists(FileName) THEN BEGIN
290 ShowMessage('File doesn''t exist!!!');
291 Exit;
292 END;
293 DB:=TSQLiteDatabase.Create(FileName);
294 Tbl:=DB.GetTable('SELECT name,value FROM globals ORDER BY name ASC');
295 REPEAT
296 IF Tbl.FieldAsString('name')='dbversion' THEN BEGIN
297 IF Tbl.FieldAsString('value')<>DBversion THEN BEGIN
298 ShowMessage('Database-file '+CrLf+'"'+FileName+'"'+CrLf+'has wrong version. (Required: '+DBversion+'; found: '+Tbl.FieldAsString('value')+')');
299 Exit;
300 END;
301 END;
302 IF Tbl.FieldAsString('name')='lvl' THEN BEGIN
303 database_level:=StrToInt(Tbl.FieldAsString('value'));
304 END;
305 IF Tbl.FieldAsString('name')='ident' THEN BEGIN
306 temps:=Tbl.FieldAsString('value');
307 FOR i:=0 TO High(database_ident) DO BEGIN
308 CASE temps[(i*2)+1+0] OF
309 '0'..'9': database_ident[i]:=Ord(temps[(i*2)+1+0])-48;
310 'A'..'F': database_ident[i]:=Ord(temps[(i*2)+1+0])-55;
311 END;
312 database_ident[i]:=database_ident[i]*16;
313 CASE temps[(i*2)+1+1] OF
314 '0'..'9': database_ident[i]:=database_ident[i]+Ord(temps[(i*2)+1+0])-48;
315 'A'..'F': database_ident[i]:=database_ident[i]+Ord(temps[(i*2)+1+0])-55;
316 END;
317 END;
318 END;
319 Tbl.Next;
320 UNTIL Tbl.EOF;
321 Tbl.Free;
322 opened_state:=opened_db;
323 END;
324
325
326
327
328END.
Note: See TracBrowser for help on using the repository browser.