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.
This commit is contained in:
catloversg
2024-03-28 11:08:09 +07:00
committed by GitHub
parent 75dabd10be
commit 8553bcb8fc
22 changed files with 358 additions and 289 deletions

View File

@@ -1,11 +1,20 @@
/* 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, "utf8");
const data = await fs.readFile(file);
const save = JSON.parse(decodeURIComponent(escape(atob(data))));
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),
@@ -13,7 +22,6 @@ async function getSave(file) {
FactionsSave: JSON.parse(saveData.FactionsSave),
AliasesSave: JSON.parse(saveData.AliasesSave),
GlobalAliasesSave: JSON.parse(saveData.GlobalAliasesSave),
MessagesSave: JSON.parse(saveData.MessagesSave),
StockMarketSave: JSON.parse(saveData.StockMarketSave),
SettingsSave: JSON.parse(saveData.SettingsSave),
VersionSave: JSON.parse(saveData.VersionSave),