2020-05-09 14:45:23 +00:00
|
|
|
/* typehints:start */
|
|
|
|
import { Component } from "../game/component";
|
|
|
|
import { GameRoot } from "../game/root";
|
|
|
|
/* typehints:end */
|
|
|
|
|
|
|
|
import { JSON_stringify } from "../core/builtins";
|
|
|
|
import { ExplainedResult } from "../core/explained_result";
|
|
|
|
import { createLogger } from "../core/logging";
|
|
|
|
// import { BuildingComponent } from "../components/impl/building";
|
|
|
|
import { gComponentRegistry } from "../core/global_registries";
|
|
|
|
import { SerializerInternal } from "./serializer_internal";
|
|
|
|
|
|
|
|
const logger = createLogger("savegame_serializer");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to serialize a savegame
|
|
|
|
*/
|
|
|
|
export class SavegameSerializer {
|
|
|
|
constructor() {
|
|
|
|
this.internal = new SerializerInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes the game root into a dump
|
|
|
|
* @param {GameRoot} root
|
|
|
|
* @param {boolean=} sanityChecks Whether to check for validity
|
2020-05-14 19:54:11 +00:00
|
|
|
* @returns {object}
|
2020-05-09 14:45:23 +00:00
|
|
|
*/
|
|
|
|
generateDumpFromGameRoot(root, sanityChecks = true) {
|
|
|
|
// Finalize particles before saving (Like granting destroy indicator rewards)
|
|
|
|
// root.particleMgr.finalizeBeforeSave();
|
|
|
|
// root.uiParticleMgr.finalizeBeforeSave();
|
|
|
|
|
|
|
|
// Now store generic savegame payload
|
2020-05-14 19:54:11 +00:00
|
|
|
const data = {
|
2020-05-09 14:45:23 +00:00
|
|
|
camera: root.camera.serialize(),
|
|
|
|
time: root.time.serialize(),
|
2020-05-14 19:54:11 +00:00
|
|
|
map: root.map.serialize(),
|
2020-05-09 14:45:23 +00:00
|
|
|
entityMgr: root.entityMgr.serialize(),
|
2020-05-14 19:54:11 +00:00
|
|
|
hubGoals: root.hubGoals.serialize(),
|
2020-05-28 12:53:11 +00:00
|
|
|
pinnedShapes: root.hud.parts.pinnedShapes.serialize(),
|
2020-05-28 18:03:35 +00:00
|
|
|
waypoints: root.hud.parts.waypoints.serialize(),
|
2020-05-14 19:54:11 +00:00
|
|
|
};
|
2020-05-09 14:45:23 +00:00
|
|
|
|
2020-05-14 19:54:11 +00:00
|
|
|
data.entities = this.internal.serializeEntityArray(root.entityMgr.entities);
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
if (!G_IS_RELEASE) {
|
|
|
|
if (sanityChecks) {
|
|
|
|
// Sanity check
|
|
|
|
const sanity = this.verifyLogicalErrors(data);
|
|
|
|
if (!sanity.result) {
|
|
|
|
logger.error("Created invalid savegame:", sanity.reason, "savegame:", data);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies if there are logical errors in the savegame
|
2020-05-14 19:54:11 +00:00
|
|
|
* @param {object} savegame
|
2020-05-09 14:45:23 +00:00
|
|
|
* @returns {ExplainedResult}
|
|
|
|
*/
|
|
|
|
verifyLogicalErrors(savegame) {
|
|
|
|
if (!savegame.entities) {
|
|
|
|
return ExplainedResult.bad("Savegame has no entities");
|
|
|
|
}
|
|
|
|
|
|
|
|
const seenUids = [];
|
|
|
|
|
|
|
|
// Check for duplicate UIDS
|
|
|
|
for (const entityListId in savegame.entities) {
|
|
|
|
for (let i = 0; i < savegame.entities[entityListId].length; ++i) {
|
|
|
|
const list = savegame.entities[entityListId][i];
|
|
|
|
for (let k = 0; k < list.length; ++k) {
|
|
|
|
const entity = list[k];
|
|
|
|
const uid = entity.uid;
|
|
|
|
if (!Number.isInteger(uid)) {
|
|
|
|
return ExplainedResult.bad("Entity has invalid uid: " + uid);
|
|
|
|
}
|
|
|
|
if (seenUids.indexOf(uid) >= 0) {
|
|
|
|
return ExplainedResult.bad("Duplicate uid " + uid);
|
|
|
|
}
|
|
|
|
seenUids.push(uid);
|
|
|
|
|
|
|
|
// Verify components
|
|
|
|
if (!entity.components) {
|
|
|
|
return ExplainedResult.bad(
|
|
|
|
"Entity is missing key 'components': " + JSON_stringify(entity)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const components = entity.components;
|
|
|
|
for (const componentId in components) {
|
|
|
|
// Verify component data
|
|
|
|
const componentData = components[componentId];
|
|
|
|
const componentClass = gComponentRegistry.findById(componentId);
|
|
|
|
|
|
|
|
// Check component id is known
|
|
|
|
if (!componentClass) {
|
|
|
|
return ExplainedResult.bad("Unknown component id: " + componentId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check component data is ok
|
|
|
|
const componentVerifyError = /** @type {typeof Component} */ (componentClass).verify(
|
|
|
|
componentData
|
|
|
|
);
|
|
|
|
if (componentVerifyError) {
|
|
|
|
return ExplainedResult.bad(
|
|
|
|
"Component " + componentId + " has invalid data: " + componentVerifyError
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExplainedResult.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to load the savegame from a given dump
|
2020-05-28 12:53:11 +00:00
|
|
|
* @param {import("./savegame_typedefs").SerializedGame} savegame
|
2020-05-09 14:45:23 +00:00
|
|
|
* @param {GameRoot} root
|
|
|
|
* @returns {ExplainedResult}
|
|
|
|
*/
|
|
|
|
deserialize(savegame, root) {
|
|
|
|
// Sanity
|
|
|
|
const verifyResult = this.verifyLogicalErrors(savegame);
|
|
|
|
if (!verifyResult.result) {
|
|
|
|
return ExplainedResult.bad(verifyResult.reason);
|
|
|
|
}
|
|
|
|
let errorReason = null;
|
|
|
|
|
|
|
|
errorReason = errorReason || root.entityMgr.deserialize(savegame.entityMgr);
|
|
|
|
errorReason = errorReason || root.time.deserialize(savegame.time);
|
|
|
|
errorReason = errorReason || root.camera.deserialize(savegame.camera);
|
2020-05-14 19:54:11 +00:00
|
|
|
errorReason = errorReason || root.map.deserialize(savegame.map);
|
|
|
|
errorReason = errorReason || root.hubGoals.deserialize(savegame.hubGoals);
|
2020-05-28 12:53:11 +00:00
|
|
|
errorReason = errorReason || root.hud.parts.pinnedShapes.deserialize(savegame.pinnedShapes);
|
2020-05-28 18:03:35 +00:00
|
|
|
errorReason = errorReason || root.hud.parts.waypoints.deserialize(savegame.waypoints);
|
2020-05-14 19:54:11 +00:00
|
|
|
errorReason = errorReason || this.internal.deserializeEntityArray(root, savegame.entities);
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
// Check for errors
|
|
|
|
if (errorReason) {
|
|
|
|
return ExplainedResult.bad(errorReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExplainedResult.good();
|
|
|
|
}
|
|
|
|
}
|