Use full paths for items used in wrapper proc-macros
This commit is contained in:
parent
4fd4bbe574
commit
b2b19c9ccc
12 changed files with 81 additions and 73 deletions
crates/lyra-scripting
|
@ -10,6 +10,7 @@ proc-macro = true
|
|||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0.70"
|
||||
proc-macro-crate = "3.2.0"
|
||||
quote = "1.0.33"
|
||||
syn = "2.0.41"
|
||||
paste = "1.0.14"
|
||||
paste = "1.0.14"
|
||||
|
|
|
@ -130,7 +130,8 @@ impl syn::parse::Parse for HandleWrapUsage {
|
|||
|
||||
pub(crate) fn lua_wrap_handle_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as HandleWrapUsage);
|
||||
|
||||
let crate_path = crate::script_crate_path();
|
||||
|
||||
let handle_path = &input.type_path;
|
||||
let handle_name = &input.type_path.segments.last().unwrap().ident;
|
||||
|
||||
|
@ -170,19 +171,19 @@ pub(crate) fn lua_wrap_handle_impl(input: proc_macro::TokenStream) -> proc_macro
|
|||
});
|
||||
|
||||
quote! {
|
||||
#[derive(Clone, Reflect)]
|
||||
pub struct #wrapper_name(pub ResHandle<#handle_path>);
|
||||
#[derive(Clone, lyra_engine::reflect::Reflect)]
|
||||
pub struct #wrapper_name(pub lyra_engine::assets::ResHandle<#handle_path>);
|
||||
|
||||
impl std::ops::Deref for #wrapper_name {
|
||||
type Target = ResHandle<#handle_path>;
|
||||
type Target = lyra_engine::assets::ResHandle<#handle_path>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ResHandle<#handle_path>> for #wrapper_name {
|
||||
fn from(value: ResHandle<#handle_path>) -> Self {
|
||||
impl From<lyra_engine::assets::ResHandle<#handle_path>> for #wrapper_name {
|
||||
fn from(value: lyra_engine::assets::ResHandle<#handle_path>) -> Self {
|
||||
#wrapper_name(value)
|
||||
}
|
||||
}
|
||||
|
@ -227,11 +228,11 @@ pub(crate) fn lua_wrap_handle_impl(input: proc_macro::TokenStream) -> proc_macro
|
|||
Ok(())
|
||||
});
|
||||
|
||||
methods.add_function(FN_NAME_INTERNAL_REFLECT_TYPE, |_, ()| {
|
||||
Ok(ScriptBorrow::from_component::<ResHandle<#handle_path>>(None))
|
||||
methods.add_function(#crate_path::lua::FN_NAME_INTERNAL_REFLECT_TYPE, |_, ()| {
|
||||
Ok(#crate_path::ScriptBorrow::from_component::<lyra_engine::assets::ResHandle<#handle_path>>(None))
|
||||
});
|
||||
methods.add_method(FN_NAME_INTERNAL_REFLECT, |_, this, ()| {
|
||||
Ok(ScriptBorrow::from_component(Some(this.0.clone())))
|
||||
methods.add_method(#crate_path::lua::FN_NAME_INTERNAL_REFLECT, |_, this, ()| {
|
||||
Ok(#crate_path::ScriptBorrow::from_component(Some(this.0.clone())))
|
||||
});
|
||||
|
||||
#extras
|
||||
|
@ -249,11 +250,11 @@ pub(crate) fn lua_wrap_handle_impl(input: proc_macro::TokenStream) -> proc_macro
|
|||
}
|
||||
}
|
||||
|
||||
impl LuaWrapper for #wrapper_name {
|
||||
type Wrap = ResHandle<#handle_path>;
|
||||
impl #crate_path::lua::LuaWrapper for #wrapper_name {
|
||||
type Wrap = lyra_engine::assets::ResHandle<#handle_path>;
|
||||
|
||||
fn wrapped_type_id() -> std::any::TypeId {
|
||||
std::any::TypeId::of::<ResHandle<#handle_path>>()
|
||||
std::any::TypeId::of::<lyra_engine::assets::ResHandle<#handle_path>>()
|
||||
}
|
||||
|
||||
fn into_wrapped(self) -> Self::Wrap {
|
||||
|
@ -261,10 +262,10 @@ pub(crate) fn lua_wrap_handle_impl(input: proc_macro::TokenStream) -> proc_macro
|
|||
}
|
||||
}
|
||||
|
||||
impl LuaHandleWrapper for #wrapper_name {
|
||||
impl #crate_path::lua::LuaHandleWrapper for #wrapper_name {
|
||||
type ResourceType = #handle_path;
|
||||
|
||||
fn from_handle(handle: ResHandle<Self::ResourceType>) -> Self {
|
||||
fn from_handle(handle: lyra_engine::assets::ResHandle<Self::ResourceType>) -> Self {
|
||||
Self(handle)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
use std::cell::LazyCell;
|
||||
|
||||
use handle_macro::lua_wrap_handle_impl;
|
||||
use lua_macro::wrap_lua_struct_impl;
|
||||
use proc_macro2::Span;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, Token};
|
||||
|
||||
|
@ -16,6 +19,22 @@ mod handle_macro;
|
|||
pub(crate) const FN_NAME_INTERNAL_REFLECT_TYPE: &str = "__lyra_internal_reflect_type";
|
||||
pub(crate) const FN_NAME_INTERNAL_REFLECT: &str = "__lyra_internal_reflect";
|
||||
|
||||
fn script_crate_path() -> proc_macro2::TokenStream {
|
||||
let name =
|
||||
proc_macro_crate::crate_name("lyra-scripting").expect("lyra-scripting is not a depdency");
|
||||
|
||||
match name {
|
||||
proc_macro_crate::FoundCrate::Itself => quote!(crate),
|
||||
proc_macro_crate::FoundCrate::Name(n) => {
|
||||
let ident = syn::Ident::new(&n, Span::call_site());
|
||||
quote!(#ident)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) const CRATE_PATH: LazyCell<proc_macro2::TokenStream> =
|
||||
LazyCell::new(|| script_crate_path());
|
||||
|
||||
#[proc_macro]
|
||||
pub fn wrap_lua_struct(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
wrap_lua_struct_impl(input)
|
||||
|
|
|
@ -4,10 +4,7 @@ use syn::{
|
|||
braced, parenthesized, parse_macro_input, punctuated::Punctuated, token, Ident, Path, Token,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
field::{Field, FieldType},
|
||||
FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE,
|
||||
};
|
||||
use crate::field::{Field, FieldType};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum SkipType {
|
||||
|
@ -422,6 +419,7 @@ impl syn::parse::Parse for WrapUsage {
|
|||
/// Creates a wrapper type for a VecN from the engine math library.
|
||||
pub fn wrap_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as WrapUsage);
|
||||
let crate_path = crate::CRATE_PATH.clone();
|
||||
|
||||
let path: Path = input.type_path;
|
||||
let type_name = &path
|
||||
|
@ -478,12 +476,12 @@ pub fn wrap_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::Token
|
|||
quote!()
|
||||
} else {
|
||||
quote! {
|
||||
methods.add_method(#FN_NAME_INTERNAL_REFLECT, |_, this, ()| {
|
||||
Ok(crate::ScriptBorrow::from_component::<#path>(Some(this.0.clone())))
|
||||
methods.add_method(#crate_path::lua::FN_NAME_INTERNAL_REFLECT, |_, this, ()| {
|
||||
Ok(#crate_path::ScriptBorrow::from_component::<#path>(Some(this.0.clone())))
|
||||
});
|
||||
|
||||
methods.add_function(#FN_NAME_INTERNAL_REFLECT_TYPE, |_, ()| {
|
||||
Ok(crate::ScriptBorrow::from_component::<#path>(None))
|
||||
methods.add_function(#crate_path::lua::FN_NAME_INTERNAL_REFLECT_TYPE, |_, ()| {
|
||||
Ok(#crate_path::ScriptBorrow::from_component::<#path>(None))
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -586,7 +584,7 @@ pub fn wrap_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::Token
|
|||
});
|
||||
|
||||
proc_macro::TokenStream::from(quote! {
|
||||
#[derive(Clone, lyra_reflect::Reflect, #(#derive_idents_iter),*)]
|
||||
#[derive(Clone, lyra_engine::reflect::Reflect, #(#derive_idents_iter),*)]
|
||||
pub struct #wrapper_typename(#[reflect(skip)] pub(crate) #path);
|
||||
|
||||
impl std::ops::Deref for #wrapper_typename {
|
||||
|
|
|
@ -117,6 +117,7 @@ fn wrapper_creation(
|
|||
}
|
||||
|
||||
fn get_reflect_lua_functions(
|
||||
crate_path: &proc_macro2::TokenStream,
|
||||
ty: &ReflectType,
|
||||
type_path: &syn::Path,
|
||||
set_data: bool,
|
||||
|
@ -130,12 +131,12 @@ fn get_reflect_lua_functions(
|
|||
match ty {
|
||||
ReflectType::Component => {
|
||||
quote! {
|
||||
Ok(ScriptBorrow::from_component::<#type_path>(#data))
|
||||
Ok(#crate_path::ScriptBorrow::from_component::<#type_path>(#data))
|
||||
}
|
||||
}
|
||||
ReflectType::Resource => {
|
||||
quote! {
|
||||
Ok(ScriptBorrow::from_component::<#type_path>(#data))
|
||||
Ok(#crate_path::ScriptBorrow::from_component::<#type_path>(#data))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -301,6 +302,7 @@ impl syn::parse::Parse for IntoLuaUsage {
|
|||
|
||||
pub fn to_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as IntoLuaUsage);
|
||||
let crate_path = crate::CRATE_PATH.clone();
|
||||
|
||||
// unwrap is fine since `Some` is ensured in parse impl
|
||||
let reflect_type = input.reflection_type.as_ref().unwrap();
|
||||
|
@ -335,8 +337,9 @@ pub fn to_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::TokenSt
|
|||
input.create.as_ref(),
|
||||
&input.fields,
|
||||
);
|
||||
let reflect_fn = get_reflect_lua_functions(reflect_type, &input.type_path, true);
|
||||
let reflect_type_fn = get_reflect_lua_functions(reflect_type, &input.type_path, false);
|
||||
let reflect_fn = get_reflect_lua_functions(&crate_path, reflect_type, &input.type_path, true);
|
||||
let reflect_type_fn =
|
||||
get_reflect_lua_functions(&crate_path, reflect_type, &input.type_path, false);
|
||||
|
||||
quote! {
|
||||
#[derive(Clone, #(#derives_iter),*)]
|
||||
|
@ -375,6 +378,8 @@ pub fn to_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::TokenSt
|
|||
|
||||
impl mlua::IntoLua for #wrapper {
|
||||
fn into_lua(self, lua: &mlua::Lua) -> mlua::Result<mlua::Value> {
|
||||
use #crate_path::lua::LuaWrapper;
|
||||
|
||||
let table = lua.create_table()?;
|
||||
#(
|
||||
#field_setters_iter
|
||||
|
@ -400,7 +405,7 @@ pub fn to_lua_struct_impl(input: proc_macro::TokenStream) -> proc_macro::TokenSt
|
|||
}
|
||||
}
|
||||
|
||||
impl LuaWrapper for #wrapper {
|
||||
impl #crate_path::lua::LuaWrapper for #wrapper {
|
||||
type Wrap = #type_path;
|
||||
|
||||
#[inline(always)]
|
||||
|
|
|
@ -21,11 +21,12 @@ use lyra_game::game::App;
|
|||
// required for some proc macros :(
|
||||
#[allow(unused_imports)]
|
||||
pub(crate) mod lyra_engine {
|
||||
pub use lyra_ecs as ecs;
|
||||
pub use lyra_reflect as reflect;
|
||||
//pub use lyra_ecs as ecs;
|
||||
//pub use lyra_reflect as reflect;
|
||||
pub use lyra_game::*;
|
||||
}
|
||||
|
||||
use lyra_reflect::{ReflectedComponent, Reflect, FromType, ReflectedResource};
|
||||
use lyra_reflect::{FromType, Reflect, ReflectedComponent, ReflectedResource};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum ReflectBranch {
|
||||
|
@ -34,20 +35,20 @@ pub enum ReflectBranch {
|
|||
}
|
||||
|
||||
impl ReflectBranch {
|
||||
/// Gets self as a [`ReflectedComponent`].
|
||||
///
|
||||
/// Gets self as a [`ReflectedComponent`].
|
||||
///
|
||||
/// # Panics
|
||||
/// If `self` is not a variant of [`ReflectBranch::Component`].
|
||||
#[deprecated(note = "use ReflectBranch::as_component instead")]
|
||||
pub fn as_component_unchecked(&self) -> &ReflectedComponent {
|
||||
match self {
|
||||
ReflectBranch::Component(c) => c,
|
||||
_ => panic!("`self` is not an instance of `ReflectBranch::Component`")
|
||||
_ => panic!("`self` is not an instance of `ReflectBranch::Component`"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets self as a [`ReflectedComponent`].
|
||||
///
|
||||
/// Gets self as a [`ReflectedComponent`].
|
||||
///
|
||||
/// Returns `None` if `self` is not a component.
|
||||
pub fn as_component(&self) -> Option<&ReflectedComponent> {
|
||||
match self {
|
||||
|
@ -61,14 +62,14 @@ impl ReflectBranch {
|
|||
matches!(self, ReflectBranch::Component(_))
|
||||
}
|
||||
|
||||
/// Gets self as a [`ReflectedResource`].
|
||||
///
|
||||
/// Gets self as a [`ReflectedResource`].
|
||||
///
|
||||
/// # Panics
|
||||
/// If `self` is not a variant of [`ReflectBranch::Resource`].
|
||||
pub fn as_resource_unchecked(&self) -> &ReflectedResource {
|
||||
match self {
|
||||
ReflectBranch::Resource(v) => v,
|
||||
_ => panic!("`self` is not an instance of `ReflectBranch::Component`")
|
||||
_ => panic!("`self` is not an instance of `ReflectBranch::Component`"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,7 +77,7 @@ impl ReflectBranch {
|
|||
pub fn as_resource(&self) -> Option<&ReflectedResource> {
|
||||
match self {
|
||||
ReflectBranch::Resource(v) => Some(v),
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,12 +115,14 @@ impl ScriptBorrow {
|
|||
/// Creates a ScriptBorrow from a Component
|
||||
pub fn from_component<T>(data: Option<T>) -> Self
|
||||
where
|
||||
T: Reflect + Component + 'static
|
||||
T: Reflect + Component + 'static,
|
||||
{
|
||||
let data = data.map(|d| Box::new(d) as Box<(dyn Reflect + 'static)>);
|
||||
|
||||
Self {
|
||||
reflect_branch: ReflectBranch::Component(<ReflectedComponent as FromType<T>>::from_type()),
|
||||
reflect_branch: ReflectBranch::Component(
|
||||
<ReflectedComponent as FromType<T>>::from_type(),
|
||||
),
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
@ -127,7 +130,7 @@ impl ScriptBorrow {
|
|||
/// Creates a ScriptBorrow from a Resource.
|
||||
pub fn from_resource<T>(data: Option<T>) -> Self
|
||||
where
|
||||
T: Reflect + ResourceObject + 'static
|
||||
T: Reflect + ResourceObject + 'static,
|
||||
{
|
||||
let data = data.map(|d| Box::new(d) as Box<(dyn Reflect + 'static)>);
|
||||
|
||||
|
@ -151,11 +154,11 @@ impl GameScriptExt for App {
|
|||
fn add_script_api_provider<T, P>(&mut self, mut provider: P)
|
||||
where
|
||||
T: ScriptHost,
|
||||
P: ScriptApiProvider<ScriptContext = T::ScriptContext> + 'static
|
||||
P: ScriptApiProvider<ScriptContext = T::ScriptContext> + 'static,
|
||||
{
|
||||
let world = &mut self.world;
|
||||
provider.prepare_world(world);
|
||||
let mut providers = world.get_resource_mut::<ScriptApiProviders<T>>().unwrap();
|
||||
providers.add_provider(provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
use lyra_ecs::ResourceObject;
|
||||
use lyra_game::sprite::ActiveAtlasAnimation;
|
||||
use lyra_reflect::Reflect;
|
||||
use sprite::{
|
||||
LuaActiveAtlasAnimation, LuaAtlasAnimations, LuaAtlasSprite, LuaSprite, LuaSpriteAnimation,
|
||||
LuaTextureAtlasHandle, LuaTile, LuaTileMap, LuaTileMapPos,
|
||||
};
|
||||
use tracing::debug;
|
||||
|
||||
use crate::{
|
||||
lua::{
|
||||
|
|
|
@ -2,17 +2,11 @@ use lyra_game::{
|
|||
gltf::{AlphaMode, Gltf, Material, Mesh, MeshIndices, PbrGlossiness, Specular},
|
||||
scene::SceneGraph,
|
||||
};
|
||||
use lyra_reflect::Reflect;
|
||||
use lyra_resource::{FilterMode, ResHandle, Texture, WrappingMode};
|
||||
use lyra_resource::{FilterMode, Texture, WrappingMode};
|
||||
use lyra_scripting_derive::{lua_wrap_handle, wrap_lua_struct};
|
||||
|
||||
use crate as lyra_scripting;
|
||||
use crate::{
|
||||
lua::{LuaWrapper, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE},
|
||||
lyra_engine, ScriptBorrow,
|
||||
};
|
||||
|
||||
use super::LuaHandleWrapper;
|
||||
use crate::lyra_engine;
|
||||
|
||||
fn filter_mode_to_str(fm: FilterMode) -> &'static str {
|
||||
match fm {
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use crate::{lua::LuaWrapper, ScriptBorrow};
|
||||
use lyra_game::scene::FreeFly3dCamera;
|
||||
use lyra_scripting_derive::to_lua_convert;
|
||||
|
||||
|
|
|
@ -8,10 +8,7 @@ use mlua::FromLuaMulti;
|
|||
|
||||
use super::LuaTextureAtlasHandle;
|
||||
use crate as lyra_scripting;
|
||||
use crate::{
|
||||
lua::{wrappers::LuaRect, LuaWrapper},
|
||||
lyra_engine, ScriptBorrow,
|
||||
};
|
||||
use crate::{lua::wrappers::LuaRect, lyra_engine};
|
||||
|
||||
// TODO: create SpriteAnimation::from_atlas in Lua
|
||||
to_lua_convert!(
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
use lyra_game::sprite::{Pivot, Sprite};
|
||||
use lyra_game::sprite::Sprite;
|
||||
use lyra_scripting_derive::to_lua_convert;
|
||||
|
||||
use super::{LuaImageHandle, LuaVec2, LuaVec3};
|
||||
use crate::lua::LuaWrapper;
|
||||
use crate::ScriptBorrow;
|
||||
use super::{LuaImageHandle, LuaVec3};
|
||||
|
||||
mod tilemap;
|
||||
pub use tilemap::*;
|
||||
|
|
|
@ -6,14 +6,9 @@ use super::LuaImageHandle;
|
|||
use crate as lyra_scripting;
|
||||
use crate::lua::wrappers::LuaVec2;
|
||||
use crate::{
|
||||
lua::{
|
||||
wrappers::{helpers, LuaRect},
|
||||
LuaHandleWrapper, LuaWrapper, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE,
|
||||
},
|
||||
lyra_engine, ScriptBorrow, ScriptEntity,
|
||||
lua::wrappers::{helpers, LuaRect},
|
||||
lyra_engine, ScriptEntity,
|
||||
};
|
||||
use lyra_reflect::Reflect;
|
||||
use lyra_resource::ResHandle;
|
||||
|
||||
to_lua_convert!(
|
||||
// Struct that is being wrapped
|
||||
|
|
Loading…
Add table
Reference in a new issue