source: oup/releases/0.16a/Unit2_functions.pas@ 455

Last change on this file since 455 was 21, checked in by alloc, 18 years ago
File size: 7.1 KB
Line 
1UNIT Unit2_functions;
2INTERFACE
3USES Classes, SysUtils, StrUtils, Math, SQLiteTable3, Unit3_data, Unit4_Exporters;
4
5FUNCTION LoadDatInfos(filename:String):Boolean;
6FUNCTION LoadDatFile(fileid:LongWord):Tdata;
7PROCEDURE SaveDatFile(fileid:LongWord; data:Tdata);
8FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
9FUNCTION LoadRawFilePart(address,size:LongWord; target:Pointer):Boolean;
10FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
11FUNCTION FormatFileSize(size:LongWord):String;
12FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
13FUNCTION ExportFile(fileid:LongWord; convert:Boolean):Integer;
14FUNCTION GetWinFileName(fileid:LongWord; substring:String):String;
15FUNCTION GetExtractPath:String;
16
17
18
19IMPLEMENTATION
20
21
22FUNCTION LoadDatInfos(filename:String):Boolean;
23 VAR i:LongWord;
24 dat_file:TFileStream;
25 BEGIN
26 Result:=True;
27 dat_filename:=filename;
28 raw_filename:=MidStr(filename,1,Length(filename)-3)+'raw';
29 dat_file:=TFileStream.Create(filename, fmOpenRead);
30 dat_file.Read(dat_header,SizeOf(dat_header));
31 FOR i:=0 TO High(dat_header.Ident) DO
32 IF dat_header.Ident[i]<>header_ident1[i] THEN BEGIN
33 Result:=False;
34 Exit;
35 END;
36{ FOR i:=0 TO High(dat_header.Ident2) DO
37 IF dat_header.Ident2[i]<>header_ident2[i] THEN BEGIN
38 Result:=False;
39 Exit;
40 END;
41}
42 SetLength(dat_filesmap,dat_header.Files);
43 SetLength(dat_files,dat_header.Files);
44 FOR i:=0 TO dat_header.Files-1 DO dat_file.Read(dat_filesmap[i],SizeOf(dat_filesmap[i]));
45 FOR i:=0 TO dat_header.Files-1 DO BEGIN
46 dat_files[i].Extension:=dat_filesmap[i].Extension;
47 dat_files[i].Extension:=ReverseString(dat_files[i].Extension);
48 dat_files[i].Size:=dat_filesmap[i].FileSize;
49 dat_files[i].FileType:=dat_filesmap[i].FileType;
50 dat_files[i].DatAddr:=dat_filesmap[i].DataAddr-8+dat_header.DataAddr;
51 IF (dat_filesmap[i].FileType AND $01)=0 THEN BEGIN
52 dat_file.Seek(dat_filesmap[i].NameAddr+dat_header.NamesAddr,soFromBeginning);
53 SetLength(dat_files[i].Name,100);
54 dat_file.Read(dat_files[i].Name[1],100);
55 dat_files[i].Name:=MidStr(dat_files[i].Name,1+4,Pos(#0,dat_files[i].Name)-1-4);
56 END ELSE BEGIN
57 dat_files[i].Name:='';
58 END;
59 dat_files[i].FileName:=FormatNumber(i,5,'0')+'-'+dat_files[i].Name+'.'+dat_files[i].Extension;
60 END;
61 dat_file.Seek($40+dat_header.Files*$14,soFromBeginning);
62 SetLength(dat_namedfilesmap,dat_header.NamedFiles);
63 FOR i:=0 TO dat_header.NamedFiles-1 DO dat_file.Read(dat_namedfilesmap[i],SizeOf(dat_namedfilesmap[i]));
64
65 dat_file.Seek($40+dat_header.Files*$14+dat_header.NamedFiles*$8,soFromBeginning);
66 SetLength(dat_extensionsmap,dat_header.Extensions);
67 FOR i:=0 TO dat_header.Extensions-1 DO dat_file.Read(dat_extensionsmap[i],SizeOf(dat_extensionsmap[i]));
68
69 dat_file.Free;
70 END;
71
72
73FUNCTION LoadDatFile(fileid:LongWord):Tdata;
74 VAR dat_file:TFileStream;
75 BEGIN
76 dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
77 dat_file.Seek(dat_files[fileid].DatAddr,soFromBeginning);
78 SetLength(Result,dat_files[fileid].Size);
79 dat_file.Read(Result[0],dat_files[fileid].Size);
80 dat_file.Free;
81 END;
82
83
84PROCEDURE SaveDatFile(fileid:LongWord; data:Tdata);
85 VAR dat_file:TFileStream;
86 BEGIN
87 dat_file:=TFileStream.Create(dat_filename, fmOpenReadWrite);
88 dat_file.Seek(dat_files[fileid].DatAddr,soFromBeginning);
89 dat_file.Write(data[0],Length(data));
90 dat_file.Free;
91 END;
92
93
94FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
95 VAR dat_file:TFileStream;
96 BEGIN
97 dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
98 Result:=True;
99 dat_file.Seek(dat_files[fileid].DatAddr+offset,soFromBeginning);
100 dat_file.Read(target^,size);
101 dat_file.Free;
102 END;
103
104
105FUNCTION LoadRawFilePart(address,size:LongWord; target:Pointer):Boolean;
106 VAR
107 filestream:TFileStream;
108 BEGIN
109 Result:=True;
110 filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.raw'),fmOpenRead);
111 filestream.Seek(address,soFromBeginning);
112 filestream.Read(target^,size);
113 filestream.Free;
114 END;
115
116
117FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
118 BEGIN
119 Result:=AnsiReplaceStr(Format('%'+IntToStr(width)+'u',[value]),' ',leadingzeros);
120 END;
121
122
123FUNCTION FormatFileSize(size:LongWord):String;
124 BEGIN
125 IF size>=1024*1024*1024 THEN BEGIN
126 Result:=FloatToStrF(size/1024/1024/1024,ffFixed,5,1)+' GB';
127 END ELSE BEGIN
128 IF size>=1024*1024 THEN BEGIN
129 Result:=FloatToStrF(size/1024/1024,ffFixed,5,1)+' MB';
130 END ELSE BEGIN
131 IF size>=1024 THEN BEGIN
132 Result:=FloatToStrF(size/1024,ffFixed,5,1)+' KB';
133 END ELSE BEGIN
134 Result:=IntToStr(size)+' B';
135 END;
136 END;
137 END;
138 END;
139
140
141FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
142 VAR
143 string_build,ascii_version:String;
144 i:LongWord;
145 BEGIN
146 string_build:='';
147 ascii_version:='';
148 FOR i:=0 TO High(data) DO BEGIN
149 IF NOT HexOnly THEN
150 IF (i MOD 16)=0 THEN
151 string_build:=string_build+'0x'+IntToHex(i,6)+' ';
152 string_build:=string_build+IntToHex(data[i],2);
153 IF NOT HexOnly THEN BEGIN
154 IF data[i]>=32 THEN ascii_version:=ascii_version+Chr(data[i])
155 ELSE ascii_version:=ascii_version+'.';
156 IF ((i+1) MOD 2)=0 THEN string_build:=string_build+#32;
157 IF ((i+1) MOD 16)=0 THEN BEGIN
158 string_build:=string_build+#32+ascii_version+CrLf;
159 ascii_version:='';
160 END;
161 END;
162 END;
163 Result:=string_build;
164 END;
165
166
167FUNCTION ExportFile(fileid:LongWord; convert:Boolean):Integer;
168 VAR
169 i:Byte;
170 BEGIN
171 Result:=export_noerror;
172
173 //ExportDefFileHeader(fileid);
174
175 IF (dat_files[fileid].FileType AND $02)=0 THEN BEGIN
176 //ExportDatFile(fileid);
177
178 FOR i:=1 TO Length(ExportHandlers)+1 DO BEGIN
179 IF i<=Length(ExportHandlers) THEN BEGIN
180 IF ExportHandlers[i].Ext=dat_files[fileid].Extension THEN BEGIN
181 IF ExportHandlers[i].needed THEN BEGIN
182 CASE ExportHandlers[i].Handler(fileid,convert) OF
183 0: Result:=0;
184 ELSE
185 Result:=export_handlererror;
186 END;
187 END;
188 Break;
189 END;
190 END ELSE BEGIN
191 Result:=export_nohandler;
192 END;
193 END;
194 END;
195 END;
196
197
198FUNCTION GetWinFileName(fileid:LongWord; substring:String):String;
199 VAR
200 name:String;
201 BEGIN
202 name:=dat_files[fileid].Name;
203 name:=AnsiReplaceStr(name,'\','__');
204 name:=AnsiReplaceStr(name,'/','__');
205 name:=AnsiReplaceStr(name,'>','__');
206 name:=AnsiReplaceStr(name,'<','__');
207 Result:=FormatNumber(fileid,5,'0')+'-'+name+'.'+substring+'.'+dat_files[fileid].Extension;
208 END;
209
210FUNCTION GetExtractPath:String;
211 BEGIN
212 Result:=ExtractFilePath(dat_filename)+'\extracted_'+ExtractFileName(dat_filename);
213 END;
214
215
216END.
Note: See TracBrowser for help on using the repository browser.