shitty title
[mmd.git] / _wad / mkwad.d
blob00293e407bab8aea26200047cd34de61421082db
1 import std.file;
2 import std.path;
3 import iv.vfs.io;
6 struct FileInfo {
7 uint ofs, size;
8 char[8] name = 0;
12 void main (string[] args) {
13 if (args.length != 2) {
14 writeln("usage: mkwad dir");
15 return;
18 auto fo = VFile(args[1].baseName~".wad", "w");
20 if (args[1].baseName == "mminer") {
21 fo.rawWriteExact("IWAD");
22 } else {
23 fo.rawWriteExact("PWAD");
25 fo.writeNum!uint(0); // file count
26 fo.writeNum!uint(0); // dir offset
28 FileInfo[] files;
30 ubyte[1024] buf;
32 foreach (DirEntry de; dirEntries(args[1], SpanMode.shallow)) {
33 import std.path;
34 import std.string;
35 string name;
36 foreach (char ch; de.name.baseName.stripExtension) {
37 if (ch >= 'a' && ch <= 'z') ch -= 32;
38 name ~= ch;
40 //if (name.indexOf('.') >= 0) continue;
41 if (!de.name.isFile) continue;
42 if (name.length > 8) continue;
43 writeln(name);
45 auto fi = VFile(de.name);
46 FileInfo ff;
47 ff.name[0..name.length] = name[];
48 ff.ofs = cast(uint)fo.tell;
49 ff.size = cast(uint)fi.size;
50 for (;;) {
51 auto rd = fi.rawRead(buf[]);
52 if (rd.length == 0) break;
53 fo.rawWriteExact(rd);
55 files ~= ff;
59 auto dofs = cast(uint)fo.tell;
60 // write dir
61 foreach (const ref fi; files) {
62 fo.writeNum!uint(fi.ofs);
63 fo.writeNum!uint(fi.size);
64 fo.rawWriteExact(fi.name[]);
67 // fix header
68 fo.seek(4);
69 fo.writeNum!uint(cast(uint)files.length);
70 fo.writeNum!uint(dofs);