Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua 👀 #3128

Open
wants to merge 110 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
6117871
quick and dirty integration of the new lua interpreter
sug44 Sep 8, 2024
ef869db
Interpreter can be selected with field now
sug44 Sep 10, 2024
7aff5d4
Gitignore part thumbnails added through the KSP symlink
sug44 Sep 10, 2024
102fa0f
Changed class and interface names after logic separation
sug44 Sep 10, 2024
b87fd23
Make interpreters not run alongside each other
sug44 Sep 11, 2024
adc8653
Interpreter specific error handling
sug44 Sep 11, 2024
5314ce5
Field work
sug44 Sep 12, 2024
286ebb3
Structure lua binding
sug44 Sep 17, 2024
a104d58
Kerboscript functions lua binding
sug44 Sep 18, 2024
e6007db
Interpreter swap rework
sug44 Sep 21, 2024
279b091
Some gitignoring
sug44 Sep 21, 2024
702456e
Fix breaking execution for lua
sug44 Sep 21, 2024
442943e
Binding work
sug44 Sep 22, 2024
907c2cf
Fix not calling Cpu.BreakExecution(true) on processor shutdown
sug44 Sep 22, 2024
6609634
Dont turn on kOSProcessor when changing its interpreter
sug44 Sep 22, 2024
3a65320
Support kerboscript yields in lua
sug44 Sep 22, 2024
3ad4f5a
Use kerboscript stack when calling kerboscript functions from lua
sug44 Sep 22, 2024
03689db
Some typos
sug44 Sep 22, 2024
bd31c04
Callback(trigger) system. Written in lua
sug44 Sep 26, 2024
12d3915
Separate types and functions from binding logic
sug44 Sep 27, 2024
85c84ae
Fix NREs from LuaInterpreter.Dispose on scene unloads. Dispose (lua/k…
sug44 Sep 28, 2024
5ccb8f2
Remove project references added in 611787190d6d2f5710393f9d660705b7df…
sug44 Sep 28, 2024
5466e4e
Add unary operator for Structures
sug44 Sep 28, 2024
eb01c2a
Define "uniqueTypes"
sug44 Sep 29, 2024
b151777
Vessel control
sug44 Oct 1, 2024
c12e195
Treat Structure suffixes with void return type as functions
sug44 Oct 1, 2024
19bb07a
Add stage, wait, loadfile, dofile, getchar functions.
sug44 Oct 2, 2024
efdbd6e
Remove unused InterpreterLanguage property on IProcessor
sug44 Oct 2, 2024
bee4350
Rename HEADING to SHIPHEADING for lua so it doesn't hide the heading(…
sug44 Oct 7, 2024
ba0296f
Add runfileon, runcommandon functions
sug44 Oct 8, 2024
9408ac4
Simpler callback system, break ship control on ctrl+C
sug44 Oct 8, 2024
e9dfb5e
Add length metamethod for Structures
sug44 Oct 9, 2024
2022bb3
Support queuing multiple lua commands without overwriting the previou…
sug44 Oct 9, 2024
e4503ba
Set onFixedUpdate to nil if it's locking up the core and Ctrl+C was p…
sug44 Oct 9, 2024
a419991
Error traceback
sug44 Oct 9, 2024
2120da7
Set index, newindex metamethods on _ENV table without creating global…
sug44 Oct 9, 2024
d817d2c
Make lua processors use 0 instructions at idle
sug44 Oct 9, 2024
40d2ca0
Use UTF8 encoding for lua. Used to be ASCII
sug44 Oct 10, 2024
efd11f7
Add onUpdate callback for lua. On lua side make it use the same callb…
sug44 Oct 11, 2024
e92ab3d
Create new CPU and Interpreter instances and dispose the old ones on …
sug44 Oct 11, 2024
ae5b4c2
Print exception type on lua error if exception has no message
sug44 Oct 14, 2024
7c7f75c
Don't call onUpdate lua callback when the game is paused
sug44 Oct 14, 2024
5390722
Dispose lua state before removing its binding info
sug44 Oct 14, 2024
7989188
Separate IPU config value for lua
sug44 Oct 14, 2024
31f4152
Remove IdleInstructions for onFixedUpdate, onUpdate callbacks
sug44 Oct 14, 2024
a6a1922
Lua error on invalid argument in a kerboscript function
sug44 Oct 15, 2024
f5948f3
Fix NRE in Interpreter.BreakExecution on a turned off processor
sug44 Oct 15, 2024
81c4d46
Add optional stack traceback to lua warn function
sug44 Oct 15, 2024
4984bd6
Dispose Interpreter on processor shutdown instead of on processor boot
sug44 Oct 17, 2024
33058c7
Clearer error traceback in processControl and callbacks
sug44 Oct 17, 2024
cca6ded
Let go of vessel controls on lua processor shutdown
sug44 Oct 17, 2024
0cf331a
Remove lua instruction debt
sug44 Oct 17, 2024
e71a0e1
Add helper functions to make vecDraws update automatically
sug44 Oct 17, 2024
684d9ef
Drop NLua
sug44 Oct 17, 2024
17169d0
Higher max lua IPU and default lua IPU
sug44 Oct 17, 2024
0e3b419
Remove libraries and basic library functions that access the "real wo…
sug44 Oct 17, 2024
afe5211
Run commands after onFixedUpdate, make commands take priority over on…
sug44 Oct 18, 2024
1c99d70
Error on attempt to hide a built-in variable when config.clobberBuilt…
sug44 Oct 19, 2024
7e6ee20
Lua error when trying to set structure suffix that doesn't exist
sug44 Oct 19, 2024
4788d7a
Whitelist libraries and globals, do initialization from LuaFunctions.…
sug44 Oct 31, 2024
184356e
Lua build workflow
sug44 Nov 4, 2024
5e8966f
Build lua
web-flow Nov 7, 2024
a10956c
Add rewritten package library
sug44 Nov 7, 2024
36f3d94
Whitelist the registry table
sug44 Nov 7, 2024
a3340c0
Define package.config instead of using the native package library config
sug44 Nov 9, 2024
212d323
Add building modules with lua-build workflow
sug44 Nov 14, 2024
510e43c
Build lua
web-flow Nov 14, 2024
33fd351
Load the cjson library to the global environment as 'json'
sug44 Nov 14, 2024
4d62454
Move lua modules from being embedded to PluginData/LuaModules/ directory
sug44 Nov 14, 2024
cc2d579
Merge branch 'develop' into lua_develop. Merge commit switching KSPBu…
sug44 Nov 14, 2024
5d4e53b
Rename "body" function to "getbody" for lua to resolve a name conflic…
sug44 Dec 4, 2024
e8b9e25
Treat all "pop" and "peek" suffixes as functions.
sug44 Dec 6, 2024
fb4b3a8
Add expression-body function syntactic sugar
sug44 Dec 6, 2024
04bfff0
Build lua
web-flow Dec 6, 2024
d9b6856
Lua development environment improvements
sug44 Dec 7, 2024
45b6a49
Fix LuaLSAddon README
sug44 Dec 8, 2024
5b28ac5
Fix annotations typo
sug44 Dec 8, 2024
cb64382
Add annotations for default modules
sug44 Dec 8, 2024
6a221bb
Fix NRE in GetHashCode for unloaded vessels
sug44 Dec 9, 2024
cd60b83
Look for a file in `dofile`, `loadfile` lua functions the same way ks…
sug44 Dec 9, 2024
1260430
Convert objects to Structures when pushing onto the kerboscript argum…
sug44 Dec 9, 2024
8ff708a
Set whitelisted string table as an __index field for strings metatable.
sug44 Dec 9, 2024
5fdeb27
Add "Node" structure as a possible type for steering in annotations
sug44 Dec 10, 2024
50c5441
Wait functions and kerboscript yields in lua rework
sug44 Dec 10, 2024
8fa991b
Fix not handling nil error value from callbacks
sug44 Dec 10, 2024
312a49c
Remove kerboscript specific functions in lua
sug44 Dec 10, 2024
478085d
Fix EC drain at "idle" running default modules
sug44 Dec 11, 2024
07c0b73
Sort callbacks by unique creationTime if priorities are equal
sug44 Dec 12, 2024
b85ef49
Fix wrong callbacks time sorting order
sug44 Dec 13, 2024
e7786b7
Fix annotations formatting, add annotations to `misc.vecdraw` function
sug44 Dec 13, 2024
14f5428
Make control bound variables only accessible with a capital name
sug44 Dec 14, 2024
082c5d1
Check for clobbering kOS functions
sug44 Dec 19, 2024
f942fe8
Fix annotations for vectorcrossproduct and vdot functions
sug44 Dec 19, 2024
25ab5df
Add documentation for the lua interpreter
sug44 Dec 19, 2024
3239c54
Share lua types across all lua states
sug44 Dec 22, 2024
535a081
Fix potential NRE in `LuaInterpreter.ECInstructionsThisUpdate`
sug44 Dec 24, 2024
acc88a9
Add `log` function to the dev library
sug44 Dec 25, 2024
39d07d4
Rename lua `ExecInfo` to `InstructionHookInfo`, move irrelevant to th…
sug44 Dec 26, 2024
6bef15e
Fix annotations
sug44 Dec 26, 2024
cbdb662
Optimize `misc.vecdraw` function, remove Vecdraw.color suffix updater
sug44 Dec 26, 2024
61b2589
Add kerboscript command alternatives section to the lua interpreter docs
sug44 Dec 26, 2024
541a636
Fix not showing error when calling kerboscript functions with argumen…
sug44 Jan 26, 2025
91aced9
Don't reset `update` function if the terminal is blocked.
sug44 Jan 26, 2025
0e7c4f0
Add "System variables" section to the docs
sug44 Jan 26, 2025
99f182d
Fix blocking the terminal when fixedUpdateCoroutine yields every tick…
sug44 Jan 26, 2025
5260a9b
Merge branch 'develop' into lua_develop
sug44 Feb 6, 2025
02a9c76
Fix merge
sug44 Feb 6, 2025
36d71f5
Get system variables without calling __index metamethod
sug44 Feb 6, 2025
0c82408
Check arguments of environment metamethods in case they are called ma…
sug44 Feb 6, 2025
f460e79
Update lua annotations
sug44 Feb 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions .github/workflows/lua-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
name: lua-build

on:
workflow_dispatch:
inputs:
push:
default: true
type: boolean
description: "Push built dynamic libraries"
workflow_call:
inputs:
push:
default: true
type: boolean
description: "Push built dynamic libraries"
permissions:
contents: write

defaults:
run:
shell: bash

env:
LUA_SOURCE_DIR: ${{ github.workspace }}/src/kOS/Lua/lua
LUA_BUILD_DIR: ${{ github.workspace }}/src/kOS/Lua/lua/_build
LUA_OUTPUT_DIR: ${{ github.workspace }}/src/kOS/Lua/include

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v4
with:
submodules: 'true'

- name: Set vars
id: vars
run: |
if [ "$RUNNER_OS" == "Linux" ]; then
echo 'LUA_LIB_NAME=liblua54.so" >> "$GITHUB_OUTPUT'
echo 'LUA_LIB_PATH=$LUA_BUILD_DIR/lib64/liblua54.so' >> "$GITHUB_OUTPUT"
echo 'LUA_LUAROCKS_LIB_PATH=/usr/local/lib/lua/5.4' >> "$GITHUB_OUTPUT"
elif [ "$RUNNER_OS" == "Windows" ]; then
echo 'LUA_LIB_NAME=lua54.dll" >> "$GITHUB_OUTPUT'
echo 'LUA_LIB_PATH=$LUA_BUILD_DIR/bin/lua54.dll' >> "$GITHUB_OUTPUT"
echo 'LUA_LUAROCKS_LIB_PATH=~/.luarocks/lib/lua/5.4' >> "$GITHUB_OUTPUT"
elif [ "$RUNNER_OS" == "macOS" ]; then
echo 'LUA_LIB_NAME=liblua54.dylib" >> "$GITHUB_OUTPUT'
echo 'LUA_LIB_PATH=$LUA_BUILD_DIR/lib64/liblua54.dylib' >> "$GITHUB_OUTPUT"
echo 'LUA_LUAROCKS_LIB_PATH=/usr/local/lib/lua/5.4' >> "$GITHUB_OUTPUT"
fi

- name: Configure CMake
run: cmake -S $LUA_SOURCE_DIR -B $LUA_BUILD_DIR

- name: Build
run: cmake --build $LUA_BUILD_DIR --config Release

- name: Test
working-directory: ${{ env.LUA_BUILD_DIR }}
run: ctest --build-config Release

- name: Install LuaRocks
run: |
mv $LUA_BUILD_DIR/bin64 $LUA_BUILD_DIR/bin # luarocks gets lost if lua binary is not in "bin" directory
if [ "$RUNNER_OS" == "Windows" ]; then
curl http://luarocks.github.io/luarocks/releases/luarocks-3.11.1-windows-64.zip --output luarocks.zip
unzip luarocks.zip -d ~/luarocks
mv ~/luarocks/luarocks-3.11.1-windows-64/* ~/luarocks
rm -rf ~/luarocks/luarocks-3.11.1-windows-64
else
wget https://luarocks.org/releases/luarocks-3.11.1.tar.gz
tar zxpf luarocks-3.11.1.tar.gz
cd luarocks-3.11.1
./configure --with-lua-bin="$LUA_BUILD_DIR/bin"
make && sudo make install
fi

- name: Install modules
run: |
if [ "$RUNNER_OS" == "Windows" ]; then
PATH=~/luarocks:$LUA_BUILD_DIR/bin:$PATH
luarocks install lua-cjson 2.1.0.10-1
else
sudo luarocks install lua-cjson 2.1.0.10-1
fi

- name: Create artifact
run: |
rm -rf artifact
mkdir artifact
cp ${{ steps.vars.outputs.LUA_LIB_PATH }} artifact/${{ steps.vars.outputs.LUA_LIB_NAME }}
mkdir artifact/LuaModules artifact/LuaModules/${{ runner.os }}
cp -r ${{ steps.vars.outputs.LUA_LUAROCKS_LIB_PATH }}/. artifact/LuaModules/${{ runner.os }}

- uses: actions/upload-artifact@v4
with:
name: ${{ runner.os }}
if-no-files-found: error
retention-days: 7
path: ${{ github.workspace }}/artifact

push:
runs-on: ubuntu-latest
needs: build
if: inputs.push
steps:
- uses: actions/checkout@v4
with:
submodules: 'true'

- name: Clear output directory
run: rm -rf $LUA_OUTPUT_DIR

- uses: actions/download-artifact@v4
with:
merge-multiple: 'true'
path: ${{ env.LUA_OUTPUT_DIR }}

- name: Push
env:
GIT_COMMITTER_NAME: lua-build automation
GIT_AUTHOR_NAME: lua-build automation
GIT_COMMITTER_EMAIL: noreply@github.com
GIT_AUTHOR_EMAIL: noreply@github.com
run: |
git add $LUA_OUTPUT_DIR
git commit -am "Build lua"
git push
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ packages/
# VisualStudio Code
.vscode/

# Rider
.idea/

# Vim
*.swp

Expand All @@ -35,10 +38,13 @@ packages/
[Rr]esources/GameData/kOS/Plugins/
[Rr]esources/*.dll
[Rr]esources/GameData/*.dll
[Rr]esources/GameData/kOS/Parts/@thumbs/
[Rr]esources/GameData/kOS/PluginData/LuaModules/
!Resources/GameData/kOS/ICSharpCode.SharpZipLib.dll

# Symlink to KSP directory
KSPdirlink
KSP

# OS Specific
.DS_STORE*
Expand All @@ -53,3 +59,5 @@ netkan.exe

# .swp files from vim (temporary files that vim creates when editing a file)
*.swp

!Resources/GameData/kOS/PluginData/LuaLSAddon/library/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/kOS/Lua/lua"]
path = src/kOS/Lua/lua
url = git@github.com:sug44/lua.git
9 changes: 9 additions & 0 deletions Resources/GameData/kOS/PluginData/LuaLSAddon/.luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"diagnostics.disable": ["lowercase-global"],
"workspace.library": [
"../../GameData/kOS/PluginData/LuaLSAddon/library",
"../../GameData/kOS/PluginData/LuaModules"
],
"runtime.plugin": "../../GameData/kOS/PluginData/LuaLSAddon/plugin/patcher.lua",
"runtime.special": { "_G": "_G" }
}
13 changes: 13 additions & 0 deletions Resources/GameData/kOS/PluginData/LuaLSAddon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This is an addon for [lua language server](https://github.com/luals/lua-language-server).
It includes:
- Patcher plugin that adds support for the arrow function syntax
- Annotations for kOS structures, functions and bound variables

### Setup for VSCode
1. Install the lua language server extension made by sumneko.
2. Copy the `.luarc.json` file to the archive at `*KSP_Folder*/Ships/Script/.luarc.json`.
3. Open the folder with VSCode and you will be asked by the patcher plugin to patch lua language server.
4. Read the message and press "Ok".
5. Restart VSCode.

After setup you should have the syntax support and annotations available.
14 changes: 14 additions & 0 deletions Resources/GameData/kOS/PluginData/LuaLSAddon/library/base.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---Function that gets called by kOS each physics tick.
---@type function | nil
fixedupdate = nil

---Function that gets called by kOS each frame.
---@type function | nil
update = nil

---Function that gets called by kOS when `Ctrl+C` is pressed.
---If `Ctrl+C` was pressed 3 times while the command code was deprived of instructions by the `fixedupdate` function
---it will be set to `nil` to prevent the core from geting stuck.
---To prevent it from happening this function must ensure the terminal is not deprived of instructions.
---@type function | nil
breakexecution = nil
Loading
Loading