UNPKG

@etothepii/satisfactory-file-parser

Version:

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

81 lines (80 loc) 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Parser = void 0; const blueprint_reader_1 = require("./satisfactory/blueprint/blueprint-reader"); const blueprint_writer_1 = require("./satisfactory/blueprint/blueprint-writer"); const satisfactory_save_1 = require("./satisfactory/save/satisfactory-save"); const save_reader_1 = require("./satisfactory/save/save-reader"); const save_writer_1 = require("./satisfactory/save/save-writer"); class Parser { static WriteSave(save, onBinaryBeforeCompressing, onHeader, onChunk) { const writer = new save_writer_1.SaveWriter(); save_writer_1.SaveWriter.WriteHeader(writer, save.header); const posAfterHeader = writer.getBufferPosition(); save_writer_1.SaveWriter.WriteSaveBodyHash(writer, save.gridHash); save_writer_1.SaveWriter.WriteGrids(writer, save.grids); save_writer_1.SaveWriter.WriteLevels(writer, save, save.header.buildVersion); writer.endWriting(); const chunkSummary = writer.generateChunks(save.compressionInfo, posAfterHeader, onBinaryBeforeCompressing, onHeader, onChunk); return chunkSummary; } static ParseSaveFile(name, file, onDecompressedSaveBody = () => { }, onProgress = () => { }) { const reader = new save_reader_1.SaveReader(new Uint8Array(file).buffer, onProgress); const header = reader.readHeader(); const save = new satisfactory_save_1.SatisfactorySave(name, header); const inflateResult = reader.inflateChunks(); onDecompressedSaveBody(reader.getBuffer()); const gridHash = reader.readSaveBodyHash(); save.gridHash = gridHash; const grids = reader.readGrids(); save.grids = grids; save.levels = reader.readLevels(); save.compressionInfo = reader.compressionInfo; save.trailingCollectedObjects = reader.trailingCollectedObjects; return save; } static WriteBlueprintFiles(blueprint, onMainFileBinaryBeforeCompressing = () => { }, onMainFileHeader = () => { }, onMainFileChunk = () => { }) { const blueprintWriter = new blueprint_writer_1.BlueprintWriter(); blueprint_writer_1.BlueprintWriter.SerializeHeader(blueprintWriter, blueprint.header); const saveBodyPos = blueprintWriter.getBufferPosition(); blueprint_writer_1.BlueprintWriter.SerializeObjects(blueprintWriter, blueprint.objects); blueprintWriter.endWriting(); let binaryChunks = []; let binaryHeader; const mainFileChunkSummary = blueprintWriter.generateChunks(blueprint.compressionInfo, saveBodyPos, onMainFileBinaryBeforeCompressing, onMainFileHeader, onMainFileChunk); const configWriter = new blueprint_writer_1.BlueprintConfigWriter(); blueprint_writer_1.BlueprintConfigWriter.SerializeConfig(configWriter, blueprint.config); const configFileBinary = configWriter.endWriting(); return { mainFileChunkSummary, configFileBinary }; } static ParseBlueprintFiles(name, blueprintFile, blueprintConfigFile, onDecompressedBlueprintBody = () => { }) { const blueprintConfigReader = new blueprint_reader_1.BlueprintConfigReader(new Uint8Array(blueprintConfigFile).buffer); const config = blueprint_reader_1.BlueprintConfigReader.ParseConfig(blueprintConfigReader); const blueprintReader = new blueprint_reader_1.BlueprintReader(new Uint8Array(blueprintFile).buffer); const header = blueprint_reader_1.BlueprintReader.ReadHeader(blueprintReader); const inflateResult = blueprintReader.inflateChunks(); onDecompressedBlueprintBody(inflateResult.inflatedData); const blueprintObjects = blueprint_reader_1.BlueprintReader.ParseObjects(blueprintReader); const blueprint = { name, compressionInfo: blueprintReader.compressionInfo, header: header, config, objects: blueprintObjects }; return blueprint; } } Parser.JSONStringifyModified = (obj, indent = 0) => JSON.stringify(obj, (key, value) => { if (typeof value === 'bigint') { return value.toString(); } else if (value === 0 && 1 / value < 0) { return '-0'; } return value; }, indent); exports.Parser = Parser;