topotopo 0.4.0 → 0.5.0
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/NOTICE +10 -10
- package/README.md +460 -117
- package/dist/cli.js +323 -146
- package/dist/config.d.ts +48 -0
- package/dist/config.js +170 -0
- package/dist/core/bake.d.ts +14 -3
- package/dist/core/bake.js +153 -4
- package/dist/core/bevel.d.ts +3 -1
- package/dist/core/bevel.js +76 -3
- package/dist/core/decimate.d.ts +65 -0
- package/dist/core/decimate.js +409 -3
- package/dist/core/erosion.d.ts +37 -0
- package/dist/core/erosion.js +41 -1
- package/dist/core/geometry.d.ts +5 -1
- package/dist/core/geometry.js +203 -3
- package/dist/core/heightfield.d.ts +27 -4
- package/dist/core/heightfield.js +54 -16
- package/dist/core/intrinsics.d.ts +7 -0
- package/dist/core/intrinsics.js +94 -0
- package/dist/core/manifold.d.ts +12 -1
- package/dist/core/manifold.js +78 -6
- package/dist/core/materials.d.ts +7 -0
- package/dist/core/materials.js +45 -0
- package/dist/core/noise.d.ts +9 -0
- package/dist/core/noise.js +10 -1
- package/dist/core/polymerge.d.ts +21 -0
- package/dist/core/polymerge.js +327 -0
- package/dist/core/repair.d.ts +2 -0
- package/dist/core/repair.js +2 -0
- package/dist/core/report.d.ts +70 -0
- package/dist/core/report.js +275 -0
- package/dist/core/scene.js +2 -1
- package/dist/core/sha256.d.ts +4 -0
- package/dist/core/sha256.js +69 -0
- package/dist/core/shapes.d.ts +3 -0
- package/dist/core/shapes.js +34 -0
- package/dist/core/snippet-lib.d.ts +14 -1
- package/dist/core/snippet-lib.js +39 -13
- package/dist/core/tangents.d.ts +2 -0
- package/dist/core/tangents.js +5 -1
- package/dist/core/texnoise.d.ts +6 -1
- package/dist/core/texnoise.js +6 -2
- package/dist/core/texsdf.d.ts +131 -0
- package/dist/core/texsdf.js +637 -0
- package/dist/core/texture.d.ts +163 -9
- package/dist/core/texture.js +844 -20
- package/dist/core/topology.d.ts +10 -0
- package/dist/core/topology.js +168 -0
- package/dist/core/unwrap.d.ts +72 -0
- package/dist/core/unwrap.js +800 -0
- package/dist/core/usercode.d.ts +27 -0
- package/dist/core/usercode.js +87 -0
- package/dist/core/uv.d.ts +4 -18
- package/dist/core/uv.js +35 -188
- package/dist/core/uvislands.d.ts +62 -0
- package/dist/core/uvislands.js +574 -0
- package/dist/core/uvoverlap.d.ts +58 -0
- package/dist/core/uvoverlap.js +220 -0
- package/dist/core/uvpack.d.ts +64 -0
- package/dist/core/uvpack.js +628 -0
- package/dist/core/uvreport.d.ts +61 -0
- package/dist/core/uvreport.js +207 -143
- package/dist/core/wrangle.d.ts +6 -1
- package/dist/core/wrangle.js +27 -16
- package/dist/core/xatlas.d.ts +16 -1
- package/dist/core/xatlas.js +66 -62
- package/dist/engines.d.ts +63 -0
- package/dist/engines.js +180 -0
- package/dist/explain.d.ts +4 -3
- package/dist/explain.js +129 -12
- package/dist/graph/engine.d.ts +92 -29
- package/dist/graph/engine.js +298 -148
- package/dist/graph/expr.d.ts +6 -3
- package/dist/graph/expr.js +18 -8
- package/dist/graph/outputs.d.ts +23 -0
- package/dist/graph/outputs.js +41 -0
- package/dist/graph/packages.d.ts +72 -0
- package/dist/graph/packages.js +261 -0
- package/dist/graph/parallel.d.ts +3 -1
- package/dist/graph/parallel.js +4 -1
- package/dist/graph/params.d.ts +6 -0
- package/dist/graph/params.js +34 -7
- package/dist/graph/scope.d.ts +55 -0
- package/dist/graph/scope.js +14 -0
- package/dist/graph/types.d.ts +75 -12
- package/dist/graph/types.js +4 -2
- package/dist/graph/vars.d.ts +16 -0
- package/dist/graph/vars.js +80 -0
- package/dist/host/artifacts.d.ts +10 -1
- package/dist/host/artifacts.js +37 -6
- package/dist/host/cook.d.ts +241 -0
- package/dist/host/cook.js +844 -0
- package/dist/host/export-targets.d.ts +42 -5
- package/dist/host/export-targets.js +72 -13
- package/dist/host/godot-commands.d.ts +4 -0
- package/dist/host/godot-commands.js +21 -0
- package/dist/host/godot.d.ts +183 -0
- package/dist/host/godot.js +916 -0
- package/dist/host/node.d.ts +12 -7
- package/dist/host/node.js +27 -9
- package/dist/host/pool.js +51 -2
- package/dist/host/sidecars.d.ts +8 -0
- package/dist/host/sidecars.js +50 -0
- package/dist/host/types.d.ts +19 -6
- package/dist/host/version.d.ts +1 -0
- package/dist/host/version.js +3 -0
- package/dist/host/worker.js +5 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/inspect.d.ts +80 -2
- package/dist/inspect.js +265 -30
- package/dist/io/common.d.ts +25 -5
- package/dist/io/export.js +2 -1
- package/dist/io/files.d.ts +23 -4
- package/dist/io/files.js +60 -8
- package/dist/io/gltf.js +134 -47
- package/dist/io/heightmap.d.ts +3 -0
- package/dist/io/heightmap.js +1 -0
- package/dist/io/import.js +10 -0
- package/dist/io/inspect-scene.d.ts +55 -0
- package/dist/io/inspect-scene.js +155 -0
- package/dist/io/kinds.d.ts +81 -0
- package/dist/io/kinds.js +163 -0
- package/dist/io/points.d.ts +2 -1
- package/dist/io/points.js +3 -1
- package/dist/io/sidecars.d.ts +24 -0
- package/dist/io/sidecars.js +57 -0
- package/dist/io/source.d.ts +65 -0
- package/dist/io/source.js +112 -0
- package/dist/io/targets.d.ts +47 -3
- package/dist/io/targets.js +148 -13
- package/dist/io/textures.js +21 -1
- package/dist/io/unreal.js +5 -1
- package/dist/mcp/docs.d.ts +10 -1
- package/dist/mcp/docs.js +786 -143
- package/dist/mcp/inputs.d.ts +12 -5
- package/dist/mcp/inputs.js +50 -13
- package/dist/mcp/server.js +56 -27
- package/dist/mcp/tools.d.ts +14 -7
- package/dist/mcp/tools.js +196 -103
- package/dist/mcp/verify.d.ts +2 -2
- package/dist/mcp/verify.js +1 -1
- package/dist/mcp/workspace.d.ts +15 -0
- package/dist/mcp/workspace.js +349 -0
- package/dist/nodes/attributes.js +198 -1
- package/dist/nodes/bake.js +151 -47
- package/dist/nodes/beam.d.ts +1 -0
- package/dist/nodes/beam.js +201 -0
- package/dist/nodes/bevel.js +51 -3
- package/dist/nodes/cloth.js +2 -0
- package/dist/nodes/code.js +21 -9
- package/dist/nodes/curves.js +433 -93
- package/dist/nodes/flow.js +53 -33
- package/dist/nodes/generate.js +12 -38
- package/dist/nodes/heightfield.js +248 -77
- package/dist/nodes/imageops.js +16 -19
- package/dist/nodes/index.d.ts +1 -0
- package/dist/nodes/index.js +1 -0
- package/dist/nodes/material.js +89 -24
- package/dist/nodes/measure.js +43 -17
- package/dist/nodes/particles.js +2 -0
- package/dist/nodes/pcg.js +6 -5
- package/dist/nodes/procedural.js +4 -3
- package/dist/nodes/remesh.js +71 -15
- package/dist/nodes/scene.js +4 -2
- package/dist/nodes/sdf.js +10 -5
- package/dist/nodes/solid.js +20 -5
- package/dist/nodes/splines.d.ts +15 -1
- package/dist/nodes/splines.js +112 -105
- package/dist/nodes/texture.d.ts +21 -6
- package/dist/nodes/texture.js +1318 -185
- package/dist/nodes/topology.d.ts +7 -0
- package/dist/nodes/topology.js +133 -6
- package/dist/nodes/transform.js +6 -2
- package/dist/nodes/uv.js +192 -174
- package/dist/nodes/wasmcall.js +5 -1
- package/dist/open.d.ts +19 -0
- package/dist/open.js +12 -0
- package/dist/package.d.ts +288 -0
- package/dist/package.js +772 -0
- package/dist/packages/check.d.ts +32 -0
- package/dist/packages/check.js +169 -0
- package/dist/packages/commands.d.ts +57 -0
- package/dist/packages/commands.js +537 -0
- package/dist/packages/registry.d.ts +796 -0
- package/dist/packages/registry.js +2998 -0
- package/dist/packages/update.d.ts +124 -0
- package/dist/packages/update.js +476 -0
- package/dist/render/options.d.ts +2 -0
- package/dist/render/options.js +44 -5
- package/dist/render/preview.d.ts +15 -4
- package/dist/render/preview.js +6 -4
- package/dist/render/raster.d.ts +66 -5
- package/dist/render/raster.js +506 -138
- package/dist/render/texview.d.ts +6 -0
- package/dist/render/texview.js +27 -0
- package/dist/scan.d.ts +61 -0
- package/dist/scan.js +228 -0
- package/dist/session.d.ts +311 -0
- package/dist/session.js +849 -0
- package/dist/viewer/app/api.js +48 -6
- package/dist/viewer/app/cooking.js +78 -0
- package/dist/viewer/app/embed.js +27 -7
- package/dist/viewer/app/export-popover.js +118 -16
- package/dist/viewer/app/grid.js +5 -1
- package/dist/viewer/app/guides.js +122 -0
- package/dist/viewer/app/keymap.js +6 -3
- package/dist/viewer/app/layers.js +4 -4
- package/dist/viewer/app/main.js +185 -52
- package/dist/viewer/app/markers.js +60 -0
- package/dist/viewer/app/outliner.js +72 -27
- package/dist/viewer/app/output-info.js +45 -0
- package/dist/viewer/app/outputs.css +147 -8
- package/dist/viewer/app/outputs.js +168 -54
- package/dist/viewer/app/packages.js +614 -0
- package/dist/viewer/app/props.js +176 -52
- package/dist/viewer/app/scene.js +30 -5
- package/dist/viewer/app/select.js +4 -3
- package/dist/viewer/app/status.js +12 -15
- package/dist/viewer/app/style.css +65 -7
- package/dist/viewer/app/vars.js +47 -0
- package/dist/viewer/app/welcome.js +72 -0
- package/dist/viewer/graph-file.d.ts +62 -0
- package/dist/viewer/graph-file.js +175 -0
- package/dist/viewer/index.html +23 -10
- package/dist/viewer/json-edit.d.ts +13 -0
- package/dist/viewer/json-edit.js +274 -0
- package/dist/viewer/outputs.d.ts +49 -5
- package/dist/viewer/outputs.js +264 -56
- package/dist/viewer/packages.d.ts +121 -0
- package/dist/viewer/packages.js +584 -0
- package/dist/viewer/server.d.ts +14 -9
- package/dist/viewer/server.js +266 -28
- package/dist/web/app/web.css +419 -0
- package/dist/web/app/web.js +188 -0
- package/dist/{gallery → web}/bake.d.ts +4 -4
- package/dist/{gallery → web}/bake.js +24 -15
- package/dist/web/folder-page.d.ts +101 -0
- package/dist/web/folder-page.js +146 -0
- package/dist/web/folders.d.ts +280 -0
- package/dist/web/folders.js +970 -0
- package/dist/web/library.d.ts +91 -0
- package/dist/web/library.js +228 -0
- package/dist/web/markdown.d.ts +45 -0
- package/dist/web/markdown.js +677 -0
- package/dist/web/pages.d.ts +258 -0
- package/dist/web/pages.js +301 -0
- package/dist/web/server.d.ts +24 -0
- package/dist/web/server.js +1068 -0
- package/dist/workspace.d.ts +88 -118
- package/dist/workspace.js +358 -460
- package/integrations/godot/addons/topotopo/LICENSE +202 -0
- package/integrations/godot/addons/topotopo/README.md +57 -0
- package/integrations/godot/addons/topotopo/context_menu.gd +43 -0
- package/integrations/godot/addons/topotopo/dock.gd +266 -0
- package/integrations/godot/addons/topotopo/dock.tscn +72 -0
- package/integrations/godot/addons/topotopo/export_filter.gd +72 -0
- package/integrations/godot/addons/topotopo/gltf_instancing.gd +131 -0
- package/integrations/godot/addons/topotopo/icons/topotopo.svg +1 -0
- package/integrations/godot/addons/topotopo/inspector.gd +38 -0
- package/integrations/godot/addons/topotopo/instances_post_import.gd +111 -0
- package/integrations/godot/addons/topotopo/package.gd +23 -0
- package/integrations/godot/addons/topotopo/package_importer.gd +187 -0
- package/integrations/godot/addons/topotopo/plugin.cfg +7 -0
- package/integrations/godot/addons/topotopo/plugin.gd +970 -0
- package/integrations/godot/addons/topotopo/record.gd +435 -0
- package/integrations/godot/addons/topotopo/runner.gd +458 -0
- package/integrations/godot/addons/topotopo/tools/check.gd +42 -0
- package/integrations/godot/addons/topotopo/watcher.gd +199 -0
- package/kernels/pkg/README.md +2 -1
- package/kernels/pkg-simd/README.md +2 -1
- package/library/architecture/versions/1.0/architecture.topo +12 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/arch.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/beam_truss.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/brick_wall.json +8 -8
- package/{shelf → library/architecture/versions/1.0/graphs}/door_frame.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/hex_grid_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/house.json +8 -8
- package/library/architecture/versions/1.0/graphs/lineup.json +42 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/pillar.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/railing.json +5 -5
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_gable.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_hip.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_skeleton.json +2 -1
- package/{shelf → library/architecture/versions/1.0/graphs}/scaffold.json +4 -4
- package/{shelf → library/architecture/versions/1.0/graphs}/spiral_stairs.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/stairs.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/tile_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/wall_segment.json +10 -10
- package/{shelf → library/architecture/versions/1.0/graphs}/window_frame.json +2 -2
- package/library/architecture/versions/1.0/version.json +1119 -0
- package/{shelf → library/nature/versions/1.0/graphs}/bush.json +2 -2
- package/{shelf → library/nature/versions/1.0/graphs}/cliff_block.json +4 -3
- package/{shelf → library/nature/versions/1.0/graphs}/forest_patch.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/grass_clumps.json +2 -2
- package/library/nature/versions/1.0/graphs/lineup.json +24 -0
- package/{shelf → library/nature/versions/1.0/graphs}/log.json +6 -6
- package/{shelf → library/nature/versions/1.0/graphs}/rock.json +5 -4
- package/{shelf → library/nature/versions/1.0/graphs}/scatter_rocks.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/tree_lowpoly.json +3 -2
- package/library/nature/versions/1.0/nature.topo +12 -0
- package/library/nature/versions/1.0/version.json +534 -0
- package/{shelf → library/props/versions/1.0/graphs}/barrel.json +5 -4
- package/{shelf → library/props/versions/1.0/graphs}/crate.json +3 -3
- package/library/props/versions/1.0/graphs/lineup.json +18 -0
- package/{shelf → library/props/versions/1.0/graphs}/pallet.json +4 -3
- package/{shelf → library/props/versions/1.0/graphs}/pipe_run.json +3 -3
- package/{shelf → library/props/versions/1.0/graphs}/sandbag_wall.json +5 -5
- package/library/props/versions/1.0/props.topo +12 -0
- package/library/props/versions/1.0/version.json +336 -0
- package/{shelf → library/street/versions/1.0/graphs}/bench.json +5 -5
- package/{shelf → library/street/versions/1.0/graphs}/bollard.json +2 -1
- package/{shelf → library/street/versions/1.0/graphs}/cable.json +4 -4
- package/{shelf → library/street/versions/1.0/graphs}/fence.json +7 -7
- package/{shelf → library/street/versions/1.0/graphs}/lamp_post.json +2 -2
- package/library/street/versions/1.0/graphs/lineup.json +22 -0
- package/{shelf → library/street/versions/1.0/graphs}/road_strip.json +2 -2
- package/{shelf → library/street/versions/1.0/graphs}/sign_post.json +1 -1
- package/library/street/versions/1.0/street.topo +12 -0
- package/library/street/versions/1.0/version.json +421 -0
- package/package.json +17 -8
- package/dist/gallery/app/gallery.css +0 -100
- package/dist/gallery/app/gallery.js +0 -80
- package/dist/gallery/cli.d.ts +0 -4
- package/dist/gallery/cli.js +0 -157
- package/dist/gallery/library.d.ts +0 -124
- package/dist/gallery/library.js +0 -366
- package/dist/gallery/pages.d.ts +0 -94
- package/dist/gallery/pages.js +0 -148
- package/dist/gallery/server.d.ts +0 -21
- package/dist/gallery/server.js +0 -391
- package/dist/graph/migrate.d.ts +0 -23
- package/dist/graph/migrate.js +0 -58
- package/dist/mcp/library.d.ts +0 -12
- package/dist/mcp/library.js +0 -159
- package/shelf/index.json +0 -190
|
@@ -0,0 +1,916 @@
|
|
|
1
|
+
// The Godot flavour of the engine cook (src/host/cook.ts) and the Godot plugin's commands:
|
|
2
|
+
// topotopo cook <.topo | package folder | folder>... --for godot [options] cook packages into their Godot project
|
|
3
|
+
// topotopo cook --jobs <jobs.json> [--force] [--no-cache] [--progress json] the plugin's form (integrations/godot/PROTOCOL.md)
|
|
4
|
+
// topotopo godot setup <project> [--no-enable] install the plugin into a project
|
|
5
|
+
// topotopo godot status <project> [--strict] [--json] what is current, for people and CI
|
|
6
|
+
// The flavour: GLBs Godot 4.7 reads (TopoTopo's glTF defaults, no compression, every required extension one Godot
|
|
7
|
+
// supports, EXT_mesh_gpu_instancing for packed instances with the instances' Cd alone as _COLOR), textures outputs as
|
|
8
|
+
// PNGs, terrain/material/volume outputs not imported (v1), no .source.json files (a .json in res:// would be imported
|
|
9
|
+
// and shipped), output files named by their res:// paths, and Godot's own scan rules (dot names unseen, a folder with
|
|
10
|
+
// .gdignore or, below the root, project.godot not entered). Options come from <id>.topo.import [params] (the plugin's
|
|
11
|
+
// import options) and flags override them, so a terminal or CI cook writes what the plugin writes.
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import zlib from 'node:zlib';
|
|
15
|
+
import { spawnSync } from 'node:child_process';
|
|
16
|
+
import { fileURLToPath } from 'node:url';
|
|
17
|
+
import { exportGeometryAsync } from '../io/export.js';
|
|
18
|
+
import { exportSource } from '../io/source.js';
|
|
19
|
+
import { resolvedExportOptions } from '../io/targets.js';
|
|
20
|
+
import { texturesExport } from '../io/textures.js';
|
|
21
|
+
import { baseOf } from '../io/common.js';
|
|
22
|
+
import { manifestAt, program, slugify, writeAtomic } from '../package.js';
|
|
23
|
+
import { GODOT_PLUGIN_IGNORED, godotProjectOf } from '../engines.js';
|
|
24
|
+
import { setInstall } from '../config.js';
|
|
25
|
+
import { forgetScans } from '../scan.js';
|
|
26
|
+
import { MANIFEST_EXT } from '../session.js';
|
|
27
|
+
import { TOPOTOPO_VERSION } from './version.js';
|
|
28
|
+
import { COOK_PROTOCOL, cachePrune, closeCookPools, cookPackage, freshness, readRecord } from './cook.js';
|
|
29
|
+
import { GODOT_COMMANDS } from './godot-commands.js';
|
|
30
|
+
export { GODOT_COMMANDS };
|
|
31
|
+
/**
|
|
32
|
+
* The glTF extensions Godot 4.7's importer reads (GLTFDocument.get_supported_gltf_extensions() of the 4.7.2 editor,
|
|
33
|
+
* its built-in document extensions included). A GLB the cook writes requires none but these, and
|
|
34
|
+
* EXT_mesh_gpu_instancing when instances stay packed (the plugin's glTF extension reads it).
|
|
35
|
+
*/
|
|
36
|
+
export const GODOT_GLTF_EXTENSIONS = [
|
|
37
|
+
'EXT_texture_webp', 'GODOT_single_root', 'KHR_animation_pointer', 'KHR_lights_punctual', 'KHR_materials_emissive_strength',
|
|
38
|
+
'KHR_materials_pbrSpecularGlossiness', 'KHR_materials_unlit', 'KHR_node_visibility', 'KHR_texture_basisu', 'KHR_texture_transform',
|
|
39
|
+
'OMI_collider', 'OMI_physics_body', 'OMI_physics_shape',
|
|
40
|
+
];
|
|
41
|
+
/** A package's TopoTopo folders, which Godot should not import (each with an empty .gdignore). */
|
|
42
|
+
export const GODOT_HIDDEN = GODOT_PLUGIN_IGNORED;
|
|
43
|
+
/** The addon's folder in a project, and its plugin.cfg as project.godot names it. */
|
|
44
|
+
export const ADDON_DIR = 'addons/topotopo';
|
|
45
|
+
export const PLUGIN_CFG = `res://${ADDON_DIR}/plugin.cfg`;
|
|
46
|
+
/** What a project's .gitignore gets: a cook's files being written, and its lock. */
|
|
47
|
+
export const GITIGNORE_LINES = ['*.topotopo-part', '**/.topotopo/cook.lock'];
|
|
48
|
+
/** What every export preset's exclude_filter gets: Godot's GDScript exporter ships the addon's scripts otherwise. */
|
|
49
|
+
export const PRESET_EXCLUDE = `${ADDON_DIR}/*`;
|
|
50
|
+
/** The import options' defaults (the plugin's package_importer.gd). */
|
|
51
|
+
export const IMPORT_DEFAULTS = { folder: 'outputs', per_output_folders: true, skip: '', mode: 0 };
|
|
52
|
+
const ext = import.meta.url.endsWith('.ts') ? 'ts' : 'js';
|
|
53
|
+
/** The CLI the plugin runs: src/cli.ts in a checkout, dist/cli.js installed. */
|
|
54
|
+
export const CLI_PATH = fileURLToPath(new URL(`../cli.${ext}`, import.meta.url));
|
|
55
|
+
/** The addon this TopoTopo carries (integrations/godot/addons/topotopo, shipped in the npm package). */
|
|
56
|
+
export const ADDON_SOURCE = fileURLToPath(new URL('../../integrations/godot/addons/topotopo', import.meta.url));
|
|
57
|
+
const posix = (p) => p.split(path.sep).join('/');
|
|
58
|
+
const byCode = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
59
|
+
const within = (inner, outer) => { const r = path.relative(path.resolve(outer), path.resolve(inner)); return !r || (!r.startsWith('..') && !path.isAbsolute(r)); };
|
|
60
|
+
const isFile = (p) => !!fs.statSync(p, { throwIfNoEntry: false })?.isFile();
|
|
61
|
+
const isDir = (p) => !!fs.statSync(p, { throwIfNoEntry: false })?.isDirectory();
|
|
62
|
+
/** A usage, setup or protocol error: exit code 2, nothing written. */
|
|
63
|
+
export class SetupError extends Error {
|
|
64
|
+
}
|
|
65
|
+
// ---------------------------------------------------------------------------------------------------------- res://
|
|
66
|
+
/** A path in a Godot project as res://… ("res://" for the project folder); throws for a path outside it. */
|
|
67
|
+
export function resPath(project, abs) {
|
|
68
|
+
const r = path.relative(path.resolve(project), path.resolve(abs));
|
|
69
|
+
if (r.startsWith('..') || path.isAbsolute(r))
|
|
70
|
+
throw new SetupError(`${abs} is outside the Godot project ${project}: Godot names files by res:// paths`);
|
|
71
|
+
return `res://${posix(r)}`;
|
|
72
|
+
}
|
|
73
|
+
/** A res:// path in a project as a path. */
|
|
74
|
+
export function fromRes(project, res) {
|
|
75
|
+
return path.join(path.resolve(project), ...res.replace(/^res:\/\//, '').split('/').filter(Boolean));
|
|
76
|
+
}
|
|
77
|
+
/** A res:// path normalised ("." and ".." resolved, no doubled or trailing slash); throws when it leaves the project. */
|
|
78
|
+
export function normalRes(res) {
|
|
79
|
+
const parts = [];
|
|
80
|
+
for (const p of res.replace(/^res:\/\//, '').split(/[\\/]/)) {
|
|
81
|
+
if (!p || p === '.')
|
|
82
|
+
continue;
|
|
83
|
+
if (p === '..') {
|
|
84
|
+
if (!parts.length)
|
|
85
|
+
throw new SetupError(`${res} leaves the Godot project`);
|
|
86
|
+
parts.pop();
|
|
87
|
+
}
|
|
88
|
+
else
|
|
89
|
+
parts.push(p);
|
|
90
|
+
}
|
|
91
|
+
return `res://${parts.join('/')}`;
|
|
92
|
+
}
|
|
93
|
+
// ------------------------------------------------------------------------------------------------ import options
|
|
94
|
+
/** A Godot Variant in text: a quoted string (with \" \\ \n \t), true/false, a number; else the text itself. */
|
|
95
|
+
function variant(v) {
|
|
96
|
+
const t = v.trim();
|
|
97
|
+
if (t === 'true' || t === 'false')
|
|
98
|
+
return t === 'true';
|
|
99
|
+
if (/^-?\d+(\.\d+)?$/.test(t))
|
|
100
|
+
return Number(t);
|
|
101
|
+
const m = /^"((?:[^"\\]|\\.)*)"$/.exec(t);
|
|
102
|
+
if (m)
|
|
103
|
+
return m[1].replace(/\\(.)/g, (_x, c) => ({ n: '\n', t: '\t', r: '\r' }[c] ?? c));
|
|
104
|
+
return t;
|
|
105
|
+
}
|
|
106
|
+
/** The [params] of a .topo's import file (<id>.topo.import), the plugin's import options; {} when there is none. */
|
|
107
|
+
export function readImportParams(topo) {
|
|
108
|
+
let text;
|
|
109
|
+
try {
|
|
110
|
+
text = fs.readFileSync(`${topo}.import`, 'utf8');
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return {};
|
|
114
|
+
}
|
|
115
|
+
const out = {};
|
|
116
|
+
let inParams = false;
|
|
117
|
+
for (const line of text.replace(/^\uFEFF/, '').split(/\r?\n/)) {
|
|
118
|
+
const s = /^\s*\[([^\]]+)\]\s*$/.exec(line);
|
|
119
|
+
if (s) {
|
|
120
|
+
inParams = s[1] === 'params';
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (!inParams)
|
|
124
|
+
continue;
|
|
125
|
+
const kv = /^\s*([^=\s][^=]*?)\s*=\s*(.*)$/.exec(line);
|
|
126
|
+
if (kv)
|
|
127
|
+
out[kv[1]] = variant(kv[2]);
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* A package's cook options: the import options of its .topo (<id>.topo.import [params]: outputs/folder, relative to
|
|
133
|
+
* the package or res://; outputs/per_output_folders; outputs/skip, comma-separated; instances/mode, 0 MultiMesh or
|
|
134
|
+
* 1 Expand), the defaults where it has none, and `given` over both; the folder as a normalised res:// path.
|
|
135
|
+
*/
|
|
136
|
+
export function godotOptions(topo, project, given = {}) {
|
|
137
|
+
const p = readImportParams(topo), dir = path.dirname(topo);
|
|
138
|
+
const str = (k, d) => (typeof p[k] === 'string' ? p[k] : d);
|
|
139
|
+
const folderText = given.into ?? str('outputs/folder', IMPORT_DEFAULTS.folder);
|
|
140
|
+
let folder;
|
|
141
|
+
if (/^res:\/\//.test(folderText))
|
|
142
|
+
folder = normalRes(folderText);
|
|
143
|
+
else
|
|
144
|
+
folder = resPath(project, path.isAbsolute(folderText) ? folderText : path.resolve(dir, folderText));
|
|
145
|
+
folder = normalRes(folder);
|
|
146
|
+
const perOutput = given.per_output_folders ?? (typeof p['outputs/per_output_folders'] === 'boolean' ? p['outputs/per_output_folders'] : IMPORT_DEFAULTS.per_output_folders);
|
|
147
|
+
const skip = given.skip ?? str('outputs/skip', IMPORT_DEFAULTS.skip).split(',').map((s) => s.trim()).filter(Boolean);
|
|
148
|
+
const mode = p['instances/mode'];
|
|
149
|
+
const instancing = given.instancing ?? (mode === 1 || mode === 'Expand' ? 'expand' : 'packed');
|
|
150
|
+
return { folder, instancing, per_output_folders: perOutput, skip: [...new Set(skip)].sort(byCode) };
|
|
151
|
+
}
|
|
152
|
+
// ------------------------------------------------------------------------------------------------------- the GLB
|
|
153
|
+
/** The JSON chunk of a GLB, or null for bytes that are none. */
|
|
154
|
+
export function glbJson(bytes) {
|
|
155
|
+
if (bytes.length < 20)
|
|
156
|
+
return null;
|
|
157
|
+
const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
158
|
+
if (dv.getUint32(0, true) !== 0x46546c67)
|
|
159
|
+
return null;
|
|
160
|
+
const len = dv.getUint32(12, true), type = dv.getUint32(16, true);
|
|
161
|
+
if (type !== 0x4e4f534a || 20 + len > bytes.length)
|
|
162
|
+
return null;
|
|
163
|
+
try {
|
|
164
|
+
return JSON.parse(new TextDecoder().decode(bytes.subarray(20, 20 + len)));
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** The package a GLB's source block names (asset.extras.source.package), or null. */
|
|
171
|
+
export function glbPackage(file) {
|
|
172
|
+
if (!/\.glb$/i.test(file))
|
|
173
|
+
return null;
|
|
174
|
+
let bytes;
|
|
175
|
+
try {
|
|
176
|
+
bytes = new Uint8Array(fs.readFileSync(file));
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
const p = glbJson(bytes)?.asset?.extras?.source?.package;
|
|
182
|
+
return typeof p === 'string' ? p : null;
|
|
183
|
+
}
|
|
184
|
+
/** The keyword of the tEXt chunk that names the package a cooked PNG was made for (PNG has no source block). */
|
|
185
|
+
export const PNG_PACKAGE_KEY = 'TopoTopo package';
|
|
186
|
+
/** A PNG with a tEXt chunk after its IHDR naming the package it was cooked for (decoders ignore it). */
|
|
187
|
+
export function markPng(bytes, pkg) {
|
|
188
|
+
if (bytes.length < 33 || bytes[12] !== 0x49 || bytes[13] !== 0x48 || bytes[14] !== 0x44 || bytes[15] !== 0x52)
|
|
189
|
+
return bytes;
|
|
190
|
+
const body = Buffer.concat([Buffer.from('tEXt', 'latin1'), Buffer.from(PNG_PACKAGE_KEY, 'latin1'), Buffer.from([0]), Buffer.from(pkg, 'latin1')]);
|
|
191
|
+
const chunk = Buffer.alloc(body.length + 8);
|
|
192
|
+
chunk.writeUInt32BE(body.length - 4, 0);
|
|
193
|
+
body.copy(chunk, 4);
|
|
194
|
+
chunk.writeUInt32BE(zlib.crc32(body) >>> 0, body.length + 4);
|
|
195
|
+
return new Uint8Array(Buffer.concat([bytes.subarray(0, 33), chunk, bytes.subarray(33)]));
|
|
196
|
+
}
|
|
197
|
+
/** The package a PNG's tEXt chunk names (markPng), or null. */
|
|
198
|
+
export function pngPackage(bytes) {
|
|
199
|
+
if (bytes.length < 8 || bytes[1] !== 0x50 || bytes[2] !== 0x4e || bytes[3] !== 0x47)
|
|
200
|
+
return null;
|
|
201
|
+
const b = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
202
|
+
for (let at = 8; at + 8 <= b.length;) {
|
|
203
|
+
const len = b.readUInt32BE(at), type = b.toString('latin1', at + 4, at + 8);
|
|
204
|
+
if (type === 'IDAT' || type === 'IEND')
|
|
205
|
+
return null;
|
|
206
|
+
if (type === 'tEXt') {
|
|
207
|
+
const data = b.subarray(at + 8, at + 8 + len), z = data.indexOf(0);
|
|
208
|
+
if (z >= 0 && data.toString('latin1', 0, z) === PNG_PACKAGE_KEY)
|
|
209
|
+
return data.toString('latin1', z + 1);
|
|
210
|
+
}
|
|
211
|
+
at += 12 + len;
|
|
212
|
+
}
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
/** The package a cooked file says it was made for: a GLB's source block, a PNG's tEXt chunk; null for other files. */
|
|
216
|
+
export function cookedFor(file) {
|
|
217
|
+
if (/\.glb$/i.test(file))
|
|
218
|
+
return glbPackage(file);
|
|
219
|
+
if (!/\.png$/i.test(file))
|
|
220
|
+
return null;
|
|
221
|
+
try {
|
|
222
|
+
return pngPackage(new Uint8Array(fs.readFileSync(file)));
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/** Why outputs of a kind are not imported (v1), or null. */
|
|
229
|
+
export function godotSkipKind(kind) {
|
|
230
|
+
return kind === 'terrain' || kind === 'material' || kind === 'volume' ? `${kind} outputs are not imported into Godot yet` : null;
|
|
231
|
+
}
|
|
232
|
+
/** The Godot flavour of the cook for one project. */
|
|
233
|
+
export function godotFlavour(project) {
|
|
234
|
+
return {
|
|
235
|
+
for: 'godot',
|
|
236
|
+
project: path.resolve(project),
|
|
237
|
+
skipKind: godotSkipKind,
|
|
238
|
+
outputDir: (o, id) => (o.per_output_folders ? path.join(fromRes(project, o.folder), id) : fromRes(project, o.folder)),
|
|
239
|
+
recordPath: (abs) => resPath(project, abs),
|
|
240
|
+
absolutePath: (res) => fromRes(project, res),
|
|
241
|
+
ownerOf: cookedFor,
|
|
242
|
+
exposed: (dir) => (godotProjectOf(dir) ? GODOT_HIDDEN.filter((d) => isDir(path.join(dir, d)) && !fs.existsSync(path.join(dir, d, '.gdignore'))).map((d) => `${d}/`) : []),
|
|
243
|
+
async write(geo, x) {
|
|
244
|
+
const warnings = [];
|
|
245
|
+
const deflate = (d) => zlib.deflateSync(d, { level: 6 });
|
|
246
|
+
if (x.kind === 'textures') {
|
|
247
|
+
// one PNG per image, nothing else (materials.json would be a .json in res://)
|
|
248
|
+
const r = texturesExport(geo, '', { deflate });
|
|
249
|
+
return { files: r.files.filter((f) => /\.png$/i.test(f.path)).map((f) => ({ name: baseOf(f.path), bytes: markPng(f.bytes, x.packageName) })), warnings };
|
|
250
|
+
}
|
|
251
|
+
const o = x.output;
|
|
252
|
+
const options = resolvedExportOptions('glb', { instancing: x.options.instancing });
|
|
253
|
+
const source = exportSource({
|
|
254
|
+
graph: o.graph, spec: o.spec, node: o.node, output: o.name, frame: 1, fps: 24, format: 'glb', options, packages: x.packages, package: x.packageName, version: TOPOTOPO_VERSION,
|
|
255
|
+
});
|
|
256
|
+
const bytes = await exportGeometryAsync(geo, 'glb', {
|
|
257
|
+
upAxis: 'y', scale: 1, splitByName: true, custom: false, materials: true, vertexColors: false, compress: 'none', quantize: false, tangents: 'auto',
|
|
258
|
+
instancing: x.options.instancing, instanceColors: true, deflate, readFile: (t) => x.host.readFile(t), warn: (m) => { if (!warnings.includes(m))
|
|
259
|
+
warnings.push(m); }, source,
|
|
260
|
+
});
|
|
261
|
+
const required = glbJson(bytes)?.extensionsRequired ?? [];
|
|
262
|
+
const allowed = new Set([...GODOT_GLTF_EXTENSIONS, ...(x.options.instancing === 'packed' ? ['EXT_mesh_gpu_instancing'] : [])]);
|
|
263
|
+
const unread = required.filter((e) => !allowed.has(e));
|
|
264
|
+
if (unread.length)
|
|
265
|
+
throw new Error(`the GLB would require ${unread.join(', ')}, which Godot 4.7 does not read; nothing was written for this output`);
|
|
266
|
+
return { files: [{ name: `${x.id}.glb`, bytes }], warnings };
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
// ------------------------------------------------------------------------------------------------ Godot's scan
|
|
271
|
+
/**
|
|
272
|
+
* Walk a Godot project as Godot's scan sees it: names starting with "." are not seen, a folder holding .gdignore and a
|
|
273
|
+
* folder below the root holding project.godot are not entered. `visit` gets each folder and its file names; it
|
|
274
|
+
* returns false to leave the folder's subfolders out.
|
|
275
|
+
*/
|
|
276
|
+
export function godotWalk(root, visit) {
|
|
277
|
+
const walk = (dir, depth) => {
|
|
278
|
+
let entries;
|
|
279
|
+
try {
|
|
280
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
281
|
+
}
|
|
282
|
+
catch {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const files = entries.filter((e) => e.isFile()).map((e) => e.name);
|
|
286
|
+
if (depth > 0 && (files.includes('.gdignore') || files.includes('project.godot')))
|
|
287
|
+
return;
|
|
288
|
+
if (visit(dir, files.filter((f) => !f.startsWith('.'))) === false || depth >= 64)
|
|
289
|
+
return;
|
|
290
|
+
for (const e of entries.sort((a, b) => byCode(a.name, b.name)))
|
|
291
|
+
if (e.isDirectory() && !e.name.startsWith('.'))
|
|
292
|
+
walk(path.join(dir, e.name), depth + 1);
|
|
293
|
+
};
|
|
294
|
+
walk(path.resolve(root), 0);
|
|
295
|
+
}
|
|
296
|
+
const topoIn = (files) => files.filter((f) => f.endsWith(MANIFEST_EXT) && f.length > MANIFEST_EXT.length).sort(byCode);
|
|
297
|
+
/**
|
|
298
|
+
* A .topo that is a published copy, not a package: one beside a version.json, in a topotopo-packages/ folder, or in
|
|
299
|
+
* the versions/ or packages/ folder of a package (a folder holding a .topo).
|
|
300
|
+
*/
|
|
301
|
+
export function isPublishedCopy(topo) {
|
|
302
|
+
const dir = path.dirname(path.resolve(topo));
|
|
303
|
+
if (isFile(path.join(dir, 'version.json')))
|
|
304
|
+
return true;
|
|
305
|
+
for (let d = dir;;) {
|
|
306
|
+
const up = path.dirname(d);
|
|
307
|
+
if (up === d)
|
|
308
|
+
return false;
|
|
309
|
+
const name = path.basename(d).toLowerCase();
|
|
310
|
+
if (name === 'topotopo-packages')
|
|
311
|
+
return true;
|
|
312
|
+
if ((name === 'versions' || name === 'packages') && topoIn(fs.readdirSync(up).filter((f) => isFile(path.join(up, f)))).length)
|
|
313
|
+
return true;
|
|
314
|
+
d = up;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/** Every .topo Godot's scan of a project sees, with whether it is a published copy. */
|
|
318
|
+
export function godotTopos(project) {
|
|
319
|
+
const out = [];
|
|
320
|
+
godotWalk(project, (dir, files) => { for (const f of topoIn(files))
|
|
321
|
+
out.push({ topo: path.join(dir, f), copy: isPublishedCopy(path.join(dir, f)) }); });
|
|
322
|
+
return out;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* The packages a cook target names: a .topo file, a package folder, or every package below a folder (dot folders,
|
|
326
|
+
* .gdignore folders, folders below it holding project.godot, versions/, packages/ and topotopo-packages/ skipped; a
|
|
327
|
+
* package is not entered). Throws a SetupError for a target that is none of these.
|
|
328
|
+
*/
|
|
329
|
+
export function cookTargets(target) {
|
|
330
|
+
const abs = path.resolve(target);
|
|
331
|
+
if (isFile(abs)) {
|
|
332
|
+
if (!abs.endsWith(MANIFEST_EXT))
|
|
333
|
+
throw new SetupError(`${target} is no package manifest (<id>${MANIFEST_EXT}), package folder or folder`);
|
|
334
|
+
return [abs];
|
|
335
|
+
}
|
|
336
|
+
if (!isDir(abs))
|
|
337
|
+
throw new SetupError(`no such file or folder: ${target}`);
|
|
338
|
+
const own = topoIn(fs.readdirSync(abs).filter((f) => isFile(path.join(abs, f))));
|
|
339
|
+
if (own.length)
|
|
340
|
+
return [path.join(abs, own[0])];
|
|
341
|
+
const out = [];
|
|
342
|
+
const walk = (dir, depth) => {
|
|
343
|
+
let entries;
|
|
344
|
+
try {
|
|
345
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
346
|
+
}
|
|
347
|
+
catch {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const files = entries.filter((e) => e.isFile()).map((e) => e.name);
|
|
351
|
+
if (depth > 0) {
|
|
352
|
+
if (files.includes('.gdignore') || files.includes('project.godot'))
|
|
353
|
+
return;
|
|
354
|
+
const t = topoIn(files);
|
|
355
|
+
if (t.length) {
|
|
356
|
+
out.push(path.join(dir, t[0]));
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (depth >= 32)
|
|
361
|
+
return;
|
|
362
|
+
for (const e of entries.sort((a, b) => byCode(a.name, b.name)))
|
|
363
|
+
if (e.isDirectory() && !e.name.startsWith('.') && !['versions', 'packages', 'topotopo-packages', 'node_modules'].includes(e.name.toLowerCase()))
|
|
364
|
+
walk(path.join(dir, e.name), depth + 1);
|
|
365
|
+
};
|
|
366
|
+
walk(abs, 0);
|
|
367
|
+
return out;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* A package to cook: its manifest (checked: one .topo, an id), not a published copy, inside a Godot project; the
|
|
371
|
+
* options from its import file with `given` over them. Throws a SetupError otherwise.
|
|
372
|
+
*/
|
|
373
|
+
export function godotJob(topo, given = {}) {
|
|
374
|
+
const abs = path.resolve(topo), dir = path.dirname(abs);
|
|
375
|
+
if (isPublishedCopy(abs))
|
|
376
|
+
throw new SetupError(`${abs} is a published copy, not a package: cook its package's working folder; hide copies from Godot with .gdignore (${program()} godot setup <project>)`);
|
|
377
|
+
let at;
|
|
378
|
+
try {
|
|
379
|
+
at = manifestAt(dir);
|
|
380
|
+
}
|
|
381
|
+
catch (e) {
|
|
382
|
+
throw new SetupError(e.message);
|
|
383
|
+
}
|
|
384
|
+
if (!at || path.resolve(at.file) !== abs)
|
|
385
|
+
throw new SetupError(`${abs} is no package manifest`);
|
|
386
|
+
const project = godotProjectOf(dir);
|
|
387
|
+
if (!project)
|
|
388
|
+
throw new SetupError(`${dir} is in no Godot project (no project.godot at or above it): a cook for Godot names its files by res:// paths`);
|
|
389
|
+
if (path.resolve(project) === dir)
|
|
390
|
+
throw new SetupError(`${dir} is a package at the root of its Godot project, which is not supported: its assets/ would hide the game's art from Godot; move the package into a folder of the project`);
|
|
391
|
+
const options = godotOptions(abs, project, given);
|
|
392
|
+
return { dir, topo: abs, id: at.id, options, adopt: given.adopt, report: given.report, timeout: given.timeout, project };
|
|
393
|
+
}
|
|
394
|
+
// ---------------------------------------------------------------------------------------------------- the commands
|
|
395
|
+
/** The cook command's usage. */
|
|
396
|
+
const cookUsage = () => GODOT_COMMANDS.filter((c) => c.usage.startsWith('cook')).map((c) => `usage: ${program()} ${c.usage}`).join('\n');
|
|
397
|
+
/** A command's arguments: valued options, flags, and the rest; an unknown option is a SetupError. */
|
|
398
|
+
function parseArgs(argv, valued, flags, usage) {
|
|
399
|
+
const opts = {}, set = new Set(), rest = [];
|
|
400
|
+
for (let i = 0; i < argv.length; i++) {
|
|
401
|
+
const a = argv[i];
|
|
402
|
+
if (valued.includes(a)) {
|
|
403
|
+
const v = argv[i + 1];
|
|
404
|
+
if (v === undefined || (v.startsWith('--') && v !== '--'))
|
|
405
|
+
throw new SetupError(`${a} needs a value\n${usage()}`);
|
|
406
|
+
opts[a] = v;
|
|
407
|
+
i++;
|
|
408
|
+
}
|
|
409
|
+
else if (flags.includes(a))
|
|
410
|
+
set.add(a);
|
|
411
|
+
else if (a.startsWith('--'))
|
|
412
|
+
throw new SetupError(`unknown option ${a}\n${usage()}`);
|
|
413
|
+
else
|
|
414
|
+
rest.push(a);
|
|
415
|
+
}
|
|
416
|
+
return { opts, set, rest };
|
|
417
|
+
}
|
|
418
|
+
const PER_PACKAGE = ['--into', '--skip', '--instancing', '--timeout', '--report'];
|
|
419
|
+
/** A list option ("a,b") as a list. */
|
|
420
|
+
const list = (v) => v.split(',').map((s) => s.trim()).filter(Boolean);
|
|
421
|
+
/**
|
|
422
|
+
* topotopo cook … --for godot, and cook --jobs <file>: every package checked first (a usage, setup or protocol error
|
|
423
|
+
* exits 2 and writes nothing), then cooked one after the other in this process; exit 1 when an output or a package
|
|
424
|
+
* failed (records and reports are still written), else 0.
|
|
425
|
+
*/
|
|
426
|
+
export async function cookCommand(argv, io) {
|
|
427
|
+
let jobs;
|
|
428
|
+
let flags;
|
|
429
|
+
try {
|
|
430
|
+
const { opts, set, rest } = parseArgs(argv, ['--for', '--into', '--skip', '--instancing', '--timeout', '--report', '--progress', '--jobs'], ['--flat', '--adopt', '--force', '--no-cache', '--quiet'], cookUsage);
|
|
431
|
+
const progress = opts['--progress'];
|
|
432
|
+
if (progress !== undefined && progress !== 'json')
|
|
433
|
+
throw new SetupError(`--progress takes json, got "${progress}"`);
|
|
434
|
+
flags = { force: set.has('--force'), noCache: set.has('--no-cache'), json: progress === 'json', quiet: set.has('--quiet') };
|
|
435
|
+
if (opts['--jobs'] !== undefined) {
|
|
436
|
+
const extra = [...PER_PACKAGE.filter((f) => opts[f] !== undefined), ...['--flat', '--adopt'].filter((f) => set.has(f)), ...(rest.length ? [rest[0]] : [])];
|
|
437
|
+
if (extra.length)
|
|
438
|
+
throw new SetupError(`--jobs names the packages and their options itself: leave out ${extra.join(', ')}\n${cookUsage()}`);
|
|
439
|
+
if (opts['--for'] !== undefined && opts['--for'] !== 'godot')
|
|
440
|
+
throw new SetupError(`--for ${opts['--for']}: only godot is supported`);
|
|
441
|
+
jobs = readJobs(opts['--jobs']);
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
if (opts['--for'] === undefined)
|
|
445
|
+
throw new SetupError(`cook needs --for godot (the engine to cook into)\n${cookUsage()}`);
|
|
446
|
+
if (opts['--for'] !== 'godot')
|
|
447
|
+
throw new SetupError(`--for ${opts['--for']}: only godot is supported`);
|
|
448
|
+
if (!rest.length)
|
|
449
|
+
throw new SetupError(`cook needs a .topo, a package folder or a folder\n${cookUsage()}`);
|
|
450
|
+
const instancing = opts['--instancing'];
|
|
451
|
+
if (instancing !== undefined && instancing !== 'packed' && instancing !== 'expand')
|
|
452
|
+
throw new SetupError(`--instancing takes packed or expand, got "${instancing}"`);
|
|
453
|
+
const timeout = opts['--timeout'] !== undefined ? Number(opts['--timeout']) : undefined;
|
|
454
|
+
if (timeout !== undefined && !(timeout > 0))
|
|
455
|
+
throw new SetupError(`--timeout takes seconds above 0, got "${opts['--timeout']}"`);
|
|
456
|
+
const topos = [...new Set(rest.flatMap(cookTargets))];
|
|
457
|
+
if (opts['--report'] !== undefined && topos.length !== 1)
|
|
458
|
+
throw new SetupError(`--report names one report file, and the targets hold ${topos.length} packages: cook them one at a time, or use --jobs`);
|
|
459
|
+
const into = opts['--into'] === undefined ? undefined : /^res:\/\//.test(opts['--into']) ? opts['--into'] : path.resolve(opts['--into']);
|
|
460
|
+
jobs = topos.map((t) => godotJob(t, {
|
|
461
|
+
into, per_output_folders: set.has('--flat') ? false : undefined, skip: opts['--skip'] !== undefined ? list(opts['--skip']) : undefined,
|
|
462
|
+
instancing: instancing, adopt: set.has('--adopt'), report: opts['--report'] !== undefined ? path.resolve(opts['--report']) : undefined, timeout,
|
|
463
|
+
}));
|
|
464
|
+
if (!jobs.length) {
|
|
465
|
+
io.out(`no package below ${rest.join(', ')}: nothing to cook`);
|
|
466
|
+
return 0;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
catch (e) {
|
|
471
|
+
io.err(e instanceof SetupError ? e.message : String(e?.message ?? e));
|
|
472
|
+
return 2;
|
|
473
|
+
}
|
|
474
|
+
const human = (s) => { if (!flags.quiet)
|
|
475
|
+
(flags.json ? io.err : io.out)(s); };
|
|
476
|
+
let ok = true;
|
|
477
|
+
try {
|
|
478
|
+
for (const job of jobs) {
|
|
479
|
+
const rep = await cookPackage(job, godotFlavour(job.project), { force: flags.force, noCache: flags.noCache, progress: flags.json ? (l) => io.out(l) : undefined });
|
|
480
|
+
ok &&= rep.ok;
|
|
481
|
+
human(summary(rep, job.project));
|
|
482
|
+
for (const line of failures(rep))
|
|
483
|
+
io.err(line);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
finally {
|
|
487
|
+
await closeCookPools();
|
|
488
|
+
cachePrune();
|
|
489
|
+
}
|
|
490
|
+
return ok ? 0 : 1;
|
|
491
|
+
}
|
|
492
|
+
/** A package's cook in a line. */
|
|
493
|
+
function summary(rep, project) {
|
|
494
|
+
const where = (() => { try {
|
|
495
|
+
return resPath(project, rep.package.dir);
|
|
496
|
+
}
|
|
497
|
+
catch {
|
|
498
|
+
return rep.package.dir;
|
|
499
|
+
} })();
|
|
500
|
+
if (!rep.outputs.length && rep.errors.length)
|
|
501
|
+
return `${rep.package.id} (${where}): did not cook`;
|
|
502
|
+
const by = (s) => rep.outputs.filter((o) => o.state === s).map((o) => o.id);
|
|
503
|
+
const parts = ['written', 'cached', 'unchanged', 'failed', 'skipped'].map((s) => [s, by(s)]).filter(([, l]) => l.length).map(([s, l]) => `${s} ${l.join(', ')}`);
|
|
504
|
+
return `${rep.package.id} (${where}): ${rep.outputs.length} output${rep.outputs.length === 1 ? '' : 's'}${parts.length ? `: ${parts.join('; ')}` : ''}; record ${rep.record}${rep.orphans.length ? `; ${rep.orphans.length} orphan file${rep.orphans.length === 1 ? '' : 's'} kept (no longer cooked): ${rep.orphans.join(', ')}` : ''} (${(rep.ms / 1000).toFixed(1)} s)`;
|
|
505
|
+
}
|
|
506
|
+
/** A package's failures and warnings, a line each, as the plugin's Output says them. */
|
|
507
|
+
function failures(rep) {
|
|
508
|
+
const out = [];
|
|
509
|
+
for (const e of rep.errors)
|
|
510
|
+
out.push(`TopoTopo: ${rep.package.id}: ${e}`);
|
|
511
|
+
for (const o of rep.outputs)
|
|
512
|
+
for (const e of o.errors)
|
|
513
|
+
out.push(`TopoTopo: ${rep.package.id} > ${o.id}: ${e.node ? `[${e.node}] ` : ''}${e.message}${e.hint ? ` (hint: ${e.hint})` : ''}`);
|
|
514
|
+
for (const w of rep.warnings)
|
|
515
|
+
out.push(`TopoTopo: ${rep.package.id}: warning: ${w}`);
|
|
516
|
+
return out;
|
|
517
|
+
}
|
|
518
|
+
/** A jobs file read and checked (protocol, flavour, each job's package and options); throws a SetupError. */
|
|
519
|
+
export function readJobs(file) {
|
|
520
|
+
let raw;
|
|
521
|
+
try {
|
|
522
|
+
raw = JSON.parse(fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, ''));
|
|
523
|
+
}
|
|
524
|
+
catch (e) {
|
|
525
|
+
throw new SetupError(`the jobs file ${file} cannot be read: ${e.message}`);
|
|
526
|
+
}
|
|
527
|
+
if (!raw || typeof raw !== 'object' || !Array.isArray(raw.jobs))
|
|
528
|
+
throw new SetupError(`${file} is no jobs file: {"for": "godot", "protocol": ${COOK_PROTOCOL}, "jobs": [...]}`);
|
|
529
|
+
const projectOf = () => { const p = raw.jobs.find((j) => typeof j?.package === 'string')?.package; const g = p ? godotProjectOf(path.dirname(path.resolve(p))) : null; return g ?? '<project>'; };
|
|
530
|
+
if (raw.protocol !== COOK_PROTOCOL)
|
|
531
|
+
throw new SetupError(`the jobs file speaks protocol ${JSON.stringify(raw.protocol)} and this TopoTopo ${TOPOTOPO_VERSION} speaks ${COOK_PROTOCOL}: update the Godot addon: ${program()} godot setup ${projectOf()}`);
|
|
532
|
+
if (raw.for !== undefined && raw.for !== 'godot')
|
|
533
|
+
throw new SetupError(`the jobs file is for ${JSON.stringify(raw.for)}: only godot is supported`);
|
|
534
|
+
return raw.jobs.map((j, i) => {
|
|
535
|
+
const at = `${file}: job ${i + 1}`;
|
|
536
|
+
if (!j || typeof j.package !== 'string')
|
|
537
|
+
throw new SetupError(`${at} names no "package" (the .topo's absolute path)`);
|
|
538
|
+
const topo = isDir(j.package) ? cookTargets(j.package)[0] : j.package;
|
|
539
|
+
if (!topo || !isFile(topo))
|
|
540
|
+
throw new SetupError(`${at}: no package at ${j.package}`);
|
|
541
|
+
if (j.instancing !== undefined && j.instancing !== 'packed' && j.instancing !== 'expand')
|
|
542
|
+
throw new SetupError(`${at}: "instancing" is packed or expand`);
|
|
543
|
+
if (j.skip !== undefined && (!Array.isArray(j.skip) || j.skip.some((s) => typeof s !== 'string')))
|
|
544
|
+
throw new SetupError(`${at}: "skip" is a list of output ids`);
|
|
545
|
+
if (j.timeout !== undefined && !(typeof j.timeout === 'number' && j.timeout > 0))
|
|
546
|
+
throw new SetupError(`${at}: "timeout" is seconds above 0`);
|
|
547
|
+
if (j.into !== undefined && typeof j.into !== 'string')
|
|
548
|
+
throw new SetupError(`${at}: "into" is a folder`);
|
|
549
|
+
return godotJob(topo, {
|
|
550
|
+
into: j.into === undefined ? undefined : /^res:\/\//.test(j.into) ? j.into : path.resolve(j.into),
|
|
551
|
+
per_output_folders: typeof j.per_output_folders === 'boolean' ? j.per_output_folders : undefined, skip: j.skip, instancing: j.instancing,
|
|
552
|
+
adopt: j.adopt === true, report: typeof j.report === 'string' ? path.resolve(j.report) : undefined, timeout: j.timeout,
|
|
553
|
+
});
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
/** Git in the project's repository: its root and a check-attr runner; null when it is no repository or git is missing. */
|
|
557
|
+
function gitOf(project) {
|
|
558
|
+
const run = (args) => spawnSync('git', args, { cwd: project, encoding: 'utf8', windowsHide: true });
|
|
559
|
+
const top = run(['rev-parse', '--show-toplevel']);
|
|
560
|
+
if (top.status !== 0 || !top.stdout.trim())
|
|
561
|
+
return null;
|
|
562
|
+
// Git expands junctions and Windows short paths. Walk and query the same physical paths it reports.
|
|
563
|
+
const root = fs.realpathSync.native(top.stdout.trim());
|
|
564
|
+
const dir = fs.realpathSync.native(project);
|
|
565
|
+
// a repository uses LFS when a .gitattributes on the way to the project names filter=lfs
|
|
566
|
+
let lfs = false;
|
|
567
|
+
for (let d = dir; within(d, root); d = path.dirname(d)) {
|
|
568
|
+
try {
|
|
569
|
+
if (/filter=lfs/.test(fs.readFileSync(path.join(d, '.gitattributes'), 'utf8'))) {
|
|
570
|
+
lfs = true;
|
|
571
|
+
break;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
catch { /* none here */ }
|
|
575
|
+
if (path.resolve(d) === root)
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
return {
|
|
579
|
+
top: root, lfs,
|
|
580
|
+
filter: (abs) => {
|
|
581
|
+
const out = new Map();
|
|
582
|
+
if (!abs.length)
|
|
583
|
+
return out;
|
|
584
|
+
// Probe files need not exist, so resolve their project prefix rather than realpathing each file.
|
|
585
|
+
const rels = abs.map((a) => posix(path.relative(root, path.join(dir, path.relative(project, a)))));
|
|
586
|
+
const r = spawnSync('git', ['check-attr', 'filter', '--', ...rels], { cwd: root, encoding: 'utf8', windowsHide: true });
|
|
587
|
+
for (const line of (r.stdout ?? '').split(/\r?\n/)) {
|
|
588
|
+
const m = /^(.*): filter: (.*)$/.exec(line);
|
|
589
|
+
if (m)
|
|
590
|
+
out.set(m[1], m[2]);
|
|
591
|
+
}
|
|
592
|
+
return new Map(rels.map((r2, i) => [abs[i], out.get(r2) ?? 'unspecified']));
|
|
593
|
+
},
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
/** The output folders of a package whose GLBs and PNGs no LFS pattern covers (res://), in a repository that uses LFS. */
|
|
597
|
+
function lfsGaps(git, project, id, o, outputs) {
|
|
598
|
+
if (!git?.lfs)
|
|
599
|
+
return [];
|
|
600
|
+
const folders = o.per_output_folders ? (outputs.length ? outputs : [id]).map((x) => path.join(fromRes(project, o.folder), x)) : [fromRes(project, o.folder)];
|
|
601
|
+
const probes = folders.flatMap((f) => [path.join(f, 'probe.glb'), path.join(f, 'probe.png')]);
|
|
602
|
+
const attrs = git.filter(probes);
|
|
603
|
+
return folders.filter((f) => [path.join(f, 'probe.glb'), path.join(f, 'probe.png')].some((p) => attrs.get(p) !== 'lfs')).map((f) => resPath(project, f));
|
|
604
|
+
}
|
|
605
|
+
/** The export presets (export_presets.cfg) whose exclude_filter does not keep the addon out. */
|
|
606
|
+
export function presetsShippingAddon(project) {
|
|
607
|
+
let text;
|
|
608
|
+
try {
|
|
609
|
+
text = fs.readFileSync(path.join(project, 'export_presets.cfg'), 'utf8');
|
|
610
|
+
}
|
|
611
|
+
catch {
|
|
612
|
+
return [];
|
|
613
|
+
}
|
|
614
|
+
const out = [];
|
|
615
|
+
for (const block of text.split(/^\[preset\.\d+\]\s*$/m).slice(1)) {
|
|
616
|
+
const body = block.split(/^\[/m)[0];
|
|
617
|
+
const key = (k) => new RegExp(`^${k}\\s*=\\s*"((?:[^"\\\\]|\\\\.)*)"`, 'm').exec(body)?.[1] ?? '';
|
|
618
|
+
if (!key('exclude_filter').split(',').map((s) => s.trim()).includes(PRESET_EXCLUDE))
|
|
619
|
+
out.push(key('name') || '(unnamed)');
|
|
620
|
+
}
|
|
621
|
+
return out;
|
|
622
|
+
}
|
|
623
|
+
/** What `topotopo godot status` finds: every .topo Godot sees, checked with the one freshness rule (this TopoTopo installed). */
|
|
624
|
+
export function godotStatus(project) {
|
|
625
|
+
const proj = path.resolve(project);
|
|
626
|
+
if (!isFile(path.join(proj, 'project.godot')))
|
|
627
|
+
throw new SetupError(`${project} is no Godot project folder (it holds no project.godot)`);
|
|
628
|
+
const res = { project: proj, packages: [], copies: [], presets: presetsShippingAddon(proj), root: [], ok: true };
|
|
629
|
+
const git = gitOf(proj);
|
|
630
|
+
const flavour = godotFlavour(proj);
|
|
631
|
+
for (const { topo, copy } of godotTopos(proj)) {
|
|
632
|
+
if (copy) {
|
|
633
|
+
res.copies.push(resPath(proj, topo));
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
const dir = path.dirname(topo);
|
|
637
|
+
if (dir === proj) {
|
|
638
|
+
res.root.push(resPath(proj, topo));
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
641
|
+
let at = null;
|
|
642
|
+
try {
|
|
643
|
+
at = manifestAt(dir);
|
|
644
|
+
}
|
|
645
|
+
catch { /* two manifests: said below */ }
|
|
646
|
+
const id = at?.id ?? path.basename(topo, MANIFEST_EXT);
|
|
647
|
+
let options;
|
|
648
|
+
try {
|
|
649
|
+
options = godotOptions(topo, proj);
|
|
650
|
+
}
|
|
651
|
+
catch (e) {
|
|
652
|
+
res.packages.push({ topo: resPath(proj, topo), id, state: 'stale', why: 'options', path: '', note: '', text: e.message, outputs: [], skipped: [], exposed: flavour.exposed(dir), lfs: [] });
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
let record = null, f;
|
|
656
|
+
try {
|
|
657
|
+
record = readRecord(dir, 'godot');
|
|
658
|
+
f = freshness(dir, record, options, { for: 'godot', absolutePath: flavour.absolutePath, installed: TOPOTOPO_VERSION });
|
|
659
|
+
}
|
|
660
|
+
catch (e) {
|
|
661
|
+
f = { state: 'stale', why: 'record', path: '', note: '', text: e.message };
|
|
662
|
+
}
|
|
663
|
+
const outputs = Object.entries(record?.outputs ?? {}).sort(([a], [b]) => byCode(a, b)).map(([oid, o]) => ({ id: oid, state: o.state, kind: o.kind, files: Object.keys(o.files ?? {}), errors: o.errors ?? [] }));
|
|
664
|
+
const skipped = Object.entries(record?.skipped ?? {}).sort(([a], [b]) => byCode(a, b)).map(([oid, s]) => ({ id: oid, why: s.why }));
|
|
665
|
+
res.packages.push({
|
|
666
|
+
topo: resPath(proj, topo), id, state: f.state, why: f.why, path: f.path, note: f.note, text: f.text, outputs, skipped,
|
|
667
|
+
exposed: flavour.exposed(dir), lfs: lfsGaps(git, proj, id, options, outputs.map((o) => o.id)),
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
res.ok = !res.copies.length && !res.root.length && res.packages.every((p) => p.state === 'ok' && !p.exposed.length);
|
|
671
|
+
return res;
|
|
672
|
+
}
|
|
673
|
+
/** A status as people read it. */
|
|
674
|
+
export function statusText(r) {
|
|
675
|
+
const lines = [`Godot project ${r.project}: ${r.packages.length} package${r.packages.length === 1 ? '' : 's'}`];
|
|
676
|
+
for (const p of r.packages) {
|
|
677
|
+
const state = p.state === 'never_cooked' ? 'never cooked' : p.state;
|
|
678
|
+
lines.push(` ${state.padEnd(12)} ${p.topo}${p.state === 'ok' ? '' : `: ${p.text}`}${p.note === 'update_topotopo' ? ' (cooked by a newer TopoTopo: update TopoTopo)' : ''}`);
|
|
679
|
+
for (const o of p.outputs) {
|
|
680
|
+
lines.push(` ${o.id}: ${o.state}${o.kind ? ` ${o.kind}` : ''}${o.files.length ? ` → ${o.files.join(', ')}` : ''}`);
|
|
681
|
+
for (const e of o.errors)
|
|
682
|
+
lines.push(` ${e.node ? `[${e.node}] ` : ''}${e.message}${e.hint ? ` (hint: ${e.hint})` : ''}`);
|
|
683
|
+
}
|
|
684
|
+
for (const s of p.skipped)
|
|
685
|
+
lines.push(` ${s.id}: skipped (${s.why})`);
|
|
686
|
+
if (p.exposed.length)
|
|
687
|
+
lines.push(` exposed: Godot imports ${p.exposed.join(', ')} (no .gdignore): ${program()} godot setup ${r.project} hides them`);
|
|
688
|
+
for (const f of p.lfs)
|
|
689
|
+
lines.push(` LFS: no pattern covers ${f} (*.glb, *.png): add one to .gitattributes`);
|
|
690
|
+
}
|
|
691
|
+
for (const c of r.copies)
|
|
692
|
+
lines.push(` copy ${c}: a published copy Godot sees: hide its folder with .gdignore (${program()} godot setup ${r.project})`);
|
|
693
|
+
for (const t of r.root)
|
|
694
|
+
lines.push(` unsupported ${t}: a package at the project's root (its assets/ would hide the game's art): move it into a folder`);
|
|
695
|
+
for (const p of r.presets)
|
|
696
|
+
lines.push(` export preset "${p}" ships the addon: add ${PRESET_EXCLUDE} to its exclude_filter (${program()} godot setup does)`);
|
|
697
|
+
lines.push(r.ok ? 'every package is current' : 'not every package is current (--strict exits 1)');
|
|
698
|
+
return lines.join('\n');
|
|
699
|
+
}
|
|
700
|
+
// ------------------------------------------------------------------------------------------------------- setup
|
|
701
|
+
/** Add lines a text file lacks, at its end. Returns the lines added. */
|
|
702
|
+
function addLines(file, lines) {
|
|
703
|
+
let text = '';
|
|
704
|
+
try {
|
|
705
|
+
text = fs.readFileSync(file, 'utf8');
|
|
706
|
+
}
|
|
707
|
+
catch { /* new */ }
|
|
708
|
+
const have = new Set(text.split(/\r?\n/).map((l) => l.trim()));
|
|
709
|
+
const missing = lines.filter((l) => !have.has(l));
|
|
710
|
+
if (missing.length)
|
|
711
|
+
fs.writeFileSync(file, `${text}${text && !text.endsWith('\n') ? '\n' : ''}${missing.join('\n')}\n`);
|
|
712
|
+
return missing;
|
|
713
|
+
}
|
|
714
|
+
/** project.godot with the plugin enabled ([editor_plugins] enabled=PackedStringArray(…)); null when it is enabled already. */
|
|
715
|
+
export function enablePlugin(text) {
|
|
716
|
+
const nl = text.includes('\r\n') ? '\r\n' : '\n';
|
|
717
|
+
const lines = text.split(/\r?\n/);
|
|
718
|
+
const start = lines.findIndex((l) => l.trim() === '[editor_plugins]');
|
|
719
|
+
const entry = JSON.stringify(PLUGIN_CFG);
|
|
720
|
+
if (start >= 0) {
|
|
721
|
+
let end = lines.findIndex((l, i) => i > start && /^\s*\[/.test(l));
|
|
722
|
+
if (end < 0)
|
|
723
|
+
end = lines.length;
|
|
724
|
+
const at = lines.findIndex((l, i) => i > start && i < end && /^\s*enabled\s*=/.test(l));
|
|
725
|
+
if (at >= 0) {
|
|
726
|
+
if (lines[at].includes(entry))
|
|
727
|
+
return null;
|
|
728
|
+
const m = /PackedStringArray\((.*)\)\s*$/.exec(lines[at]);
|
|
729
|
+
const items = m && m[1].trim() ? `${m[1].trim()}, ${entry}` : entry;
|
|
730
|
+
lines[at] = `enabled=PackedStringArray(${items})`;
|
|
731
|
+
}
|
|
732
|
+
else
|
|
733
|
+
lines.splice(start + 1, 0, '', `enabled=PackedStringArray(${entry})`);
|
|
734
|
+
return lines.join(nl);
|
|
735
|
+
}
|
|
736
|
+
// a new section, where Godot sorts it (sections in name order)
|
|
737
|
+
const sections = lines.map((l, i) => [i, /^\[([^\]]+)\]\s*$/.exec(l)?.[1]]).filter(([, n]) => n && n !== 'editor_plugins' && !/^(config_version)$/.test(n));
|
|
738
|
+
const after = sections.find(([, n]) => n > 'editor_plugins');
|
|
739
|
+
const block = ['[editor_plugins]', '', `enabled=PackedStringArray(${entry})`, ''];
|
|
740
|
+
if (after)
|
|
741
|
+
lines.splice(after[0], 0, ...block);
|
|
742
|
+
else {
|
|
743
|
+
while (lines.length && lines[lines.length - 1] === '')
|
|
744
|
+
lines.pop();
|
|
745
|
+
lines.push('', ...block);
|
|
746
|
+
}
|
|
747
|
+
return lines.join(nl);
|
|
748
|
+
}
|
|
749
|
+
/** export_presets.cfg with addons/topotopo/* in every preset's exclude_filter; null when nothing changes. */
|
|
750
|
+
export function excludeAddon(text) {
|
|
751
|
+
let changed = false;
|
|
752
|
+
const out = text.replace(/^(\s*exclude_filter\s*=\s*")((?:[^"\\]|\\.)*)("\s*)$/gm, (all, a, v, b) => {
|
|
753
|
+
const items = v.split(',').map((s) => s.trim()).filter(Boolean);
|
|
754
|
+
if (items.includes(PRESET_EXCLUDE))
|
|
755
|
+
return all;
|
|
756
|
+
changed = true;
|
|
757
|
+
return `${a}${[...items, PRESET_EXCLUDE].join(', ')}${b}`;
|
|
758
|
+
});
|
|
759
|
+
return changed ? out : null;
|
|
760
|
+
}
|
|
761
|
+
/** Copy the addon into the project, replacing what an earlier setup copied; the .uid and .import files Godot wrote beside it stay. */
|
|
762
|
+
function copyAddon(from, to) {
|
|
763
|
+
const files = [];
|
|
764
|
+
const walk = (d, rel) => { for (const e of fs.readdirSync(d, { withFileTypes: true })) {
|
|
765
|
+
const r = rel ? `${rel}/${e.name}` : e.name;
|
|
766
|
+
if (e.isDirectory())
|
|
767
|
+
walk(path.join(d, e.name), r);
|
|
768
|
+
else if (e.isFile())
|
|
769
|
+
files.push(r);
|
|
770
|
+
} };
|
|
771
|
+
walk(from, '');
|
|
772
|
+
for (const f of files) {
|
|
773
|
+
const dest = path.join(to, ...f.split('/'));
|
|
774
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
775
|
+
fs.copyFileSync(path.join(from, ...f.split('/')), dest);
|
|
776
|
+
}
|
|
777
|
+
const keep = new Set(files);
|
|
778
|
+
const clean = (d, rel) => {
|
|
779
|
+
let entries;
|
|
780
|
+
try {
|
|
781
|
+
entries = fs.readdirSync(d, { withFileTypes: true });
|
|
782
|
+
}
|
|
783
|
+
catch {
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
for (const e of entries) {
|
|
787
|
+
const r = rel ? `${rel}/${e.name}` : e.name;
|
|
788
|
+
if (e.isDirectory()) {
|
|
789
|
+
clean(path.join(d, e.name), r);
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
const engineSide = /\.(uid|import)$/i.test(e.name) && keep.has(r.replace(/\.(uid|import)$/i, ''));
|
|
793
|
+
if (!keep.has(r) && !engineSide)
|
|
794
|
+
fs.rmSync(path.join(d, e.name), { force: true });
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
clean(to, '');
|
|
798
|
+
return files.length;
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* topotopo godot setup: install the plugin into a Godot project, saying each step. Throws a SetupError for a folder
|
|
802
|
+
* that is no Godot project or a TopoTopo without the addon.
|
|
803
|
+
*/
|
|
804
|
+
export function godotSetup(project, o = {}) {
|
|
805
|
+
const proj = path.resolve(project);
|
|
806
|
+
if (!isFile(path.join(proj, 'project.godot')))
|
|
807
|
+
throw new SetupError(`${project} is no Godot project folder (it holds no project.godot)`);
|
|
808
|
+
const addon = o.addon ?? ADDON_SOURCE;
|
|
809
|
+
if (!isFile(path.join(addon, 'plugin.cfg')))
|
|
810
|
+
throw new SetupError(`this TopoTopo (${TOPOTOPO_VERSION}) carries no Godot addon at ${addon}`);
|
|
811
|
+
const said = [];
|
|
812
|
+
const n = copyAddon(addon, path.join(proj, ...ADDON_DIR.split('/')));
|
|
813
|
+
said.push(`copied the addon (${n} files) into ${ADDON_DIR}/`);
|
|
814
|
+
const cfg = setInstall({ node: o.node ?? process.execPath, cli: o.cli ?? CLI_PATH });
|
|
815
|
+
said.push(`recorded where TopoTopo is (node ${o.node ?? process.execPath}, ${o.cli ?? CLI_PATH}) in ${cfg} "install"`);
|
|
816
|
+
// Godot imports none of TopoTopo's working folders: a .gdignore in each a package has
|
|
817
|
+
const hidden = [];
|
|
818
|
+
for (const { topo, copy } of godotTopos(proj)) {
|
|
819
|
+
if (copy)
|
|
820
|
+
continue;
|
|
821
|
+
const dir = path.dirname(topo);
|
|
822
|
+
if (dir === proj) {
|
|
823
|
+
said.push(`not supported: ${path.basename(topo)} makes the project's root a package (its assets/ would hide the game's art from Godot): move the package into a folder; no .gdignore was written for it`);
|
|
824
|
+
continue;
|
|
825
|
+
}
|
|
826
|
+
for (const d of GODOT_HIDDEN) {
|
|
827
|
+
const f = path.join(dir, d, '.gdignore');
|
|
828
|
+
if (isDir(path.join(dir, d)) && !fs.existsSync(f)) {
|
|
829
|
+
fs.writeFileSync(f, '');
|
|
830
|
+
hidden.push(resPath(proj, path.join(dir, d)));
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
// a workspace's shared copies
|
|
834
|
+
}
|
|
835
|
+
godotWalk(proj, (dir) => {
|
|
836
|
+
const shared = path.join(dir, 'topotopo-packages');
|
|
837
|
+
if (isDir(shared) && !fs.existsSync(path.join(shared, '.gdignore'))) {
|
|
838
|
+
fs.writeFileSync(path.join(shared, '.gdignore'), '');
|
|
839
|
+
hidden.push(resPath(proj, shared));
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
forgetScans();
|
|
843
|
+
said.push(hidden.length ? `hid ${hidden.length} TopoTopo folder${hidden.length === 1 ? '' : 's'} from Godot (an empty .gdignore in each): ${hidden.join(', ')}` : 'every TopoTopo folder is hidden from Godot already (.gdignore)');
|
|
844
|
+
if (o.enable !== false) {
|
|
845
|
+
const file = path.join(proj, 'project.godot');
|
|
846
|
+
const text = fs.readFileSync(file, 'utf8'), next = enablePlugin(text);
|
|
847
|
+
if (next !== null) {
|
|
848
|
+
writeAtomic(file, next);
|
|
849
|
+
said.push(`enabled the plugin in project.godot ([editor_plugins] ${PLUGIN_CFG}); if Godot has the project open, tick Project > Project Settings > Plugins > TopoTopo instead, or reopen the project`);
|
|
850
|
+
}
|
|
851
|
+
else
|
|
852
|
+
said.push('the plugin is enabled in project.godot already');
|
|
853
|
+
}
|
|
854
|
+
else
|
|
855
|
+
said.push('left project.godot as it is (--no-enable): enable the plugin in Project > Project Settings > Plugins > TopoTopo');
|
|
856
|
+
const ignored = addLines(path.join(proj, '.gitignore'), GITIGNORE_LINES);
|
|
857
|
+
said.push(ignored.length ? `added to .gitignore: ${ignored.join(', ')}` : '.gitignore has the cook\'s lines already');
|
|
858
|
+
const presets = path.join(proj, 'export_presets.cfg');
|
|
859
|
+
if (isFile(presets)) {
|
|
860
|
+
const next = excludeAddon(fs.readFileSync(presets, 'utf8'));
|
|
861
|
+
if (next !== null) {
|
|
862
|
+
writeAtomic(presets, next);
|
|
863
|
+
said.push(`added ${PRESET_EXCLUDE} to the exclude_filter of every export preset (export_presets.cfg): Godot would ship the addon's scripts otherwise`);
|
|
864
|
+
}
|
|
865
|
+
else
|
|
866
|
+
said.push(`every export preset excludes ${PRESET_EXCLUDE} already`);
|
|
867
|
+
}
|
|
868
|
+
else
|
|
869
|
+
said.push(`no export_presets.cfg yet: when you add an export preset, run setup again (it adds ${PRESET_EXCLUDE} to its exclude_filter)`);
|
|
870
|
+
const git = gitOf(proj);
|
|
871
|
+
if (git?.lfs)
|
|
872
|
+
for (const { topo, copy } of godotTopos(proj)) {
|
|
873
|
+
if (copy || path.dirname(topo) === proj)
|
|
874
|
+
continue;
|
|
875
|
+
let options;
|
|
876
|
+
try {
|
|
877
|
+
options = godotOptions(topo, proj);
|
|
878
|
+
}
|
|
879
|
+
catch {
|
|
880
|
+
continue;
|
|
881
|
+
}
|
|
882
|
+
for (const f of lfsGaps(git, proj, path.basename(topo, MANIFEST_EXT), options, Object.keys(readRecord(path.dirname(topo), 'godot')?.outputs ?? {})))
|
|
883
|
+
said.push(`warning: the repository uses LFS and no pattern covers ${f} (*.glb, *.png): add one to .gitattributes`);
|
|
884
|
+
}
|
|
885
|
+
return said;
|
|
886
|
+
}
|
|
887
|
+
/** topotopo godot setup | status. */
|
|
888
|
+
export async function godotCommand(argv, io) {
|
|
889
|
+
const usage = () => GODOT_COMMANDS.filter((c) => c.usage.startsWith('godot')).map((c) => `usage: ${program()} ${c.usage}`).join('\n');
|
|
890
|
+
const [sub, ...rest] = argv;
|
|
891
|
+
try {
|
|
892
|
+
if (sub === 'setup') {
|
|
893
|
+
const { set, rest: pos } = parseArgs(rest, [], ['--no-enable'], usage);
|
|
894
|
+
if (pos.length !== 1)
|
|
895
|
+
throw new SetupError(`godot setup needs the Godot project folder\n${usage()}`);
|
|
896
|
+
for (const l of godotSetup(pos[0], { enable: !set.has('--no-enable') }))
|
|
897
|
+
io.out(l);
|
|
898
|
+
return 0;
|
|
899
|
+
}
|
|
900
|
+
if (sub === 'status') {
|
|
901
|
+
const { set, rest: pos } = parseArgs(rest, [], ['--strict', '--json'], usage);
|
|
902
|
+
if (pos.length !== 1)
|
|
903
|
+
throw new SetupError(`godot status needs the Godot project folder\n${usage()}`);
|
|
904
|
+
const r = godotStatus(pos[0]);
|
|
905
|
+
io.out(set.has('--json') ? JSON.stringify(r, null, 2) : statusText(r));
|
|
906
|
+
return set.has('--strict') && !r.ok ? 1 : 0;
|
|
907
|
+
}
|
|
908
|
+
throw new SetupError(sub ? `unknown godot command "${sub}"\n${usage()}` : usage());
|
|
909
|
+
}
|
|
910
|
+
catch (e) {
|
|
911
|
+
io.err(e instanceof SetupError ? e.message : String(e?.message ?? e));
|
|
912
|
+
return 2;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
/** A package id from a folder name, for messages about folders without a manifest. */
|
|
916
|
+
export const idOf = (dir) => slugify(path.basename(dir)) || 'package';
|