Skip to content

Commit f60f33b

Browse files
authored
refactor: extract bevy_mod_scripting_asset and bevy_mod_scripting_display crates, decouple concerns (#477)
1 parent 6efda4d commit f60f33b

File tree

155 files changed

+6014
-5828
lines changed

Some content is hidden

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

155 files changed

+6014
-5828
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
applyTo: docs/, **/*.md
3+
---
4+
5+
# Documentation Instructions
6+
7+
The `/docs` folder contain documentation for the project as well as release notes.
8+
9+
Newer release notes should be added to `/docs/src/ReleaseNotes/`.
10+
11+
When making changes to the documentation, please ensure that you follow these guidelines:
12+
- Use clear and concise language, focusing on EXAMPLES and practical usage.
13+
- Use these naming conventions:
14+
- Use lowercase letters and hyphens for file names (e.g., `my-documentation-file.md`).
15+
- for migration guides use the format `0.0.1-to-0.0.2.md`.
16+
- for release notes use the format `0.0.1.md`.
17+
18+
19+
# Release Notes Instructions
20+
21+
Release notes should highlight the major highlights of each release, while leaving the breaking changes for migration guides.
22+
23+
When creating release notes, please follow these guidelines:
24+
- Include links to relevant issues or pull requests when applicable.
25+
- YOU MUST use the following heading structure:
26+
- `# Version - Short Description`
27+
- `## Summary`
28+
- `### <Theme>` for each major theme or change
29+
- `### Other Changes` (if applicable) for smaller changes worth mentioning
30+
- `## Migration Guide` (if applicable) providing a link to the detailed migration guide.
31+
32+
33+
# Migration Guide Instructions
34+
35+
When creating migration guides, please follow these guidelines:
36+
- Provide step-by-step instructions for updating projects to the new version.
37+
- Each breaking change should come under a heading related to the change i.e. `### ScriptAsset removed`
38+
- Include code snippets to illustrate changes.
39+
- Use diff format when talking about concrete changes to specific structures or traits.
40+
- Include links to relevant issues or pull requests when applicable.
41+
- Generally each pull request has a migration guide section, which must be taken into account and expanded on when writing relevant migration guides.
42+
43+
44+
# Examples
45+
46+
## Release Notes Example
47+
48+
```markdown
49+
# 0.15.0 - Asset Handles and Context Policies
50+
51+
This release focuses on aligning `bevy_mod_scripting` with modern Bevy practices, most notably by switching to `Handle<ScriptAsset>` for script management. This change simplifies the API, removes boilerplate, and makes script handling more idiomatic.
52+
53+
## Summary
54+
55+
### Asset-First Workflow
56+
Scripts are now treated as first-class Bevy assets. The old `ScriptId` (which was a string) has been replaced by `AssetId<ScriptAsset>`, and you'll primarily interact with scripts via `Handle<ScriptAsset>`.
57+
58+
```rust,ignore
59+
// New way
60+
let handle: Handle<ScriptAsset> = asset_server.load("my_script.lua");
61+
commands.spawn(ScriptComponent(vec![handle]));
62+
```
63+
64+
Scripts are now only evaluated when they are attached to a `ScriptComponent` or added to `StaticScripts`, which means you have more control over when and how scripts are executed.
65+
66+
### Other Changes
67+
- **`Recipients` Enum:** The `Recipients` enum for events has been redesigned to align with the new context policies, offering `AllScripts` and `AllContexts` variants, and removing some variants which don't fit the new model. If you need the old behaviour, you can simply query the ECS first before sending events.
68+
- **API Cleanup:** Several types and traits were removed or simplified, including `ScriptAssetSettings`, `AssetPathToScriptIdMapper`, and `ScriptMetadataStore`, as they are no longer needed with the new asset-based approach.
69+
70+
## Migration Guide
71+
This release contains significant breaking changes. Please refer to the migration guide for detailed instructions on updating your project.
72+
73+
- [Migration Guide: 0.14 to 0.15](https://makspll.github.io/bevy_mod_scripting/Migration/0.14-to-0.15.html)
74+
75+
```
76+
77+
## Migration Guide Example
78+
79+
```markdown
80+
# Migration Guide: <from> to <to>
81+
82+
## Changes to pre handling callbacks
83+
84+
This change affects the parameters for the `context_pre_handling_initializers`
85+
```diff
86+
- context_pre_handling_initializers: vec![|script_id, entity, context| {
87+
+ context_pre_handling_initializers: vec![|context_key, context| {
88+
```
89+
and `context_initializers`:
90+
```diff
91+
- context_initializers: vec![|script_id, context| {
92+
+ context_initializers: vec![|context_key, context| {
93+
```
94+
95+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
applyTo: **
3+
---
4+
5+
# Repository Instructions
6+
7+
## High-Level Structure
8+
9+
### Core Components
10+
- **bevy_mod_scripting** - Main workspace crate that re-exports key functionality.
11+
- **bevy_mod_scripting_core** - Core framework with language-agnostic scripting functionality
12+
- **bevy_mod_scripting_asset** - Handles script assets, loading, and language detection
13+
- **bevy_mod_scripting_derive** - Procedural macros for generating bindings
14+
- **bevy_mod_scripting_functions** - Core Bevy function bindings, and plugin managing various binding crates via features.
15+
16+
### Language Implementations
17+
- **bevy_mod_scripting_lua** - Lua language implementation
18+
- **bevy_mod_scripting_rhai** - Rhai language implementation
19+
20+
### Binding Crates
21+
- Multiple **bevy_*_bms_bindings** crates (e.g., transform, asset, pbr, etc.) that provide specific Bevy module bindings
22+
- These are automatically generated via the `/codegen` tools, per bevy release
23+
24+
### Development Tools
25+
- **xtask** - Custom task runner for development workflows
26+
- **codegen** - Code generation tools for generating bindings from Bevy's API
27+
- **docs** - Documentation built with mdbook
28+
- **docs/src/ReleaseNotes** - Release notes and migration guides
29+
30+
## Key Features
31+
32+
1. **Script Management** - Loading, hot reloading, and lifecycle management via Bevy's asset system
33+
2. **Flexible Bindings** - Attach bindings to any Reflect-implementing types
34+
3. **Dynamic Systems** - Create ECS systems from scripts that run in parallel
35+
4. **Component Creation** - Register and use components from scripts
36+
5. **Multiple Language Support** - Currently supports Lua and Rhai
37+
38+
39+
## Xtask Commands
40+
- `cargo xtask check` - Builds the project and runs clippy

assets/lua_to_rhai_test_conversion_prompt.md renamed to .github/instructions/rhai.instructions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// perfect application of AI
2-
// helps proompting LLMs to do good for this project
1+
---
2+
applyTo: **/.rhai
3+
---
34

45
# Convert lua to rhai script
56
Convert the current test to a rhai test. below are examples and instructions on the conversions necessary:

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ bevy_mod_scripting_rhai = { workspace = true, optional = true }
9999
bevy_mod_scripting_functions = { workspace = true }
100100
bevy_mod_scripting_derive = { workspace = true }
101101
bevy_mod_scripting_asset = { workspace = true }
102+
bevy_mod_scripting_bindings = { workspace = true }
103+
bevy_mod_scripting_display = { workspace = true }
102104

103105
[workspace.dependencies]
104106
# local crates
@@ -112,7 +114,8 @@ ladfile_builder = { path = "crates/ladfile_builder", version = "0.5.1" }
112114
bevy_mod_scripting_lua = { path = "crates/languages/bevy_mod_scripting_lua", version = "0.15.1", default-features = false }
113115
bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", version = "0.15.1", default-features = false }
114116
bevy_mod_scripting_asset = { path = "crates/bevy_mod_scripting_asset", version = "0.15.1", default-features = false }
115-
117+
bevy_mod_scripting_bindings = { path = "crates/bevy_mod_scripting_bindings", version = "0.15.1", default-features = false }
118+
bevy_mod_scripting_display = { path = "crates/bevy_mod_scripting_display", version = "0.15.1", default-features = false }
116119
# bevy
117120

118121
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.15.1" }
@@ -190,7 +193,7 @@ itertools = { version = "0.14", default-features = false }
190193
fixedbitset = { version = "0.5", default-features = false }
191194
variadics_please = { version = "1.1.0", default-features = false }
192195
anyhow = { version = "1.0", default-features = false }
193-
196+
indent_write = { version = "2", default-features = false, features = ["std"] }
194197

195198
# development and testing
196199

@@ -245,6 +248,8 @@ members = [
245248
"crates/bevy_system_reflection",
246249
"crates/bindings/*",
247250
"crates/bevy_mod_scripting_asset",
251+
"crates/bevy_mod_scripting_bindings",
252+
"crates/bevy_mod_scripting_display",
248253
]
249254
resolver = "2"
250255
exclude = ["codegen", "crates/macro_tests", "xtask"]

assets/tests/add_system/added_systems_run_in_parallel.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ digraph {
2626
node_1 [label="bevy_asset::assets::Assets<bevy_asset::assets::LoadedUntypedAsset>::asset_events"];
2727
node_2 [label="bevy_asset::assets::Assets<()>::asset_events"];
2828
node_3 [label="bevy_asset::assets::Assets<bevy_mod_scripting_asset::script_asset::ScriptAsset>::asset_events"];
29-
node_4 [label="bevy_mod_scripting_core::bindings::allocator::garbage_collector"];
29+
node_4 [label="bevy_mod_scripting_bindings::allocator::garbage_collector"];
3030
node_5 [label="script_integration_test_harness::dummy_before_post_update_system"];
3131
node_6 [label="script_integration_test_harness::dummy_post_update_system"];
3232
node_7 [label="on_test_post_update"];

assets/tests/add_system/added_systems_run_in_parallel.rhai

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ digraph {
2525
node_1 [label="bevy_asset::assets::Assets<bevy_asset::assets::LoadedUntypedAsset>::asset_events"];
2626
node_2 [label="bevy_asset::assets::Assets<()>::asset_events"];
2727
node_3 [label="bevy_asset::assets::Assets<bevy_mod_scripting_asset::script_asset::ScriptAsset>::asset_events"];
28-
node_4 [label="bevy_mod_scripting_core::bindings::allocator::garbage_collector"];
28+
node_4 [label="bevy_mod_scripting_bindings::allocator::garbage_collector"];
2929
node_5 [label="script_integration_test_harness::dummy_before_post_update_system"];
3030
node_6 [label="script_integration_test_harness::dummy_post_update_system"];
3131
node_7 [label="on_test_post_update"];

assets/tests/construct/construct_tuple_struct.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ local constructed = construct(type, {
33
_1 = 123
44
})
55

6-
print(constructed:display_value())
7-
8-
assert(constructed._1 == 123, "Value was constructed incorrectly, expected constructed.foo to be 123 but got " .. constructed._1)
6+
assert(constructed._1 == 123,
7+
"Value was constructed incorrectly, expected constructed.foo to be 123 but got " .. constructed._1)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
local vec = Vec3.new(1, 2, 3)
2+
3+
assert(vec:display() == "Vec3 { x: 1.0, y: 2.0, z: 3.0 }",
4+
"Vec3 display incorrect, expected Vec3 { x: 1.0, y: 2.0, z: 3.0 } but got " .. vec:display())
5+
6+
7+
-- multiline
8+
local function strip_alloc_ids(s)
9+
s = s:gsub("ReflectAllocationId%b()", "ReflectAllocationId()")
10+
return s
11+
end
12+
13+
local expected_vec3_debug = [[
14+
ReflectReference {
15+
base: ReflectBaseType {
16+
type_id: TypeId(
17+
"glam::Vec3",
18+
),
19+
base_id: Owned(
20+
ReflectAllocationId(*anything*),
21+
),
22+
},
23+
reflect_path: ParsedPath(
24+
[],
25+
),
26+
}
27+
]]
28+
-- normalize allocation ids before comparison so tests don't fail on runtime-generated ids
29+
do
30+
local actual = vec:debug()
31+
local expected = expected_vec3_debug
32+
actual = strip_alloc_ids(actual)
33+
expected = strip_alloc_ids(expected)
34+
assert_str_eq(actual, expected)
35+
end
36+
37+
38+
local test_resource = world.get_resource(types.TestResource)
39+
40+
local expected_test_resource_debug = [[
41+
ReflectReference {
42+
base: ReflectBaseType {
43+
type_id: TypeId(
44+
"test_utils::test_data::TestResource",
45+
),
46+
base_id: Resource(
47+
ComponentId(
48+
"test_utils::test_data::TestResource",
49+
),
50+
),
51+
},
52+
reflect_path: ParsedPath(
53+
[],
54+
),
55+
}
56+
]]
57+
58+
-- normalize allocation ids before comparison so tests don't fail on runtime-generated ids
59+
do
60+
local actual = test_resource:debug()
61+
local expected = expected_test_resource_debug
62+
actual = strip_alloc_ids(actual)
63+
expected = strip_alloc_ids(expected)
64+
assert_str_eq(actual, expected,
65+
"TestResource debug incorrect, expected " .. expected .. " but got " .. actual)
66+
end
67+
68+
69+
assert_str_eq(test_resource:display(), "TestResource { bytes: [0, 1, 2, 3, 4, 5] }",
70+
"TestResource display incorrect, expected TestResource { bytes: [0, 1, 2, 3, 4, 5] } but got " ..
71+
test_resource:display())

assets/tests/functions/contains_reflect_reference_functions.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ for _, function_ref in pairs(functions) do
1919
table.insert(available_names, function_ref.name)
2020
end
2121

22-
assert(contains(available_names, "display_ref"), "functions should contain display_ref, but got: " .. table.concat(available_names, ", "))
22+
assert(contains(available_names, "display"),
23+
"functions should contain display, but got: " .. table.concat(available_names, ", "))

assets/tests/functions/contains_reflect_reference_functions.rhai

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ for function_ref in functions {
1111
available_names.push(function_ref.name);
1212
}
1313

14-
assert("display_ref" in available_names, "functions should contain display_ref, but got: " + available_names);
14+
assert("display" in available_names, "functions should contain display, but got: " + available_names);

0 commit comments

Comments
 (0)