Expose structs to Lua and write Lua type annotations #28

Merged
SeanOMik merged 15 commits from feat/lua-type-defs into main 2024-10-19 15:17:00 +00:00
4 changed files with 28 additions and 9 deletions
Showing only changes of commit 9e9478966b - Show all commits

View File

@ -3,6 +3,7 @@ use std::{collections::VecDeque, sync::Arc};
use async_std::task::block_on;
use glam::{DVec2, IVec2, UVec2};
use lyra_ecs::Entity;
use lyra_reflect::Reflect;
use rustc_hash::FxHashMap;
use tracing::{debug, error, warn};
use winit::{
@ -22,9 +23,11 @@ use super::WindowOptions;
/// A struct that contains a [`DeviceEvent`](winit::event::DeviceEvent) with its source
/// [`DeviceId`](winit::event::DeviceId).
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Reflect)]
pub struct DeviceEventPair {
#[reflect(skip)]
pub device_src: winit::event::DeviceId,
#[reflect(skip)]
pub event: winit::event::DeviceEvent,
}

View File

@ -132,10 +132,18 @@ pub trait RegisterLuaType {
where
T: Clone + mlua::FromLua + mlua::IntoLua + LuaWrapper + 'static;
/// Registers a type that can be converted to and from lua and adds a lookup entry.
///
/// This is a shortcut for `register_lua_convert` and `add_component_lookup_entry`.
fn register_lua_convert_component<T>(&mut self, name: &str)
where
T: Clone + mlua::FromLua + mlua::IntoLua + LuaWrapper + 'static,
T::Wrap: lyra_ecs::Component + Reflect;
/// Add an entry for a component in the [`TypeLookup`] table.
fn add_component_lookup_entry<T>(&mut self, name: &str)
where
T: lyra_ecs::Component;
}
impl RegisterLuaType for World {
@ -177,10 +185,16 @@ impl RegisterLuaType for World {
T::Wrap: lyra_ecs::Component + Reflect
{
self.register_lua_convert::<T>();
self.add_component_lookup_entry::<T::Wrap>(name);
}
let mut lookup = self.get_resource_or_default::<LuaTableProxyLookup>();
lookup.comp_info_from_name.insert(name.into(), lyra_ecs::ComponentInfo::new::<T::Wrap>());
lookup.typeid_from_name.insert(name.into(), std::any::TypeId::of::<T::Wrap>());
fn add_component_lookup_entry<T>(&mut self, name: &str)
where
T: lyra_ecs::Component
{
let mut lookup = self.get_resource_or_default::<TypeLookup>();
lookup.comp_info_from_name.insert(name.into(), lyra_ecs::ComponentInfo::new::<T>());
lookup.typeid_from_name.insert(name.into(), std::any::TypeId::of::<T>());
}
}

View File

@ -58,9 +58,11 @@ where
}
}
/// A struct that is used for retrieving rust type ids of types that implement `TableProxy`.
/// ECS resource that can be used to lookup types via name.
///
/// You can get the [`TypeId`] of the type via name, or the [`ComponentInfo`].
#[derive(Default)]
pub struct LuaTableProxyLookup {
pub struct TypeLookup {
pub(crate) typeid_from_name: HashMap<String, TypeId>,
pub(crate) comp_info_from_name: HashMap<String, ComponentInfo>,
}

View File

@ -10,7 +10,7 @@ use lyra_resource::ResourceManager;
use mlua::{IntoLua, ObjectLike};
use super::{
reflect_user_data, wrappers::LuaResHandleToComponent, Error, LuaTableProxyLookup, ReflectLuaProxy, ReflectedIterator, FN_NAME_INTERNAL_AS_COMPONENT, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE
reflect_user_data, wrappers::LuaResHandleToComponent, Error, TypeLookup, ReflectLuaProxy, ReflectedIterator, FN_NAME_INTERNAL_AS_COMPONENT, FN_NAME_INTERNAL_REFLECT, FN_NAME_INTERNAL_REFLECT_TYPE
};
impl mlua::FromLua for ScriptEntity {
@ -115,7 +115,7 @@ impl mlua::UserData for ScriptWorldPtr {
mlua::Value::Table(t) => {
let name: String = t.get(mlua::MetaMethod::Type.name())?;
let lookup = world.get_resource::<LuaTableProxyLookup>().ok_or(
let lookup = world.get_resource::<TypeLookup>().ok_or(
mlua::Error::runtime(
"Unable to lookup table proxy, none were ever registered!",
),
@ -195,7 +195,7 @@ impl mlua::UserData for ScriptWorldPtr {
mlua::Value::Table(tbl) => {
let name: String = tbl.get(mlua::MetaMethod::Type.name())?;
let lookup = world.get_resource::<LuaTableProxyLookup>().unwrap();
let lookup = world.get_resource::<TypeLookup>().unwrap();
*lookup.typeid_from_name.get(&name).unwrap()
}
_ => {