# Native development The selected architecture is a maintained Script Extender fork, covering gameplay accessibility, graph UI, input, and speech. Native speech is the first integration. ## Next milestone: graph UI with a native host Use a Lua graph core and screen recipes, with native services in our SE fork for input interception, safe game access, and Prism speech. This decision replaces the earlier proposal to put the core and every screen adapter in C++. Start with full main-menu and settings coverage and keyboard operation; the current Lua readers do not satisfy this milestone. - The graph owns focus, navigation, control state, and speech descriptions. - Main-menu and settings controls expose the game's localized text and actual state. - Keyboard activation invokes the corresponding game action. Settings coverage includes tabs, toggles, sliders, choices, scrolling, and confirmation dialogs. - Focus survives UI rebuilds and value changes; entering and leaving screens keeps navigation predictable. Text entry retains its normal editing behavior. - Validate keyboard-only use in game, including changing settings, applying or cancelling changes, and returning to the main menu. Gameplay accessibility remains part of the wider SE-fork architecture. The engine-independent X4 Lua kernel and its conformance suite have been ported to `lua/a11y-graph.lua` and `tests/graph-kernel.lua`. The module returns its API without writing globals. The bootstrap now loads the graph host and navigator, which use the new native `Ext.Graph` API. The first main-menu/settings integration builds and passes offline tests. Sounds and tab navigation are now confirmed in DX11. The graph yields text editing, binding capture, and unsupported screens to the game. The generic UI fallback is removed; retained gameplay readers run only in their explicit screen/world contexts. Loading tips and the splash prompt use a passive reader that leaves the game's input and closing action intact. See [Graph architecture and preparation](graph-kernel.md) for the division of responsibilities, provenance, and first integration milestone. ## Source dependencies | Path | Upstream | Baseline | | --- | --- | --- | | `external/bg3se` | glos-access/bg3se (fork of Norbyte/bg3se) | `main` | | `external/prism` | ethindp/prism | `v0.18.2`, `f237af67d0460a6dad312cc446623856ad823c50` | Submodule updates use the parent repository's recorded commits. Prism's optional Godot submodule is unnecessary. Our native changes (speech, graph UI host, world navigation, input injection) live on the `main` branch of the [glos-access/bg3se](https://github.com/glos-access/bg3se) fork, which is Norbyte's `main` merged with our commits. The fork's `upstream` remote is Norbyte/bg3se; SE has no upstream `master`, `main` is its development branch. Push an SE commit to the fork before committing the parent's submodule reference to it, or clones cannot check it out. ## Build The root `justfile` follows the other accessibility projects: PowerShell recipes, optional `.env` paths (see `env.example`), and a recipe list when running `just` alone. | Command | Action | | --- | --- | | `just setup` | Initialize submodules and build native dependencies and Prism | | `just build` | Build SE, Prism, and the Lua pak | | `just build-native` / `just pak` | Build only the native side or package only Lua | | `just test` | Configure, build, and run speech, Lua adapter, graph kernel, input, and UI tests | | `just smoke` | Run tests, then initialize and release a real speech backend without speech | | `just install` | Rebuild and install; refuses while BG3 is running | | `just status` / `just boot` | Inspect installation status or the latest bootstrap report | | `just push` | Copy Lua and reload it in a running game | Mod messages now use BG3's native localization XML resources and the game's language. `just pak` packages English/Russian resources; a GustavX installation copies them into the host module's Localization directories. `just push` compares their installed hashes, stages both XML files under `A11y\Localization`, and after the reload calls `Lang.loadOverrides()`, which writes the changed BG3Access handles of the game's language into the repository with `Ext.Loca.UpdateTranslatedString`. That lasts until BG3 exits; close BG3 and run `just install` to make it permanent. Use `tools/check-lang.ps1` to validate handles and numbered arguments. The offline localization suite tests the shipped resources, Russian count forms, formatting, argument reordering, missing-resource fallbacks, and parsing in SE's Lua dialect. `game_path` defaults to automatic game detection. `divine_path` defaults to `~/.local/bin/lslib/Tools/Divine.exe`. Override them per invocation or set `BG3_DIR` and `BG3ACCESS_DIVINE` in `.env`. Use `just install_mode=Pak install` to select the pak loading route explicitly. Native builds use SE's Game Release configuration. The equivalent scripts remain available: Requires Visual Studio 2026 C++ Build Tools (v145), Windows SDK, CMake 3.24+, Ninja, Python 3, Git, and LSLib 1.20.4. Scripts import the installed x64 MSVC environment. ```powershell git submodule update --init -- external/bg3se external/prism tools/build-native.ps1 tools/build-mod.ps1 -Pak -NoGraft -Divine "$env:USERPROFILE\.local\bin\lslib\Tools\Divine.exe" ``` `-SetupOnly` builds dependencies and Prism. `-SkipSetup` skips SE dependency setup on subsequent builds. Dependencies live in ignored `external/bg3se/External`. Protobuf, Abseil, and UTF-8 validation are combined into a static library using the same CRT as SE. Only BG3Extender and its dependencies are built; the installed DWrite.dll supplies the loader. Upstream's automatic post-build game copy is disabled. Some dependency branches float when first cloned; retain their checkouts to reproduce an existing local build. Outputs: - `external/bg3se/x64/Game Release/BG3ScriptExtender.dll` - `build/native/prism/prism.dll` - `dist/BG3Access.pak` ## Native speech Client Lua API: ```lua Ext.Speech.Speak("Hello", true) -- returns whether the request was queued Ext.Speech.Speak("Next line", false) Ext.Speech.Stop() Ext.Speech.Refresh() -- stop pending speech and reselect the backend local state, backend, err, pending = Ext.Speech.GetStatus() ``` `A11y.say` preserves the readers' interface and uses these calls. It no longer writes speech.txt. Missing native support produces a bootstrap error; there is no companion fallback. Status is idle, initializing, ready, error, or stopped. Queued success does not prove that the backend spoke or the user heard the text. The worker owns Prism and a COM apartment. Text is copied as UTF-8; no Lua or game pointers cross threads. Interrupt clears pending messages; noninterrupting speech preserves order. The queue holds at most 256 requests, each limited to 64 KiB without embedded NULs. Stop and refresh work with a full queue. Prism output includes braille on supporting backends. A failed backend is reselected on the next request. Failure to load the DLL or create the Prism context requires restarting BG3 after repair. Lua recreation and session/module unload cancel pending speech. The normal game Exit transition joins the worker outside DllMain. The service has process lifetime to avoid joining threads or unloading Prism under the Windows loader lock. Abrupt process termination relies on OS cleanup. ## Install ```powershell tools/install-native.ps1 -GameDir 'D:\SteamLibrary\steamapps\common\Baldurs Gate 3' ``` The installer refuses to run while BG3 is open. It copies the two DLLs into bin and verifies hashes. It writes DebugLoadSE=true and DisableUpdates=true into ScriptExtenderUpdaterConfig.json, selecting our build and preventing official runtime updates. Existing configuration keys are preserved with a one-time .before-native backup. DeveloperMode and CreateConsole are enabled in SE settings. With a Public profile it installs and enables the pak. Without one it uses the existing GustavX graft, which can load on the first launch. An existing BG3Access graft keeps that mode. `-Mode Pak` switches an owned graft to the pak. Unknown graft contents or duplicate loading routes are rejected. Every installation creates and updates the complete loose Lua set in `%LOCALAPPDATA%\Larian Studios\Baldur's Gate 3\Script Extender\A11y`. These are the active module files, preferred by the bootstrap over the pak/graft fallback, and each copy is hash-verified. This includes fresh installs: console reload cannot load missing modules from the fallback with `Ext.Require` outside mod startup. Use `just push` to copy Lua changes and reload the running layer. Full installation still requires closing BG3 because it also replaces the native DLLs and bootstrap. Only BG3Access's legacy speech task, startup entry, and companion processes are retired. The game and NVDA are not launched or stopped. Old companion files are retained. Use this script for native development; the legacy release installer does not deploy this custom runtime. To restore official SE, close BG3 and restore the updater configuration backup, or set DebugLoadSE and DisableUpdates to false. Restore the older Lua mod and its speech service as well if returning to the legacy speech path. ## Checks and live validation ```powershell . tools/native-env.ps1 cmake -S tests/native -B build/native/tests -G Ninja -DCMAKE_BUILD_TYPE=Release cmake --build build/native/tests ctest --test-dir build/native/tests --output-on-failure ``` Tests cover queue ordering, interruption, saturation, stop/refresh, invalid text, missing-library diagnostics, shutdown, and the Lua adapter. The test interpreter uses SE's Lua dialect with inert string-cache/debugger hooks. To check the real backend, copy speech-tests.exe and the freshly built prism.dll into a separate directory and run `speech-tests.exe backend`. It initializes and releases the backend without speech. The graph tests include X4's 95 kernel checks, host-boundary enforcement, the production native input queue, and 38 simulated UI/navigation checks. All five suites passed on 2026-09-26. The SE Game Release build passed with zero errors and one reported warning. These checks do not validate graph navigation in BG3. On 2026-09-26 the x64 Game Release build, tests, LSPK v18 packaging, and standalone NVDA backend selection passed. These checks do not confirm game startup, heard speech, controller navigation, save-load recovery, or both renderer paths. Installed on that date into `D:\SteamLibrary\steamapps\common\Baldurs Gate 3` (game 4.1.1.7398727), using the GustavX graft because no user profile existed. All copied files passed SHA-256 comparison. The game stayed closed; the status check found no connected controller and no bootstrap report yet. ### First live test: 2026-09-26 BG3 launched through Steam with `--skip-launcher` using DirectX 11. The client bootstrap reported SE v33, BG3Access running, Russian localization, and Prism's NVDA backend ready with no speech error. The console recorded focus on Russian main-menu items. The user confirmed hearing speech and confirmed closing the game themselves. The user also confirmed that controller menu navigation and the speech-interruption check were already performed during this same run. This verifies the native speech path in game on DX11 and records the controller test as completed. The separate manual queue probe was not sent because the game had already closed. Explicit stop, save-load recovery, and Vulkan remain to be tested. The UI reader produced repeated unsupported `TimeSpan` property warnings and slow scans, including a Tick callback above 100 ms. Windows recorded a launcher crash; no BG3 crash or hang event was found for this run. After launching normally, inspect Script Extender/A11y/boot.json, or run `tools/mod-status.ps1`. Bootstrap records native backend changes and errors. In the client console, `Mods.BG3Access.A11yBoot.speechStatus()` refreshes the diagnostic. Confirm English/Russian text, rapid navigation interruption, queued narration, save loading, and clean exit in game. Current Lua readers still require a controller. The ignored ref directory holds research material. Historical investigation is in [native speech investigation](native-speech-investigation.md). SE and Prism retain their own licenses; the parent's MIT license does not replace them.