gulp-armapbo
Version:
The plugin for Gulp which allows to pack ArmA pbo files from sources.
61 lines (59 loc) • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const packingMethod_1 = require("../domain/packingMethod");
const lzhCompressor_1 = require("./lzh/lzhCompressor");
const lzhReporter_1 = require("./lzh/lzhReporter");
class PboBodyWriter {
constructor(options) {
this._reporter = new lzhReporter_1.LzhReporter(options);
this._lzhCompressor = new lzhCompressor_1.LzhCompressor(options);
}
writeBody(buffer, header) {
const size = header.entries.reduce((accumulated, entry) => {
const copied = this._writeEntry(buffer, entry, accumulated);
const offset = accumulated + copied;
return offset;
}, 0);
return size;
}
_writeEntry(buffer, entry, offset) {
const impl = entry.packingMethod === packingMethod_1.PackingMethod.packed
? this._writeCompressed
: this._writeUncompressed;
entry.dataSize = impl.call(this, buffer, entry, offset);
return entry.dataSize;
}
_writeUncompressed(buffer, entry, offset) {
const written = entry.contents.copy(buffer, offset, 0, entry.contents.length);
return written;
}
_writeCompressed(buffer, entry, offset) {
let written = this._safeWriteCompressed(buffer, entry, offset);
if (written >= entry.originalSize) {
written = this._writeUncompressed(buffer, entry, offset);
entry.__fallbackToUncompressed();
this._reporter.reportFile(entry.name, 1, 1); //0% compression
}
else {
this._reporter.reportFile(entry.name, entry.originalSize, written);
}
return written;
}
_safeWriteCompressed(buffer, entry, offset) {
let written = 0;
try {
written = this._lzhCompressor.writeCompressed(entry, buffer, offset);
}
catch (ex) {
if (ex instanceof RangeError) { //in some circumstances thrown when the last entry has been compressed to a size greater than original size and has owerflown the buffer space remaining
written = buffer.length;
}
else {
throw ex;
}
}
return written;
}
}
exports.PboBodyWriter = PboBodyWriter;
//# sourceMappingURL=pboBodyWriter.js.map