Skip to content

Commit 1a783f3

Browse files
committed
bring in bms_lua changes
1 parent 44f66c3 commit 1a783f3

File tree

33 files changed

+15203
-14429
lines changed

33 files changed

+15203
-14429
lines changed

crates/bevy_api_gen/src/args.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn default_ignored_types() -> String {
112112
"bevy_reflect::DynamicTuple",
113113
"bevy_reflect::DynamicTupleStruct",
114114
"bevy_reflect::DynamicEnum",
115+
"bevy_reflect::DynamicSet",
115116
"bevy_reflect::OsString", // TODO: once macros allow Vecs for primitives as args remove this from ignored types
116117
]
117118
.join(",")

crates/bevy_api_gen/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ impl ReflectType<'_> {
8383

8484
pub(crate) const DEF_PATHS_FROM_LUA: [&str; 2] = ["value::FromLuaMulti", "mlua::FromLuaMulti"];
8585
pub(crate) const DEF_PATHS_INTO_LUA: [&str; 2] = ["value::IntoLuaMulti", "mlua::IntoLuaMulti"];
86-
pub(crate) const DEF_PATHS_REFLECT: [&str; 2] = ["bevy_reflect::Reflect", "reflect::Reflect"];
86+
pub(crate) const DEF_PATHS_REFLECT: [&str; 2] =
87+
["bevy_reflect::PartialReflect", "reflect::PartialReflect"];
8788
pub(crate) const DEF_PATHS_GET_TYPE_REGISTRATION: [&str; 2] = [
8889
"bevy_reflect::GetTypeRegistration",
8990
"reflect::GetTypeRegistration",

crates/bevy_api_gen/templates/footer.tera

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct {{ "ScriptingPlugin" | prefix_cratename | convert_case(case="upper_ca
3333
impl bevy::app::Plugin for {{ "ScriptingPlugin" | prefix_cratename | convert_case(case="upper_camel")}} {
3434
fn build(&self, app: &mut bevy::prelude::App) {
3535
{% for item in items %}
36-
app.register_proxy::<{{ item.import_path }}>();
36+
app.register_lua_proxy::<{{ item.import_path }}>();
3737
{% endfor %}
3838
app.add_context_initializer::<()>({{ "ContextInitializer" | prefix_cratename | convert_case(case="snake") }});
3939
app.add_documentation_fragment(

crates/bevy_api_gen/templates/header.tera

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::{{crate}}::*;
1212
use bevy_mod_scripting_core::{AddContextInitializer, StoreDocumentation, bindings::ReflectReference};
1313

1414
{% if args.self_is_bms_lua %}
15-
use crate::{bindings::proxy::{LuaReflectRefProxy,LuaReflectRefMutProxy,LuaReflectValProxy,LuaValProxy,IdentityProxy},RegisterLuaProxy, tealr::mlu::mlua::IntoLua};
15+
use crate::{bindings::proxy::{LuaReflectRefProxy,LuaReflectRefMutProxy,LuaReflectValProxy,LuaValProxy,LuaIdentityProxy},RegisterLua, tealr::mlu::mlua::IntoLua};
1616
{% else %}
17-
use bevy_mod_scripting::{lua::bindings::proxy::{LuaReflectRefProxy,LuaReflectRefMutProxy,LuaReflectValProxy,LuaValProxy,IdentityProxy}, RegisterLuaProxy, tealr::mlu::mlua::IntoLua};
17+
use bevy_mod_scripting::{lua::bindings::proxy::{LuaReflectRefProxy,LuaReflectRefMutProxy,LuaReflectValProxy,LuaValProxy,LuaIdentityProxy}, RegisterLua, tealr::mlu::mlua::IntoLua};
1818
{% endif %}

crates/bevy_api_gen/templates/item.tera

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{% set bms_lua_path="bevy_mod_scripting::lua" %}
1212
{% endif %}
1313

14-
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
14+
#[derive(bevy_mod_scripting_derive::LuaProxy)]
1515
#[proxy(
1616
remote="{{ item.import_path }}",
1717
bms_core_path="{{bms_core_path}}",

crates/bevy_api_gen/templates/macros.tera

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% macro vector_index(num_type) %}
22
#[lua(metamethod="Index")]
3-
fn index(self, idx: usize) -> IdentityProxy<{{ num_type }}> {
3+
fn index(self, idx: usize) -> LuaIdentityProxy<{{ num_type }}> {
44
_self[idx - 1]
55
}
66
{% endmacro vector_index %}
@@ -14,15 +14,13 @@ fn index(&mut self, idx: usize, val: {{ num_type }}) -> () {
1414

1515
{% macro matrix_index(col_type, mat_type, bms_core_path) %}
1616
#[lua(metamethod="Index")]
17-
fn index(_self: IdentityProxy<Self>, idx: usize) -> IdentityProxy<{{ col_type | prefix_lua }}> {
17+
fn index(_self: LuaIdentityProxy<Self>, idx: usize) -> LuaIdentityProxy<{{ col_type | prefix_lua }}> {
1818
let mut curr_ref = _self.0.clone();
1919
let def_ref = {{bms_core_path}}::bindings::DeferredReflection{
2020
get: std::sync::Arc::new(|ref_| Err(bevy::reflect::ReflectPathError::InvalidDowncast)),
2121
get_mut: std::sync::Arc::new(move |ref_| {
22-
if ref_.is::<bevy::math::{{ mat_type }}>(){
23-
Ok(ref_.downcast_mut::<bevy::math::{{ mat_type }}>()
24-
.unwrap()
25-
.col_mut(idx - 1))
22+
if let Some(ret) = ref_.try_as_reflect_mut().map(|ret| ret.downcast_mut::<bevy::math::{{ mat_type }}>()).flatten(){
23+
Ok(ret.col_mut(idx - 1))
2624
} else {
2725
Err(bevy::reflect::ReflectPathError::InvalidDowncast)
2826
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "bevy_mod_scripting_derive"
3+
version = "0.6.0"
4+
authors = ["Maksymilian Mozolewski <makspl17@gmail.com>"]
5+
edition = "2021"
6+
license = "MIT OR Apache-2.0"
7+
description = "Necessary functionality for Lua support with bevy_mod_scripting"
8+
repository = "https://github.com/makspll/bevy_mod_scripting"
9+
homepage = "https://github.com/makspll/bevy_mod_scripting"
10+
keywords = ["bevy", "gamedev", "scripting", "rhai"]
11+
categories = ["game-development"]
12+
readme = "readme.md"
13+
14+
[lib]
15+
name = "bevy_mod_scripting_derive"
16+
path = "src/lib.rs"
17+
proc-macro = true
18+
19+
[dependencies]
20+
paste = "1.0.7"
21+
darling = "0.20.3"
22+
syn = { version = "2.0.38", features = ["full", "fold", "extra-traits"] }
23+
quote = "1.0.8"
24+
proc-macro2 = "1.0"
25+
convert_case = "0.5.0"
26+
rustdoc-types = "0.11.0"
27+
serde = { version = "1.0", features = ["derive"] }
28+
serde_derive = "1.0.137"
29+
indexmap = { version = "1.9.1", features = ["serde"] }
30+
strum = { version = "0.24.1", features = ["derive"] }
31+
vec1 = "1.10.1"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# bevy_mod_scripting_lua_derive
2+
3+
This crate is a part of the ["bevy_mod_scripting" workspace](https://github.com/makspll/bevy_mod_scripting).
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use darling::{util::Flag, FromDeriveInput, FromMeta};
2+
use proc_macro2::Ident;
3+
use std::ops::{Deref, DerefMut};
4+
use syn::{spanned::Spanned, visit_mut::VisitMut, Attribute, Field, TraitItemFn, Variant};
5+
6+
#[derive(FromMeta)]
7+
pub struct BMSCorePath(pub syn::Path);
8+
9+
impl Default for BMSCorePath {
10+
fn default() -> Self {
11+
Self(syn::parse_quote!(bevy_mod_scripting::core))
12+
}
13+
}
14+
15+
#[derive(FromMeta)]
16+
pub struct BMSLuaPath(pub syn::Path);
17+
18+
impl Default for BMSLuaPath {
19+
fn default() -> Self {
20+
Self(syn::parse_quote!(bevy_mod_scripting::lua))
21+
}
22+
}
23+
24+
#[derive(FromDeriveInput)]
25+
#[darling(attributes(proxy), forward_attrs(allow, doc, cfg))]
26+
pub struct ProxyInput {
27+
/// The name of the type for which we are generating a proxy (target type)
28+
pub ident: syn::Ident,
29+
/// The visibility of the target type
30+
pub vis: syn::Visibility,
31+
/// The generics on the target type
32+
pub generics: syn::Generics,
33+
/// The attributes on the target type
34+
pub attrs: Vec<Attribute>,
35+
36+
/// The path to the type for which we are generating a proxy if it's a foreign type
37+
pub remote: Option<syn::Path>,
38+
39+
/// if provided will call the function at this path to get the world callback access. Normally this is retrieved using a global variable.
40+
pub get_world_callback_access_fn: Option<syn::Path>,
41+
42+
/// If set will use the given path as the type for the proxy instead of generating a new one
43+
/// Only used for the special world proxies, probably not useful for anything else, the macro assumes we have an inner ReflectReference in the wrapper
44+
pub proxy_as_type: Option<syn::Path>,
45+
46+
/// The path to the bevy_mod_scripting_core crate
47+
#[darling(default)]
48+
pub bms_core_path: BMSCorePath,
49+
/// The path to the bevy_mod_scripting_lua crate
50+
#[darling(default)]
51+
pub bms_lua_path: BMSLuaPath,
52+
53+
/// The name to use for the proxy type, if not provided the language derive macro
54+
/// will generate one using a standard prefix.
55+
#[darling(rename = "name")]
56+
pub proxy_name: Option<Ident>,
57+
58+
/// The body of the type for which we are generating a proxy
59+
pub data: darling::ast::Data<Variant, Field>,
60+
61+
/// A list of multi-lang function definitions to be generated on the proxy type
62+
#[darling(default)]
63+
pub functions: TraitItemFnsWrapper,
64+
}
65+
66+
#[derive(Default)]
67+
pub struct TraitItemFnsWrapper(pub Vec<TraitItemFn>);
68+
69+
impl FromMeta for TraitItemFnsWrapper {
70+
fn from_string(value: &str) -> darling::Result<Self> {
71+
let token_stream: proc_macro2::TokenStream = value.parse().map_err(syn::Error::from)?;
72+
let trait_items_vec = vec![syn::parse2(token_stream)?];
73+
Ok(TraitItemFnsWrapper(trait_items_vec))
74+
}
75+
76+
fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
77+
Ok(TraitItemFnsWrapper(
78+
items
79+
.iter()
80+
.map(Self::from_nested_meta)
81+
.collect::<Result<Vec<_>, _>>()?
82+
.into_iter()
83+
.flat_map(|v| v.0.into_iter())
84+
.collect::<Vec<_>>(),
85+
))
86+
}
87+
}
88+
89+
impl Deref for TraitItemFnsWrapper {
90+
type Target = Vec<TraitItemFn>;
91+
92+
fn deref(&self) -> &Self::Target {
93+
&self.0
94+
}
95+
}
96+
impl DerefMut for TraitItemFnsWrapper {
97+
fn deref_mut(&mut self) -> &mut Self::Target {
98+
&mut self.0
99+
}
100+
}
101+
102+
/// Replaces every occurence of an identifier with
103+
/// the given string while preserving the original span
104+
pub struct IdentifierRenamingVisitor<'a> {
105+
pub target: &'a str,
106+
pub replacement: &'a str,
107+
}
108+
109+
impl VisitMut for IdentifierRenamingVisitor<'_> {
110+
fn visit_ident_mut(&mut self, i: &mut Ident) {
111+
if *i == self.target {
112+
*i = Ident::new(self.replacement, i.span());
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)