source: oup/releases/0.12a/Unit2_functions.pas@ 522

Last change on this file since 522 was 21, checked in by alloc, 18 years ago
File size: 6.7 KB
RevLine 
[21]1UNIT Unit2_functions;
2INTERFACE
3USES Classes, SysUtils, StrUtils, Math, Unit3_data, Unit4_Exporters;
4
5FUNCTION LoadDatInfos(filename:String):Boolean;
6FUNCTION LoadDatFile(fileid:LongWord):Tdata;
7FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
8FUNCTION LoadRawFilePart(address,size:LongWord; target:Pointer):Boolean;
9FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
10FUNCTION FormatFileSize(size:LongWord):String;
11FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
12FUNCTION ExportFile(fileid:LongWord; convert:Boolean):Integer;
13FUNCTION GetWinFileName(fileid:LongWord; substring:String):String;
14FUNCTION GetExtractPath:String;
15
16
17
18IMPLEMENTATION
19
20
21FUNCTION LoadDatInfos(filename:String):Boolean;
22 VAR i:LongWord;
23 dat_file:TFileStream;
24 BEGIN
25 Result:=True;
26 IF Length(filename)>0 THEN dat_file.Free;
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
84FUNCTION LoadDatFilePart(fileid,offset,size:LongWord; target:Pointer):Boolean;
85 VAR dat_file:TFileStream;
86 BEGIN
87 dat_file:=TFileStream.Create(dat_filename, fmOpenRead);
88 Result:=True;
89 dat_file.Seek(dat_files[fileid].DatAddr+offset,soFromBeginning);
90 dat_file.Read(target^,size);
91 dat_file.Free;
92 END;
93
94
95FUNCTION LoadRawFilePart(address,size:LongWord; target:Pointer):Boolean;
96 VAR
97 filestream:TFileStream;
98 BEGIN
99 Result:=True;
100 filestream:=TFileStream.Create(AnsiReplaceStr(dat_filename,'.dat','.raw'),fmOpenRead);
101 filestream.Seek(address,soFromBeginning);
102 filestream.Read(target^,size);
103 filestream.Free;
104 END;
105
106
107FUNCTION FormatNumber(value:LongWord; width:Byte; leadingzeros:Char):String;
108 BEGIN
109 Result:=AnsiReplaceStr(Format('%'+IntToStr(width)+'u',[value]),' ',leadingzeros);
110 END;
111
112
113FUNCTION FormatFileSize(size:LongWord):String;
114 BEGIN
115 IF size>=1024*1024*1024 THEN BEGIN
116 Result:=FloatToStrF(size/1024/1024/1024,ffFixed,5,1)+' GB';
117 END ELSE BEGIN
118 IF size>=1024*1024 THEN BEGIN
119 Result:=FloatToStrF(size/1024/1024,ffFixed,5,1)+' MB';
120 END ELSE BEGIN
121 IF size>=1024 THEN BEGIN
122 Result:=FloatToStrF(size/1024,ffFixed,5,1)+' KB';
123 END ELSE BEGIN
124 Result:=IntToStr(size)+' B';
125 END;
126 END;
127 END;
128 END;
129
130
131FUNCTION CreateHexString(data:Tdata; HexOnly:Boolean):String;
132 VAR
133 string_build,ascii_version:String;
134 i:LongWord;
135 BEGIN
136 string_build:='';
137 ascii_version:='';
138 FOR i:=0 TO High(data) DO BEGIN
139 IF NOT HexOnly THEN
140 IF (i MOD 16)=0 THEN
141 string_build:=string_build+'0x'+IntToHex(i,6)+' ';
142 string_build:=string_build+IntToHex(data[i],2);
143 IF NOT HexOnly THEN BEGIN
144 IF data[i]>=32 THEN ascii_version:=ascii_version+Chr(data[i])
145 ELSE ascii_version:=ascii_version+'.';
146 IF ((i+1) MOD 2)=0 THEN string_build:=string_build+#32;
147 IF ((i+1) MOD 16)=0 THEN BEGIN
148 string_build:=string_build+#32+ascii_version+CrLf;
149 ascii_version:='';
150 END;
151 END;
152 END;
153 Result:=string_build;
154 END;
155
156
157FUNCTION ExportFile(fileid:LongWord; convert:Boolean):Integer;
158 VAR
159 i:Byte;
160 BEGIN
161 Result:=export_noerror;
162
163 ExportDefFileHeader(fileid);
164
165 IF (dat_files[fileid].FileType AND $02)=0 THEN BEGIN
166 ExportDatFile(fileid);
167
168 FOR i:=1 TO Length(ExportHandlers)+1 DO BEGIN
169 IF i<=Length(ExportHandlers) THEN BEGIN
170 IF ExportHandlers[i].Ext=dat_files[fileid].Extension THEN BEGIN
171 IF ExportHandlers[i].needed THEN BEGIN
172 CASE ExportHandlers[i].Handler(fileid,convert) OF
173 0: Result:=0;
174 ELSE
175 Result:=export_handlererror;
176 END;
177 END;
178 Break;
179 END;
180 END ELSE BEGIN
181 Result:=export_nohandler;
182 END;
183 END;
184 END;
185 END;
186
187
188FUNCTION GetWinFileName(fileid:LongWord; substring:String):String;
189 VAR
190 name:String;
191 BEGIN
192 name:=dat_files[fileid].Name;
193 name:=AnsiReplaceStr(name,'\','__');
194 name:=AnsiReplaceStr(name,'/','__');
195 name:=AnsiReplaceStr(name,'>','__');
196 name:=AnsiReplaceStr(name,'<','__');
197 Result:=FormatNumber(fileid,5,'0')+'-'+name+'.'+substring+'.'+dat_files[fileid].Extension;
198 END;
199
200FUNCTION GetExtractPath:String;
201 BEGIN
202 Result:=ExtractFilePath(dat_filename)+'\extracted_'+ExtractFileName(dat_filename);
203 END;
204
205
206END.
Note: See TracBrowser for help on using the repository browser.