@etothepii/satisfactory-file-parser
Version:
A file parser for satisfactory files. Includes save files and blueprint files.
87 lines (86 loc) • 4.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaveStreamWriter = void 0;
class ModeStateTracker {
constructor(mode) {
this.mode = mode;
}
checkIsComingFrom(...allowedPredecessors) {
if (!allowedPredecessors.includes(this.mode)) {
throw new Error(`Wrong order of commands. mode is ${this.mode}. but only ${allowedPredecessors.join(', ')} is/are allowed.`);
}
}
advance(newMode) {
this.mode = newMode;
}
}
class SaveStreamWriter {
constructor(writer) {
this.writer = writer;
this.createExecutionFunction = async (allowedInputModes, fn, targetMode) => {
this.tracker.checkIsComingFrom(...allowedInputModes);
await fn();
this.tracker.advance(targetMode);
};
this.beginSave = () => this.createExecutionFunction(['BEFORE_START'], async () => {
await this.writer.write('{');
}, 'OPENED_SAVE');
this.writeHeader = (header) => this.createExecutionFunction(['OPENED_SAVE'], async () => {
await this.writer.write(`"header": ${JSON.stringify(header)}`);
}, 'FINISHED_HEADER');
this.writeCompressionInfo = (compressionInfo) => this.createExecutionFunction(['FINISHED_HEADER'], async () => {
await this.writer.write(`, "compressionInfo": ${JSON.stringify(compressionInfo)}`);
}, 'WROTE_COMPRESSION_INFO');
this.writeGridHash = (gridHash) => this.createExecutionFunction(['WROTE_COMPRESSION_INFO'], async () => {
await this.writer.write(`, "gridHash": ${JSON.stringify(gridHash)}`);
}, 'WROTE_GRID_HASH');
this.writeGrids = (grids) => this.createExecutionFunction(['WROTE_GRID_HASH'], async () => {
await this.writer.write(`, "grids": ${JSON.stringify(grids)}`);
}, 'WROTE_GRIDS');
this.openLevels = () => this.createExecutionFunction(['WROTE_GRIDS'], async () => {
await this.writer.write(`, "levels": [`);
}, 'OPENED_LEVELS');
this.openLevel = (levelName) => this.createExecutionFunction(['OPENED_LEVELS', 'FINISHED_LEVEL'], async () => {
this.formatTracker.levels.push({
objectCount: 0, collectablesCount: 0
});
const prefix = this.formatTracker.levels.length > 1 ? ', ' : '';
await this.writer.write(`${prefix}{"name": "${levelName}", "objects": [`);
}, 'OPENED_LEVEL');
this.writeObjects = (...objects) => this.createExecutionFunction(['OPENED_LEVEL', 'WROTE_OBJECT'], async () => {
const stringified = objects.map(saveObj => JSON.stringify(saveObj));
for (const obj of stringified) {
await this.writer.write(`${this.formatTracker.levels.at(-1).objectCount >= 1 ? ', ' : ''}${obj}`);
this.formatTracker.levels.at(-1).objectCount++;
}
}, 'WROTE_OBJECT');
this.switchInLevelToCollectables = (...objects) => this.createExecutionFunction(['OPENED_LEVEL', 'WROTE_OBJECT'], async () => {
await this.writer.write(`], "collectables": [`);
}, 'SWITCH_TO_COLLECTABLES');
this.writeCollectables = (...collectables) => this.createExecutionFunction(['SWITCH_TO_COLLECTABLES', 'WROTE_COLLECTABLE'], async () => {
const stringified = collectables.map(coll => JSON.stringify(coll));
for (const obj of stringified) {
await this.writer.write(`${this.formatTracker.levels.at(-1).collectablesCount >= 1 ? ', ' : ''}${obj}`);
this.formatTracker.levels.at(-1).collectablesCount++;
}
}, 'WROTE_COLLECTABLE');
this.endLevel = () => this.createExecutionFunction(['SWITCH_TO_COLLECTABLES', 'WROTE_COLLECTABLE'], async () => {
await this.writer.write(`]}`);
}, 'FINISHED_LEVEL');
this.endLevels = () => this.createExecutionFunction(['OPENED_LEVELS', 'FINISHED_LEVEL'], async () => {
await this.writer.write(`]`);
}, 'FINISHED_LEVELS');
this.endSave = () => this.createExecutionFunction(['FINISHED_LEVELS'], async () => {
await this.writer.write('}');
}, 'FINISHED_SAVE');
this.mode = 'BEFORE_START';
this.tracker = new ModeStateTracker(this.mode);
this.formatTracker = {
levels: []
};
}
async close() {
return this.writer.close();
}
}
exports.SaveStreamWriter = SaveStreamWriter;