From 1a0bb8f8c45a6f06398583c57cb2ed5c61aa790e Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 17 Sep 2024 17:17:12 -0700 Subject: [PATCH] paste old code --- Cargo.lock | 7 ++++++ Cargo.toml | 1 + src/runner/cframe.rs | 51 +++++++++++++++++++++++++++++++++++++++++++ src/runner/mod.rs | 3 +++ src/runner/runner.rs | 48 ++++++++++++++++++++++++++++++++++++++++ src/runner/vector3.rs | 51 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 161 insertions(+) create mode 100644 src/runner/cframe.rs create mode 100644 src/runner/vector3.rs diff --git a/Cargo.lock b/Cargo.lock index 57cc3ab..be2a2a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,6 +93,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "glam" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "779ae4bf7e8421cf91c0b3b64e7e8b40b862fba4d393f59150042de7c4965a94" + [[package]] name = "lazy_static" version = "1.5.0" @@ -314,6 +320,7 @@ dependencies = [ name = "roblox_emulator" version = "0.1.0" dependencies = [ + "glam", "mlua", "rbx_dom_weak", "rbx_reflection_database", diff --git a/Cargo.toml b/Cargo.toml index 884596f..749bc87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +glam = "0.28.0" mlua = { version = "0.9.9", features = ["luau"] } rbx_dom_weak = { version = "2.7.0", registry = "strafesnet" } rbx_reflection_database = { version = "0.2.10", registry = "strafesnet" } diff --git a/src/runner/cframe.rs b/src/runner/cframe.rs new file mode 100644 index 0000000..690aeb3 --- /dev/null +++ b/src/runner/cframe.rs @@ -0,0 +1,51 @@ +use super::vector3::Vector3; + +#[derive(Clone,Copy)] +pub struct CFrame(pub(crate)glam::Affine3A); + +impl CFrame{ + pub fn new(x:f32,y:f32,z:f32)->Self{ + Self(glam::Affine3A::from_translation(glam::vec3(x,y,z))) + } + pub 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){ + //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)); + + //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::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{ + 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!(), + } + } +} diff --git a/src/runner/mod.rs b/src/runner/mod.rs index 9b6e5d2..905b778 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -1,3 +1,6 @@ mod runner; +mod cframe; +mod vector3; + pub use runner::Runner; diff --git a/src/runner/runner.rs b/src/runner/runner.rs index c03c38e..cfec65a 100644 --- a/src/runner/runner.rs +++ b/src/runner/runner.rs @@ -1,8 +1,51 @@ +use super::vector3::Vector3; +use super::cframe::CFrame; + pub struct Runner{ lua:mlua::Lua, } fn init(lua:&mlua::Lua)->mlua::Result<()>{ + lua.sandbox(true)?; + + //global environment + let globals=lua.globals(); + + //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_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)?; + } + Ok(()) } @@ -15,6 +58,11 @@ impl Runner{ Ok(runner) } pub fn set_script(&self,script:rbx_dom_weak::types::Ref)->mlua::Result<()>{ + //TODO: Instance type + let script_table=self.lua.create_table()?; + //script.Name + script_table.raw_set("Name","Placeholder")?; + self.lua.globals().set("script",script_table)?; Ok(()) } pub fn run(&self,source:String,context:&mut crate::context::Context)->mlua::Result<()>{ diff --git a/src/runner/vector3.rs b/src/runner/vector3.rs new file mode 100644 index 0000000..8c8cb9a --- /dev/null +++ b/src/runner/vector3.rs @@ -0,0 +1,51 @@ +#[derive(Clone,Copy)] +pub struct Vector3(pub(crate)glam::Vec3A); + +impl Vector3{ + pub const fn new(x:f32,y:f32,z:f32)->Self{ + 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| { + this.0.x = val; + Ok(()) + }); + 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| { + 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)); + + 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, + )) + ); + } +} + +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!(), + } + } +}