From e8c93654658e9807602f388c9f1882ff6cbe8b53 Mon Sep 17 00:00:00 2001 From: PostCrafter Date: Mon, 2 Jan 2017 13:02:21 +0100 Subject: [PATCH 1/2] Make memory property configurable This allows actually using the custom serialization explained at http://support.screeps.com/hc/en-us/articles/203016642-Working-with-memory Right now `Creep#memory`, `Flag#memory`, `Room#memory` and `Spawn#Memory` always access `globals.Memory` which is not changeable from inside users code. Therefor Memory parsing will happen twice, once by the users code and once by the first access to `globals.Memory`, resulting in two different objects. The solution to this would be tracking down why setting `Memory` (or `global.Memory`) in code doesn't change `globals.Memory`, but I don't have enough experience with the screeps server and the vm module. For now this just makes the `memory` property configurable, so we can change it to actually access our version of the parsed Memory. Test case: ``` module.exports.loop = function loop() { Memory = JSON.parse(RawMemory.get()); const creepName = "test"; const creep = Game.creeps[creepName]; if (!creep) { _.first(_.values(Game.spawns)).createCreep([MOVE], creepName); } else { console.log(`--- Test case (tick: ${Game.time}) ---`); Memory.creeps[creepName].tick = Game.time; const gMemory = Memory.creeps[creepName].tick; const cMemory = creep.memory.tick; if (globalMemoryTick === creepMemoryTick) { console.log(`success: ${gMemory} === ${cMemory}`); } else { console.log(`failed: ${gMemory} !== ${cMemory}`); } } RawMemory.set(JSON.stringify(Memory)); } ``` --- src/game/creeps.js | 3 ++- src/game/flags.js | 3 ++- src/game/rooms.js | 3 ++- src/game/structures.js | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/game/creeps.js b/src/game/creeps.js index fa1238d5..682271ed 100644 --- a/src/game/creeps.js +++ b/src/game/creeps.js @@ -98,7 +98,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) { throw new Error('Could not set creep memory'); } globals.Memory.creeps[this.name] = value; - } + }, + configurable: true }); Creep.prototype.toString = register.wrapFn(function() { diff --git a/src/game/flags.js b/src/game/flags.js index 1469c89c..66ed3b64 100644 --- a/src/game/flags.js +++ b/src/game/flags.js @@ -47,7 +47,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) { throw new Error('Could not set flag memory'); } globals.Memory.flags[this.name] = value; - } + }, + configurable: true }); Flag.prototype.toString = register.wrapFn(function() { diff --git a/src/game/rooms.js b/src/game/rooms.js index b2ee09ba..b484430e 100644 --- a/src/game/rooms.js +++ b/src/game/rooms.js @@ -532,7 +532,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) { throw new Error('Could not set room memory'); } globals.Memory.rooms[this.name] = value; - } + }, + configurable: true }); Room.prototype.find = register.wrapFn(function(type, opts) { diff --git a/src/game/structures.js b/src/game/structures.js index 5df958ae..bf256f51 100644 --- a/src/game/structures.js +++ b/src/game/structures.js @@ -912,7 +912,8 @@ exports.make = function(_runtimeData, _intents, _register, _globals) { throw new Error('Could not set spawn memory'); } globals.Memory.spawns[data(this.id).name] = value; - } + }, + configurable: true }); StructureSpawn.prototype.toString = register.wrapFn(function() { From dfa538e129c11ef0288063bd9b08b834e125888f Mon Sep 17 00:00:00 2001 From: PostCrafter Date: Tue, 3 Jan 2017 00:41:35 +0100 Subject: [PATCH 2/2] Add setter to global.Memory --- src/game/game.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/game/game.js b/src/game/game.js index 224a3e2b..8a323a3c 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -429,13 +429,16 @@ runCodeCache[userId].memory._parsed = null; } + this.Memory = runCodeCache[userId].memory._parsed; + + return runCodeCache[userId].memory._parsed; + }, + set(memory) { Object.defineProperty(runCodeCache[userId].globals, 'Memory', { configurable: true, enumerable: true, - value: runCodeCache[userId].memory._parsed + value: memory }); - - return runCodeCache[userId].memory._parsed; } });