diff options
| author | NotRexed <therealnothingfn@gmail.com> | 2026-11-27 00:06:53 -0500 |
|---|---|---|
| committer | NotRexed <therealnothingfn@gmail.com> | 2026-11-27 00:11:50 -0500 |
| commit | 581440a31921a7676cd7ab12e83bddc7ddab99f2 (patch) | |
| tree | 93dd0a606b36defde1d5d76ea19ebc7db56ccc65 /docs/lua-modding.md | |
| parent | 44511989734b97615844a111fb028a75348c48a1 (diff) | |
Lua mods: an example mod, the docs, and the mod manager lists them
What: mods/example_lua (mod.json disabled by default, main.lua: a frame-rate and
time line in the lower left, a page in the F4 menu with a toggle, a number and two
actions, an F9 key handler); docs/lua-modding.md (the API reference, the sandbox
rules, what is not available and why, what was not tried), pointers in
docs/README.md, docs/mod-menu.md, docs/modding.md and a row in d3d12/README.md.
tools/leonida_mods.py reads "script": it must be a .lua path inside the folder, the
list shows `Lua main.lua`, the JSON report has "script", and a Lua mod no longer
warns that it has no archetype_swaps.
Why: a Lua mod is mods\<name>\mod.json plus a script, so the manager that lists the
asset mods should list these too, and a mod author needs the reference.
How checked: python -I tests/test_leonida_mods.py (13 tests, 3 new, pass; the one
failure, test_run_print_builds_gta6rt_command, fails the same on the base: no gta6rt
build here). The example's script was not run in the title. AI-assisted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'docs/lua-modding.md')
| -rw-r--r-- | docs/lua-modding.md | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/docs/lua-modding.md b/docs/lua-modding.md new file mode 100644 index 000000000..6a1310f0f --- /dev/null +++ b/docs/lua-modding.md | |||
| @@ -0,0 +1,215 @@ | |||
| 1 | # Lua mods | ||
| 2 | |||
| 3 | The D3D12 runtime (`d3d12/`, `leonida.exe`) runs Lua 5.4 mods. A mod is a folder in `mods\` with the same | ||
| 4 | `mod.json` the mod manager (`tools/leonida_mods.py`, `Mods.bat`) already lists, and a script next to it: | ||
| 5 | |||
| 6 | ``` | ||
| 7 | mods\ | ||
| 8 | my_mod\ | ||
| 9 | mod.json {"name": "My mod", "enabled": true, "script": "main.lua"} | ||
| 10 | main.lua | ||
| 11 | helper.lua require("helper") | ||
| 12 | ``` | ||
| 13 | |||
| 14 | The scripts run in a sandbox on a thread of their own and can use what the runtime itself controls: frame, key and | ||
| 15 | menu events, an overlay drawn over the picture, the live settings the F1 and F4 menus change, pages in the F4 trainer | ||
| 16 | menu, and checked reads and writes of the title's memory with a byte-pattern scan. Single player only; nothing | ||
| 17 | here touches anything online. The code is `d3d12/crates/leonida-lua`, `d3d12/crates/leonida-menu/src/overlay.rs` and | ||
| 18 | `mods.rs`, and `d3d12/crates/leonida-gpu/src/host/trainer.rs` (where the overlay is drawn). | ||
| 19 | |||
| 20 | Status: built and unit-tested (`cargo test -p leonida-lua --lib`, 34 tests, and the menu crate's); **not yet tried in | ||
| 21 | the running title**. What that leaves open is listed at the end. | ||
| 22 | |||
| 23 | ## Trying it | ||
| 24 | |||
| 25 | 1. Build with diagnostics for screenshots (`cargo build --profile diag -p leonida --features diagnostics`); the Lua | ||
| 26 | mods themselves are in every build. | ||
| 27 | 2. Find the mods folder: `LEONIDA_MODS=<folder>` if set, else `mods` beside `leonida.exe` or in a folder above it | ||
| 28 | (so a build in `d3d12\target\...` finds the repository's `mods\`), else `mods` in the current folder. The log says | ||
| 29 | which (`Lua mods` line, target `lua`). | ||
| 30 | 3. Set `"enabled": true` in `mods\example_lua\mod.json` (it ships disabled), start the title, and look at the lower | ||
| 31 | left corner: a frame-rate line. F4 opens the trainer menu; the last item, Mods, has the example's page. | ||
| 32 | After editing a script, F4, Mods, Reload Lua mods loads them all again (a fresh Lua state). | ||
| 33 | |||
| 34 | `LEONIDA_LUA=0` turns it off completely: no thread, nothing per frame but one atomic load at present, no Mods page. | ||
| 35 | |||
| 36 | ## Events | ||
| 37 | |||
| 38 | ```lua | ||
| 39 | leonida.on("frame", function(dt) end) -- once per presented frame (see below); dt in seconds | ||
| 40 | leonida.on("key", function(key, down) end) -- "W", "F4", "Space", "Shift"...; true on press, false on release | ||
| 41 | leonida.on("menu_open", function() end) -- the F4 menu was opened | ||
| 42 | ``` | ||
| 43 | |||
| 44 | Handlers run on the Lua thread, never on the present. If the Lua thread is slower than the frames, frames are | ||
| 45 | coalesced: the handler runs once for all the presents since its last run, with `dt` the time since then. Key events | ||
| 46 | come only while one of the runtime's windows is in front. Up to 64 handlers a mod. An unknown event name is an error. | ||
| 47 | |||
| 48 | ## Input | ||
| 49 | |||
| 50 | ```lua | ||
| 51 | leonida.key_down("W") -- bool. Names: letters, digits, F1-F24, Enter Esc Tab Back Space Shift Ctrl Alt | ||
| 52 | -- LShift RShift LCtrl RCtrl LAlt RAlt Up Down Left Right Insert Delete Home End | ||
| 53 | -- PageUp PageDown Num0-Num9 NumAdd NumSub NumMul NumDiv NumDot CapsLock Pause | ||
| 54 | -- MouseLeft MouseRight MouseMiddle Semicolon Equals Comma Minus Period Slash | ||
| 55 | -- Tilde LBracket Backslash RBracket Quote. An unknown name is an error. | ||
| 56 | local p = leonida.pad() -- the first connected pad: p.connected, p.a p.b p.x p.y p.lb p.rb p.back p.start | ||
| 57 | -- p.ls p.rs p.up p.down p.left p.right (booleans), p.lt p.rt (0 to 1), | ||
| 58 | -- p.lx p.ly p.rx p.ry (-1 to 1) | ||
| 59 | leonida.hold_keys({"W", "Shift"}) -- the title sees these keys down, as LEONIDA_KEYS_FILE feeds them | ||
| 60 | leonida.release_keys() | ||
| 61 | ``` | ||
| 62 | |||
| 63 | `hold_keys` sets the whole list (it replaces the mod's previous one); the title gets the union of what all mods hold. | ||
| 64 | The stand-in pad the keyboard drives when no pad is connected sees them too. A mod that fails lets its keys go. | ||
| 65 | |||
| 66 | ## Overlay | ||
| 67 | |||
| 68 | ```lua | ||
| 69 | leonida.text(x, y, "hello", {size = 20, color = "#FFE08A"}) -- (x, y) is the top left of the text | ||
| 70 | leonida.rect(x, y, w, h, {0, 0, 0, 150}) | ||
| 71 | ``` | ||
| 72 | |||
| 73 | Coordinates and sizes are pixels of a 1080-line picture and scale with the real picture's height. The list is cleared | ||
| 74 | at the start of each pass: draw again each frame (in a `frame` handler) for what is to stay. Colours: a table | ||
| 75 | `{r, g, b[, a]}` or `{r=, g=, b=, a=}` (0 to 255), a string `"#RRGGBB"` or `"#RRGGBBAA"`, or an integer (`0xRRGGBB`, or | ||
| 76 | `0xRRGGBBAA` when it is above 24 bits); the default is white for text and translucent black for a rectangle. At most | ||
| 77 | 1024 commands, 300 characters a text, text 6 to 200 px. The system's own fonts draw it (Segoe UI on Windows; the | ||
| 78 | built-in dot font where there are none). | ||
| 79 | |||
| 80 | The list is painted on the CPU into one picture the size of what is drawn (up to 4096 x 2304) and laid over the | ||
| 81 | frame in one blended quad, where the F3 panel and the F4 menu are; an unchanged list is not painted again, so many | ||
| 82 | small things cost little and a full-screen rectangle costs a fill of that size when it changes. A present pays one | ||
| 83 | atomic load while nothing is drawn. The drawing is part of the picture frame generation sees; screenshots and saved | ||
| 84 | frames do not have it. | ||
| 85 | |||
| 86 | ## Settings | ||
| 87 | |||
| 88 | ```lua | ||
| 89 | leonida.get("fps_cap") -- nil when this run does not have it | ||
| 90 | local ok, why = leonida.set("fps_cap", 60) | ||
| 91 | for _, name in ipairs(leonida.settings) do ... end | ||
| 92 | ``` | ||
| 93 | |||
| 94 | | Name | Value | Same as | | ||
| 95 | |---|---|---| | ||
| 96 | | `fps_cap` | frames a second 1 to 1000, `0` for none, `"game"` for the title's own pace | the F1 menu's FRAME RATE CAP, `LEONIDA_FPS_CAP` | | ||
| 97 | | `dlss_sharpness` | 0 to 1 (there when the title's upscale is DLSS) | DLSS SHARPNESS, `LEONIDA_DLSS_SHARPNESS` | | ||
| 98 | | `panel` | true or false (diagnostics builds) | the F3 panel | | ||
| 99 | | `menu_scale` | 0.6 to 1.6 | F4, Settings, Menu, Menu size | | ||
| 100 | | `game_speed` | 0.1 to 2 (experimental, `docs/mod-menu.md`) | F4, World, Game speed | | ||
| 101 | |||
| 102 | Any other name is refused: `set` returns `false` and why, `get` returns `nil`. Values outside the range are clamped. | ||
| 103 | Nothing else of the runtime is reachable. | ||
| 104 | |||
| 105 | ## Trainer menu pages | ||
| 106 | |||
| 107 | ```lua | ||
| 108 | local page = leonida.menu.page("My mod") | ||
| 109 | local on = page:toggle("Enabled", true, function(value) end, "help text") | ||
| 110 | local num = page:number("Amount", 2.5, {min = 0, max = 10, step = 0.5, decimals = 1, suffix = "x"}, function(value) end) | ||
| 111 | page:action("Do it", function() end, "help text") | ||
| 112 | on:get(); on:set(false) -- read or change what the menu shows | ||
| 113 | ``` | ||
| 114 | |||
| 115 | The pages of all mods are under F4, Mods (the last item of the main menu), after "Reload Lua mods". Callbacks run on | ||
| 116 | the Lua thread when the player flips, moves or runs the item. At most 64 pages and 64 items a page (shared); labels | ||
| 117 | are cut at 48 characters. After a reload, or when a mod fails and its pages go, the menu is on the Mods page again. | ||
| 118 | |||
| 119 | ## Memory | ||
| 120 | |||
| 121 | Single-player modding of the title's own memory, through the runtime's checked copies (`host::read_into`, | ||
| 122 | `safecopy`): an address that is unmapped, or a write to a protected page, gives `nil` or `false` and never faults. | ||
| 123 | |||
| 124 | ```lua | ||
| 125 | local m = leonida.memory | ||
| 126 | m.read_u8 / read_u16 / read_u32 / read_i32 / read_u64 / read_f32 / read_f64 (address) -- value or nil | ||
| 127 | m.read_bytes(address [, n = 16]) -- Lua string or nil (n <= 1 MiB) | ||
| 128 | m.write_u8 / write_u16 / write_u32 / write_i32 / write_u64 / write_f32 / write_f64 (address, value) -- true or false | ||
| 129 | m.write_bytes(address, string) -- true or false | ||
| 130 | m.base() -- the title's executable image: base, size (nothing before it is mapped) | ||
| 131 | m.scan("48 8B ?? 05", {max = 100, start = address, size = n}) -- list of addresses, or nil and why | ||
| 132 | ``` | ||
| 133 | |||
| 134 | Little-endian, as the x86-64 title is; addresses are Lua integers (64-bit). Addresses below 64 KiB or above the user | ||
| 135 | range are refused. Patterns are hex bytes with `??` (or `?`) for any byte, spaces optional, at least one real byte; | ||
| 136 | `scan` covers the whole image by default, skips pages that are not mapped, never matches across a gap and returns at | ||
| 137 | most `max` (default 100, ceiling 10,000) addresses in order. A scan of the 114 MB image reads it once in 1 MiB steps | ||
| 138 | on the Lua thread, and the time it takes does not count against the mod's time budget. | ||
| 139 | |||
| 140 | What it does not do: it does not know what the title keeps where. There is no table of the title's data here (the | ||
| 141 | title's own script natives were not found, `docs/mod-menu.md`); a mod author finds addresses with `scan` and their | ||
| 142 | own tools and takes the risk that a write corrupts the title. A write is checked to land on a page that can be | ||
| 143 | written, not that it is sensible. Reads and writes race with the title's threads, as any trainer's do. | ||
| 144 | |||
| 145 | ## Misc | ||
| 146 | |||
| 147 | ```lua | ||
| 148 | leonida.api_version -- 1. It changes only when something a mod may rely on does. | ||
| 149 | leonida.version -- the runtime's version, a string | ||
| 150 | leonida.log("a", 1, tbl) -- values joined by spaces, to the runtime's log (tracing target `lua`, the mod's name) | ||
| 151 | print(...) -- the same, joined by tabs | ||
| 152 | leonida.screenshot() -- the path the next frame is saved to, or nil and why (diagnostics builds) | ||
| 153 | leonida.frame() -- presents counted so far (as the Lua thread last saw them) | ||
| 154 | leonida.time() -- seconds since the mods started, a float | ||
| 155 | leonida.read_file("data.txt") -- the contents, or nil and why | ||
| 156 | leonida.write_file("sub/data.txt", s) -- true, or false and why | ||
| 157 | require("helper") -- helper.lua (or helper/init.lua; dots are folders) of the mod's own folder, run once | ||
| 158 | ``` | ||
| 159 | |||
| 160 | Files: only the mod's own folder, plain relative paths (no `..`, drive letters, streams or reserved device names; a | ||
| 161 | link that leads out is refused), reads up to 4 MiB, writes up to 1 MiB, never `mod.json` or a program (`.exe`, `.dll`, | ||
| 162 | `.bat`, ...). There is no `io`. | ||
| 163 | |||
| 164 | ## The sandbox | ||
| 165 | |||
| 166 | Each mod gets an environment of its own: copies of `assert error getmetatable ipairs next pairs pcall rawequal rawget | ||
| 167 | rawlen rawset select setmetatable tonumber tostring type xpcall` and of the `coroutine`, `table`, `string`, `utf8` | ||
| 168 | and `math` libraries, `os.time os.clock os.date os.difftime`, and `print`, `load`, `collectgarbage` and `require` | ||
| 169 | made for it. One mod changing `string.rep` or `math.floor` changes nothing for another, and `getmetatable("")` gives | ||
| 170 | a copy. | ||
| 171 | |||
| 172 | Not there: `io`, `package` (so `package.loadlib` and any C module), `debug`, `dofile`, `loadfile`, `string.dump`, | ||
| 173 | `os.execute`, `os.remove`, `os.rename`, `os.exit`, `os.getenv`, `os.tmpname`, `os.setlocale`. `load` takes text | ||
| 174 | chunks only (a precompiled chunk can take the interpreter down), in the mod's environment unless one is given; | ||
| 175 | `collectgarbage` does `"collect"` and `"count"`. There is no socket, HTTP or any other network function: the | ||
| 176 | runtime offers none and the standard libraries that could reach one are not loaded. | ||
| 177 | |||
| 178 | Limits: a call into a mod (loading it, a handler, a menu callback) may run for 200 ms (the load 2 s) of Lua; a loop | ||
| 179 | that does not end stops with an error, also inside `pcall`, `xpcall` and `coroutine.resume`, which hand that error | ||
| 180 | back instead of swallowing it. The whole Lua state may use 128 MiB. A coroutine can still yield through `pcall`. | ||
| 181 | |||
| 182 | An error anywhere (the script's own, a handler, a menu callback, a bad argument, an exceeded limit) is logged with | ||
| 183 | the mod's name and **disables that mod only**: its handlers, its menu pages and the keys it holds go, the other mods | ||
| 184 | go on. A syntax error or a missing script is the same at load. A Rust panic in the runtime stops the Lua thread's work | ||
| 185 | (the mods are dropped; three in all end the thread) and not the title. | ||
| 186 | |||
| 187 | `mod.json`: `"script"` (a `.lua` path inside the folder), `"enabled"` (default true; the example ships `false`), | ||
| 188 | `"name"` (shown in logs). A folder without `"script"` (an asset mod, `docs/modding.md`) is not a Lua mod and is left | ||
| 189 | alone; a mod can have both. Load order is the folder names', sorted. | ||
| 190 | |||
| 191 | ## What is not here, and why | ||
| 192 | |||
| 193 | - God mode, wanted level, vehicles, weapons, weather, time of day, teleport: these are the title's own script | ||
| 194 | functions, which the runtime cannot call (no table of them was found in the executable, `docs/mod-menu.md`), so | ||
| 195 | there is no `leonida.player` and no `leonida.world`. A mod that wants them has the memory API and has to find the | ||
| 196 | addresses itself. | ||
| 197 | - Anything online, any file outside the mod's folder, any process or library: not offered, on purpose. | ||
| 198 | - No keys, decryption or protection workarounds, and no game data in the repository: a mod holds its own script and | ||
| 199 | the addresses its author found, not pieces of the title. | ||
| 200 | |||
| 201 | ## Not tried yet | ||
| 202 | |||
| 203 | Built, unit-tested and compiled into the runtime, but not run in the title: where the overlay lands on the real back | ||
| 204 | buffer and how it looks with frame generation; that the `hold_keys` keys reach the title as `LEONIDA_KEYS_FILE`'s do; | ||
| 205 | the checked write on the title's guarded memory; a scan of the real 114 MB image (the time it takes); the menu | ||
| 206 | callbacks over the real F4 menu; Linux (the code is portable, but key and pad reading return nothing off Windows | ||
| 207 | for now). `cargo test -p leonida-gpu --features diagnostics --lib host::trainer` was not run (it draws on a GPU); | ||
| 208 | the draw path for the mods' overlay is not covered by it. | ||
| 209 | |||
| 210 | ## Tests | ||
| 211 | |||
| 212 | `cargo test -p leonida-lua --lib` (the sandbox, event dispatch, a mod failing alone, endless loops, memory on a local | ||
| 213 | buffer with unmapped, protected and out-of-range addresses, scans across chunks and holes, overlay, menu pages, | ||
| 214 | settings, files, require, manifests, the API version) and `cargo test -p leonida-menu --lib` (the Mods page, the | ||
| 215 | overlay painter, injected keys, `Menu::replace_pages`). No GPU, no title. | ||