basic serialize

This commit is contained in:
Quaternions 2024-02-26 00:44:00 -08:00
parent 9ecc1f09b2
commit 17ec20cc34

View File

@ -58,6 +58,13 @@ impl mlua::UserData for Vector3{
//methods.add_method("area",|_,this,()| Ok(this.length * this.width)); //methods.add_method("area",|_,this,()| Ok(this.length * this.width));
methods.add_meta_function(mlua::MetaMethod::Add,|_,(this,val):(Self,Self)|Ok(Self(this.0+val.0))); methods.add_meta_function(mlua::MetaMethod::Add,|_,(this,val):(Self,Self)|Ok(Self(this.0+val.0)));
methods.add_meta_function(mlua::MetaMethod::ToString,|_,this:Self|
Ok(format!("Vector3.new({},{},{})",
this.0.x,
this.0.y,
this.0.z,
))
);
} }
} }
@ -93,6 +100,22 @@ impl mlua::UserData for CFrame{
//methods.add_meta_method(mlua::MetaMethod::Mul,|_,this,val:&Vector3|Ok(Vector3(this.0.matrix3*val.0+this.0.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))); methods.add_meta_function(mlua::MetaMethod::Mul,|_,(this,val):(Self,Self)|Ok(Self(this.0*val.0)));
methods.add_meta_function(mlua::MetaMethod::ToString,|_,this:Self|
Ok(format!("CFrame.new({},{},{},{},{},{},{},{},{},{},{},{})",
this.0.translation.x,
this.0.translation.y,
this.0.translation.z,
this.0.matrix3.x_axis.x,
this.0.matrix3.y_axis.x,
this.0.matrix3.z_axis.x,
this.0.matrix3.x_axis.y,
this.0.matrix3.y_axis.y,
this.0.matrix3.z_axis.y,
this.0.matrix3.x_axis.z,
this.0.matrix3.y_axis.z,
this.0.matrix3.z_axis.z,
))
);
} }
} }
impl<'lua> mlua::FromLua<'lua> for CFrame{ impl<'lua> mlua::FromLua<'lua> for CFrame{
@ -104,6 +127,49 @@ impl<'lua> mlua::FromLua<'lua> for CFrame{
} }
} }
fn serialize(value:mlua::Value)->mlua::Result<String>{
Ok(match value{
mlua::Value::Nil=>"nil".to_owned(),
mlua::Value::Boolean(boolean)=>format!("{boolean}"),
mlua::Value::Integer(value)=>format!("{value}"),
mlua::Value::Number(value)=>format!("{value}"),
mlua::Value::Vector(value)=>format!("{value}"),
mlua::Value::String(value)=>format!("\"{}\"",value.to_str()?),
mlua::Value::Table(table)=>{
let mut s=String::new();
s.push('{');
let isnt_empty=!table.is_empty();
for pair in table.pairs::<mlua::Value,mlua::Value>(){
let (i,v)=pair?;
s.push('[');
s.push_str(serialize(i)?.as_str());
s.push_str("]=");
s.push_str(serialize(v)?.as_str());
s.push(',');
}
if isnt_empty{
//remove last comma
s.pop();
}
s.push('}');
s
},
mlua::Value::UserData(userdata)=>{
let met=userdata.get_metatable()?;
let f:mlua::Function=met.get("__tostring")?;
let s:mlua::String=f.call(userdata)?;
serialize(mlua::Value::String(s))?
},
mlua::Value::Function(f)=>{
println!("WARN: Serializing function {:?} as nil",f);
"nil".to_owned()
},
mlua::Value::Thread(_)=>unimplemented!(),
mlua::Value::LightUserData(_)=>unimplemented!(),
mlua::Value::Error(_)=>unimplemented!(),
})
}
fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{ fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{
//read the file //read the file
let script={ let script={
@ -202,7 +268,7 @@ fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{
let call_result:mlua::Result<mlua::Table>=lua.load(script).call(()); let call_result:mlua::Result<mlua::Table>=lua.load(script).call(());
match call_result{ match call_result{
Ok(ret)=>{ Ok(ret)=>{
println!("gundata={:?}",ret); println!("gundata={}",serialize(mlua::Value::Table(ret))?);
}, },
Err(e)=>println!("Lua Error: {e}"), Err(e)=>println!("Lua Error: {e}"),
} }