@ringozz/godot 4.7.2-649 → 4.7.2-654
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 +15 -2
- package/package.json +4 -4
- package/src/dump-deps.gd +126 -112
- package/src/editor.ts +2 -2
- package/src/export_presets.example.cfg +5 -1
- package/src/load.ts +10 -1
- package/src/pack.ts +241 -87
- package/src/preload.ts +33 -18
package/README.md
CHANGED
|
@@ -53,7 +53,7 @@ await import('./app.ts'); // may now import @ringozz/godot / react-godot freely
|
|
|
53
53
|
| `@ringozz/godot/runtime` | Low-level dispatch: `_C`, `_S`, `_R`, `_G`, `_P`, `_V`, `toValueType`, `getGodot`, `GodotVar` |
|
|
54
54
|
| `@ringozz/godot/boot` | Platform boot: `getNativeModule()`; web `preloadGodot()` (awaited before importing the app; no-op on desktop) |
|
|
55
55
|
| `@ringozz/godot/load` | Async resource loading: `loadResourceAsync`, `loadAsset`, `loadAssetPack` (native `.pck` packs) |
|
|
56
|
-
| `@ringozz/godot/pack` | `
|
|
56
|
+
| `@ringozz/godot/pack` | `buildAssetPacks()` + the `godot pack` subcommand: bake a project's `.pck`(s) + loose-texture manifest, one pack per scene |
|
|
57
57
|
| `@ringozz/godot/debug` | `initDebug()`, `dumpStr`, `snapshot`, `inspect`, `click`, `find`, `gc` |
|
|
58
58
|
|
|
59
59
|
Class-scoped enums use bare names (import `ProcessMode` from `@ringozz/godot/Node`, not `NodeProcessMode`).
|
|
@@ -90,9 +90,22 @@ const node = scene.instantiate();
|
|
|
90
90
|
const tex = await loadResourceAsync('res://textures/icon.jpg', Texture2D); // Texture2D
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
-
- `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.
|
|
93
|
+
- `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. `files` lists overlap (a scene and the model it instantiates can share one loose texture), so a product may need re-staging after another module's cleanup: `materialize` creates the parent directory itself and treats an already-staged file as done, because the engine's `copyToFS` throws on a path that is already there while `FileAccess.fileExists('res://…')` cannot see the Emscripten spelling of it.
|
|
94
94
|
- `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.
|
|
95
95
|
|
|
96
|
+
### Asset packs
|
|
97
|
+
|
|
98
|
+
`godot pack` (the `godot` bin, `@ringozz/godot/pack`) bakes a project's assets for the runtime: it imports them, asks `dump-deps.gd` for each source's packed closure and its loose (WebP-backed) textures, and exports native `.pck`s — the engine-converted binary resources plus a remap table — through the project's committed `export_presets.cfg`. `loadAssetPack(url)` fetches a pack into MEMFS and hands it to `ProjectSettings.loadResourcePack`; packs are add-only, so several can be mounted and `res://` resolves across all of them.
|
|
99
|
+
|
|
100
|
+
The bake always splits: **one pack per scene, plus a shared pack**, so a host fetches the shared data and only the level it is playing. A project with no scene under its roots gets a single `shared.pck`. What the runtime reads is `.godot/packs/packs.json` beside the packs: which packs exist, which sources each owns, and which loose (WebP-backed) textures the host must stage and transcode per file. The preload plugin mounts the right packs per asset, so importing a level pulls in the shared pack and that level's, nothing else. How the split is decided — and why it needs no configuration — is in `AGENTS.md`.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { loadAssetPack } from '@ringozz/godot/load';
|
|
104
|
+
await loadAssetPack('/packs/shared.pck'); // usually the preload plugin does this per asset
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Asset imports
|
|
108
|
+
|
|
96
109
|
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. Only **JS imports** are virtualized — HTML asset references (`<link rel="icon">`, `<img src>`) and CSS `url()` refs fall through to Bun's normal (content-hashed) asset handling, so favicons and other web assets co-exist with Godot imports:
|
|
97
110
|
|
|
98
111
|
```ts
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ringozz/godot",
|
|
3
3
|
"author": "Vladimir Davidovich",
|
|
4
|
-
"version": "4.7.2-
|
|
4
|
+
"version": "4.7.2-654",
|
|
5
5
|
"description": "Node-API bindings for Godot Engine — call Godot classes from JavaScript",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -66,9 +66,9 @@
|
|
|
66
66
|
"precision": "single"
|
|
67
67
|
},
|
|
68
68
|
"optionalDependencies": {
|
|
69
|
-
"@ringozz/godot-macos-arm64": "^4.7.2-
|
|
70
|
-
"@ringozz/godot-windows-x86_64": "^4.7.2-
|
|
71
|
-
"@ringozz/godot-web-wasm32": "^4.7.2-
|
|
69
|
+
"@ringozz/godot-macos-arm64": "^4.7.2-654",
|
|
70
|
+
"@ringozz/godot-windows-x86_64": "^4.7.2-654",
|
|
71
|
+
"@ringozz/godot-web-wasm32": "^4.7.2-654"
|
|
72
72
|
},
|
|
73
73
|
"peerDependencies": {
|
|
74
74
|
"@types/bun": "*"
|
package/src/dump-deps.gd
CHANGED
|
@@ -1,112 +1,126 @@
|
|
|
1
|
-
@tool
|
|
2
|
-
extends SceneTree
|
|
3
|
-
## Dumps, for every
|
|
4
|
-
## texture sources
|
|
5
|
-
## by the host
|
|
6
|
-
##
|
|
7
|
-
##
|
|
8
|
-
##
|
|
9
|
-
##
|
|
10
|
-
##
|
|
11
|
-
##
|
|
12
|
-
##
|
|
13
|
-
##
|
|
14
|
-
##
|
|
15
|
-
##
|
|
16
|
-
##
|
|
17
|
-
##
|
|
18
|
-
|
|
19
|
-
##
|
|
20
|
-
##
|
|
21
|
-
##
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
var
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
1
|
+
@tool
|
|
2
|
+
extends SceneTree
|
|
3
|
+
## Dumps, for every packable (`.import`-less) source under the given roots, two things:
|
|
4
|
+
## - `loose`: the **loose** (WebP-backed) texture sources it needs — those must stay out of
|
|
5
|
+
## the asset packs and be staged/transcoded by the host, and the preload plugin
|
|
6
|
+
## (`@ringozz/godot/preload`) reads them from the manifest beside the packs so it never
|
|
7
|
+
## scans source text for references.
|
|
8
|
+
## - `packed`: the packed sources reachable from it — the graph `@ringozz/godot/pack`'s
|
|
9
|
+
## `splitPacks` uses to decide which pack owns a source (the ones only one scene reaches
|
|
10
|
+
## are that scene's).
|
|
11
|
+
##
|
|
12
|
+
## Sources of both (union): the engine's `ResourceLoader.get_dependencies` (for imported
|
|
13
|
+
## sources the real refs live on the imported product, reached via the `.import`
|
|
14
|
+
## `dest_files`), plus a text scan for string-valued paths (`albedo_array =
|
|
15
|
+
## "res://.../*.webparray"`, `load("res://...")`) that no static resource edge exposes. A dep
|
|
16
|
+
## is *loose* when it has an `.import` sidecar but is not a packed resource type — the set is
|
|
17
|
+
## derived, not listed.
|
|
18
|
+
##
|
|
19
|
+
## Driven by `@ringozz/godot/pack`'s `buildAssetPacks`, which sets:
|
|
20
|
+
## GODOT_DUMP_OUT – output res:// path (default res://.godot/pack-dump.json) — a build
|
|
21
|
+
## input: the bake merges it into the manifest beside the packs.
|
|
22
|
+
## GODOT_DUMP_ROOTS – comma-separated res:// roots to scan (default res://)
|
|
23
|
+
|
|
24
|
+
## Imported source extensions: `_classify` recurses into these (an imported source's own deps are
|
|
25
|
+
## empty — its *product* carries the refs) where any other extension with a sidecar is a loose texture.
|
|
26
|
+
## `gltfx` is a scene-composition source and `gltfxmesh` a shared-mesh one: their products reference the
|
|
27
|
+
## meshes and textures they compose.
|
|
28
|
+
const PACKED := ["tscn", "tres", "gltf", "gltfx", "gltfxmesh", "obj", "gd", "gdshader", "gdshaderinc", "ttf", "otf", "po"]
|
|
29
|
+
const SCANNED := ["tscn", "tres", "gd", "gdshader", "gdshaderinc", "gltf", "obj"]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
func _initialize() -> void:
|
|
33
|
+
var out := OS.get_environment("GODOT_DUMP_OUT")
|
|
34
|
+
if out.is_empty():
|
|
35
|
+
out = "res://.godot/pack-dump.json"
|
|
36
|
+
var roots := OS.get_environment("GODOT_DUMP_ROOTS").split(",", false)
|
|
37
|
+
if roots.is_empty():
|
|
38
|
+
roots = ["res://"]
|
|
39
|
+
var sources := {}
|
|
40
|
+
for r in roots:
|
|
41
|
+
_scan(String(r).rstrip("/") + "/", sources)
|
|
42
|
+
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(out.get_base_dir()))
|
|
43
|
+
var f := FileAccess.open(out, FileAccess.WRITE)
|
|
44
|
+
f.store_string(JSON.stringify(sources, " "))
|
|
45
|
+
f.close()
|
|
46
|
+
print("[dump-deps] ", sources.size(), " sources -> ", out)
|
|
47
|
+
quit()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
func _scan(path: String, sources: Dictionary) -> void:
|
|
51
|
+
var d := DirAccess.open(path)
|
|
52
|
+
if d == null:
|
|
53
|
+
return
|
|
54
|
+
for dir in d.get_directories():
|
|
55
|
+
if dir.begins_with(".") or dir == "node_modules":
|
|
56
|
+
continue
|
|
57
|
+
_scan(path.path_join(dir) + "/", sources)
|
|
58
|
+
for file in d.get_files():
|
|
59
|
+
var p := path.path_join(file)
|
|
60
|
+
if p.get_extension().to_lower() in PACKED:
|
|
61
|
+
var seen := {}
|
|
62
|
+
var loose := _loose(p, seen)
|
|
63
|
+
# The graph holds authored packed sources only: the walk also visits the engine's
|
|
64
|
+
# own products (`res://.godot/imported/…`, reached through the `.import` sidecar),
|
|
65
|
+
# and a product is carried by its *source*, not owned in its own right.
|
|
66
|
+
var packed := seen.keys().filter(func(s: String) -> bool: return not s.begins_with("res://.godot/"))
|
|
67
|
+
packed.sort()
|
|
68
|
+
sources[p] = {"packed": packed, "loose": loose}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
## The loose texture sources reachable from `path` (transitive), sorted. `seen` collects every
|
|
72
|
+
## packed source the walk touches — the graph — including `path` itself.
|
|
73
|
+
func _loose(path: String, seen: Dictionary) -> Array:
|
|
74
|
+
var loose := {}
|
|
75
|
+
_collect(path, loose, seen)
|
|
76
|
+
var arr := loose.keys()
|
|
77
|
+
arr.sort()
|
|
78
|
+
return arr
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
func _classify(dep: String, loose: Dictionary, seen: Dictionary) -> void:
|
|
82
|
+
if dep.is_empty():
|
|
83
|
+
return
|
|
84
|
+
if dep.get_extension().to_lower() in PACKED:
|
|
85
|
+
_collect(dep, loose, seen)
|
|
86
|
+
elif FileAccess.file_exists(dep + ".import"):
|
|
87
|
+
loose[dep] = true
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
func _collect(path: String, loose: Dictionary, seen: Dictionary) -> void:
|
|
91
|
+
if seen.has(path):
|
|
92
|
+
return
|
|
93
|
+
seen[path] = true
|
|
94
|
+
# An imported source's own `get_dependencies` is empty (the importer hides the
|
|
95
|
+
# refs); the imported *product* carries them — recurse through the sidecar.
|
|
96
|
+
var imp := path + ".import"
|
|
97
|
+
if FileAccess.file_exists(imp):
|
|
98
|
+
var cfg := ConfigFile.new()
|
|
99
|
+
if cfg.load(imp) == OK:
|
|
100
|
+
for dst in cfg.get_value("deps", "dest_files", []):
|
|
101
|
+
_collect(String(dst), loose, seen)
|
|
102
|
+
for dep in ResourceLoader.get_dependencies(path):
|
|
103
|
+
_classify(_res_path(String(dep)), loose, seen)
|
|
104
|
+
# String-valued resource paths are invisible to dependency analysis; scan the
|
|
105
|
+
# text for loose refs the engine cannot report.
|
|
106
|
+
if path.get_extension().to_lower() in SCANNED:
|
|
107
|
+
for p in _text_refs(path):
|
|
108
|
+
_classify(p, loose, seen)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
## `res://` path from a dependency entry (`uid://…::::res://…` or plain).
|
|
112
|
+
func _res_path(dep: String) -> String:
|
|
113
|
+
var i := dep.find("res://")
|
|
114
|
+
if i < 0:
|
|
115
|
+
return ""
|
|
116
|
+
return dep.substr(i)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
## Literal `res://` paths appearing in a text source.
|
|
120
|
+
func _text_refs(path: String) -> Array:
|
|
121
|
+
var re := RegEx.new()
|
|
122
|
+
re.compile('res://[^"\']+')
|
|
123
|
+
var refs := []
|
|
124
|
+
for m in re.search_all(FileAccess.get_file_as_string(path)):
|
|
125
|
+
refs.append(m.get_string())
|
|
126
|
+
return refs
|
package/src/editor.ts
CHANGED
|
@@ -107,8 +107,8 @@ export async function resolveEditor(): Promise<string> {
|
|
|
107
107
|
if (import.meta.main) {
|
|
108
108
|
const command = process.argv[2];
|
|
109
109
|
if (command === 'pack') {
|
|
110
|
-
const {
|
|
111
|
-
await
|
|
110
|
+
const { buildAssetPacks, parsePackArgs } = await import('./pack.ts');
|
|
111
|
+
await buildAssetPacks(parsePackArgs(process.argv.slice(3)));
|
|
112
112
|
process.exit(0);
|
|
113
113
|
}
|
|
114
114
|
const exe = await resolveEditor();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
; Example Godot export preset for `@ringozz/godot/pack`'s `
|
|
1
|
+
; Example Godot export preset for `@ringozz/godot/pack`'s `buildAssetPacks()`.
|
|
2
2
|
; Copy to the project root as `export_presets.cfg` (the project owns it) and add
|
|
3
3
|
; your own non-asset trees to `exclude_filter`.
|
|
4
4
|
;
|
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
; engine has no WebP decoder, so those products are staged/transcoded by the host
|
|
7
7
|
; (see `dump-deps.gd`), not read from the pack. `export_filter="all_resources"`
|
|
8
8
|
; ignores `include_filter` — scope with `exclude_filter` only.
|
|
9
|
+
;
|
|
10
|
+
; The bake always splits — one pack per scene (`Pack: <scene>`) plus a shared one — and
|
|
11
|
+
; derives those presets from this one, pruning them again after exporting. Don't write
|
|
12
|
+
; them by hand.
|
|
9
13
|
|
|
10
14
|
[preset.0]
|
|
11
15
|
|
package/src/load.ts
CHANGED
|
@@ -143,7 +143,16 @@ async function materialize([resPath, entry]: [string, AssetFile]): Promise<void>
|
|
|
143
143
|
} else {
|
|
144
144
|
return;
|
|
145
145
|
}
|
|
146
|
-
|
|
146
|
+
// A staged product may already be there even though `FileAccess.fileExists(resPath)` says
|
|
147
|
+
// otherwise — the engine's `res://` lookup and the Emscripten path disagree by the leading
|
|
148
|
+
// slash — and the engine's `copyToFS` throws on a file it already has (`FS.createDataFile`
|
|
149
|
+
// does not replace). `files` lists overlap, and staged products are removed once their
|
|
150
|
+
// resource loads, so this is the normal re-stage, not an error: the bytes are the same file.
|
|
151
|
+
try {
|
|
152
|
+
stageFile?.(ProjectSettings.globalizePath(resPath), bytes);
|
|
153
|
+
} catch (err) {
|
|
154
|
+
if (!FileAccess.fileExists(ProjectSettings.globalizePath(resPath).replace(/\/{2,}/g, '/'))) throw err;
|
|
155
|
+
}
|
|
147
156
|
}
|
|
148
157
|
|
|
149
158
|
const nextTick = () => new Promise(requestAnimationFrame);
|
package/src/pack.ts
CHANGED
|
@@ -1,87 +1,241 @@
|
|
|
1
|
-
/**********************************************************************
|
|
2
|
-
Copyright (c) Vladimir Davidovich. All rights reserved.
|
|
3
|
-
***********************************************************************/
|
|
4
|
-
|
|
5
|
-
import { spawnSync } from 'node:child_process';
|
|
6
|
-
import { copyFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
7
|
-
import { dirname, resolve } from 'node:path';
|
|
8
|
-
import { fileURLToPath } from 'node:url';
|
|
9
|
-
import { resolveEditor } from './editor.ts';
|
|
10
|
-
|
|
11
|
-
// Bakes a Godot project's assets for the runtime: imports,
|
|
12
|
-
//
|
|
13
|
-
// `
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
// The
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
// `export_presets.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
1
|
+
/**********************************************************************
|
|
2
|
+
Copyright (c) Vladimir Davidovich. All rights reserved.
|
|
3
|
+
***********************************************************************/
|
|
4
|
+
|
|
5
|
+
import { spawnSync } from 'node:child_process';
|
|
6
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
7
|
+
import { dirname, resolve } from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import { resolveEditor } from './editor.ts';
|
|
10
|
+
|
|
11
|
+
// Bakes a Godot project's assets for the runtime: imports them, then exports one native
|
|
12
|
+
// `.pck` **per scene** plus a shared one, so a host fetches the shared data and only the
|
|
13
|
+
// level it is playing. `dump-deps.gd` supplies both inputs (no host-side scanning): each
|
|
14
|
+
// source's packed closure — which decides what a pack owns — and the loose (WebP-backed)
|
|
15
|
+
// texture sources, which the host stages and transcodes per file.
|
|
16
|
+
//
|
|
17
|
+
// The result the runtime reads is one file beside the packs: `<out>/packs.json`, holding
|
|
18
|
+
// each pack (its path, whether it mounts always, and the sources it owns) plus those loose
|
|
19
|
+
// texture lists. `preload.ts` reads it to mount the right packs per asset.
|
|
20
|
+
//
|
|
21
|
+
// The project owns `export_presets.cfg` (a committed, editor-managed file), and the scoping
|
|
22
|
+
// has to live there: `--export-pack` runs against a preset. Per-pack scoping is therefore an
|
|
23
|
+
// `exclude_filter` — the repo's `all_resources` convention, which ignores `include_filter`
|
|
24
|
+
// and, unlike the selected modes, never pulls dependencies back in — so each pack excludes
|
|
25
|
+
// exactly the sources the others own (see `splitPacks`). Those presets are cloned from the
|
|
26
|
+
// project's own base preset, one written at a time, and the file is restored when the bake
|
|
27
|
+
// ends: they are derived and the file is tracked.
|
|
28
|
+
|
|
29
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
30
|
+
|
|
31
|
+
/** The dump's one build-time output, merged into the manifest beside the packs. */
|
|
32
|
+
const DUMP = '.godot/pack-dump.json';
|
|
33
|
+
|
|
34
|
+
export interface BuildAssetPackOptions {
|
|
35
|
+
/** res:// roots whose assets are baked (default `['res://src']`). */
|
|
36
|
+
roots?: string[];
|
|
37
|
+
/** Output directory for the packs + manifest, project-relative (default `.godot/packs`). */
|
|
38
|
+
out?: string;
|
|
39
|
+
/** The `export_presets.cfg` preset the generated pack presets derive from (default `Pack`). */
|
|
40
|
+
preset?: string;
|
|
41
|
+
/** Editor executable override (default: `resolveEditor()`). */
|
|
42
|
+
godot?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A pack's assignment: its name, whether it mounts always, and the sources it owns. */
|
|
46
|
+
interface AssetPackSplit {
|
|
47
|
+
/** `shared`, or a scene's file stem. */
|
|
48
|
+
name: string;
|
|
49
|
+
/** Mount for every asset (the shared pack). */
|
|
50
|
+
always?: boolean;
|
|
51
|
+
/** The packable sources this pack owns — the lookup the plugin mounts by. */
|
|
52
|
+
owns: string[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** One baked pack as the runtime sees it. */
|
|
56
|
+
interface AssetPackManifestEntry extends AssetPackSplit {
|
|
57
|
+
/** `res://` path of the `.pck`, for `loadAssetPack`. */
|
|
58
|
+
pck: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The runtime manifest beside the packs: what to mount, and what to stage per file. */
|
|
62
|
+
export interface AssetPackManifest {
|
|
63
|
+
packs: AssetPackManifestEntry[];
|
|
64
|
+
/** Packed source → the loose texture sources it needs at runtime (from `dump-deps.gd`). */
|
|
65
|
+
deps: Record<string, string[]>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Each packed source's packed closure, as `dump-deps.gd` reports it. */
|
|
69
|
+
interface Graph {
|
|
70
|
+
[source: string]: string[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The two build-time outputs of `dump-deps.gd`. */
|
|
74
|
+
interface Dump {
|
|
75
|
+
graph: Graph;
|
|
76
|
+
deps: Record<string, string[]>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The ownership partition, from the dependency graph. A **level is a scene** (`.tscn`) among
|
|
81
|
+
* the scanned sources — a project's levels are its scenes, so nothing declares them — and:
|
|
82
|
+
* **a source exactly one scene reaches is that scene's** (so a model only one level uses
|
|
83
|
+
* rides its pack, though it lives in a shared asset tree), while **a source several scenes
|
|
84
|
+
* reach, or none does, is shared**. A scene whose own pack would be empty is dropped: every
|
|
85
|
+
* source it reaches is reached by another scene too, so it is already in `shared`, and a pack
|
|
86
|
+
* is mounted by *owner*.
|
|
87
|
+
*/
|
|
88
|
+
export function splitPacks(graph: Graph): AssetPackSplit[] {
|
|
89
|
+
const stem = (asset: string) => asset.slice(asset.lastIndexOf('/') + 1).replace(/\.[^.]+$/, '');
|
|
90
|
+
const scenes = Object.keys(graph).filter((s) => s.toLowerCase().endsWith('.tscn')).sort();
|
|
91
|
+
const closure = new Map(scenes.map((s) => [s, new Set([s, ...(graph[s] ?? [])])]));
|
|
92
|
+
|
|
93
|
+
// One pack per scene, named after its file stem (`shared` is reserved; a repeated stem —
|
|
94
|
+
// two `level.tscn` in different directories — gets a suffix).
|
|
95
|
+
const used = new Set(['shared']);
|
|
96
|
+
const packOf = new Map<string, AssetPackSplit>();
|
|
97
|
+
for (const scene of scenes) {
|
|
98
|
+
let name = stem(scene);
|
|
99
|
+
for (let n = 2; used.has(name); n++) name = `${stem(scene)}-${n}`;
|
|
100
|
+
used.add(name);
|
|
101
|
+
packOf.set(scene, { name, owns: [] });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const shared: string[] = [];
|
|
105
|
+
const sources = new Set([...Object.keys(graph), ...Object.values(graph).flat()]);
|
|
106
|
+
for (const source of sources) {
|
|
107
|
+
const owners = scenes.filter((scene) => closure.get(scene)!.has(source));
|
|
108
|
+
if (owners.length === 1) packOf.get(owners[0])!.owns.push(source);
|
|
109
|
+
else shared.push(source);
|
|
110
|
+
}
|
|
111
|
+
shared.sort();
|
|
112
|
+
return [
|
|
113
|
+
{ name: 'shared', always: true, owns: shared },
|
|
114
|
+
...[...packOf.values()]
|
|
115
|
+
.filter((pack) => pack.owns.length > 0)
|
|
116
|
+
.map((pack) => ({ ...pack, owns: pack.owns.sort() })),
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Bakes the packs (see {@link splitPacks}) and writes the runtime manifest. A project with no
|
|
122
|
+
* scene under its roots gets a single `shared.pck` and the same manifest.
|
|
123
|
+
*/
|
|
124
|
+
export async function buildAssetPacks(opts: BuildAssetPackOptions = {}): Promise<void> {
|
|
125
|
+
const editor = opts.godot ?? (await resolveEditor());
|
|
126
|
+
const cwd = process.cwd();
|
|
127
|
+
const roots = opts.roots ?? ['res://src'];
|
|
128
|
+
const out = opts.out ?? '.godot/packs';
|
|
129
|
+
const base = opts.preset ?? 'Pack';
|
|
130
|
+
const prefix = `${base}: `;
|
|
131
|
+
const presets = resolve(cwd, 'export_presets.cfg');
|
|
132
|
+
|
|
133
|
+
const cfg = readFileSync(presets, 'utf8');
|
|
134
|
+
const basePreset = presetBlock(cfg, base);
|
|
135
|
+
const baseExcludes = excludeFilter(basePreset);
|
|
136
|
+
mkdirSync(resolve(cwd, out), { recursive: true });
|
|
137
|
+
|
|
138
|
+
const run = runner(editor, cwd);
|
|
139
|
+
run(['--headless', '--path', '.', '--import']);
|
|
140
|
+
const { graph, deps } = dump(run, cwd, roots);
|
|
141
|
+
|
|
142
|
+
const packs = splitPacks(graph);
|
|
143
|
+
const owned = packs.flatMap((pack) => pack.owns);
|
|
144
|
+
const manifest: AssetPackManifestEntry[] = [];
|
|
145
|
+
|
|
146
|
+
// The project's own presets, with any this tool generated in an earlier run dropped.
|
|
147
|
+
const original = pruneGenerated(cfg, prefix);
|
|
148
|
+
const index = (original.match(/^\[preset\.\d+\]/gm) ?? []).length;
|
|
149
|
+
try {
|
|
150
|
+
for (const { name, always, owns } of packs) {
|
|
151
|
+
const exclude = [
|
|
152
|
+
...baseExcludes,
|
|
153
|
+
...owned.filter((source) => !owns.includes(source)).map((source) => source.replace(/^res:\/\//, '')),
|
|
154
|
+
];
|
|
155
|
+
writeFileSync(presets, `${original.trimEnd()}\n\n${presetFor(basePreset, index, `${prefix}${name}`, exclude)}\n`);
|
|
156
|
+
const pck = `${out}/${name}.pck`;
|
|
157
|
+
run(['--headless', '--path', '.', '--export-pack', `${prefix}${name}`, pck]);
|
|
158
|
+
manifest.push({ name, pck: resPath(pck), ...(always ? { always } : {}), owns });
|
|
159
|
+
}
|
|
160
|
+
} finally {
|
|
161
|
+
// Derived — leave the project's own file as it was, so a bake does not churn a tracked,
|
|
162
|
+
// editor-managed file.
|
|
163
|
+
writeFileSync(presets, original);
|
|
164
|
+
}
|
|
165
|
+
writeFileSync(resolve(cwd, out, 'packs.json'), `${JSON.stringify({ packs: manifest, deps } satisfies AssetPackManifest, null, '\t')}\n`);
|
|
166
|
+
console.log(`[pack] ${manifest.length} packs (${manifest.map((p) => `${p.name}:${p.owns.length}`).join(', ')}) over ${owned.length} sources`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A `spawnSync` runner that throws on a non-zero editor exit. */
|
|
170
|
+
function runner(editor: string, cwd: string) {
|
|
171
|
+
return (args: string[], env?: Record<string, string>) => {
|
|
172
|
+
const r = spawnSync(editor, args, { cwd, stdio: 'inherit', env: { ...process.env, ...env } });
|
|
173
|
+
if ((r.status ?? 1) !== 0) {
|
|
174
|
+
throw new Error(`godot-assets: \`${editor} ${args.join(' ')}\` failed (${r.status})`);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Runs `dump-deps.gd` and splits its output into the graph and the loose-texture lists. */
|
|
180
|
+
function dump(run: (args: string[], env?: Record<string, string>) => void, cwd: string, roots: string[]): Dump {
|
|
181
|
+
copyFileSync(resolve(HERE, 'dump-deps.gd'), resolve(cwd, '.godot', 'pack-dump-deps.gd'));
|
|
182
|
+
run(['--headless', '--path', '.', '--script', 'res://.godot/pack-dump-deps.gd'], {
|
|
183
|
+
GODOT_DUMP_OUT: resPath(DUMP),
|
|
184
|
+
GODOT_DUMP_ROOTS: roots.join(','),
|
|
185
|
+
});
|
|
186
|
+
const abs = resolve(cwd, DUMP);
|
|
187
|
+
if (!existsSync(abs)) throw new Error(`godot-assets: the dump wrote no ${DUMP} (see dump-deps.gd).`);
|
|
188
|
+
const sources = JSON.parse(readFileSync(abs, 'utf8')) as Record<string, { packed: string[]; loose: string[] }>;
|
|
189
|
+
const entries = Object.entries(sources);
|
|
190
|
+
return {
|
|
191
|
+
graph: Object.fromEntries(entries.map(([source, s]) => [source, s.packed])),
|
|
192
|
+
deps: Object.fromEntries(entries.map(([source, s]) => [source, s.loose])),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const resPath = (rel: string) => `res://${rel.replace(/^res:\/\//, '')}`;
|
|
197
|
+
|
|
198
|
+
/** The preset whose `name=` matches, as its raw `[preset.N]` … block (options included). */
|
|
199
|
+
function presetBlock(cfg: string, name: string): string {
|
|
200
|
+
const m = new RegExp(`^name=${JSON.stringify(name)}$`, 'm').exec(cfg);
|
|
201
|
+
if (!m) throw new Error(`godot-assets: no "${name}" preset in export_presets.cfg (see export_presets.example.cfg).`);
|
|
202
|
+
const rest = cfg.slice(cfg.lastIndexOf('[preset.', m.index));
|
|
203
|
+
// The next *preset* header — not this one's `[preset.N.options]`.
|
|
204
|
+
const end = rest.search(/\n\[preset\.\d+\]/);
|
|
205
|
+
return (end < 0 ? rest : rest.slice(0, end)).trimEnd();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** A preset block's `exclude_filter` entries. */
|
|
209
|
+
function excludeFilter(block: string): string[] {
|
|
210
|
+
const line = /^exclude_filter="([^"]*)"/m.exec(block);
|
|
211
|
+
return (line?.[1] ?? '').split(',').map((s) => s.trim()).filter(Boolean);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** A pack's preset: the base preset's own (platform + options), renamed and re-excluded. */
|
|
215
|
+
function presetFor(base: string, index: number, name: string, exclude: string[]): string {
|
|
216
|
+
return base
|
|
217
|
+
.replace(/^\[preset\.\d+\]/, `[preset.${index}]`)
|
|
218
|
+
.replace(/\[preset\.\d+\.options\]/, `[preset.${index}.options]`)
|
|
219
|
+
.replace(/^name="[^"]*"/m, `name=${JSON.stringify(name)}`)
|
|
220
|
+
.replace(/^exclude_filter="[^"]*"/m, `exclude_filter=${JSON.stringify(exclude.join(','))}`);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Drops the presets this tool generated in an earlier run (so re-bakes are idempotent). */
|
|
224
|
+
function pruneGenerated(cfg: string, prefix: string): string {
|
|
225
|
+
const generated = new RegExp(`^name="${prefix}[^"]*"$`, 'm');
|
|
226
|
+
return cfg.split(/(?=\[preset\.\d+\])/).filter((block) => !generated.test(block)).join('');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Parses `godot pack` CLI args into {@link BuildAssetPackOptions}. */
|
|
230
|
+
export function parsePackArgs(args: string[]): BuildAssetPackOptions {
|
|
231
|
+
const opt = (flag: string): string | undefined => {
|
|
232
|
+
const i = args.indexOf(flag);
|
|
233
|
+
return i >= 0 ? args[i + 1] : undefined;
|
|
234
|
+
};
|
|
235
|
+
return {
|
|
236
|
+
roots: opt('--roots')?.split(',').filter(Boolean),
|
|
237
|
+
out: opt('--out'),
|
|
238
|
+
preset: opt('--preset'),
|
|
239
|
+
godot: opt('--godot'),
|
|
240
|
+
};
|
|
241
|
+
}
|
package/src/preload.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { existsSync, readFileSync } from 'node:fs';
|
|
6
6
|
import { dirname, resolve } from 'node:path';
|
|
7
7
|
import { plugin } from 'bun';
|
|
8
|
+
import type { AssetPackManifest } from './pack.ts';
|
|
8
9
|
import type { BunPlugin } from 'bun';
|
|
9
10
|
|
|
10
11
|
// Registers the godot-assets plugin: imports of Godot source assets become
|
|
@@ -12,13 +13,13 @@ import type { BunPlugin } from 'bun';
|
|
|
12
13
|
// load the resource.
|
|
13
14
|
//
|
|
14
15
|
// A packed source asset (`tscn`/`tres`/`gltf`/`gd`/font/…) is loaded from the
|
|
15
|
-
// `.pck` baked by `godot pack`: the module mounts the pack (`loadAssetPack`),
|
|
16
|
+
// `.pck` baked by `godot pack`: the module mounts the pack(s) (`loadAssetPack`),
|
|
16
17
|
// whose remap table resolves the source path to the packed binary (`.scn`/`.res`)
|
|
17
18
|
// with refs the engine resolves itself. The
|
|
18
19
|
// plugin never scans source text for references — the only per-file staging is
|
|
19
20
|
// for *loose* (WebP-backed) textures the wasm engine cannot decode, whose list
|
|
20
|
-
// is computed by the engine (`@ringozz/godot/pack`'s `dump-deps.gd
|
|
21
|
-
//
|
|
21
|
+
// is computed by the engine (`@ringozz/godot/pack`'s `dump-deps.gd`, via
|
|
22
|
+
// `ResourceLoader.get_dependencies`) and shipped in the manifest beside the packs.
|
|
22
23
|
//
|
|
23
24
|
// The load promise persists on `import.meta.hot.data` (carried across HMR
|
|
24
25
|
// re-evaluations) so `use()` never re-suspends; a rejected load is evicted so
|
|
@@ -72,17 +73,29 @@ const extOf = (abs: string) => abs.slice(abs.lastIndexOf('.') + 1).toLowerCase()
|
|
|
72
73
|
const resOf = (abs: string) => 'res://' + abs.slice(ROOT.length + 1).replaceAll('\\', '/');
|
|
73
74
|
const isLoose = (abs: string) => LOOSE.has(extOf(abs));
|
|
74
75
|
|
|
75
|
-
// A project
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
76
|
+
// A project bakes native `.pck`s (`godot --export-pack`) — the engine-converted binary
|
|
77
|
+
// resources + remap tables — through `@ringozz/godot/pack`'s `buildAssetPacks`: one per
|
|
78
|
+
// scene, plus a shared one, so a host fetches the shared data and only the level it is
|
|
79
|
+
// playing. The manifest beside them (`<pack dir>/packs.json`) is the whole runtime contract:
|
|
80
|
+
// which packs exist, which sources each owns, and which loose (WebP-backed) textures the host
|
|
81
|
+
// must stage per file. A module mounts the packs marked `always` (the shared pack) plus its
|
|
82
|
+
// asset's owner, so importing a level brings in exactly what it needs.
|
|
83
|
+
//
|
|
84
|
+
// Everything is optional: with no manifest (desktop, or a project that has not baked) modules
|
|
85
|
+
// load source paths directly.
|
|
86
|
+
const readJson = <T>(file: string): T | null =>
|
|
87
|
+
existsSync(file) ? (JSON.parse(readFileSync(file, 'utf8')) as T) : null;
|
|
88
|
+
|
|
89
|
+
/** Where the packs were baked — `godot pack`'s `--out` (`GODOT_PACKS` overrides for a host). */
|
|
90
|
+
const PACK_DIR = resolve(ROOT, process.env.GODOT_PACKS ?? '.godot/packs');
|
|
91
|
+
const MANIFEST = readJson<AssetPackManifest>(resolve(PACK_DIR, 'packs.json'));
|
|
92
|
+
|
|
93
|
+
/** The packs an asset needs mounted: the always-mounted ones plus its owner. */
|
|
94
|
+
function packsFor(resPath: string): string[] {
|
|
95
|
+
return (MANIFEST?.packs ?? [])
|
|
96
|
+
.filter((pack) => pack.always || pack.owns.includes(resPath))
|
|
97
|
+
.map((pack) => resolve(ROOT, pack.pck.replace(/^res:\/\//, '')));
|
|
98
|
+
}
|
|
86
99
|
|
|
87
100
|
/** Reads the imported destination (`dest_files`) out of an `.import` sidecar. */
|
|
88
101
|
function parseSidecarDest(text: string): string | null {
|
|
@@ -117,7 +130,7 @@ function analyze(abs: string): Map<string, 'text' | 'file'> {
|
|
|
117
130
|
if (isLoose(abs)) {
|
|
118
131
|
addLoose(abs, files);
|
|
119
132
|
}
|
|
120
|
-
for (const loose of
|
|
133
|
+
for (const loose of MANIFEST?.deps[resOf(abs)] ?? []) {
|
|
121
134
|
addLoose(resolve(ROOT, loose.slice('res://'.length)), files);
|
|
122
135
|
}
|
|
123
136
|
return files;
|
|
@@ -133,9 +146,11 @@ function ensureModule(abs: string): string {
|
|
|
133
146
|
`import { ${cls} } from '@ringozz/godot/${cls}';`,
|
|
134
147
|
];
|
|
135
148
|
let ready = 'undefined';
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
149
|
+
const mounts = packsFor(res);
|
|
150
|
+
if (mounts.length) {
|
|
151
|
+
mounts.forEach((url, i) => imports.push(`import pack${i} from ${JSON.stringify(url)} with { type: 'file' };`));
|
|
152
|
+
const loads = mounts.map((_, i) => `loadAssetPack(pack${i})`);
|
|
153
|
+
ready = loads.length === 1 ? loads[0] : `Promise.all([${loads.join(', ')}])`;
|
|
139
154
|
}
|
|
140
155
|
const map: string[] = [];
|
|
141
156
|
let fi = 0;
|