From e2c62a6522ea16cf32bb2ada55292c91b0d87e51 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 11 Jul 2024 15:43:08 -0500 Subject: [PATCH] refactor: script API --- app/client-ecs.js | 20 ++++++++++++++++---- app/ecs-components/inventory.js | 2 +- app/ecs-components/plant.js | 2 +- app/engine.js | 19 ++++++++++++++----- app/util/script.js | 23 +---------------------- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/app/client-ecs.js b/app/client-ecs.js index 2e4c589..791a270 100644 --- a/app/client-ecs.js +++ b/app/client-ecs.js @@ -41,10 +41,22 @@ export default class ClientEcs extends Ecs { } return cache.get(key); } - async readScript(uri, context = {}) { - const code = await this.readAsset(uri); - if (code.byteLength > 0) { - return Script.fromCode((new TextDecoder()).decode(code), context); + async readScript(uriOrCode, context = {}) { + if (!uriOrCode) { + return undefined; + } + let code = ''; + if (!uriOrCode.startsWith('/')) { + code = uriOrCode; + } + else { + const buffer = await this.readAsset(uriOrCode); + if (buffer.byteLength > 0) { + code = (new TextDecoder()).decode(buffer); + } + } + if (code) { + return Script.fromCode(code, context); } } } diff --git a/app/ecs-components/inventory.js b/app/ecs-components/inventory.js index 93cdaac..2dbb4cd 100644 --- a/app/ecs-components/inventory.js +++ b/app/ecs-components/inventory.js @@ -82,7 +82,7 @@ class ItemProxy { if (this.scripts.projectionCheckInstance) { this.scripts.projectionCheckInstance.context.ecs = this.Component.ecs; this.scripts.projectionCheckInstance.context.projected = projected; - return this.scripts.projectionCheckInstance.evaluateSync(); + return this.scripts.projectionCheckInstance.evaluate(); } else { return projected; diff --git a/app/ecs-components/plant.js b/app/ecs-components/plant.js index 61d45bf..bf41c10 100644 --- a/app/ecs-components/plant.js +++ b/app/ecs-components/plant.js @@ -7,7 +7,7 @@ export default class Plant extends Component { const Instance = super.instanceFromSchema(); return class PlantInstance extends Instance { mayGrow() { - return this.mayGrowScriptInstance.evaluateSync(); + return this.mayGrowScriptInstance.evaluate(); } grow() { const {Ticking} = ecs.get(this.entity); diff --git a/app/engine.js b/app/engine.js index 7d9dd5c..4491018 100644 --- a/app/engine.js +++ b/app/engine.js @@ -76,13 +76,22 @@ export default class Engine { } return cache.get(key); } - async readScript(uri, context) { - if (!uri) { + async readScript(uriOrCode, context) { + if (!uriOrCode) { return undefined; } - const code = await this.readAsset(uri); - if (code.byteLength > 0) { - return Script.fromCode((new TextDecoder()).decode(code), context); + let code = ''; + if (!uriOrCode.startsWith('/')) { + code = uriOrCode; + } + else { + const buffer = await this.readAsset(uriOrCode); + if (buffer.byteLength > 0) { + code = (new TextDecoder()).decode(buffer); + } + } + if (code) { + return Script.fromCode(code, context); } } async switchEcs(entity, path, updates) { diff --git a/app/util/script.js b/app/util/script.js index 7c3b894..fb063af 100644 --- a/app/util/script.js +++ b/app/util/script.js @@ -65,28 +65,7 @@ export default class Script { }; } - // async evaluate(callback) { - // this.sandbox.reset(); - // let {done, value} = this.sandbox.next(); - // if (value instanceof Promise) { - // await value; - // } - // while (!done) { - // ({done, value} = this.sandbox.next()); - // if (value instanceof Promise) { - // // eslint-disable-next-line no-await-in-loop - // await value; - // } - // } - // if (value instanceof Promise) { - // value.then(callback); - // } - // else { - // callback(value); - // } - // } - - evaluateSync() { + evaluate() { this.sandbox.reset(); const {value} = this.sandbox.run(); return value;