1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Only store changed properties for all components

This commit is contained in:
tobspr
2020-08-10 22:53:02 +02:00
parent bb431b8490
commit 9701a143ec
22 changed files with 100 additions and 152 deletions

View File

@@ -871,14 +871,17 @@ export class TypeArray extends BaseDataType {
* @returns {string|void} String error code or null on success
*/
deserialize(value, targetObject, targetKey, root) {
const result = new Array(value.length);
let destination = targetObject[targetKey];
if (!destination) {
targetObject[targetKey] = destination = new Array(value.length);
}
for (let i = 0; i < value.length; ++i) {
const errorStatus = this.innerType.deserializeWithVerify(value[i], result, i, root);
const errorStatus = this.innerType.deserializeWithVerify(value[i], destination, i, root);
if (errorStatus) {
return errorStatus;
}
}
targetObject[targetKey] = result;
}
getAsJsonSchemaUncached() {
@@ -1226,15 +1229,18 @@ export class TypeStructuredObject extends BaseDataType {
* @returns {string|void} String error code or null on success
*/
deserialize(value, targetObject, targetKey, root) {
let result = {};
let target = targetObject[targetKey];
if (!target) {
targetObject[targetKey] = target = {};
}
for (const key in value) {
const valueType = this.descriptor[key];
const errorCode = valueType.deserializeWithVerify(value[key], result, key, root);
const errorCode = valueType.deserializeWithVerify(value[key], target, key, root);
if (errorCode) {
return errorCode;
}
}
targetObject[targetKey] = result;
}
getAsJsonSchemaUncached() {

View File

@@ -1,6 +1,10 @@
import { gComponentRegistry } from "../core/global_registries";
import { createLogger } from "../core/logging";
import { Vector } from "../core/vector";
import { getBuildingDataFromCode } from "../game/building_codes";
import { Entity } from "../game/entity";
import { enumLayer, GameRoot } from "../game/root";
import { GameRoot } from "../game/root";
const logger = createLogger("serializer_internal");
// Internal serializer methods
export class SerializerInternal {
@@ -37,19 +41,27 @@ export class SerializerInternal {
* @param {Entity} payload
*/
deserializeEntity(root, payload) {
const entity = new Entity(root);
this.deserializeComponents(entity, payload.components);
entity.layer = payload.layer;
const staticData = payload.components.StaticMapEntity;
assert(staticData, "entity has no static data");
if (!enumLayer[payload.layer]) {
assert(false, "Invalid layer: " + payload.layer);
}
const code = staticData.code;
const data = getBuildingDataFromCode(code);
const metaBuilding = data.metaInstance;
const entity = metaBuilding.createEntity({
root,
origin: Vector.fromSerializedObject(staticData.origin),
rotation: staticData.rotation,
originalRotation: staticData.originalRotation,
rotationVariant: data.rotationVariant,
variant: data.variant,
});
this.deserializeComponents(entity, payload.components);
root.entityMgr.registerEntity(entity, payload.uid);
if (entity.components.StaticMapEntity) {
root.map.placeStaticEntity(entity);
}
root.map.placeStaticEntity(entity);
}
/////// COMPONENTS ////
@@ -62,10 +74,12 @@ export class SerializerInternal {
*/
deserializeComponents(entity, data) {
for (const componentId in data) {
const componentClass = gComponentRegistry.findById(componentId);
const componentHandle = new componentClass({});
entity.addComponent(componentHandle);
const errorStatus = componentHandle.deserialize(data[componentId]);
if (!entity.components[componentId]) {
logger.warn("Entity no longer has component:", componentId);
continue;
}
const errorStatus = entity.components[componentId].deserialize(data[componentId]);
if (errorStatus) {
return errorStatus;
}