From 1ac594f1957f05ec443829749a27b21d7e788a04 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sun, 25 Feb 2024 23:25:40 -0800 Subject: [PATCH] line 206 --- src/main.rs | 147 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 59 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3236724..366e237 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,77 +25,86 @@ fn main()->Result<(),anyhow::Error>{ } } -struct Vector3(glam::Vec3); +#[derive(Clone,Copy)] +struct Vector3(glam::Vec3A); impl Vector3{ fn new(x:f32,y:f32,z:f32)->Self{ - Self(glam::vec3(x,y,z)) + Self(glam::vec3a(x,y,z)) } } impl mlua::UserData for Vector3{ - fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) { - fields.add_field_method_get("magnitude", |_, this| Ok(this.0.length())); - fields.add_field_method_get("x", |_, this| Ok(this.0.x)); - fields.add_field_method_set("x", |_, this, val| { + fn add_fields<'lua,F: mlua::UserDataFields<'lua,Self>>(fields: &mut F) { + fields.add_field_method_get("magnitude",|_,this| Ok(this.0.length())); + fields.add_field_method_get("x",|_,this| Ok(this.0.x)); + fields.add_field_method_set("x",|_,this,val| { this.0.x = val; Ok(()) }); - fields.add_field_method_get("y", |_, this| Ok(this.0.y)); - fields.add_field_method_set("y", |_, this, val| { + fields.add_field_method_get("y",|_,this| Ok(this.0.y)); + fields.add_field_method_set("y",|_,this,val| { this.0.y = val; Ok(()) }); - fields.add_field_method_get("z", |_, this| Ok(this.0.z)); - fields.add_field_method_set("z", |_, this, val| { + fields.add_field_method_get("z",|_,this| Ok(this.0.z)); + fields.add_field_method_set("z",|_,this,val| { this.0.z = val; Ok(()) }); } - fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) { - //methods.add_method("area", |_, this, ()| Ok(this.length * this.width)); + fn add_methods<'lua,M: mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ + //methods.add_method("area",|_,this,()| Ok(this.length * this.width)); - //methods.add_meta_method(mlua::MetaMethod::Add, |_, this, val:&Self| Ok(Self(this.0+val.0))); + methods.add_meta_function(mlua::MetaMethod::Add,|_,(this,val):(Self,Self)|Ok(Self(this.0+val.0))); } } -impl<'lua> mlua::FromLua<'lua> for &Vector3{ - fn from_lua(value:mlua::prelude::LuaValue<'lua>,lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ - value.as_userdata() - .ok_or(mlua::prelude::LuaError::FromLuaConversionError{from:"LuaValue",to:"UserData",message:None})? - .user_value() - } -} - -struct CFrame{ - translation:glam::Vec3, - rotation:glam::Mat3, -} - -impl CFrame{ - fn new(x:f32,y:f32,z:f32)->Self{ - Self{ - translation:glam::vec3(x,y,z), - rotation:glam::Mat3::IDENTITY, +impl<'lua> mlua::FromLua<'lua> for Vector3{ + fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ + match value { + mlua::Value::UserData(ud) => Ok(*ud.borrow::()?), + _ => unreachable!(), } } } +#[derive(Clone,Copy)] +struct CFrame(glam::Affine3A); + +impl CFrame{ + fn new(x:f32,y:f32,z:f32)->Self{ + Self(glam::Affine3A::from_translation(glam::vec3(x,y,z))) + } + fn angles(x:f32,y:f32,z:f32)->Self{ + Self(glam::Affine3A::from_mat3(glam::Mat3::from_euler(glam::EulerRot::YXZ,y,x,z))) + } +} + impl mlua::UserData for CFrame{ - fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) { - fields.add_field_method_get("p", |ctx, this| Ok(Vector3(this.translation))); + fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){ + //CFrame.p + fields.add_field_method_get("p",|_,this|Ok(Vector3(this.0.translation))); } - fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) { - //methods.add_method("area", |_, this, ()| Ok(this.length * this.width)); + fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ + //methods.add_method("area",|_,this,()|Ok(this.length*this.width)); - methods.add_meta_method(mlua::MetaMethod::Mul, |_, this, val:&Vector3| Ok(Vector3(this.rotation*val.0+this.translation))); + //methods.add_meta_method(mlua::MetaMethod::Mul,|_,this,val:&Vector3|Ok(Vector3(this.0.matrix3*val.0+this.0.translation))); + methods.add_meta_function(mlua::MetaMethod::Mul,|_,(this,val):(Self,Self)|Ok(Self(this.0*val.0))); + } +} +impl<'lua> mlua::FromLua<'lua> for CFrame{ + fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ + match value { + mlua::Value::UserData(ud) => Ok(*ud.borrow::()?), + _ => unreachable!(), + } } } fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{ - //read the file let script={ let mut file=std::fs::File::open(script_path)?; @@ -106,35 +115,55 @@ fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{ let lua=mlua::Lua::new(); + //global environment + let globals=lua.globals(); + //Vector3 - let vector3=lua.create_table()?; - - let new_vec=lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|{ - let v=ctx.create_userdata(Vector3::new(x,y,z)); - Ok(v) - })?; - - vector3.raw_set("new",new_vec)?; - - lua.globals().set("Vector3",vector3)?; + { + let vector3_table=lua.create_table()?; + + //Vector3.new + vector3_table.raw_set("new", + lua.create_function(|ctx,(x,y,z):(f32,f32,f32)| + Ok(ctx.create_userdata(Vector3::new(x,y,z))) + )? + )?; + + globals.set("Vector3",vector3_table)?; + } //CFrame - let cframe=lua.create_table()?; + { + let cframe_table=lua.create_table()?; + + //CFrame.new + cframe_table.raw_set("new", + lua.create_function(|ctx,(x,y,z):(f32,f32,f32)| + Ok(ctx.create_userdata(CFrame::new(x,y,z))) + )? + )?; + + //CFrame.Angles + cframe_table.raw_set("Angles", + lua.create_function(|ctx,(x,y,z):(f32,f32,f32)| + Ok(ctx.create_userdata(CFrame::angles(x,y,z))) + )? + )?; + + globals.set("CFrame",cframe_table)?; + } - let new_cf=lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|{ - let v=ctx.create_userdata(CFrame::new(x,y,z)); - Ok(v) - })?; - - cframe.raw_set("new",new_cf)?; - - lua.globals().set("CFrame",cframe)?; + //script + { + let script_table=lua.create_table()?; + //script.Name + script_table.raw_set("Name","Placeholder")?; + globals.set("script",script_table)?; + } match lua.load(script).exec(){ Ok(())=>(), - Err(e)=>{ - println!("Error: {e}"); - } + Err(e)=>println!("Lua Error: {e}"), } Ok(())