paste old code

This commit is contained in:
Quaternions 2024-09-17 17:17:12 -07:00
parent 362b5709ff
commit 1a0bb8f8c4
6 changed files with 161 additions and 0 deletions

7
Cargo.lock generated
View File

@ -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",

View File

@ -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" }

51
src/runner/cframe.rs Normal file
View File

@ -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<Self>{
match value {
mlua::Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
_ => unreachable!(),
}
}
}

View File

@ -1,3 +1,6 @@
mod runner;
mod cframe;
mod vector3;
pub use runner::Runner;

View File

@ -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<()>{

51
src/runner/vector3.rs Normal file
View File

@ -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<Self>{
match value {
mlua::Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
_ => unreachable!(),
}
}
}