This commit is contained in:
Quaternions 2024-02-25 23:25:40 -08:00
parent ef6ae30394
commit 1ac594f195

View File

@ -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<Self>{
value.as_userdata()
.ok_or(mlua::prelude::LuaError::FromLuaConversionError{from:"LuaValue",to:"UserData",message:None})?
.user_value()
impl<'lua> mlua::FromLua<'lua> for Vector3{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value {
mlua::Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
_ => unreachable!(),
}
}
}
struct CFrame{
translation:glam::Vec3,
rotation:glam::Mat3,
}
#[derive(Clone,Copy)]
struct CFrame(glam::Affine3A);
impl CFrame{
fn new(x:f32,y:f32,z:f32)->Self{
Self{
translation:glam::vec3(x,y,z),
rotation:glam::Mat3::IDENTITY,
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<Self>{
match value {
mlua::Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
_ => 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 vector3_table=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.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)))
)?
)?;
vector3.raw_set("new",new_vec)?;
lua.globals().set("Vector3",vector3)?;
globals.set("Vector3",vector3_table)?;
}
//CFrame
let cframe=lua.create_table()?;
{
let cframe_table=lua.create_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.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.raw_set("new",new_cf)?;
//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)))
)?
)?;
lua.globals().set("CFrame",cframe)?;
globals.set("CFrame",cframe_table)?;
}
//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(())