UNPKG

@etothepii/satisfactory-file-parser

Version:

A file parser for satisfactory files. Includes save files and blueprint files.

120 lines (119 loc) 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SaveStreamWriter = void 0; class SaveStreamWriter { constructor(writer) { this.writer = writer; this.mode = 'BEFORE_START'; this.formatTracker = { levels: [] }; } async beginSave() { if (this.mode !== 'BEFORE_START') { throw new Error(`Wrong order of commands. Already opened save. mode is ${this.mode}.`); } await this.writer.write('{'); this.mode = 'OPENED_SAVE'; } async writeHeader(header) { if (this.mode !== 'OPENED_SAVE') { throw new Error(`Wrong order of commands. Header has to come after save open. mode is ${this.mode}.`); } await this.writer.write(`"header": ${JSON.stringify(header)}`); this.mode = 'FINISHED_HEADER'; } async writeCompressionInfo(compressionInfo) { if (this.mode !== 'FINISHED_HEADER') { throw new Error(`Wrong order of commands. Compression info has to come after header. mode is ${this.mode}.`); } await this.writer.write(`, "compressionInfo": ${JSON.stringify(compressionInfo)}`); this.mode = 'WROTE_COMPRESSION_INFO'; } async writeGridHash(gridHash) { if (this.mode !== 'WROTE_COMPRESSION_INFO') { throw new Error(`Wrong order of commands. Save hbody hash has to come after compression info. mode is ${this.mode}.`); } await this.writer.write(`, "gridHash": ${JSON.stringify(gridHash)}`); this.mode = 'WROTE_GRID_HASH'; } async writeGrids(grids) { if (this.mode !== 'WROTE_GRID_HASH') { throw new Error(`Wrong order of commands. Grids have to come after save body hash info. mode is ${this.mode}.`); } await this.writer.write(`, "grids": ${JSON.stringify(grids)}`); this.mode = 'WROTE_GRIDS'; } async openLevels() { if (this.mode !== 'WROTE_GRIDS') { throw new Error(`Wrong order of commands. Levels have to come after grids info. mode is ${this.mode}.`); } await this.writer.write(`, "levels": [`); this.mode = 'OPENED_LEVELS'; } async openLevel(levelName) { if (this.mode !== 'OPENED_LEVELS' && this.mode !== 'FINISHED_LEVEL') { throw new Error(`Wrong order of commands. Single level can only come after opening levels array or after finishing single level. mode is ${this.mode}.`); } this.formatTracker.levels.push({ objectCount: 0, collectablesCount: 0 }); const prefix = this.formatTracker.levels.length > 1 ? ', ' : ''; await this.writer.write(`${prefix}{"name": "${levelName}", "objects": [`); this.mode = 'OPENED_LEVEL'; } async writeObjects(...objects) { if (this.mode !== 'OPENED_LEVEL' && this.mode !== 'WROTE_OBJECT') { throw new Error(`Wrong order of commands. An object can not be written into the level if it was not opened or other objects were not written. mode is ${this.mode}.`); } 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++; } this.mode = 'WROTE_OBJECT'; } async switchInLevelToCollectables() { if (this.mode !== 'OPENED_LEVEL' && this.mode !== 'WROTE_OBJECT') { throw new Error(`Wrong order of commands. The level structure can not be switched to collectables if the level was not opened recently or has not written objects. mode is ${this.mode}.`); } await this.writer.write(`], "collectables": [`); this.mode = 'SWITCH_TO_COLLECTABLES'; } async writeCollectables(...collectables) { if (this.mode !== 'SWITCH_TO_COLLECTABLES' && this.mode !== 'WROTE_COLLECTABLE') { throw new Error(`Wrong order of commands. A collectable can not be written into the level if we did not switch to collectables or other collectables were not written. mode is ${this.mode}.`); } 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++; } this.mode = 'WROTE_COLLECTABLE'; } async endLevel() { if (this.mode !== 'SWITCH_TO_COLLECTABLES' && this.mode !== 'WROTE_COLLECTABLE') { throw new Error(`Wrong order of commands. Single level can not be closed if a switch to collectibles or writing of collectible did not happen. mode is ${this.mode}.`); } await this.writer.write(`]}`); this.mode = 'FINISHED_LEVEL'; } async endLevels() { if (this.mode !== 'OPENED_LEVELS' && this.mode !== 'FINISHED_LEVEL') { throw new Error(`Wrong order of commands. Levels can only be closed after opening levels array or after finishing single level. mode is ${this.mode}.`); } await this.writer.write(`]`); this.mode = 'FINISHED_LEVELS'; } async endSave() { if (this.mode !== 'FINISHED_LEVELS') { throw new Error(`Wrong order of commands. Save has to end after levels. mode is ${this.mode}.`); } await this.writer.write('}'); this.mode = 'FINISHED_SAVE'; } async close() { return this.writer.close(); } } exports.SaveStreamWriter = SaveStreamWriter;