From 77d21108f12e3439c45578e9750cd43f6f7181b5 Mon Sep 17 00:00:00 2001 From: Matt Harding Date: Fri, 20 Dec 2024 23:17:50 +0000 Subject: [PATCH] Reorganise and rename config constants etc --- runtimes/web/src/config-constants.ts | 7 +++++++ runtimes/web/src/constants.ts | 1 - runtimes/web/src/devkit.ts | 11 +++++------ runtimes/web/src/runtime.ts | 7 ++++--- runtimes/web/src/ui/app.ts | 10 +++++----- 5 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 runtimes/web/src/config-constants.ts diff --git a/runtimes/web/src/config-constants.ts b/runtimes/web/src/config-constants.ts new file mode 100644 index 00000000..d8c15bc3 --- /dev/null +++ b/runtimes/web/src/config-constants.ts @@ -0,0 +1,7 @@ +/** True if the runtime is in Game Developer mode, with access to the devtools window. */ +// WASM4_GAMEDEV_MODE is defined in vite.config.ts +export const GAMEDEV_MODE = WASM4_GAMEDEV_MODE; + +/** True if we're developers of the WASM4 runtime itself, and are running the runtime + using `npm start` or `vite` etc. */ +export const PLATFORM_DEVELOPER_MODE = import.meta.env.MODE === "development"; \ No newline at end of file diff --git a/runtimes/web/src/constants.ts b/runtimes/web/src/constants.ts index c4c2b93f..5ae58046 100644 --- a/runtimes/web/src/constants.ts +++ b/runtimes/web/src/constants.ts @@ -1,4 +1,3 @@ -export const GAMEDEV_MODE = WASM4_GAMEDEV_MODE; export const WIDTH = 160; export const HEIGHT = 160; diff --git a/runtimes/web/src/devkit.ts b/runtimes/web/src/devkit.ts index fed09ed9..bc39d3de 100644 --- a/runtimes/web/src/devkit.ts +++ b/runtimes/web/src/devkit.ts @@ -1,9 +1,8 @@ -import * as constants from "./constants"; -// True if we're developers of the WASM4 runtime itself, and are running the runtime -// using `npm start` or `vite` etc. In this case, there's no wasm4 CLI web socket to -// connect to, but we might be fighting for the Vite websocket. -const PLATFORM_DEVELOPER_MODE = import.meta.env.MODE === "development"; +import { GAMEDEV_MODE, PLATFORM_DEVELOPER_MODE } from "./config-constants"; -export const websocket = constants.GAMEDEV_MODE && !PLATFORM_DEVELOPER_MODE +// The w4 CLI web socket only exists for gamedev mode. +// But if we're running `npm start` or `vite` there is no w4 CLI to connect to, +// but there is a vite websocket we may be fighting for, causing issues with vite. +export const cli_websocket = GAMEDEV_MODE && !PLATFORM_DEVELOPER_MODE ? new WebSocket((location.protocol == "https:" ? "wss" : "ws") + "://" + location.host) : null; diff --git a/runtimes/web/src/runtime.ts b/runtimes/web/src/runtime.ts index 515cc151..dd3ceec6 100644 --- a/runtimes/web/src/runtime.ts +++ b/runtimes/web/src/runtime.ts @@ -5,6 +5,7 @@ import { Framebuffer } from "./framebuffer"; import { WebGLCompositor } from "./compositor"; import * as devkit from "./devkit"; import { wasmPatchExportGlobals } from "./wasm-patch"; +import * as configConstants from "./config-constants"; export class Runtime { canvas: HTMLCanvasElement; @@ -120,7 +121,7 @@ export class Runtime { this.wasm = null; if (wasmBuffer.byteLength > limit) { - if (constants.GAMEDEV_MODE) { + if (configConstants.GAMEDEV_MODE) { if (!this.warnedFileSize) { this.warnedFileSize = true; this.print(`Warning: Cart is larger than ${limit} bytes. Ensure the release build of your cart is small enough to be bundled.`); @@ -262,8 +263,8 @@ export class Runtime { } printToServer (str: string) { - if (devkit.websocket != null && devkit.websocket.readyState == 1) { - devkit.websocket.send(str); + if (devkit.cli_websocket != null && devkit.cli_websocket.readyState == 1) { + devkit.cli_websocket.send(str); } } diff --git a/runtimes/web/src/ui/app.ts b/runtimes/web/src/ui/app.ts index 0b3d1274..132771bd 100644 --- a/runtimes/web/src/ui/app.ts +++ b/runtimes/web/src/ui/app.ts @@ -2,6 +2,7 @@ import { LitElement, html, css } from "lit"; import { customElement, state, query } from 'lit/decorators.js'; import * as constants from "../constants"; +import * as configConstants from "../config-constants"; import * as devkit from "../devkit"; import * as utils from "./utils"; import * as z85 from "../z85"; @@ -141,7 +142,7 @@ export class App extends LitElement { // Nothing }, }; - if (constants.GAMEDEV_MODE) { + if (configConstants.GAMEDEV_MODE) { devtoolsManager = await import('@wasm4/web-devtools').then(({ DevtoolsManager}) => new DevtoolsManager()); } @@ -153,8 +154,8 @@ export class App extends LitElement { this.copyNetplayLink(); } - if (constants.GAMEDEV_MODE) { - devkit.websocket?.addEventListener("message", async event => { + if (configConstants.GAMEDEV_MODE) { + devkit.cli_websocket?.addEventListener("message", async event => { switch (event.data) { case "reload": this.resetCart(await loadCartWasm()); @@ -481,8 +482,7 @@ export class App extends LitElement { runtime.composite(); - if (constants.GAMEDEV_MODE) { - // FIXED(2023-12-13): Pass the correct FPS for display + if (configConstants.GAMEDEV_MODE) { devtoolsManager.updateCompleted(runtime, timeFrameStart - lastTimeFrameStart); lastTimeFrameStart = timeFrameStart; }