7 // ////////////////////////////////////////////////////////////////////////// //
8 public struct PakFileInfo
{
14 // ////////////////////////////////////////////////////////////////////////// //
15 private struct WadFileInfo
{
23 __gshared WadFileInfo
[string
] files
;
24 __gshared
bool firstWad
= true;
27 // ////////////////////////////////////////////////////////////////////////// //
28 public void forEachWadFile (void delegate (string name
, uint size
) dg
) {
29 foreach (const ref fi
; files
.byValue
) dg(fi
.name
, fi
.size
);
33 // ////////////////////////////////////////////////////////////////////////// //
34 public ubyte[] wadLoadFile (const(char)[] name
) {
35 if (auto fi
= name
in files
) {
36 if (fi
.size
> 1024*1024*8) throw new Exception("file too big");
37 if (fi
.size
== 0) return null;
38 auto res
= new ubyte[](fi
.size
);
40 fi
.wadfl
.seek(fi
.ofs
);
41 while (pos
< res
.length
) {
42 //writeln(name, ": pos=", pos, "; len=", res.length);
43 auto rd
= fi
.wadfl
.rawRead(res
[pos
..$]);
44 if (rd
.length
== 0) throw new Exception("read error ("~name
.idup
~")");
45 pos
+= cast(uint)rd
.length
;
49 throw new Exception("file not found");
53 // ////////////////////////////////////////////////////////////////////////// //
54 uint readUInt (File fl
) {
56 foreach (immutable shift
; 0..4) {
58 if (fl
.rawRead((&b
)[0..1]).length
!= 1) throw new Exception("read error");
64 void readBuf(T
) (File fl
, T
[] buf
) {
65 if (buf
.length
== 0) return;
66 if (buf
.length
> int.max
/2) assert(0, "wtf?!");
68 while (pos
< buf
.length
) {
69 auto rd
= fl
.rawRead(buf
[pos
..$]);
70 if (rd
.length
== 0) throw new Exception("read error");
71 pos
+= cast(uint)rd
.length
;
76 // ////////////////////////////////////////////////////////////////////////// //
77 public void registerWad (string pak
) {
82 if (sign
!= "IWAD") throw new Exception("invalid wad");
85 if (sign
!= "PWAD") throw new Exception("invalid wad");
87 uint fcount
= fl
.readUInt
;
88 uint dofs
= fl
.readUInt
;
89 if (fcount
== 0) return;
90 if (fcount
> 1024) throw new Exception("invalid wad");
94 uint ofs
= fl
.readUInt
;
95 uint size
= fl
.readUInt
;
98 foreach (immutable idx
, ref char ch
; nbuf
[]) {
100 if (ch
>= 'A' && ch
<= 'Z') ch
+= 32;
103 string name
= nm
.idup
;
104 files
[name
] = WadFileInfo(ofs
, size
, name
, fl
);