mirror of
https://github.com/tobspr/shapez.io.git
synced 2026-03-02 03:39:21 +00:00
Initial support for blueprints (Buggy)
This commit is contained in:
@@ -11,8 +11,7 @@ import { createLogger } from "../core/logging";
|
||||
import { globalConfig } from "../core/config";
|
||||
import { SavegameInterface_V1000 } from "./schemas/1000";
|
||||
import { getSavegameInterface } from "./savegame_interface_registry";
|
||||
import { compressObject } from "./savegame_compressor";
|
||||
import { compressX64 } from "../core/lzstring";
|
||||
import { SavegameInterface_V1001 } from "./schemas/1001";
|
||||
|
||||
const logger = createLogger("savegame");
|
||||
|
||||
@@ -29,7 +28,7 @@ export class Savegame extends ReadWriteProxy {
|
||||
this.internalId = internalId;
|
||||
this.metaDataRef = metaDataRef;
|
||||
|
||||
/** @type {SavegameData} */
|
||||
/** @type {import("./savegame_typedefs").SavegameData} */
|
||||
this.currentData = this.getDefaultData();
|
||||
}
|
||||
|
||||
@@ -39,14 +38,14 @@ export class Savegame extends ReadWriteProxy {
|
||||
* @returns {number}
|
||||
*/
|
||||
static getCurrentVersion() {
|
||||
return 1000;
|
||||
return 1001;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {typeof BaseSavegameInterface}
|
||||
*/
|
||||
static getReaderClass() {
|
||||
return SavegameInterface_V1000;
|
||||
return SavegameInterface_V1001;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,7 +57,7 @@ export class Savegame extends ReadWriteProxy {
|
||||
|
||||
/**
|
||||
* Returns the savegames default data
|
||||
* @returns {SavegameData}
|
||||
* @returns {import("./savegame_typedefs").SavegameData}
|
||||
*/
|
||||
getDefaultData() {
|
||||
return {
|
||||
@@ -73,18 +72,25 @@ export class Savegame extends ReadWriteProxy {
|
||||
|
||||
/**
|
||||
* Migrates the savegames data
|
||||
* @param {SavegameData} data
|
||||
* @param {import("./savegame_typedefs").SavegameData} data
|
||||
*/
|
||||
migrate(data) {
|
||||
if (data.version < 1000) {
|
||||
return ExplainedResult.bad("Can not migrate savegame, too old");
|
||||
}
|
||||
|
||||
console.log("TODO: Migrate from", data.version);
|
||||
if (data.version === 1000) {
|
||||
SavegameInterface_V1001.migrate1000to1001(data);
|
||||
data.version = 1001;
|
||||
}
|
||||
|
||||
return ExplainedResult.good();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the savegames data
|
||||
* @param {SavegameData} data
|
||||
* @param {import("./savegame_typedefs").SavegameData} data
|
||||
*/
|
||||
verify(data) {
|
||||
if (!data.dump) {
|
||||
@@ -109,7 +115,7 @@ export class Savegame extends ReadWriteProxy {
|
||||
}
|
||||
/**
|
||||
* Returns the statistics of the savegame
|
||||
* @returns {SavegameStats}
|
||||
* @returns {import("./savegame_typedefs").SavegameStats}
|
||||
*/
|
||||
getStatistics() {
|
||||
return this.currentData.stats;
|
||||
@@ -132,7 +138,7 @@ export class Savegame extends ReadWriteProxy {
|
||||
|
||||
/**
|
||||
* Returns the current game dump
|
||||
* @returns {SerializedGame}
|
||||
* @returns {import("./savegame_typedefs").SerializedGame}
|
||||
*/
|
||||
getCurrentDump() {
|
||||
return this.currentData.dump;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { BaseSavegameInterface } from "./savegame_interface";
|
||||
import { SavegameInterface_V1000 } from "./schemas/1000";
|
||||
import { createLogger } from "../core/logging";
|
||||
import { SavegameInterface_V1001 } from "./schemas/1001";
|
||||
|
||||
/** @type {Object.<number, typeof BaseSavegameInterface>} */
|
||||
const interfaces = {
|
||||
1000: SavegameInterface_V1000,
|
||||
1001: SavegameInterface_V1001,
|
||||
};
|
||||
|
||||
const logger = createLogger("savegame_interface_registry");
|
||||
|
||||
@@ -4,14 +4,7 @@
|
||||
* }} SavegameStats
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* x: number,
|
||||
* y: number,
|
||||
* uid: number,
|
||||
* key: string
|
||||
* }} SerializedMapResource
|
||||
*/
|
||||
import { Entity } from "../game/entity";
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
@@ -20,7 +13,7 @@
|
||||
* entityMgr: any,
|
||||
* map: any,
|
||||
* hubGoals: any,
|
||||
* entities: Array<any>
|
||||
* entities: Array<Entity>
|
||||
* }} SerializedGame
|
||||
*/
|
||||
|
||||
|
||||
52
src/js/savegame/schemas/1001.js
Normal file
52
src/js/savegame/schemas/1001.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import { SavegameInterface_V1000 } from "./1000.js";
|
||||
import { createLogger } from "../../core/logging.js";
|
||||
|
||||
const schema = require("./1001.json");
|
||||
|
||||
const logger = createLogger("savegame_interface/1001");
|
||||
|
||||
export class SavegameInterface_V1001 extends SavegameInterface_V1000 {
|
||||
getVersion() {
|
||||
return 1001;
|
||||
}
|
||||
|
||||
getSchemaUncached() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("../savegame_typedefs.js").SavegameData} data
|
||||
*/
|
||||
static migrate1000to1001(data) {
|
||||
logger.log("Migrating 1000 to 1001");
|
||||
const dump = data.dump;
|
||||
if (!dump) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const entities = dump.entities;
|
||||
for (let i = 0; i < entities.length; ++i) {
|
||||
const entity = entities[i];
|
||||
|
||||
const staticComp = entity.components.StaticMapEntity;
|
||||
const beltComp = entity.components.Belt;
|
||||
if (staticComp) {
|
||||
if (staticComp.spriteKey) {
|
||||
staticComp.blueprintSpriteKey = staticComp.spriteKey.replace(
|
||||
"sprites/buildings",
|
||||
"sprites/blueprints"
|
||||
);
|
||||
} else {
|
||||
if (entity.components.Hub) {
|
||||
staticComp.blueprintSpriteKey = "";
|
||||
} else if (beltComp) {
|
||||
const direction = beltComp.direction;
|
||||
staticComp.blueprintSpriteKey = "sprites/blueprints/belt_" + direction + ".png";
|
||||
} else {
|
||||
assertAlways(false, "Could not deduct entity type for migrating 1000 -> 1001");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
5
src/js/savegame/schemas/1001.json
Normal file
5
src/js/savegame/schemas/1001.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"type": "object",
|
||||
"required": [],
|
||||
"additionalProperties": true
|
||||
}
|
||||
@@ -43,7 +43,7 @@ export class SerializerInternal {
|
||||
* @param {Entity} payload
|
||||
*/
|
||||
deserializeEntity(root, payload) {
|
||||
const entity = new Entity(null);
|
||||
const entity = new Entity(root);
|
||||
this.deserializeComponents(entity, payload.components);
|
||||
|
||||
root.entityMgr.registerEntity(entity, payload.uid);
|
||||
|
||||
Reference in New Issue
Block a user