@ringozz/godot 4.7.1-8 → 4.7.1-9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,123 +1,123 @@
1
- # @ringozz/godot
2
-
3
- Node-API bindings for the Godot Engine — call Godot classes, value types, and utility functions from JavaScript.
4
-
5
- This is the core runtime package. The React layer is a separate package ([`@ringozz/react-godot`](../react-godot/README.md)).
6
-
7
- ## Installation
8
-
9
- ```sh
10
- bun add @ringozz/godot
11
- ```
12
-
13
- The package ships no engine binary itself — it auto-selects a platform addon from its optional dependencies on install: `@ringozz/godot-macos-arm64`, `@ringozz/godot-windows-x86_64`, or `@ringozz/godot-web-wasm32` (the WASM build is installed everywhere and used on web). No `project.godot` is required — the engine boots from engine defaults with `res://` = your working directory (if a `project.godot` is present at cwd, it is loaded and `res://` still maps to cwd).
14
-
15
- ## Usage
16
-
17
- On **desktop**, the engine is already running when you import — `getGodot()` returns the instance synchronously. Pump frames with `runGodot()`:
18
-
19
- ```ts
20
- import { runGodot } from '@ringozz/godot';
21
- import { Engine } from '@ringozz/godot/Engine';
22
- import { SceneTree } from '@ringozz/godot/SceneTree';
23
-
24
- const tree = Engine.getMainLoop() as SceneTree;
25
- const root = tree.root;
26
-
27
- const label = new Label();
28
- label.text = 'hello from godot-node';
29
- root.addChild(label);
30
-
31
- const done = runGodot(); // pumps frames until aborted
32
- ```
33
-
34
- ### Web
35
-
36
- The wasm engine boots asynchronously, so on web the entry script must boot it **before** importing the app. `@ringozz/godot/runtime` reads the booted native module synchronously (handed over through the shared `@ringozz/godot/boot` module instance)this keeps the shared module graph top-level-await free, which is also what makes Bun's HMR dev server work.
37
-
38
- ```ts
39
- // your web entry (referenced by <script type="module"> in the HTML)
40
- import { preloadGodot } from '@ringozz/godot/boot';
41
- await preloadGodot();
42
- await import('./app.ts'); // may now import @ringozz/godot / react-godot freely
43
- ```
44
-
45
- (That's exactly what `dev/demo.html.ts` does in this repo. A second `<script type="module">` tag can't replace the dynamic import — Bun's fullstack HTML bundler merges all script entries into one chunk, so the browser's in-order script/TLA serialization never applies.)
46
-
47
- ### What's exported where
48
-
49
- | Specifier | Contents |
50
- |---|---|
51
- | `@ringozz/godot` | Value types (`Vector3`, `Color`, …), global enums, heap types, utility functions, global constants, `runGodot()`, RAF polyfills |
52
- | `@ringozz/godot/ClassName` | Classes, one per module — e.g. `@ringozz/godot/Label`, `@ringozz/godot/Node` |
53
- | `@ringozz/godot/runtime` | Low-level dispatch: `_C`, `_S`, `_get`, `_set`, `_R`, `_G`, `_P`, `getGodot`, `GodotVar` |
54
- | `@ringozz/godot/boot` | Web boot: `preloadGodot()` (awaited before importing the app) |
55
- | `@ringozz/godot/load` | Async resource loading: `loadResourceAsync` |
56
- | `@ringozz/godot/debug` | `initDebug()`, `dumpStr`, `dumpTreeStr`, `statsStr`, `gc` |
57
-
58
- Class-scoped enums use bare names (import `ProcessMode` from `@ringozz/godot/Node`, not `NodeProcessMode`).
59
-
60
- ### Async resource loading
61
-
62
- The engine loads pre-baked native resources (`.scn`, `.ctex`, …) through `ResourceLoader`. On desktop these are real files (`res://` = cwd); on web there is no filesystem, so the asset's bundled files — `.import` sidecars as text and imported products (`.scn`/`.ctex`) as emitted asset paths — are staged into Emscripten's in-memory MEMFS (transient, no IndexedDB, no persistent copy) by the generated asset modules, handing the fetched bytes straight to MEMFS via Godot's own `copyToFS` (`FS.createDataFile` with `canOwn` — zero copy, JS-heap only). On web, `.ctex` files are decoded first: the embedded PNG/WebP blobs go through the browser's `createImageBitmap` and the container is rebuilt as raw RGBA (`DATA_FORMAT_IMAGE`), so no image codec runs in wasm. The source file itself is never fetched.
63
-
64
- ```ts
65
- import { loadResourceAsync } from '@ringozz/godot/load';
66
- import { PackedScene } from '@ringozz/godot/PackedScene';
67
- import { Texture2D } from '@ringozz/godot/Texture2D';
68
-
69
- const scene = await loadResourceAsync('res://models/foo.gltf', PackedScene); // PackedScene
70
- const node = scene.instantiate();
71
- const tex = await loadResourceAsync('res://textures/icon.jpg', Texture2D); // Texture2D
72
- ```
73
-
74
- - `loadResourceAsync(path, cls, files?)` — generic over the Godot class: `cls.name` is the `ResourceLoader` type hint, and the return type is `InstanceType<typeof cls>`. Loads in the background (`loadThreadedRequest` + status polling). Godot default arguments apply: the napi dispatch truncates trailing `undefined` args, so `loadThreadedRequest` uses its default `CACHE_MODE_REUSE` and the resource stays cached in the engine (scene re-references don't re-read the file). On web it only works for paths already staged into MEMFS (generated asset modules handle this). When the asset's `files` map is passed, the staged product files (`.ctex`/`.scn`/native sources) are deleted from MEMFS after the resource loads — only the `.import` sidecars are kept, since they route imported source paths to their products. The decoded texture bytes don't linger in the JS heap.
75
- - `loadAsset(path, cls, files, deps, data)` — the generated asset modules' entry point (also usable directly): returns `{ materialize, load }`. It checks the engine's `ResourceCache` first (when the asset is already loaded, `load` resolves to the cached instance and staging is skipped), stages the bundled files via Godot's `copyToFS` (zero-copy handover of the fetched bytes; a no-op on desktop where the files already exist), loads via `loadResourceAsync`, and memoizes `load` on `data` (pass `import.meta.hot.data`) so HMR re-evaluations reuse the same fulfilled promise.
76
-
77
- Asset imports are resolved by `@ringozz/godot/preload` (a Bun plugin, registered via `bunfig.toml` preload for `bun run`/`bun test` and via `[serve.static] plugins` for `Bun.serve`'s fullstack HTML routes; the module's default export is the single plugin object) into a module per asset:
78
-
79
- ```ts
80
- import foo from './models/foo.gltf';
81
- const scene = await foo; // PackedScene
82
- ```
83
-
84
- Each asset module's default export is its load **promise** (`const { materialize, load } = loadAsset(resPath, Class, files, deps, import.meta.hot.data)` — the default is `load`, memoized on the module's HMR data), started eagerly at module import, and it also exports a `materialize` promise (its own files plus its deps' files). It statically imports its own files Godot reads — `.import` sidecars and native text sources (`tscn`/`tres`/`po`) via the `text` loader, imported products (`.scn`/`.ctex`) via the `file` loader — and imports its **dependencies as generated modules too**, waiting only for their `materialize` before loading itself (dep loads start at module evaluation and run concurrently). Text-based source assets (`.gltf`/`.tscn`/`.tres`/`.obj`) are scanned for references (`res://` paths and relative paths ending in known asset extensions); referenced source assets become dep modules, raw `.mtl` files (OBJ materials) are recursed into, and raw `.bin` buffers are import-time only. The modules are virtual (namespace `godot`), and every import of the same asset resolves to the same one (evaluated once) — so the promise is a stable singleton, read with `await` or React 19's `use()`:
85
-
86
- ```tsx
87
- import foo from './models/foo.gltf';
88
- import { Suspense, use } from 'react';
89
-
90
- function Foo() {
91
- const scene = use(foo); // suspends until the scene loads
92
- return <FooView scene={scene} />;
93
- }
94
-
95
- <Suspense fallback={<Label text="Loading…" />}>
96
- <Foo />
97
- </Suspense>
98
- ```
99
-
100
- Each extension maps to its Godot class (source assets only — `gltf`/`tscn`/`obj` → `PackedScene`, `jpg`/`jpeg`/`png`/`webp` → `CompressedTexture2D`, `svg` → `Texture2D`, `exr`/`hdr` → `TextureLayered`, `wav` → `AudioStreamWAV`, `ogg` → `AudioStreamOggVorbis`, `mp3` → `AudioStreamMP3`, `tres` → `Resource`, `po` → `Translation`), declared in `src/assets.d.ts`. Imported products (`.scn`/`.ctex`) are never imported directly.
101
-
102
- Textures are shared only through Godot's `ResourceLoader` path cache. The glTF import pipeline may produce per-scene texture instances (same `res://` path, separate objects), so don't assume two scenes referencing the same image share one texture object.
103
-
104
- ### Debugging
105
-
106
- ```ts
107
- import { initDebug } from '@ringozz/godot/debug';
108
- initDebug();
109
- // singletons + helpers now on globalThis.$: $.Engine, $.dumpStr(node), $.dumpTreeStr(root), $.statsStr(), $.gc()
110
- ```
111
-
112
- `dumpStr` renders value types inline (`(1.0, 2.0, 3.0)`, arrays, dictionaries) via Godot's `str()` and dumps objects via Godot's `var_to_str`, with the node's `name` prepended.
113
-
114
- `initDebug` also registers an `uncaughtException` handler that keeps the process alive, polyfills `DOMRect` (not defined in Bun, needed by `getBoundingClientRect()`), and adds `getBoundingClientRect()` to `CanvasItem`/`Node3D`.
115
-
116
- ## Notes
117
-
118
- - **Memory**: non-RefCounted objects (`Node`, `Node2D`, `Node3D`, …) must call `free()` explicitly. RefCounted objects are managed by Godot's ref counting; calling `free()` clears the JS wrapper (decrements the ref).
119
- - **Class-registration tree-shaking**: `ClassDB.instantiate('X')` requires the JS class to be value-imported (`import { Label } from '@ringozz/godot/Label'`). If a class is only type-used (e.g. `as Label` casts), bundlers may drop its registration side effect, and wrappers fall back to an ancestor class. Render through JSX is unaffected. Workaround: keep a value reference (`void Label;`).
120
-
121
- ## Development
122
-
123
- `gen/` is generated (and gitignored) by `dev/codegen.ts` from `godot --dump-extension-api-with-docs`. Regenerate + typecheck with `bun run prebuild`. Build the native addon with `bun run build`. See [`AGENTS.md`](../../AGENTS.md) for the full workflow.
1
+ # @ringozz/godot
2
+
3
+ Node-API bindings for the Godot Engine — call Godot classes, value types, and utility functions from JavaScript.
4
+
5
+ This is the core runtime package. The React layer is a separate package ([`@ringozz/react-godot`](../react-godot/README.md)).
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ bun add @ringozz/godot
11
+ ```
12
+
13
+ The package ships no engine binary itself — it auto-selects a platform addon from its optional dependencies on install: `@ringozz/godot-macos-arm64`, `@ringozz/godot-windows-x86_64`, or `@ringozz/godot-web-wasm32` (the WASM build is installed everywhere and used on web). No `project.godot` is required — the engine boots from engine defaults with `res://` = your working directory (if a `project.godot` is present at cwd, it is loaded and `res://` still maps to cwd).
14
+
15
+ ## Usage
16
+
17
+ On **desktop**, the engine is already running when you import — `getGodot()` returns the instance synchronously. Pump frames with `runGodot()`:
18
+
19
+ ```ts
20
+ import { runGodot } from '@ringozz/godot';
21
+ import { Engine } from '@ringozz/godot/Engine';
22
+ import { SceneTree } from '@ringozz/godot/SceneTree';
23
+
24
+ const tree = Engine.getMainLoop() as SceneTree;
25
+ const root = tree.root;
26
+
27
+ const label = new Label();
28
+ label.text = 'hello from godot-node';
29
+ root.addChild(label);
30
+
31
+ const done = runGodot(); // pumps frames until aborted
32
+ ```
33
+
34
+ ### Web
35
+
36
+ The wasm engine boots asynchronously, so on web the entry script must boot it **before** importing the app. `@ringozz/godot/runtime` reads the booted native module synchronously through the `@ringozz/godot/boot` export — the package's `browser` field remaps the desktop `boot.ts` leaf to `boot.browser.ts` in browser-targeted bundles, which hands the module over to `runtime.ts` via a shared module instance — keeping the shared module graph top-level-await free, which is also what makes Bun's HMR dev server work. On desktop `preloadGodot()` is a no-op (the engine boots at import).
37
+
38
+ ```ts
39
+ // your web entry (referenced by <script type="module"> in the HTML)
40
+ import { preloadGodot } from '@ringozz/godot/boot';
41
+ await preloadGodot();
42
+ await import('./app.ts'); // may now import @ringozz/godot / react-godot freely
43
+ ```
44
+
45
+ (That's exactly what `dev/demo.html.ts` does in this repo. A second `<script type="module">` tag can't replace the dynamic import — Bun's fullstack HTML bundler merges all script entries into one chunk, so the browser's in-order script/TLA serialization never applies.)
46
+
47
+ ### What's exported where
48
+
49
+ | Specifier | Contents |
50
+ |---|---|
51
+ | `@ringozz/godot` | Value types (`Vector3`, `Color`, …), global enums, heap types, utility functions, global constants, `runGodot()`, RAF polyfills |
52
+ | `@ringozz/godot/ClassName` | Classes, one per module — e.g. `@ringozz/godot/Label`, `@ringozz/godot/Node` |
53
+ | `@ringozz/godot/runtime` | Low-level dispatch: `_C`, `_S`, `_get`, `_set`, `_R`, `_G`, `_P`, `getGodot`, `GodotVar` |
54
+ | `@ringozz/godot/boot` | Platform boot: `getNativeModule()`; web `preloadGodot()` (awaited before importing the app; no-op on desktop) |
55
+ | `@ringozz/godot/load` | Async resource loading: `loadResourceAsync` |
56
+ | `@ringozz/godot/debug` | `initDebug()`, `dumpStr`, `dumpTreeStr`, `statsStr`, `gc` |
57
+
58
+ Class-scoped enums use bare names (import `ProcessMode` from `@ringozz/godot/Node`, not `NodeProcessMode`).
59
+
60
+ ### Async resource loading
61
+
62
+ The engine loads pre-baked native resources (`.scn`, `.ctex`, …) through `ResourceLoader`. On desktop these are real files (`res://` = cwd); on web there is no filesystem, so the asset's bundled files — `.import` sidecars as text and imported products (`.scn`/`.ctex`) as emitted asset paths — are staged into Emscripten's in-memory MEMFS (transient, no IndexedDB, no persistent copy) by the generated asset modules, handing the fetched bytes straight to MEMFS via Godot's own `copyToFS` (`FS.createDataFile` with `canOwn` — zero copy, JS-heap only). On web, `.ctex` files are decoded first: the embedded PNG/WebP blobs go through the browser's `createImageBitmap` and the container is rebuilt as raw RGBA (`DATA_FORMAT_IMAGE`), so no image codec runs in wasm. The source file itself is never fetched.
63
+
64
+ ```ts
65
+ import { loadResourceAsync } from '@ringozz/godot/load';
66
+ import { PackedScene } from '@ringozz/godot/PackedScene';
67
+ import { Texture2D } from '@ringozz/godot/Texture2D';
68
+
69
+ const scene = await loadResourceAsync('res://models/foo.gltf', PackedScene); // PackedScene
70
+ const node = scene.instantiate();
71
+ const tex = await loadResourceAsync('res://textures/icon.jpg', Texture2D); // Texture2D
72
+ ```
73
+
74
+ - `loadResourceAsync(path, cls, files?)` — generic over the Godot class: `cls.name` is the `ResourceLoader` type hint, and the return type is `InstanceType<typeof cls>`. Loads in the background (`loadThreadedRequest` + status polling). Godot default arguments apply: the napi dispatch truncates trailing `undefined` args, so `loadThreadedRequest` uses its default `CACHE_MODE_REUSE` and the resource stays cached in the engine (scene re-references don't re-read the file). On web it only works for paths already staged into MEMFS (generated asset modules handle this). When the asset's `files` map is passed, the staged product files (`.ctex`/`.scn`/native sources) are deleted from MEMFS after the resource loads — only the `.import` sidecars are kept, since they route imported source paths to their products. The decoded texture bytes don't linger in the JS heap.
75
+ - `loadAsset(path, cls, files, deps, data)` — the generated asset modules' entry point (also usable directly): returns `{ materialize, load }`. It checks the engine's `ResourceCache` first (when the asset is already loaded, `load` resolves to the cached instance and staging is skipped), stages the bundled files via Godot's `copyToFS` (zero-copy handover of the fetched bytes; a no-op on desktop where the files already exist), loads via `loadResourceAsync`, and memoizes `load` on `data` (pass `import.meta.hot.data`) so HMR re-evaluations reuse the same fulfilled promise.
76
+
77
+ Asset imports are resolved by `@ringozz/godot/preload` (a Bun plugin, registered via `bunfig.toml` preload for `bun run`/`bun test` and via `[serve.static] plugins` for `Bun.serve`'s fullstack HTML routes; the module's default export is the single plugin object) into a module per asset:
78
+
79
+ ```ts
80
+ import foo from './models/foo.gltf';
81
+ const scene = await foo; // PackedScene
82
+ ```
83
+
84
+ Each asset module's default export is its load **promise** (`const { materialize, load } = loadAsset(resPath, Class, files, deps, import.meta.hot.data)` — the default is `load`, memoized on the module's HMR data), started eagerly at module import, and it also exports a `materialize` promise (its own files plus its deps' files). It statically imports its own files Godot reads — `.import` sidecars and native text sources (`tscn`/`tres`/`po`) via the `text` loader, imported products (`.scn`/`.ctex`) via the `file` loader — and imports its **dependencies as generated modules too**, waiting only for their `materialize` before loading itself (dep loads start at module evaluation and run concurrently). Text-based source assets (`.gltf`/`.tscn`/`.tres`/`.obj`) are scanned for references (`res://` paths and relative paths ending in known asset extensions); referenced source assets become dep modules, raw `.mtl` files (OBJ materials) are recursed into, and raw `.bin` buffers are import-time only. The modules are virtual (namespace `godot`), and every import of the same asset resolves to the same one (evaluated once) — so the promise is a stable singleton, read with `await` or React 19's `use()`:
85
+
86
+ ```tsx
87
+ import foo from './models/foo.gltf';
88
+ import { Suspense, use } from 'react';
89
+
90
+ function Foo() {
91
+ const scene = use(foo); // suspends until the scene loads
92
+ return <FooView scene={scene} />;
93
+ }
94
+
95
+ <Suspense fallback={<Label text="Loading…" />}>
96
+ <Foo />
97
+ </Suspense>
98
+ ```
99
+
100
+ Each extension maps to its Godot class (source assets only — `gltf`/`tscn`/`obj` → `PackedScene`, `jpg`/`jpeg`/`png`/`webp` → `CompressedTexture2D`, `svg` → `Texture2D`, `exr`/`hdr` → `TextureLayered`, `wav` → `AudioStreamWAV`, `ogg` → `AudioStreamOggVorbis`, `mp3` → `AudioStreamMP3`, `tres` → `Resource`, `po` → `Translation`), declared in `src/assets.d.ts`. Imported products (`.scn`/`.ctex`) are never imported directly.
101
+
102
+ Textures are shared only through Godot's `ResourceLoader` path cache. The glTF import pipeline may produce per-scene texture instances (same `res://` path, separate objects), so don't assume two scenes referencing the same image share one texture object.
103
+
104
+ ### Debugging
105
+
106
+ ```ts
107
+ import { initDebug } from '@ringozz/godot/debug';
108
+ initDebug();
109
+ // singletons + helpers now on globalThis.$: $.Engine, $.dumpStr(node), $.dumpTreeStr(root), $.statsStr(), $.gc()
110
+ ```
111
+
112
+ `dumpStr` renders value types inline (`(1.0, 2.0, 3.0)`, arrays, dictionaries) via Godot's `str()` and dumps objects via Godot's `var_to_str`, with the node's `name` prepended.
113
+
114
+ `initDebug` also registers an `uncaughtException` handler that keeps the process alive, polyfills `DOMRect` (not defined in Bun, needed by `getBoundingClientRect()`), and adds `getBoundingClientRect()` to `CanvasItem`/`Node3D`.
115
+
116
+ ## Notes
117
+
118
+ - **Memory**: non-RefCounted objects (`Node`, `Node2D`, `Node3D`, …) must call `free()` explicitly. RefCounted objects are managed by Godot's ref counting; calling `free()` clears the JS wrapper (decrements the ref).
119
+ - **Class-registration tree-shaking**: `ClassDB.instantiate('X')` requires the JS class to be value-imported (`import { Label } from '@ringozz/godot/Label'`). If a class is only type-used (e.g. `as Label` casts), bundlers may drop its registration side effect, and wrappers fall back to an ancestor class. Render through JSX is unaffected. Workaround: keep a value reference (`void Label;`).
120
+
121
+ ## Development
122
+
123
+ `gen/` is generated (and gitignored) by `dev/codegen.ts` from `godot --dump-extension-api-with-docs`. Regenerate + typecheck with `bun run prebuild`. Build the native addon with `bun run build`. See [`AGENTS.md`](../../AGENTS.md) for the full workflow.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ringozz/godot",
3
3
  "author": "Vladimir Davidovich",
4
- "version": "4.7.1-8",
4
+ "version": "4.7.1-9",
5
5
  "description": "Node-API bindings for Godot Engine — call Godot classes from JavaScript",
6
6
  "keywords": [
7
7
  "godot"
@@ -18,6 +18,9 @@
18
18
  "./preload": "./src/preload.ts",
19
19
  "./*": "./gen/classes/*.ts"
20
20
  },
21
+ "browser": {
22
+ "./src/boot.ts": "./src/boot.browser.ts"
23
+ },
21
24
  "files": [
22
25
  "src/",
23
26
  "gen/",
@@ -35,7 +38,7 @@
35
38
  "optionalDependencies": {
36
39
  "@ringozz/godot-macos-arm64": "^4.7.1-3",
37
40
  "@ringozz/godot-windows-x86_64": "^4.7.1-3",
38
- "@ringozz/godot-web-wasm32": "^4.7.1-8"
41
+ "@ringozz/godot-web-wasm32": "^4.7.1-9"
39
42
  },
40
43
  "peerDependencies": {
41
44
  "@types/bun": "*"
package/src/assets.d.ts CHANGED
@@ -1,116 +1,116 @@
1
- /**********************************************************************
2
- Copyright (c) Vladimir Davidovich. All rights reserved.
3
- ***********************************************************************/
4
-
5
- // Ambient types for Godot asset imports, resolved by `@ringozz/godot/preload`
6
- // into modules that export a `materialize` promise (files staged into MEMFS) and
7
- // default to the load promise (`const { materialize, load } = loadAsset(path,
8
- // Class, files, deps, import.meta.hot.data)` — `load` memoized on the module's
9
- // HMR data, so re-evaluations return the same fulfilled promise). The default is
10
- // a stable module singleton, so it can be passed to React's `use()` under
11
- // `<Suspense>`.
12
-
13
- declare module '*.tres' {
14
- import type { Resource } from '@ringozz/godot/Resource';
15
- const asset: Promise<Resource>;
16
- export default asset;
17
- export const materialize: Promise<unknown>;
18
- }
19
-
20
- declare module '*.po' {
21
- import type { Translation } from '@ringozz/godot/Translation';
22
- const asset: Promise<Translation>;
23
- export default asset;
24
- export const materialize: Promise<unknown>;
25
- }
26
-
27
- declare module '*.gltf' {
28
- import type { PackedScene } from '@ringozz/godot/PackedScene';
29
- const asset: Promise<PackedScene>;
30
- export default asset;
31
- export const materialize: Promise<unknown>;
32
- }
33
-
34
- declare module '*.tscn' {
35
- import type { PackedScene } from '@ringozz/godot/PackedScene';
36
- const asset: Promise<PackedScene>;
37
- export default asset;
38
- export const materialize: Promise<unknown>;
39
- }
40
-
41
- declare module '*.obj' {
42
- import type { PackedScene } from '@ringozz/godot/PackedScene';
43
- const asset: Promise<PackedScene>;
44
- export default asset;
45
- export const materialize: Promise<unknown>;
46
- }
47
-
48
- declare module '*.jpg' {
49
- import type { CompressedTexture2D } from '@ringozz/godot/CompressedTexture2D';
50
- const asset: Promise<CompressedTexture2D>;
51
- export default asset;
52
- export const materialize: Promise<unknown>;
53
- }
54
-
55
- declare module '*.jpeg' {
56
- import type { CompressedTexture2D } from '@ringozz/godot/CompressedTexture2D';
57
- const asset: Promise<CompressedTexture2D>;
58
- export default asset;
59
- export const materialize: Promise<unknown>;
60
- }
61
-
62
- declare module '*.png' {
63
- import type { CompressedTexture2D } from '@ringozz/godot/CompressedTexture2D';
64
- const asset: Promise<CompressedTexture2D>;
65
- export default asset;
66
- export const materialize: Promise<unknown>;
67
- }
68
-
69
- declare module '*.webp' {
70
- import type { CompressedTexture2D } from '@ringozz/godot/CompressedTexture2D';
71
- const asset: Promise<CompressedTexture2D>;
72
- export default asset;
73
- export const materialize: Promise<unknown>;
74
- }
75
-
76
- declare module '*.svg' {
77
- import type { Texture2D } from '@ringozz/godot/Texture2D';
78
- const asset: Promise<Texture2D>;
79
- export default asset;
80
- export const materialize: Promise<unknown>;
81
- }
82
-
83
- declare module '*.exr' {
84
- import type { TextureLayered } from '@ringozz/godot/TextureLayered';
85
- const asset: Promise<TextureLayered>;
86
- export default asset;
87
- export const materialize: Promise<unknown>;
88
- }
89
-
90
- declare module '*.hdr' {
91
- import type { TextureLayered } from '@ringozz/godot/TextureLayered';
92
- const asset: Promise<TextureLayered>;
93
- export default asset;
94
- export const materialize: Promise<unknown>;
95
- }
96
-
97
- declare module '*.wav' {
98
- import type { AudioStreamWAV } from '@ringozz/godot/AudioStreamWAV';
99
- const asset: Promise<AudioStreamWAV>;
100
- export default asset;
101
- export const materialize: Promise<unknown>;
102
- }
103
-
104
- declare module '*.ogg' {
105
- import type { AudioStreamOggVorbis } from '@ringozz/godot/AudioStreamOggVorbis';
106
- const asset: Promise<AudioStreamOggVorbis>;
107
- export default asset;
108
- export const materialize: Promise<unknown>;
109
- }
110
-
111
- declare module '*.mp3' {
112
- import type { AudioStreamMP3 } from '@ringozz/godot/AudioStreamMP3';
113
- const asset: Promise<AudioStreamMP3>;
114
- export default asset;
115
- export const materialize: Promise<unknown>;
116
- }
1
+ /**********************************************************************
2
+ Copyright (c) Vladimir Davidovich. All rights reserved.
3
+ ***********************************************************************/
4
+
5
+ // Ambient types for Godot asset imports, resolved by `@ringozz/godot/preload`
6
+ // into modules that export a `materialize` promise (files staged into MEMFS) and
7
+ // default to the load promise (`const { materialize, load } = loadAsset(path,
8
+ // Class, files, deps, import.meta.hot.data)` — `load` memoized on the module's
9
+ // HMR data, so re-evaluations return the same fulfilled promise). The default is
10
+ // a stable module singleton, so it can be passed to React's `use()` under
11
+ // `<Suspense>`.
12
+
13
+ declare module '*.tres' {
14
+ import type { Resource } from '@ringozz/godot/Resource';
15
+ const asset: Promise<Resource>;
16
+ export default asset;
17
+ export const materialize: Promise<unknown>;
18
+ }
19
+
20
+ declare module '*.po' {
21
+ import type { Translation } from '@ringozz/godot/Translation';
22
+ const asset: Promise<Translation>;
23
+ export default asset;
24
+ export const materialize: Promise<unknown>;
25
+ }
26
+
27
+ declare module '*.gltf' {
28
+ import type { PackedScene } from '@ringozz/godot/PackedScene';
29
+ const asset: Promise<PackedScene>;
30
+ export default asset;
31
+ export const materialize: Promise<unknown>;
32
+ }
33
+
34
+ declare module '*.tscn' {
35
+ import type { PackedScene } from '@ringozz/godot/PackedScene';
36
+ const asset: Promise<PackedScene>;
37
+ export default asset;
38
+ export const materialize: Promise<unknown>;
39
+ }
40
+
41
+ declare module '*.obj' {
42
+ import type { PackedScene } from '@ringozz/godot/PackedScene';
43
+ const asset: Promise<PackedScene>;
44
+ export default asset;
45
+ export const materialize: Promise<unknown>;
46
+ }
47
+
48
+ declare module '*.jpg' {
49
+ import type { CompressedTexture2D } from '@ringozz/godot/CompressedTexture2D';
50
+ const asset: Promise<CompressedTexture2D>;
51
+ export default asset;
52
+ export const materialize: Promise<unknown>;
53
+ }
54
+
55
+ declare module '*.jpeg' {
56
+ import type { CompressedTexture2D } from '@ringozz/godot/CompressedTexture2D';
57
+ const asset: Promise<CompressedTexture2D>;
58
+ export default asset;
59
+ export const materialize: Promise<unknown>;
60
+ }
61
+
62
+ declare module '*.png' {
63
+ import type { CompressedTexture2D } from '@ringozz/godot/CompressedTexture2D';
64
+ const asset: Promise<CompressedTexture2D>;
65
+ export default asset;
66
+ export const materialize: Promise<unknown>;
67
+ }
68
+
69
+ declare module '*.webp' {
70
+ import type { CompressedTexture2D } from '@ringozz/godot/CompressedTexture2D';
71
+ const asset: Promise<CompressedTexture2D>;
72
+ export default asset;
73
+ export const materialize: Promise<unknown>;
74
+ }
75
+
76
+ declare module '*.svg' {
77
+ import type { Texture2D } from '@ringozz/godot/Texture2D';
78
+ const asset: Promise<Texture2D>;
79
+ export default asset;
80
+ export const materialize: Promise<unknown>;
81
+ }
82
+
83
+ declare module '*.exr' {
84
+ import type { TextureLayered } from '@ringozz/godot/TextureLayered';
85
+ const asset: Promise<TextureLayered>;
86
+ export default asset;
87
+ export const materialize: Promise<unknown>;
88
+ }
89
+
90
+ declare module '*.hdr' {
91
+ import type { TextureLayered } from '@ringozz/godot/TextureLayered';
92
+ const asset: Promise<TextureLayered>;
93
+ export default asset;
94
+ export const materialize: Promise<unknown>;
95
+ }
96
+
97
+ declare module '*.wav' {
98
+ import type { AudioStreamWAV } from '@ringozz/godot/AudioStreamWAV';
99
+ const asset: Promise<AudioStreamWAV>;
100
+ export default asset;
101
+ export const materialize: Promise<unknown>;
102
+ }
103
+
104
+ declare module '*.ogg' {
105
+ import type { AudioStreamOggVorbis } from '@ringozz/godot/AudioStreamOggVorbis';
106
+ const asset: Promise<AudioStreamOggVorbis>;
107
+ export default asset;
108
+ export const materialize: Promise<unknown>;
109
+ }
110
+
111
+ declare module '*.mp3' {
112
+ import type { AudioStreamMP3 } from '@ringozz/godot/AudioStreamMP3';
113
+ const asset: Promise<AudioStreamMP3>;
114
+ export default asset;
115
+ export const materialize: Promise<unknown>;
116
+ }
@@ -0,0 +1,39 @@
1
+ /**********************************************************************
2
+ Copyright (c) Vladimir Davidovich. All rights reserved.
3
+ ***********************************************************************/
4
+
5
+ // Web boot leaf (the `browser`-field replacement for `boot.ts`): boots the
6
+ // Godot wasm engine and hands the module to `runtime.ts` as the native module
7
+ // *namespace* (whose `.default` is the wasm module). This module is a leaf — it
8
+ // imports nothing from the godot class graph, so a web entry can call
9
+ // `preloadGodot()` *before* importing the app without pulling in `runtime.ts`.
10
+ // `runtime.ts` statically imports this module too (through the browser-field
11
+ // remap), so both sides share the same module instance and `nativeModule`
12
+ // below is the handoff — no global needed.
13
+
14
+ let nativeModule: unknown = null;
15
+
16
+ /** The cached wasm module import (its namespace), or `null` until `preloadGodot()` completes. */
17
+ export function getNativeModule(): unknown {
18
+ return nativeModule;
19
+ }
20
+
21
+ /**
22
+ * Boots the Godot wasm engine so `@ringozz/godot/runtime` can read the native
23
+ * module synchronously via {@link getNativeModule} — keeping the shared module
24
+ * graph top-level-await free (which is what lets Bun's HMR module loader
25
+ * evaluate sibling importers in order). Returns the wasm module's namespace
26
+ * (the native module is its `.default`, which `runtime.ts` destructures).
27
+ * Idempotent: subsequent calls return the cached import without re-booting.
28
+ *
29
+ * A web entry must `await preloadGodot()` before importing the app:
30
+ *
31
+ * ```ts
32
+ * import { preloadGodot } from '@ringozz/godot/boot';
33
+ * await preloadGodot();
34
+ * await import('./app.ts');
35
+ * ```
36
+ */
37
+ export async function preloadGodot(): Promise<unknown> {
38
+ return nativeModule ??= await import('@ringozz/godot-web-wasm32');
39
+ }
package/src/boot.ts CHANGED
@@ -2,36 +2,25 @@
2
2
  Copyright (c) Vladimir Davidovich. All rights reserved.
3
3
  ***********************************************************************/
4
4
 
5
- // Web boot helper. This module is a leaf it imports nothing from the godot
6
- // class graph, so a web entry can call `preloadGodot()` *before* importing the
7
- // app without pulling in `runtime.ts` (which throws until the native module is
8
- // available). `runtime.ts` statically imports this module too, so both sides
9
- // share the same module instance and `nativeModule` below is the handoff — no
10
- // global needed.
5
+ // Desktop boot leaf: resolves the native addon for the current host and
6
+ // hands it to `runtime.ts` as the native module *namespace* (whose `.default`
7
+ // is the addon's `module.exports`). `runtime.ts` imports this module, and the
8
+ // package's `browser` field remaps it to `boot.browser.ts` when bundling for
9
+ // `target: 'browser'` so the Bun-only `import.meta.require` below never
10
+ // reaches a web bundle.
11
11
 
12
- let nativeModule: unknown = null;
12
+ const { platform, arch } = globalThis.process ?? {};
13
+ const mapping: any = {
14
+ 'darwin-arm64': 'macos-arm64',
15
+ 'win32-x64': 'windows-x86_64',
16
+ };
13
17
 
14
- /** The cached wasm module import (its namespace), or `null` until `preloadGodot()` completes. */
18
+ /** The native addon's module namespace (sync dlopen; engine boots on first call). */
15
19
  export function getNativeModule(): unknown {
16
- return nativeModule;
20
+ return import.meta.require(`@ringozz/godot-${mapping[`${platform}-${arch}`]}`);
17
21
  }
18
22
 
19
- /**
20
- * Boots the Godot wasm engine so `@ringozz/godot/runtime` can read the native
21
- * module synchronously via {@link getNativeModule} — keeping the shared module
22
- * graph top-level-await free (which is what lets Bun's HMR module loader
23
- * evaluate sibling importers in order). Returns the wasm module's namespace
24
- * (the native module is its `.default`, which `runtime.ts` destructures).
25
- * Idempotent: subsequent calls return the cached import without re-booting.
26
- *
27
- * A web entry must `await preloadGodot()` before importing the app:
28
- *
29
- * ```ts
30
- * import { preloadGodot } from '@ringozz/godot/boot';
31
- * await preloadGodot();
32
- * await import('./app.ts');
33
- * ```
34
- */
23
+ /** Desktop boots synchronously at import — nothing to preload. */
35
24
  export async function preloadGodot(): Promise<unknown> {
36
- return nativeModule ??= await import('@ringozz/godot-web-wasm32');
25
+ return undefined;
37
26
  }