How dan-lore/tfg-planner works
1. Overall shape
- This is a TypeScript/Vite React web app for planning factory/production “schemes” for TerraFirmaGreg-Modern.
- The runtime app is separate from an offline data-generation toolchain that builds the recipe packs consumed by the UI.
2. Application shell
- src/main.tsx boots the app.
- src/app/* defines the top-level layout and route split between the home page and the editor page.
- The UI is deployed as a static site, including a GitHub Pages build path variant.
3. Primary user workflow
- The user selects a pack/version, opens an editor, places machines, connects ports, sets target output rates, and saves/loads .tfgp scheme files.
- That workflow is the central data path through the repository: pack selection → scheme editing → flow calculation → validation/visualization → persistence/export.
4. Editor subsystem
- src/editor, src/canvas, and src/components make up the editing surface.
- The canvas layer handles node rendering, edges, port menus, selection, drag interactions, obstacles, and route planning.
- The editor layer provides toolbar, inspector, sidebar, issue panels, and action handling around the canvas.
5. Scheme model and validation
- src/schema defines the .tfgp schema and validation rules.
- src/lib/read-tfgp-file.ts, editor-persist.ts, and related helpers support import/export and browser persistence.
- src/scheme-check performs structural checks such as wiring issues and cycles, turning invalid schemes into user-visible diagnostics.
6. Flow calculation engine
- src/calculator is the mathematical core: flow rates, machine counts, energy, buffers, port resolution, constraints, and result metrics.
- The calculator turns a scheme graph plus pack recipe data into computed throughput and required machine quantities.
- This layer is heavily unit-tested and appears designed as a pure domain engine, separate from UI concerns.
7. Background execution boundary
- src/workers/flow-worker.ts and src/stores/flow-compute-runtime.ts indicate that expensive flow solving runs off the main UI thread.
- The worker boundary keeps interactive editing responsive while recalculations happen asynchronously.
- Stores mediate between the UI and the worker-produced results.
8. Pack data runtime
- public/data/packs/<version>/ contains the versioned pack artifacts the app loads at runtime, including recipe shards, manifests, metadata, and language bundles.
- src/data, src/stores/pack-store.ts, and src/lib/pack-* manage pack selection, loading, caching, and version mismatch handling.
- This pack layer is the main runtime dependency for recipe lookup, names, and machine metadata.
9. Language and lexicon support
- src/data/pack-lang-loader.ts, src/data/lang-cache.ts, and src/lib/product-lexicon/* resolve in-game names, tags, and localized labels.
- The app supports RU/EN UI language behavior and pack-specific language assets, which affect recipe search, picker display, and labels in the editor.
10. Offline parser/build pipeline
- tools/parser is a separate Node/TypeScript toolchain that extracts modpack data from snapshots, KubeJS scripts, and related sources.
- It normalizes recipes, enriches metadata, builds language bundles, validates output, and generates the sharded pack format used by the web app.
- This pipeline is maintenance-only and not part of the browser runtime, but it is essential to the data architecture.
11. Static assets and examples
- examples/ and src/lib/fixtures contain sample schemes and routing fixtures used for testing and documentation.
- data/packs and public/data/packs reflect the runtime-vs-build-time split: generated content is checked into the web app for direct static hosting.
12. Quality and enforcement boundaries
- Tests are spread across calculator, canvas, parser, schema, and store layers, showing a strong separation of domain logic from UI.
- CI, Semgrep, dependency-cruiser, and knip are used to keep the architecture bounded and detect unwanted coupling.
- The docs under docs/ act as the project’s architectural spec for editor behavior, parser pipeline, and data formats.
Main entry points: calculator Calculator · pack-store.ts Pack store · routes.tsx Routes · EditorPage.tsx Editor page
Components 18
Web app
Browser runtime for editing schemes and computing flows.
- Entry · Bootstraps the React app.
src/main.tsx - Shell · Top-level layout and route split.
src/app/AppLayout.tsx - Routes · Home/editor route wiring.
src/app/routes.tsx - Home · Pack selection and entry point.
src/pages/HomePage.tsx - Editor page · Hosts the interactive scheme editor.
src/pages/EditorPage.tsx - Editor UI · Toolbar, sidebar, inspector, issues, and actions around the canvas.
src/editor - Canvas · Machine nodes, edges, selection, routing, and drag interactions.
src/canvas - Persistence · Import/export and browser persistence for .tfgp schemes.
src/lib/editor-persist.ts - Flow runtime · Asynchronous compute boundary between UI and solver.
src/workers/flow-worker.ts - Stores · Editor, scheme, flow, and runtime state coordination.
src/stores
Domain core
Pure scheme, validation, and calculation logic.
- Scheme checks · Structural scheme checks that surface wiring and cycle problems.
src/scheme-check - Scheme schema · Schema and validation rules for .tfgp files.
src/schema - Calculator · Pure flow, machine count, energy, buffer, and metric solving.
src/calculator
Pack data
Versioned pack artifacts and language metadata used at runtime.
- Pack store · Pack selection, caching, and version mismatch handling.
src/stores/pack-store.ts - Pack data · Static recipe, manifest, metadata, and language artifacts.
public/data/packs/0.12.8 - Lang layer · Pack language loading and product lexicon resolution.
src/data/pack-lang-loader.ts
Offline tooling
Parser and build pipeline for generating pack artifacts.
- Parser build · Node toolchain that extracts, normalizes, validates, and builds pack artifacts.
tools/parser/src - Docs & CI · Architecture docs, workflow checks, and repo enforcement.
docs/architecture.md
Connections 23
- Entry → Shell: mounts · App entry renders the shell.
- Shell → Routes: hosts · Shell delegates page selection to routes.
- Routes → Home: home · Route to the pack selection landing page.
- Routes → Editor page: editor · Route to the interactive editor page.
- Editor page → Editor UI: contains · Editor page composes the editing controls.
- Editor UI → Canvas: drives · Actions and panels operate on the canvas surface.
- Editor UI → Stores: state · Editor UI reads and updates shared state.
- Canvas → Calculator: requests · Scheme edits trigger flow recomputation.
- Canvas → Scheme checks: checks · Canvas changes feed structural validation.
- Stores → Flow runtime: dispatches · Stores send compute jobs to the worker boundary.
- Flow runtime → Calculator: runs · Worker executes the solver off the main thread.
- Calculator → Scheme schema: consumes · Calculator operates on validated scheme structures.
- Scheme checks → Scheme schema: validates · Scheme checker enforces schema and structural rules.
- Persistence → Scheme schema: serializes · Save/load logic reads and writes .tfgp schemes.
- Pack store → Pack data: loads · Runtime store selects versioned pack artifacts.
- Lang layer → Pack data: resolves · Language layer reads pack language assets and labels.
- Home → Pack store: selects · Home page chooses an active pack/version.
- Editor page → Pack store: uses · Editor depends on the active pack for recipes and machines.
- Calculator → Pack store: looks up · Flow solving depends on pack recipe and machine metadata.
- Parser build → Pack data: generates · Offline pipeline builds the pack artifacts consumed by the app.
- Parser build → Lang layer: emits · Pipeline produces localized language bundles and lexicons.
- Docs & CI → Parser build: specifies · Docs and CI constrain the parser pipeline and architecture.
- Docs & CI → Calculator: specifies · Docs and checks encode calculator and editor boundaries.