@etothepii/satisfactory-file-parser
Version:
A file parser for satisfactory files. Includes save files and blueprint files.
140 lines (139 loc) • 5.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ByteWriter = void 0;
const alignment_enum_1 = require("./alignment.enum");
class ByteWriter {
constructor(alignment, bufferSize = 500) {
this.getBufferPosition = () => this.currentByte;
this.getBufferSlice = (start, end) => this.bufferArray.slice(start, end);
this.alignment = alignment;
this.bufferArray = new ArrayBuffer(bufferSize);
this.bufferView = new DataView(this.bufferArray);
this.currentByte = 0;
}
skipBytes(count = 1) {
this.extendBufferIfNeeded(count);
this.currentByte += count;
}
jumpTo(pos) {
const count = pos - this.getBufferPosition();
this.skipBytes(count);
}
writeByte(value) {
this.extendBufferIfNeeded(1);
this.bufferView.setUint8(this.currentByte, value);
this.currentByte += 1;
}
writeBytesArray(bytes) {
this.writeBytes(new Uint8Array(bytes));
}
writeBytes(bytes) {
this.extendBufferIfNeeded(bytes.length);
bytes.forEach(byte => this.bufferView.setUint8(this.currentByte++, byte));
}
writeInt8(value) {
this.extendBufferIfNeeded(1);
this.bufferView.setInt8(this.currentByte, value);
this.currentByte += 1;
}
writeUint8(value) {
this.writeByte(value);
}
writeInt16(value) {
this.extendBufferIfNeeded(2);
this.bufferView.setInt16(this.currentByte, value, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
this.currentByte += 2;
}
writeUint16(value) {
this.extendBufferIfNeeded(2);
this.bufferView.setUint16(this.currentByte, value, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
this.currentByte += 2;
}
writeInt32(value) {
this.extendBufferIfNeeded(4);
this.bufferView.setInt32(this.currentByte, value, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
this.currentByte += 4;
}
writeUint32(value) {
this.extendBufferIfNeeded(4);
this.bufferView.setUint32(this.currentByte, value, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
this.currentByte += 4;
}
writeInt64(value) {
this.extendBufferIfNeeded(8);
this.bufferView.setBigInt64(this.currentByte, value, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
this.currentByte += 8;
}
writeUint64(value) {
this.extendBufferIfNeeded(8);
this.bufferView.setBigUint64(this.currentByte, value, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
this.currentByte += 8;
}
writeFloat(value) {
this.extendBufferIfNeeded(4);
this.bufferView.setFloat32(this.currentByte, value, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
this.currentByte += 4;
}
writeDouble(value) {
this.extendBufferIfNeeded(8);
this.bufferView.setFloat64(this.currentByte, value, this.alignment === alignment_enum_1.Alignment.LITTLE_ENDIAN);
this.currentByte += 8;
}
writeString(value) {
if (value.length === 0) {
this.writeInt32(0);
return;
}
if (ByteWriter.IsASCIICompatible(value)) {
this.writeInt32(value.length + 1);
for (let i = 0; i < value.length; i++) {
this.writeByte(value.charCodeAt(i));
}
this.writeUint8(0);
}
else {
this.writeInt32(-value.length - 1);
for (let i = 0; i < value.length; i++) {
this.writeUint16(value.charCodeAt(i));
}
this.writeUint16(0);
}
}
writeBinarySizeFromPosition(lenIndicatorPos, start) {
const after = this.getBufferPosition();
const writtenBytes = after - start;
this.jumpTo(lenIndicatorPos);
this.writeInt32(writtenBytes);
this.jumpTo(after);
}
extendBufferIfNeeded(countNeededBytes, factor = 1.5) {
if (this.currentByte + countNeededBytes > this.bufferView.byteLength) {
this.bufferArray = ByteWriter.AppendBuffer(this.bufferArray, new ArrayBuffer(factor * this.bufferArray.byteLength));
this.bufferView = new DataView(this.bufferArray);
}
}
truncateBuffer() {
this.bufferArray = this.bufferArray.slice(0, this.currentByte);
}
endWriting() {
this.truncateBuffer();
return this.bufferArray;
}
static ToInt32(num) {
return new Uint8Array([
(num & 0xff000000) >> 24,
(num & 0x00ff0000) >> 16,
(num & 0x0000ff00) >> 8,
(num & 0x000000ff)
]);
}
static AppendBuffer(buffer1, buffer2) {
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
}
;
}
ByteWriter.IsASCIICompatible = (value) => /^[\x00-\x7F]*$/.test(value);
exports.ByteWriter = ByteWriter;