slightly better light
[dd2d.git] / wadarc.d
blobe6f0e6d399e50ac740523c13edca5c1f7326d4c8
1 module wadarc is aliced;
2 private:
4 static import core.sync.mutex;
5 import std.stdio : File;
7 import console;
9 //import iv.encoding;
10 import iv.ziparc;
13 // ////////////////////////////////////////////////////////////////////////// //
14 public string koi8lotranslit (string s) {
15 usize pos = 0;
16 while (pos < s.length && s.ptr[pos] < 0x80) ++pos;
17 if (pos >= s.length) return s; // nothing to do
18 string res = s[0..pos];
19 while (pos < s.length) {
20 char ch = s.ptr[pos++];
21 if (ch == '\xe1' || ch == '\xc1') res ~= "a";
22 else if (ch == '\xe2' || ch == '\xc2') res ~= "b";
23 else if (ch == '\xf7' || ch == '\xd7') res ~= "v";
24 else if (ch == '\xe7' || ch == '\xc7') res ~= "g";
25 else if (ch == '\xe4' || ch == '\xc4') res ~= "d";
26 else if (ch == '\xe5' || ch == '\xc5') res ~= "e";
27 else if (ch == '\xb3' || ch == '\xa3') res ~= "yo";
28 else if (ch == '\xf6' || ch == '\xd6') res ~= "zh";
29 else if (ch == '\xfa' || ch == '\xda') res ~= "z";
30 else if (ch == '\xe9' || ch == '\xc9') res ~= "i";
31 else if (ch == '\xea' || ch == '\xca') res ~= "j";
32 else if (ch == '\xeb' || ch == '\xcb') res ~= "k";
33 else if (ch == '\xec' || ch == '\xcc') res ~= "l";
34 else if (ch == '\xed' || ch == '\xcd') res ~= "m";
35 else if (ch == '\xee' || ch == '\xce') res ~= "n";
36 else if (ch == '\xef' || ch == '\xcf') res ~= "o";
37 else if (ch == '\xf0' || ch == '\xd0') res ~= "p";
38 else if (ch == '\xf2' || ch == '\xd2') res ~= "r";
39 else if (ch == '\xf3' || ch == '\xd3') res ~= "s";
40 else if (ch == '\xf4' || ch == '\xd4') res ~= "t";
41 else if (ch == '\xf5' || ch == '\xd5') res ~= "u";
42 else if (ch == '\xe6' || ch == '\xc6') res ~= "f";
43 else if (ch == '\xe8' || ch == '\xc8') res ~= "h";
44 else if (ch == '\xe3' || ch == '\xc3') res ~= "c";
45 else if (ch == '\xfe' || ch == '\xde') res ~= "ch";
46 else if (ch == '\xfb' || ch == '\xdb') res ~= "sh";
47 else if (ch == '\xfd' || ch == '\xdd') res ~= "sch";
48 else if (ch == '\xff' || ch == '\xdf') res ~= "x";
49 else if (ch == '\xf9' || ch == '\xd9') res ~= "y";
50 else if (ch == '\xf8' || ch == '\xd8') res ~= "w";
51 else if (ch == '\xfc' || ch == '\xdc') res ~= "e";
52 else if (ch == '\xe0' || ch == '\xc0') res ~= "hu";
53 else if (ch == '\xf1' || ch == '\xd1') res ~= "ja";
54 else res ~= ch;
56 return res;
60 // ////////////////////////////////////////////////////////////////////////// //
61 public immutable char[256] koi8from866Table = [
62 '\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0a','\x0b','\x0c','\x0d','\x0e','\x0f',
63 '\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1a','\x1b','\x1c','\x1d','\x1e','\x1f',
64 '\x20','\x21','\x22','\x23','\x24','\x25','\x26','\x27','\x28','\x29','\x2a','\x2b','\x2c','\x2d','\x2e','\x2f',
65 '\x30','\x31','\x32','\x33','\x34','\x35','\x36','\x37','\x38','\x39','\x3a','\x3b','\x3c','\x3d','\x3e','\x3f',
66 '\x40','\x41','\x42','\x43','\x44','\x45','\x46','\x47','\x48','\x49','\x4a','\x4b','\x4c','\x4d','\x4e','\x4f',
67 '\x50','\x51','\x52','\x53','\x54','\x55','\x56','\x57','\x58','\x59','\x5a','\x5b','\x5c','\x5d','\x5e','\x5f',
68 '\x60','\x61','\x62','\x63','\x64','\x65','\x66','\x67','\x68','\x69','\x6a','\x6b','\x6c','\x6d','\x6e','\x6f',
69 '\x70','\x71','\x72','\x73','\x74','\x75','\x76','\x77','\x78','\x79','\x7a','\x7b','\x7c','\x7d','\x7e','\x7f',
70 '\xe1','\xe2','\xf7','\xe7','\xe4','\xe5','\xf6','\xfa','\xe9','\xea','\xeb','\xec','\xed','\xee','\xef','\xf0',
71 '\xf2','\xf3','\xf4','\xf5','\xe6','\xe8','\xe3','\xfe','\xfb','\xfd','\xff','\xf9','\xf8','\xfc','\xe0','\xf1',
72 '\xc1','\xc2','\xd7','\xc7','\xc4','\xc5','\xd6','\xda','\xc9','\xca','\xcb','\xcc','\xcd','\xce','\xcf','\xd0',
73 '\x90','\x91','\x92','\x81','\x87','\xb2','\x3f','\x3f','\x3f','\xb5','\xa1','\xa8','\xae','\x3f','\xac','\x83',
74 '\x84','\x89','\x88','\x86','\x80','\x8a','\xaf','\xb0','\xab','\xa5','\xbb','\xb8','\xb1','\xa0','\xbe','\xb9',
75 '\xba','\x3f','\x3f','\xaa','\xa9','\xa2','\x3f','\x3f','\xbc','\x85','\x82','\x8d','\x8c','\x8e','\x8f','\x8b',
76 '\xd2','\xd3','\xd4','\xd5','\xc6','\xc8','\xc3','\xde','\xdb','\xdd','\xdf','\xd9','\xd8','\xdc','\xc0','\xd1',
77 '\xb3','\xa3','\xb4','\xa4','\xb7','\xa7','\x3f','\x3f','\x9c','\x95','\x9e','\x96','\x3f','\x3f','\x94','\x9a',
80 public immutable char[256] koi8from1251Table = [
81 '\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0a','\x0b','\x0c','\x0d','\x0e','\x0f',
82 '\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1a','\x1b','\x1c','\x1d','\x1e','\x1f',
83 '\x20','\x21','\x22','\x23','\x24','\x25','\x26','\x27','\x28','\x29','\x2a','\x2b','\x2c','\x2d','\x2e','\x2f',
84 '\x30','\x31','\x32','\x33','\x34','\x35','\x36','\x37','\x38','\x39','\x3a','\x3b','\x3c','\x3d','\x3e','\x3f',
85 '\x40','\x41','\x42','\x43','\x44','\x45','\x46','\x47','\x48','\x49','\x4a','\x4b','\x4c','\x4d','\x4e','\x4f',
86 '\x50','\x51','\x52','\x53','\x54','\x55','\x56','\x57','\x58','\x59','\x5a','\x5b','\x5c','\x5d','\x5e','\x5f',
87 '\x60','\x61','\x62','\x63','\x64','\x65','\x66','\x67','\x68','\x69','\x6a','\x6b','\x6c','\x6d','\x6e','\x6f',
88 '\x70','\x71','\x72','\x73','\x74','\x75','\x76','\x77','\x78','\x79','\x7a','\x7b','\x7c','\x7d','\x7e','\x7f',
89 '\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f',
90 '\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f','\x3f',
91 '\x9a','\x3f','\x3f','\x3f','\x3f','\xbd','\x3f','\x3f','\xb3','\xbf','\xb4','\x3f','\x3f','\x3f','\x3f','\xb7',
92 '\x9c','\x3f','\xb6','\xa6','\xad','\x3f','\x3f','\x9e','\xa3','\x3f','\xa4','\x3f','\x3f','\x3f','\x3f','\xa7',
93 '\xe1','\xe2','\xf7','\xe7','\xe4','\xe5','\xf6','\xfa','\xe9','\xea','\xeb','\xec','\xed','\xee','\xef','\xf0',
94 '\xf2','\xf3','\xf4','\xf5','\xe6','\xe8','\xe3','\xfe','\xfb','\xfd','\xff','\xf9','\xf8','\xfc','\xe0','\xf1',
95 '\xc1','\xc2','\xd7','\xc7','\xc4','\xc5','\xd6','\xda','\xc9','\xca','\xcb','\xcc','\xcd','\xce','\xcf','\xd0',
96 '\xd2','\xd3','\xd4','\xd5','\xc6','\xc8','\xc3','\xde','\xdb','\xdd','\xdf','\xd9','\xd8','\xdc','\xc0','\xd1',
99 // char toupper/tolower, koi8
100 public immutable char[256] koi8tolowerTable = [
101 '\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0a','\x0b','\x0c','\x0d','\x0e','\x0f',
102 '\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1a','\x1b','\x1c','\x1d','\x1e','\x1f',
103 '\x20','\x21','\x22','\x23','\x24','\x25','\x26','\x27','\x28','\x29','\x2a','\x2b','\x2c','\x2d','\x2e','\x2f',
104 '\x30','\x31','\x32','\x33','\x34','\x35','\x36','\x37','\x38','\x39','\x3a','\x3b','\x3c','\x3d','\x3e','\x3f',
105 '\x40','\x61','\x62','\x63','\x64','\x65','\x66','\x67','\x68','\x69','\x6a','\x6b','\x6c','\x6d','\x6e','\x6f',
106 '\x70','\x71','\x72','\x73','\x74','\x75','\x76','\x77','\x78','\x79','\x7a','\x5b','\x5c','\x5d','\x5e','\x5f',
107 '\x60','\x61','\x62','\x63','\x64','\x65','\x66','\x67','\x68','\x69','\x6a','\x6b','\x6c','\x6d','\x6e','\x6f',
108 '\x70','\x71','\x72','\x73','\x74','\x75','\x76','\x77','\x78','\x79','\x7a','\x7b','\x7c','\x7d','\x7e','\x7f',
109 '\x80','\x81','\x82','\x83','\x84','\x85','\x86','\x87','\x88','\x89','\x8a','\x8b','\x8c','\x8d','\x8e','\x8f',
110 '\x90','\x91','\x92','\x93','\x94','\x95','\x96','\x97','\x98','\x99','\x9a','\x9b','\x9c','\x9d','\x9e','\x9f',
111 '\xa0','\xa1','\xa2','\xa3','\xa4','\xa5','\xa6','\xa7','\xa8','\xa9','\xaa','\xab','\xac','\xad','\xae','\xaf',
112 '\xb0','\xb1','\xb2','\xa3','\xa4','\xb5','\xa6','\xa7','\xb8','\xb9','\xba','\xbb','\xbc','\xad','\xbe','\xbf',
113 '\xc0','\xc1','\xc2','\xc3','\xc4','\xc5','\xc6','\xc7','\xc8','\xc9','\xca','\xcb','\xcc','\xcd','\xce','\xcf',
114 '\xd0','\xd1','\xd2','\xd3','\xd4','\xd5','\xd6','\xd7','\xd8','\xd9','\xda','\xdb','\xdc','\xdd','\xde','\xdf',
115 '\xc0','\xc1','\xc2','\xc3','\xc4','\xc5','\xc6','\xc7','\xc8','\xc9','\xca','\xcb','\xcc','\xcd','\xce','\xcf',
116 '\xd0','\xd1','\xd2','\xd3','\xd4','\xd5','\xd6','\xd7','\xd8','\xd9','\xda','\xdb','\xdc','\xdd','\xde','\xdf',
119 public immutable char[256] koi8toupperTable = [
120 '\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0a','\x0b','\x0c','\x0d','\x0e','\x0f',
121 '\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1a','\x1b','\x1c','\x1d','\x1e','\x1f',
122 '\x20','\x21','\x22','\x23','\x24','\x25','\x26','\x27','\x28','\x29','\x2a','\x2b','\x2c','\x2d','\x2e','\x2f',
123 '\x30','\x31','\x32','\x33','\x34','\x35','\x36','\x37','\x38','\x39','\x3a','\x3b','\x3c','\x3d','\x3e','\x3f',
124 '\x40','\x41','\x42','\x43','\x44','\x45','\x46','\x47','\x48','\x49','\x4a','\x4b','\x4c','\x4d','\x4e','\x4f',
125 '\x50','\x51','\x52','\x53','\x54','\x55','\x56','\x57','\x58','\x59','\x5a','\x5b','\x5c','\x5d','\x5e','\x5f',
126 '\x60','\x41','\x42','\x43','\x44','\x45','\x46','\x47','\x48','\x49','\x4a','\x4b','\x4c','\x4d','\x4e','\x4f',
127 '\x50','\x51','\x52','\x53','\x54','\x55','\x56','\x57','\x58','\x59','\x5a','\x7b','\x7c','\x7d','\x7e','\x7f',
128 '\x80','\x81','\x82','\x83','\x84','\x85','\x86','\x87','\x88','\x89','\x8a','\x8b','\x8c','\x8d','\x8e','\x8f',
129 '\x90','\x91','\x92','\x93','\x94','\x95','\x96','\x97','\x98','\x99','\x9a','\x9b','\x9c','\x9d','\x9e','\x9f',
130 '\xa0','\xa1','\xa2','\xb3','\xb4','\xa5','\xb6','\xb7','\xa8','\xa9','\xaa','\xab','\xac','\xbd','\xae','\xaf',
131 '\xb0','\xb1','\xb2','\xb3','\xb4','\xb5','\xb6','\xb7','\xb8','\xb9','\xba','\xbb','\xbc','\xbd','\xbe','\xbf',
132 '\xe0','\xe1','\xe2','\xe3','\xe4','\xe5','\xe6','\xe7','\xe8','\xe9','\xea','\xeb','\xec','\xed','\xee','\xef',
133 '\xf0','\xf1','\xf2','\xf3','\xf4','\xf5','\xf6','\xf7','\xf8','\xf9','\xfa','\xfb','\xfc','\xfd','\xfe','\xff',
134 '\xe0','\xe1','\xe2','\xe3','\xe4','\xe5','\xe6','\xe7','\xe8','\xe9','\xea','\xeb','\xec','\xed','\xee','\xef',
135 '\xf0','\xf1','\xf2','\xf3','\xf4','\xf5','\xf6','\xf7','\xf8','\xf9','\xfa','\xfb','\xfc','\xfd','\xfe','\xff',
138 public immutable ubyte[32] koi8alphaTable = [
139 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0xff,0xff,0x07,0xfe,0xff,0xff,0x07,
140 0x00,0x00,0x00,0x00,0xd8,0x20,0xd8,0x20,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
143 public char dos2koi8 (char ch) pure nothrow @trusted @nogc {
144 pragma(inline, true);
145 return koi8from866Table.ptr[cast(int)ch];
148 public char koi8lower (char ch) pure nothrow @trusted @nogc {
149 pragma(inline, true);
150 return koi8tolowerTable.ptr[cast(int)ch];
153 public char koi8upper (char ch) pure nothrow @trusted @nogc {
154 pragma(inline, true);
155 return koi8toupperTable.ptr[cast(int)ch];
158 public bool koi8isAlpha (char ch) pure nothrow @trusted @nogc {
159 pragma(inline, true);
160 return ((koi8alphaTable.ptr[ch/8]&(1<<(ch%8))) != 0);
164 // ////////////////////////////////////////////////////////////////////////// //
165 //public __gshared WadArchive[] wadList;
166 __gshared ZipArchive[] pk3List;
167 __gshared core.sync.mutex.Mutex glock;
168 __gshared string dataPath;
171 shared static this () {
172 glock = new core.sync.mutex.Mutex;
176 public void setDataPath (const(char)[] path) {
177 while (path.length > 1 && path[0] == '/' && path[1] == '/') path = path[1..$];
178 if (path != "/") while (path.length && path[$-1] == '/') path = path[0..$-1];
179 dataPath = path.idup~"/";
182 public string getDataPath () { pragma(inline, true); return dataPath; }
186 public void addWad (string fname) {
187 if (glock !is null) glock.lock();
188 scope(exit) if (glock !is null) glock.unlock();
189 conwriteln("adding '", fname, "'...");
190 wadList ~= new WadArchive(fname);
195 public void addPK3 (string fname) {
196 if (glock !is null) glock.lock();
197 scope(exit) if (glock !is null) glock.unlock();
198 conwriteln("; adding '", fname, "'...");
199 pk3List ~= new ZipArchive(fname);
203 public File openFile (string fname) {
204 if (glock !is null) glock.lock();
205 scope(exit) if (glock !is null) glock.unlock();
206 try {
207 return File(dataPath~fname);
208 } catch (Exception) {}
209 // now try pk3
210 foreach_reverse (ZipArchive pk3; pk3List) {
211 try {
212 return pk3.fopen(fname);
213 } catch (Exception) {}
215 return File(fname); // to throw a correct exception, lol
219 public string loadTextFile (string fname) {
220 auto fl = openFile(fname);
221 auto sz = fl.size;
222 if (sz < 0 || sz > 1024*1024) throw new Exception("invalid text file size: '"~fname~"'");
223 if (sz == 0) return "";
224 auto res = new char[](cast(uint)sz);
225 if (fl.rawRead(res[]).length != res.length) throw new Exception("error reading text file '"~fname~"'");
226 import std.exception : assumeUnique;
227 return res.assumeUnique;
231 // ////////////////////////////////////////////////////////////////////////// //
233 final class WadArchive {
234 public:
235 import std.stdio : File;
237 private:
238 static struct FileInfo {
239 uint ofs;
240 uint size;
241 string path;
242 string name;
245 // for dir range
246 public static struct DirEntry {
247 string path;
248 string name;
249 uint size;
252 private:
253 File zfl;
254 ulong flstpos;
255 FileInfo[] dir;
257 public:
258 this (string fname) {
259 import std.stdio : File;
260 initLock();
261 zfl = File(fname);
262 open(zfl);
263 scope(failure) { zfl.close; zfl = zfl.init; }
266 // it now owns the file (if no exception was thrown)
267 this (File fl) {
268 initLock();
269 open(fl);
270 scope(success) zfl = fl;
273 @property auto files () {
274 static struct Range {
275 private:
276 WadArchive me;
277 uint curindex;
279 nothrow @safe @nogc:
280 this (WadArchive ame, uint aidx=0) { me = ame; curindex = aidx; }
282 public:
283 @property bool empty () const { return (curindex >= me.dir.length); }
284 @property DirEntry front () const {
285 return DirEntry(
286 (curindex < me.dir.length ? me.dir[cast(usize)curindex].path : null),
287 (curindex < me.dir.length ? me.dir[cast(usize)curindex].name : null),
288 (curindex < me.dir.length ? me.dir[cast(usize)curindex].size : 0));
290 @property Range save () { return Range(me, curindex); }
291 void popFront () { if (curindex < me.dir.length) ++curindex; }
292 @property uint length () const { return me.dir.length; }
293 @property uint position () const { return curindex; } // current position
294 @property void position (uint np) { curindex = np; }
295 void rewind () { curindex = 0; }
297 return Range(this);
300 File fopen (ref in DirEntry de) {
301 string dname = de.name;
302 usize pos = dname.length;
303 while (pos > 0 && dname.ptr[pos-1] != '.') --pos;
304 if (pos > 1) dname = dname[0..pos-1];
305 foreach (immutable idx, ref fi; dir) {
306 if (/*fi.path == de.path &&*/ fi.name == dname) return openDirEntry(idx, fi.name);
308 throw new NamedException!"WadArchive"("file not found");
311 File fopen (const(char)[] fname) {
312 DirEntry de;
313 auto pos = fname.length;
314 while (pos > 0 && fname[pos-1] != '/') --pos;
315 if (pos) {
316 de.path = cast(string)fname[0..pos]; // it's safe here
317 de.name = cast(string)fname[pos..$]; // it's safe here
318 //conwriteln("[", de.path, "] [", de.name, "]");
319 } else {
320 de.name = cast(string)fname; // it's safe here
322 return fopen(de);
325 private:
326 void cleanup () {
327 dir.length = 0;
328 if (zfl.isOpen) zfl.close;
329 zfl = zfl.init;
332 void open (File fl) {
333 import std.uni : icmp;
335 immutable string[$] msn = [
336 "SARG", "TROO", "POSS", "SPOS", "CYBR", "CPOS", "BOSS", "BOS2", "HEAD", "SKUL",
337 "PAIN", "SPID", "BSPI", "FATT", "SKEL", "VILE", "FISH", "BAR1", "ROBO", "PLAY"
340 string curpath;
342 string fixName (const(char)[] name) {
343 return name.idup;
344 if (name.length >= 4 && name[0..4] == "stcf") return name.idup~".vga";
345 if (name.length >= 4 && name[0..4] == "stbf") return name.idup~".vga";
346 if (name.length >= 5 && name[0..5] == "winum") return name.idup~".vga";
347 if (name == "wicolon" || name == "wiminus" || name == "wipcnt") return name.idup~".vga";
348 if (name.length > 3 && name[0..3] == "map") return name.idup~".d2m";
349 if (name == "playpal") return "playpal.pal";
350 switch (name) {
351 case "endoom": return "endoom.b80";
352 case "endanim": return "endanim.a8";
353 case "end2anim": return "end2anim.a8";
354 case "darts": return "darts.a8";
355 case "colormap": return "colormap.tbl";
356 case "mixmap": return "mixmap.tbl";
357 case "titlepic": return "titlepic.vga";
358 case "interpic": return "interpic.vga";
359 case "cd1pic": return "cd1pic.vga";
360 case "endpic": return "endpic.vraw";
361 case "m_therml": return "m_therml.vga";
362 case "m_thermm": return "m_thermm.vga";
363 case "m_thermo": return "m_thermo.vga";
364 case "m_thermr": return "m_thermr.vga";
365 case "m_lscntr": return "m_lscntr.vga";
366 case "m_lsleft": return "m_lsleft.vga";
367 case "m_lsrght": return "m_lsrght.vga";
368 default:
371 import std.algorithm;
372 if (curpath == "sounds/") return name.idup~".snd";
373 if (curpath == "music/") return (name.length > 3 && name[0..3] == "dmi" ? name.idup~".dmi" : name.idup~".dmm");
374 if (curpath == "tilegfx/") return name.idup~".vga";
375 if (curpath.startsWith("sprites/")) return name.idup~".vga";
376 return name.idup;
379 string fixPath (const(char)[] name) {
380 switch (name) {
381 case "m_therml":
382 case "m_thermm":
383 case "m_thermo":
384 case "m_thermr":
385 case "m_lscntr":
386 case "m_lsleft":
387 case "m_lsrght":
388 return "menugfx/";
389 case "rsky1":
390 case "rsky2":
391 case "rsky3":
392 return "sprites/sky/";
393 default:
395 if (name.length >= 4 && name[0..4] == "stcf") return "fonts/stcf/";
396 if (name.length >= 4 && name[0..4] == "stbf") return "fonts/stbf/";
397 if (name.length >= 5 && name[0..5] == "winum") return "fonts/winum/";
398 if (name == "wicolon" || name == "wiminus" || name == "wipcnt") return "fonts/winum/";
399 if (name.length > 3 && name[0..3] == "map") return "maps/";
400 if (name == "d_start") curpath = "sounds/";
401 if (name == "m_start") curpath = "music/";
402 if (name == "w_start") curpath = "tilegfx/";
403 if (name == "s_start") curpath = "sprites/";
404 if (curpath == "sprites/" && name.length > 4) {
405 switch (name[0..4]) {
406 case "sarg": return "sprites/monsters/demon/";
407 case "troo": return "sprites/monsters/imp/";
408 case "poss": return "sprites/monsters/zombie/";
409 case "spos": return "sprites/monsters/sergeant/";
410 case "cybr": return "sprites/monsters/cyberdemon/";
411 case "cpos": return "sprites/monsters/chaingunner/";
412 case "boss": return "sprites/monsters/baron/";
413 case "bos2": return "sprites/monsters/knight/";
414 case "head": return "sprites/monsters/cacodemon/";
415 case "skul": return "sprites/monsters/soul/";
416 case "pain": return "sprites/monsters/painel/";
417 case "spid": return "sprites/monsters/mastermind/";
418 case "bspi": return "sprites/monsters/arachnotron/";
419 case "fatt": return "sprites/monsters/mancubus/";
420 case "skel": return "sprites/monsters/revenant/";
421 case "vile": return "sprites/monsters/archvile/";
422 case "fish": return "sprites/monsters/fish/";
423 case "bar1": return "sprites/monsters/barrel/";
424 case "robo": return "sprites/monsters/robot/";
425 case "play": return "sprites/monsters/player/";
426 default:
429 return curpath;
432 import core.stdc.stdio : SEEK_CUR, SEEK_END;
433 scope(failure) cleanup();
435 uint readU32 () {
436 ubyte[4] data;
437 if (fl.rawRead(data[]).length != data.length) throw new NamedException!"WadArchive"("reading error");
438 return cast(uint)(data[0]+0x100*data[1]+0x10000*data[2]+0x1000000*data[3]);
441 uint lmpofs;
442 uint lmpsize;
443 char[8] lmpname;
445 flstpos = fl.tell;
447 if (fl.rawRead(lmpname[0..4]).length != 4) throw new NamedException!"WadArchive"("reading error");
448 if (lmpname[0..4] != "PWAD" && lmpname[0..4] != "IWAD") throw new NamedException!"WadArchive"("not a WAD file");
449 auto count = readU32();
450 auto dofs = readU32();
451 if (count == 0) return;
452 if (dofs < 3*4 || count == uint.max) throw new NamedException!"WadArchive"("invalid WAD file");
453 fl.seek(dofs-3*4, SEEK_CUR);
454 while (count-- > 0) {
455 lmpofs = readU32();
456 lmpsize = readU32();
457 if (fl.rawRead(lmpname[]).length != 8) throw new NamedException!"WAD"("reading error");
458 int pos = 0;
459 while (pos < 8 && lmpname[pos] != 0) {
460 if (lmpname[pos] >= 'A' && lmpname[pos] <= 'Z') lmpname[pos] += 32;
461 ++pos;
463 if (pos == 0) continue;
464 auto nm = lmpname[0..pos];
465 if (nm.length > 6 && nm[$-6..$] == "_start") {
466 fixPath(nm);
467 continue;
469 if (nm.length > 4 && nm[$-4..$] == "_end") {
470 curpath = null;
471 continue;
473 uint fidx = uint.max;
474 foreach (immutable idx, ref de; dir) if (de.name == lmpname[0..pos]) { fidx = cast(uint)idx; break; }
475 if (fidx >= dir.length) {
476 fidx = cast(uint)dir.length;
477 dir ~= FileInfo();
479 dir[fidx].ofs = lmpofs;
480 dir[fidx].size = lmpsize;
481 //dir[fidx].name = fixName(nm);
482 //dir[fidx].path = fixPath(nm);
484 import std.uni : toLower;
485 dir[fidx].name = koi8lotranslit(recodeToKOI8(recode(fixName(nm), "utf-8", "cp866").toLower, "utf-8"));
486 dir[fidx].path = koi8lotranslit(recodeToKOI8(recode(fixPath(nm), "utf-8", "cp866").toLower, "utf-8"));
488 //debug conwriteln(dir[fidx].path, " : ", dir[fidx].name);
490 debug conwriteln(dir.length, " files found");
494 // ////////////////////////////////////////////////////////////////////// //
495 static import core.sync.mutex;
497 core.sync.mutex.Mutex lock;
499 void initLock () {
500 lock = new core.sync.mutex.Mutex;
503 auto openDirEntry (uint idx, string filename) {
504 import core.sys.linux.stdio : fopencookie;
505 import core.stdc.stdio : FILE;
506 import core.stdc.stdio : fopen, fclose;
507 import core.stdc.stdlib : calloc, free;
508 import etc.c.zlib;
509 import std.internal.cstring : tempCString;
510 import core.memory : GC;
512 if (!zfl.isOpen) throw new NamedException!"WadArchive"("archive wasn't opened");
513 if (idx >= dir.length) throw new NamedException!"WadArchive"("invalid dir index");
515 // create cookied `FILE*`
516 auto fc = cast(InnerFileCookied*)calloc(1, InnerFileCookied.sizeof);
517 scope(exit) if (fc !is null) free(fc);
518 if (fc is null) {
519 import core.exception : onOutOfMemoryErrorNoGC;
520 onOutOfMemoryErrorNoGC();
522 (*fc) = InnerFileCookied.init;
523 (*fc).stpos = flstpos+dir[idx].ofs;
524 (*fc).size = cast(uint)dir[idx].size;
525 (*fc).lock = lock;
526 GC.addRange(fc, InnerFileCookied.sizeof);
527 (*fc).xfl = zfl;
528 // open `cooked` file
529 FILE* fres = fopencookie(cast(void*)fc, "r", fcdatpkCallbacks);
530 if (fres is null) {
531 // alas
532 if ((*fc).fl !is null) fclose((*fc).fl);
533 try { (*fc).xfl.detach(); } catch (Exception) {}
534 throw new NamedException!"WadArchive"("can't open cookied file");
536 // ok
537 fc = null;
538 return File(fres, filename);
542 // ////////////////////////////////////////////////////////////////////// //
543 // "inner" file processor; processes both packed and unpacked files
544 // can be used as normal disk file processor too
545 static struct InnerFileCookied {
546 private import core.sys.posix.sys.types : ssize_t, off64_t = off_t;
547 private import core.stdc.stdio : FILE;
549 enum ibsize = 32768;
551 core.sync.mutex.Mutex lock;
552 // note that either one of `fl` or `xfl` must be opened and operational
553 FILE* fl; // disk file, can be `null`
554 File xfl; // disk file, can be closed
555 long stpos; // starting position
556 uint size; // unpacked size
557 uint pos; // current file position
558 //uint prpos; // previous file position
559 //uint pkpos; // current position in DAT
560 //ubyte[] pkb; // packed data
561 bool eoz;
563 @disable this (this);
565 nothrow:
566 ~this () { close(); }
568 @property bool isOpen () @safe /*@nogc*/ { return (fl !is null || xfl.isOpen); }
570 void close () {
571 import core.memory : GC;
572 import core.stdc.stdlib : free;
574 if (lock !is null) lock.lock();
575 scope(exit) if (lock !is null) lock.unlock();
576 if (fl !is null) {
577 import core.stdc.stdio : fclose;
578 fclose(fl);
579 fl = null;
581 try { xfl.detach(); } catch (Exception) {} // it's safe to detach closed File
583 eoz = true;
586 ssize_t read (void* buf, size_t count) {
587 if (buf is null) return -1;
588 if (count == 0 || size == 0) return 0;
589 lock.lock();
590 scope(exit) lock.unlock();
591 if (!isOpen) return -1; // read error
592 if (pos >= size) return 0; // EOF
594 import core.stdc.stdio : ferror, fread;
595 import core.sys.posix.stdio : fseeko;
596 if (size-pos < count) count = cast(size_t)(size-pos);
597 if (fl !is null) {
598 // `FILE*`
599 if (fseeko(fl, stpos+pos, 0) < 0) return -1;
600 auto rd = fread(buf, 1, count, fl);
601 if (rd != count && (rd < 0 || ferror(fl))) rd = -1;
602 if (rd > 0) pos += rd;
603 return rd;
604 } else {
605 // std.stdio.File
606 try {
607 xfl.seek(stpos+pos, 0);
608 auto rd = xfl.rawRead(buf[0..count]);
609 pos += rd.length;
610 return (rd.length == count ? rd.length : -1);
611 } catch (Exception) {} //BAD DOGGY!
612 return -1;
617 long seek (long ofs, int whence) {
618 lock.lock();
619 scope(exit) lock.unlock();
620 if (!isOpen) return -1;
621 //TODO: overflow checks
622 switch (whence) {
623 case 0: // SEEK_SET
624 break;
625 case 1: // SEEK_CUR
626 ofs += pos;
627 break;
628 case 2: // SEEK_END
629 if (ofs > 0) ofs = 0;
630 ofs += size;
631 break;
632 default:
633 return -1;
635 if (ofs < 0) return -1;
636 if (ofs > size) ofs = size;
637 pos = cast(uint)ofs;
638 return ofs;
643 static:
644 // ////////////////////////////////////////////////////////////////////// //
645 extern(C) nothrow {
646 import core.sys.linux.stdio : cookie_io_functions_t;
647 import core.sys.posix.sys.types : ssize_t, off64_t = off_t;
649 ssize_t fcdatpkRead (void* cookie, char* buf, size_t count) {
650 //conwriteln("reading ", count, " bytes");
651 import core.stdc.errno;
652 auto fc = cast(InnerFileCookied*)cookie;
653 auto res = fc.read(buf, count);
654 if (res < 0) { errno = EIO; return -1; }
655 return res;
658 ssize_t fcdatpkWrite (void* cookie, const(char)* buf, size_t count) {
659 //conwriteln("writing ", count, " bytes");
660 import core.stdc.errno;
661 errno = EIO; //FIXME: find better code
662 return 0; // error; write should not return `-1`
665 int fcdatpkSeek (void* cookie, off64_t* offset, int whence) {
666 //conwriteln("seeking ", *offset, " bytes, whence=", whence);
667 import core.stdc.errno;
668 auto fc = cast(InnerFileCookied*)cookie;
669 auto res = fc.seek(*offset, whence);
670 if (res < 0) { errno = EIO; return -1; }
671 *offset = cast(off64_t)res;
672 return 0;
675 int fcdatpkClose (void* cookie) {
676 import core.memory : GC;
677 import core.stdc.stdlib : free;
678 //conwriteln("closing");
679 auto fc = cast(InnerFileCookied*)cookie;
680 //fc.close();
681 GC.removeRange(cookie);
682 try { fc.__dtor(); } catch (Exception) {}
683 // no need to run finalizers, we SHOULD NOT have any
684 //try { GC.runFinalizers(cookie[0..InnerFileCookied.sizeof]); } catch (Exception) {}
685 //fc.xfl.__dtor();
686 free(cookie);
687 //conwriteln("closed");
688 return 0;
692 __gshared cookie_io_functions_t fcdatpkCallbacks = cookie_io_functions_t(
693 /*.read =*/ &fcdatpkRead,
694 /*.write =*/ &fcdatpkWrite,
695 /*.seek =*/ &fcdatpkSeek,
696 /*.close =*/ &fcdatpkClose,
699 static:
700 T[] xalloc(T) (size_t len) {
701 import core.stdc.stdlib : malloc;
702 if (len < 1) return null;
703 auto res = cast(T*)malloc(len*T.sizeof);
704 if (res is null) {
705 import core.exception : onOutOfMemoryErrorNoGC;
706 onOutOfMemoryErrorNoGC();
708 res[0..len] = T.init;
709 return res[0..len];
712 void xfree(T) (ref T[] slc) {
713 if (slc.ptr !is null) {
714 import core.stdc.stdlib : free;
715 free(slc.ptr);
717 slc = null;