2020-05-09 14:45:23 +00:00
|
|
|
import { ExplainedResult } from "../core/explained_result";
|
|
|
|
import { createLogger } from "../core/logging";
|
|
|
|
import { gComponentRegistry } from "../core/global_registries";
|
|
|
|
import { SerializerInternal } from "./serializer_internal";
|
|
|
|
|
2020-08-06 09:28:28 +00:00
|
|
|
/**
|
|
|
|
* @typedef {import("../game/component").Component} Component
|
|
|
|
* @typedef {import("../game/component").StaticComponent} StaticComponent
|
|
|
|
* @typedef {import("../game/entity").Entity} Entity
|
|
|
|
* @typedef {import("../game/root").GameRoot} GameRoot
|
|
|
|
* @typedef {import("../savegame/savegame_typedefs").SerializedGame} SerializedGame
|
|
|
|
*/
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
const logger = createLogger("savegame_serializer");
|
|
|
|
|
|
|
|
/**
|
2020-08-06 09:28:28 +00:00
|
|
|
* Serializes a savegame
|
2020-05-09 14:45:23 +00:00
|
|
|
*/
|
|
|
|
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) {
|
2020-08-06 09:28:28 +00:00
|
|
|
/** @type {SerializedGame} */
|
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-08-06 09:28:28 +00:00
|
|
|
entities: this.internal.serializeEntityArray(root.entityMgr.entities),
|
2020-06-26 16:24:02 +00:00
|
|
|
beltPaths: root.systemMgr.systems.belt.serializePaths(),
|
2020-05-14 19:54:11 +00:00
|
|
|
};
|
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-08-06 09:28:28 +00:00
|
|
|
* @param {SerializedGame} 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
|
2020-08-06 09:28:28 +00:00
|
|
|
for (let i = 0; i < savegame.entities.length; ++i) {
|
|
|
|
/** @type {Entity} */
|
|
|
|
const entity = savegame.entities[i];
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const componentClass = gComponentRegistry.findById(componentId);
|
|
|
|
|
|
|
|
// Check component id is known
|
|
|
|
if (!componentClass) {
|
|
|
|
return ExplainedResult.bad("Unknown component id: " + componentId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify component data
|
|
|
|
const componentData = components[componentId];
|
|
|
|
const componentVerifyError = /** @type {StaticComponent} */ (componentClass).verify(
|
|
|
|
componentData
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check component data is ok
|
|
|
|
if (componentVerifyError) {
|
|
|
|
return ExplainedResult.bad(
|
|
|
|
"Component " + componentId + " has invalid data: " + componentVerifyError
|
|
|
|
);
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExplainedResult.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to load the savegame from a given dump
|
2020-08-06 09:28:28 +00:00
|
|
|
* @param {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-06-26 16:24:02 +00:00
|
|
|
errorReason = errorReason || root.systemMgr.systems.belt.deserializePaths(savegame.beltPaths);
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
// Check for errors
|
|
|
|
if (errorReason) {
|
|
|
|
return ExplainedResult.bad(errorReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExplainedResult.good();
|
|
|
|
}
|
|
|
|
}
|