Skip to content

Commit 0a9a37d

Browse files
authored
feat: Lua definition file generation, ladfile re-work (#497)
1 parent 2b34279 commit 0a9a37d

File tree

48 files changed

+3468
-708
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3468
-708
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ assets/scripts/tlconfig.lua
1919

2020
/assets/**/*.lad.json
2121
/docs/src/ladfiles/*.lad.json
22+
/assets/**/bindings.lua

.luarc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
3+
"workspace.library": [
4+
"assets/definitions"
5+
],
6+
"runtime.version": "Lua 5.4",
7+
"hint.enable": false,
8+
"diagnostics.workspaceDelay": 30000
9+
}

Cargo.toml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ default = [
4444
"bevy_core_pipeline_bindings",
4545
]
4646

47-
lua = [
48-
"bevy_mod_scripting_lua",
49-
"bevy_mod_scripting_functions/lua_bindings",
50-
] ## lua
47+
48+
# lua
49+
lua = ["bevy_mod_scripting_lua", "bevy_mod_scripting_functions/lua_bindings"]
5150
# one of these must be selected
5251
lua51 = ["bevy_mod_scripting_lua/lua51", "lua"]
5352
lua52 = ["bevy_mod_scripting_lua/lua52", "lua"]
@@ -56,6 +55,10 @@ lua54 = ["bevy_mod_scripting_lua/lua54", "lua"]
5655
luajit = ["bevy_mod_scripting_lua/luajit", "lua"]
5756
luajit52 = ["bevy_mod_scripting_lua/luajit52", "lua"]
5857
luau = ["bevy_mod_scripting_lua/luau", "lua"]
58+
lua_language_server_files = [
59+
"ladfile_builder",
60+
"ladfile_builder/lua_language_server_files",
61+
]
5962

6063
# bindings
6164
core_functions = ["bevy_mod_scripting_functions/core_functions"]
@@ -111,8 +114,10 @@ bevy_mod_scripting_functions = { workspace = true }
111114
bevy_mod_scripting_derive = { workspace = true }
112115
bevy_mod_scripting_asset = { workspace = true }
113116
bevy_mod_scripting_bindings = { workspace = true }
117+
bevy_mod_scripting_bindings_domain = { workspace = true }
114118
bevy_mod_scripting_display = { workspace = true }
115119
bevy_mod_scripting_script = { workspace = true }
120+
ladfile_builder = { workspace = true, optional = true }
116121

117122
[workspace.dependencies]
118123
# local crates
@@ -127,8 +132,10 @@ bevy_mod_scripting_lua = { path = "crates/languages/bevy_mod_scripting_lua", ver
127132
bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", version = "0.16.0", default-features = false }
128133
bevy_mod_scripting_asset = { path = "crates/bevy_mod_scripting_asset", version = "0.16.0", default-features = false }
129134
bevy_mod_scripting_bindings = { path = "crates/bevy_mod_scripting_bindings", version = "0.16.0", default-features = false }
135+
bevy_mod_scripting_bindings_domain = { path = "crates/bevy_mod_scripting_bindings_domain", version = "0.16.0", default-features = false }
130136
bevy_mod_scripting_display = { path = "crates/bevy_mod_scripting_display", version = "0.16.0", default-features = false }
131137
bevy_mod_scripting_script = { path = "crates/bevy_mod_scripting_script", version = "0.16.0", default-features = false }
138+
lua_language_server_lad_backend = { path = "crates/lad_backends/lua_language_server_lad_backend", version = "0.16.0", default-features = false }
132139

133140
# bevy
134141

@@ -215,7 +222,7 @@ pretty_assertions = { version = "1.4", default-features = false, features = [
215222
"std",
216223
] }
217224
manifest-dir-macros = { version = "0.1.18", default-features = false }
218-
assert_cmd = { version = "2.0", default-features = false }
225+
assert_cmd = { version = "2.1", default-features = false }
219226
tokio = { version = "1", default-features = false }
220227
bevy_console = { version = "0.14", default-features = false }
221228
tracing-tracy = { version = "0.11", default-features = false }
@@ -260,6 +267,7 @@ members = [
260267
"crates/testing_crates/script_integration_test_harness",
261268
"crates/bevy_mod_scripting_derive",
262269
"crates/ladfile",
270+
"crates/lad_backends/lua_language_server_lad_backend",
263271
"crates/lad_backends/mdbook_lad_preprocessor",
264272
"crates/ladfile_builder",
265273
"crates/bevy_system_reflection",
@@ -268,6 +276,7 @@ members = [
268276
"crates/bevy_mod_scripting_bindings",
269277
"crates/bevy_mod_scripting_display",
270278
"crates/bevy_mod_scripting_script",
279+
"crates/bevy_mod_scripting_bindings_domain",
271280
]
272281
resolver = "2"
273282
exclude = ["codegen", "crates/macro_tests", "xtask"]

assets/scripts/game_of_life.lua

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,35 @@ info("Lua: The game_of_life.lua script just got loaded")
55

66
math.randomseed(os.time())
77

8-
function fetch_life_state()
9-
-- find the first entity with life state
10-
local i,v = next(world.query():component(LifeState):build())
8+
function fetch_life_state()
9+
-- find the first entity with life state
10+
local i, v = next(world.query():component(LifeState):build())
1111
return v:components()[1]
12-
end
12+
end
1313

1414
function on_script_loaded()
1515
info("Lua: Hello! I am initiating the game of life simulation state with randomness!")
1616
info("Lua: Click on the screen to set cells alive after running the `gol start` command")
17-
17+
1818
local life_state = fetch_life_state()
19+
1920
local cells = life_state.cells
20-
21+
2122
-- set some cells alive
22-
for _=1,1000 do
23+
for _ = 1, 1000 do
2324
local index = math.random(#cells)
2425
cells[index] = 255
2526
end
26-
end
27+
end
2728

28-
function on_click(x,y)
29+
function on_click(x, y)
2930
-- get the settings
3031
info("Lua: Clicked at x: " .. x .. " y: " .. y)
3132
local life_state = fetch_life_state()
3233
local cells = life_state.cells
3334

3435
local settings = world.get_resource(Settings)
36+
3537
local dimensions = settings.physical_grid_dimensions
3638
local screen = settings.display_grid_dimensions
3739

@@ -51,18 +53,18 @@ function on_click(x,y)
5153

5254
-- toggle a bunch of cells around if they exist
5355
local cell_offsets = {
54-
{0,0},
55-
{1,0},
56-
{0,1},
57-
{1,1},
58-
{-1,0},
59-
{0,-1},
60-
{-1,-1},
61-
{1,-1},
62-
{-1,1}
56+
{ 0, 0 },
57+
{ 1, 0 },
58+
{ 0, 1 },
59+
{ 1, 1 },
60+
{ -1, 0 },
61+
{ 0, -1 },
62+
{ -1, -1 },
63+
{ 1, -1 },
64+
{ -1, 1 }
6365
}
6466

65-
for _,offset in pairs(cell_offsets) do
67+
for _, offset in pairs(cell_offsets) do
6668
local offset_x = offset[1]
6769
local offset_y = offset[2]
6870
local new_index = index + offset_x + offset_y * dimension_x
@@ -81,10 +83,10 @@ function on_update()
8183

8284
-- primitives are passed by value to lua, keep a hold of old state but turn 255's into 1's
8385
local prev_state = {}
84-
for v in pairs(cells) do
85-
prev_state[#prev_state+1] = (not(v == 0)) and 1 or 0
86+
for v in pairs(cells) do
87+
prev_state[#prev_state + 1] = (not (v == 0)) and 1 or 0
8688
end
87-
for i=1,(dimension_x * dimension_y) do
89+
for i = 1, (dimension_x * dimension_y) do
8890
-- wrap around the north and south edges
8991
local north = prev_state[i - dimension_x] or prev_state[i + dimension_x * (dimension_y - 1)]
9092
local south = prev_state[i + dimension_x] or prev_state[i - dimension_x * (dimension_y - 1)]
@@ -95,13 +97,13 @@ function on_update()
9597
local northwest = prev_state[i - dimension_x - 1] or 0
9698
local southwest = prev_state[i + dimension_x - 1] or 0
9799

98-
local neighbours = north + south + east + west
100+
local neighbours = north + south + east + west
99101
+ northeast + southeast + northwest + southwest
100-
102+
101103
-- was dead and got 3 neighbours now
102104
if prev_state[i] == 0 and neighbours == 3 then
103105
cells[i] = 255
104-
-- was alive and should die now
106+
-- was alive and should die now
105107
elseif prev_state[i] == 1 and ((neighbours < 2) or (neighbours > 3)) then
106108
cells[i] = 0
107109
end
@@ -114,7 +116,7 @@ function on_script_unloaded()
114116
-- set state to 0's
115117
local life_state = fetch_life_state()
116118
local cells = life_state.cells
117-
for i=1,#cells do
119+
for i = 1, #cells do
118120
cells[i] = 0
119121
end
120-
end
122+
end

crates/bevy_mod_scripting_bindings/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ bevy_mod_scripting_asset = { workspace = true }
1717
bevy_mod_scripting_derive = { workspace = true }
1818
bevy_mod_scripting_display = { workspace = true }
1919
bevy_mod_scripting_script = { workspace = true }
20+
bevy_mod_scripting_bindings_domain = { workspace = true }
2021
bevy_system_reflection = { workspace = true }
2122
bevy_diagnostic = { workspace = true }
2223
bevy_ecs = { workspace = true }
@@ -30,6 +31,7 @@ itertools = { workspace = true }
3031
profiling = { workspace = true }
3132
bevy_asset = { workspace = true }
3233
variadics_please = { workspace = true }
34+
serde = { workspace = true }
3335

3436
[dev-dependencies]
3537
pretty_assertions = { workspace = true }

crates/bevy_mod_scripting_bindings/src/docgen/info.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ variadics_please::all_tuples!(impl_documentable, 0, 13, T);
229229

230230
#[cfg(test)]
231231
mod test {
232+
use bevy_mod_scripting_bindings_domain::ReflectionPrimitiveKind;
233+
232234
use crate::{
233235
docgen::typed_through::UntypedWrapperKind,
234236
function::from::{Mut, Ref, Val},
@@ -252,15 +254,15 @@ mod test {
252254
assert_eq!(info.arg_info[1].type_id, TypeId::of::<f32>());
253255

254256
match info.arg_info[0].type_info.as_ref().unwrap() {
255-
ThroughTypeInfo::TypeInfo(type_info) => {
256-
assert_eq!(type_info.type_id(), TypeId::of::<i32>());
257+
ThroughTypeInfo::Primitive(prim) => {
258+
assert_eq!(*prim, ReflectionPrimitiveKind::I32);
257259
}
258260
_ => panic!("Expected TypeInfo"),
259261
}
260262

261263
match info.arg_info[1].type_info.as_ref().unwrap() {
262-
ThroughTypeInfo::TypeInfo(type_info) => {
263-
assert_eq!(type_info.type_id(), TypeId::of::<f32>());
264+
ThroughTypeInfo::Primitive(prim) => {
265+
assert_eq!(*prim, ReflectionPrimitiveKind::F32);
264266
}
265267
_ => panic!("Expected TypeInfo"),
266268
}

0 commit comments

Comments
 (0)