bitburner-src/tools/pretty-save.js
catloversg 8553bcb8fc
MISC: Support compression of save data (#1162)
* Use Compression Streams API instead of jszip or other libraries.
* Remove usage of base64 in the new binary format.
* Do not convert binary data to string and back. The type of save data is SaveData, it's either string (old base64 format) or Uint8Array (new binary format).
* Proper support for interacting with electron-related code. Electron-related code assumes that save data is in the base64 format.
* Proper support for other tools (DevMenu, pretty-save.js). Full support for DevMenu will be added in a follow-up PR. Check the comments in src\DevMenu\ui\SaveFileDev.tsx for details.
2024-03-27 21:08:09 -07:00

63 lines
2.0 KiB
JavaScript

/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require("fs").promises;
const path = require("path");
const { isBinaryFormat } = require("../electron/saveDataBinaryFormat");
async function getSave(file) {
const data = await fs.readFile(file);
let jsonSaveString;
if (isBinaryFormat(data)) {
const decompressedReadableStream = new Blob([data]).stream().pipeThrough(new DecompressionStream("gzip"));
jsonSaveString = await new Response(decompressedReadableStream).text();
} else {
jsonSaveString = decodeURIComponent(escape(atob(data)));
}
const save = JSON.parse(jsonSaveString);
const saveData = save.data;
let gameSave = {
PlayerSave: JSON.parse(saveData.PlayerSave),
CompaniesSave: JSON.parse(saveData.CompaniesSave),
FactionsSave: JSON.parse(saveData.FactionsSave),
AliasesSave: JSON.parse(saveData.AliasesSave),
GlobalAliasesSave: JSON.parse(saveData.GlobalAliasesSave),
StockMarketSave: JSON.parse(saveData.StockMarketSave),
SettingsSave: JSON.parse(saveData.SettingsSave),
VersionSave: JSON.parse(saveData.VersionSave),
LastExportBonus: JSON.parse(saveData.LastExportBonus),
StaneksGiftSave: JSON.parse(saveData.StaneksGiftSave),
SaveTimestamp: new Date(parseInt(saveData.SaveTimestamp ?? "0", 10)).toLocaleString(),
};
const serverStrings = JSON.parse(saveData.AllServersSave);
const servers = {};
for (const [key, value] of Object.entries(serverStrings)) {
servers[key] = value.data;
}
gameSave.AllServersSave = servers;
if (saveData.AllGangsSave) {
gameSave.AllGangsSave = JSON.parse(saveData.AllGangsSave);
}
return gameSave;
}
async function main(input, output) {
const result = await getSave(input);
await fs.writeFile(output, JSON.stringify(result, null, 2));
return result;
}
const input = path.resolve(process.argv[2]);
const output = path.resolve(process.argv[3]);
console.log(`Input: ${input}`);
console.log(`Output: ${output}`);
main(input, output).then(() => {
console.log("Done!");
});