UNPKG

node-wad

Version:
87 lines (73 loc) 2.35 kB
// Generated by CoffeeScript 1.12.6 var BufferReader, BufferWriter, WAD, WDirectory, WHeader, WLump, fs, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; fs = require("fs"); WHeader = require("./header.js"); WDirectory = require("./directory.js"); WLump = require("./lump.js"); BufferReader = require("buffer-utils").BufferReader; BufferWriter = require("buffer-utils").BufferWriter; WAD = (function() { function WAD(bPWAD) { this.bPWAD = bPWAD; this.addLump = bind(this.addLump, this); this.write = bind(this.write, this); this.lumps = []; this._header = new WHeader(this); this._directory = new WDirectory(this); this.ptype = null; } WAD.read = function(buf) { var data, dirRead, dirdata, doff, i, llen, loff, lump, lumpData, numLumps, ptype, r, w; r = new BufferReader(buf); if (r._buffer.length < 12) { throw new Error("Invalid or corrupt WAD file: insufficient length for a full header!"); } ptype = r._buffer.slice(0, 4).toString('ascii'); r.readUInt32LE(); w = new WAD(ptype === "PWAD"); w.ptype = ptype; numLumps = r.readUInt32LE(); doff = r.readUInt32LE() - 12; data = buf.slice(11); if (data.length < doff) { throw new Error("Corrupt or incorrect WAD directory offset!"); } lumpData = data.slice(0, doff + 1); dirdata = data.slice(doff + 1); i = 0; while (i * 16 < dirdata.length) { dirRead = new BufferReader(dirdata.slice(i * 16, i * 16 + 16)); loff = dirRead.readUInt32LE() - 11; llen = dirRead.readUInt32LE(); lump = new WLump(this, lumpData.slice(loff, loff + llen).toString(), dirdata.slice(i * 16 + 8, i * 16 + 16).toString()); w.lumps.push(lump); i++; } return w; }; WAD.prototype.write = function() { var bw, j, l, len, ref; bw = new BufferWriter(); this._header.write(bw); ref = this.lumps; for (j = 0, len = ref.length; j < len; j++) { l = ref[j]; l.write(bw); } this._directory.write(bw); return bw.getContents(); }; WAD.prototype.addLump = function(name, data) { if (data == null) { data = ""; } if (name == null) { name = "_"; } this.lumps.push(new WLump(this, data, name)); return this; }; return WAD; })(); module.exports = WAD;