From c8622c6814fd2791e133bdf93756a855e38ca78b Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 27 Jun 2024 07:37:20 -0500 Subject: [PATCH] feat: engine readAsset --- app/ecs-components/inventory.js | 3 ++- app/engine.js | 4 ++-- app/react-components/ui.jsx | 3 +++ app/websocket.js | 8 ++++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/ecs-components/inventory.js b/app/ecs-components/inventory.js index 52c0128..1aaa4d3 100644 --- a/app/ecs-components/inventory.js +++ b/app/ecs-components/inventory.js @@ -48,8 +48,9 @@ export default class Inventory extends Component { if (!(slot in slots)) { return undefined; } + const {Engine: {readAsset}} = Component.ecs.get(1); const json = await ( - fetch([slots[slot].source, 'item.json'].join('/')) + readAsset([slots[slot].source, 'item.json'].join('/')) .then((response) => (response.ok ? response.json() : {})) ); const item = { diff --git a/app/engine.js b/app/engine.js index 2ae6729..e2cc752 100644 --- a/app/engine.js +++ b/app/engine.js @@ -217,11 +217,11 @@ export default class Engine { } async loadEcs(path) { - this.ecses[path] = Ecs.deserialize( + const ecs = this.ecses[path] = await Ecs.deserialize( this.createEcs(), await this.server.readData(path), ); - this.ecses[path].get(1).Engine.engine = this; + ecs.get(1).Engine.readAsset = (uri) => this.server.readAsset(uri); } async loadPlayer(id) { diff --git a/app/react-components/ui.jsx b/app/react-components/ui.jsx index 6b77bda..6a41954 100644 --- a/app/react-components/ui.jsx +++ b/app/react-components/ui.jsx @@ -154,6 +154,9 @@ export default function Ui({disconnected}) { return; } await ecs.apply(payload.ecs); + if (payload.ecs[1]) { + ecs.get(1).Engine.readAsset = (uri) => fetch(new URL(uri, window.location.origin)); + } for (const listener of client.listeners[':Ecs'] ?? []) { listener(payload.ecs); } diff --git a/app/websocket.js b/app/websocket.js index 4c2a4d6..9544bac 100644 --- a/app/websocket.js +++ b/app/websocket.js @@ -48,6 +48,14 @@ class SocketServer extends Server { static qualify(path) { return join(import.meta.dirname, 'data', 'remote', 'UNIVERSE', path); } + async readAsset(path) { + const url = new URL(path, 'https://localhost:3000') + console.log({url}); + if ('production' === process.env.NODE_ENV) { + url.protocol = 'http:'; + } + return fetch(url.href); + } async readData(path) { const qualified = this.constructor.qualify(path); await this.ensurePath(dirname(qualified));