isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
46 lines (45 loc) • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveToDisk = saveToDisk;
const SerializationType_1 = require("../../../../enums/SerializationType");
const deepCopy_1 = require("../../../../functions/deepCopy");
const jsonHelpers_1 = require("../../../../functions/jsonHelpers");
const log_1 = require("../../../../functions/log");
const table_1 = require("../../../../functions/table");
const constants_1 = require("./constants");
function saveToDisk(mod, saveDataMap, saveDataConditionalFuncMap) {
const allSaveData = getAllSaveDataToWriteToDisk(saveDataMap, saveDataConditionalFuncMap);
const jsonString = (0, jsonHelpers_1.jsonEncode)(allSaveData);
mod.SaveData(jsonString);
(0, log_1.log)(`The save data manager wrote data to the "save#.dat" file for mod: ${mod.Name}`);
}
function getAllSaveDataToWriteToDisk(saveDataMap, saveDataConditionalFuncMap) {
const allSaveData = new LuaMap();
(0, table_1.iterateTableInOrder)(saveDataMap, (subscriberName, saveData) => {
// Handle the feature of the save data manager where certain mod features can conditionally
// write their data to disk.
const conditionalFunc = saveDataConditionalFuncMap.get(subscriberName);
if (conditionalFunc !== undefined) {
const shouldSave = conditionalFunc();
if (!shouldSave) {
return;
}
}
// Strip out the room part of the save data (and any other arbitrary fields that they might
// have added).
const saveDataWithoutRoom = {
persistent: saveData.persistent,
run: saveData.run,
level: saveData.level,
};
// If there is no data, then we can move on to the next feature.
if ((0, table_1.isTableEmpty)(saveDataWithoutRoom)) {
return;
}
// We need to serialize TypeScriptToLua maps and Isaac API objects such as `Color`.
// Recursively convert all such objects to Lua tables.
const saveDataCopy = (0, deepCopy_1.deepCopy)(saveDataWithoutRoom, SerializationType_1.SerializationType.SERIALIZE, subscriberName);
allSaveData.set(subscriberName, saveDataCopy);
}, constants_1.SAVE_DATA_MANAGER_DEBUG);
return allSaveData;
}