Compare commits
72 Commits
raw-pointe
...
instance
Author | SHA1 | Date | |
---|---|---|---|
73b7b50c43 | |||
fbe4b56c62 | |||
a8236265fd | |||
d84c48f336 | |||
ae0b46e217 | |||
56f6738028 | |||
29c8cd94df | |||
a9a4ed2c46 | |||
4988eeed65 | |||
ff9c122470 | |||
eccf7243c4 | |||
b457da64a1 | |||
ed3f9f9b30 | |||
136b360590 | |||
7b8f091ab3 | |||
3a716373db | |||
ef78778708 | |||
f4dc80713f | |||
9030646f39 | |||
ba9918f2d5 | |||
acbb1c8478 | |||
96c8b1035a | |||
67223efa26 | |||
0148476648 | |||
cb5b842276 | |||
e4114ab2cc | |||
9b077d6db7 | |||
d496a2dac5 | |||
e110365325 | |||
c0323d12df | |||
7774fe9eb4 | |||
3fabb6dbae | |||
7d67e762b1 | |||
db8e22c34d | |||
a88debe9f1 | |||
317400548d | |||
c12ad7df0f | |||
00a5d71b2b | |||
d272ac242b | |||
e67f0f0889 | |||
40655cdf44 | |||
6c12bc2bf6 | |||
5a1df16bd9 | |||
9d219004f4 | |||
6ec0d61b3d | |||
748f544a4b | |||
7403888348 | |||
c06ba65662 | |||
fb38f1076d | |||
f5e9287798 | |||
6b66009c44 | |||
a4e2137463 | |||
3bb496f87b | |||
9d56773343 | |||
6a3f0ca9aa | |||
4a939e74d1 | |||
72874d450f | |||
7780e1a3f1 | |||
0e4ea2e722 | |||
8aa5aa2c5a | |||
a1626b464e | |||
0ea8551781 | |||
b0df1fb3bd | |||
66d9279445 | |||
b765f4cb21 | |||
4f65c35407 | |||
90609f4ad4 | |||
ff76f3c865 | |||
ededfd9e55 | |||
e2c74b8dfe | |||
701897b00d | |||
930ac1c331 |
55
Cargo.lock
generated
55
Cargo.lock
generated
@ -95,9 +95,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "glam"
|
||||
version = "0.28.0"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "779ae4bf7e8421cf91c0b3b64e7e8b40b862fba4d393f59150042de7c4965a94"
|
||||
checksum = "c28091a37a5d09b555cb6628fd954da299b536433834f5b8e59eba78e0cbbf8a"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
@ -183,6 +183,48 @@ version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
|
||||
[[package]]
|
||||
name = "phf"
|
||||
version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc"
|
||||
dependencies = [
|
||||
"phf_macros",
|
||||
"phf_shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "phf_generator"
|
||||
version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0"
|
||||
dependencies = [
|
||||
"phf_shared",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "phf_macros"
|
||||
version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b"
|
||||
dependencies = [
|
||||
"phf_generator",
|
||||
"phf_shared",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "phf_shared"
|
||||
version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b"
|
||||
dependencies = [
|
||||
"siphasher",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.30"
|
||||
@ -318,10 +360,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "roblox_emulator"
|
||||
version = "0.1.0"
|
||||
version = "0.4.1"
|
||||
dependencies = [
|
||||
"glam",
|
||||
"mlua",
|
||||
"phf",
|
||||
"rbx_dom_weak",
|
||||
"rbx_reflection",
|
||||
"rbx_reflection_database",
|
||||
@ -360,6 +403,12 @@ version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "siphasher"
|
||||
version = "0.3.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.77"
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "roblox_emulator"
|
||||
version = "0.1.0"
|
||||
version = "0.4.1"
|
||||
edition = "2021"
|
||||
repository = "https://git.itzana.me/StrafesNET/roblox_emulator"
|
||||
license = "MIT OR Apache-2.0"
|
||||
@ -8,8 +8,9 @@ description = "Run embedded Luau scripts which manipulate the DOM."
|
||||
authors = ["Rhys Lloyd <krakow20@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
glam = "0.28.0"
|
||||
glam = "0.29.0"
|
||||
mlua = { version = "0.9.9", features = ["luau"] }
|
||||
phf = { version = "0.11.2", features = ["macros"] }
|
||||
rbx_dom_weak = { version = "2.7.0", registry = "strafesnet" }
|
||||
rbx_reflection = { version = "4.7.0", registry = "strafesnet" }
|
||||
rbx_reflection_database = { version = "0.2.10", registry = "strafesnet" }
|
||||
|
@ -1,4 +1,4 @@
|
||||
use rbx_dom_weak::{types::Ref,WeakDom};
|
||||
use rbx_dom_weak::{types::Ref,InstanceBuilder,WeakDom};
|
||||
|
||||
pub fn class_is_a(class:&str,superclass:&str)->bool{
|
||||
class==superclass
|
||||
@ -16,8 +16,22 @@ pub struct Context{
|
||||
}
|
||||
|
||||
impl Context{
|
||||
pub const fn new(dom:WeakDom)->Context{
|
||||
Context{dom}
|
||||
pub const fn new(dom:WeakDom)->Self{
|
||||
Self{dom}
|
||||
}
|
||||
pub fn script_singleton(source:String)->(Context,crate::runner::instance::Instance,Services){
|
||||
let script=InstanceBuilder::new("Script")
|
||||
.with_property("Source",rbx_types::Variant::String(source));
|
||||
let script_ref=script.referent();
|
||||
let mut context=Self::new(WeakDom::new(
|
||||
InstanceBuilder::new("DataModel")
|
||||
.with_child(script)
|
||||
));
|
||||
let services=context.convert_into_place();
|
||||
(context,crate::runner::instance::Instance::new(script_ref),services)
|
||||
}
|
||||
pub fn from_ref(dom:&WeakDom)->&Context{
|
||||
unsafe{&*(dom as *const WeakDom as *const Context)}
|
||||
}
|
||||
pub fn from_mut(dom:&mut WeakDom)->&mut Context{
|
||||
unsafe{&mut *(dom as *mut WeakDom as *mut Context)}
|
||||
@ -28,7 +42,43 @@ impl Context{
|
||||
class_is_a(instance.class.as_ref(),superclass)
|
||||
).map(|instance|instance.referent())
|
||||
}
|
||||
pub fn scripts(&self)->Vec<crate::script::Script>{
|
||||
self.superclass_iter("LuaSourceContainer").map(crate::script::Script::new).collect()
|
||||
pub fn scripts(&self)->Vec<crate::runner::instance::Instance>{
|
||||
self.superclass_iter("LuaSourceContainer").map(crate::runner::instance::Instance::new).collect()
|
||||
}
|
||||
|
||||
pub fn find_services(&self)->Option<Services>{
|
||||
Some(Services{
|
||||
workspace:*self.dom.root().children().iter().find(|&&r|
|
||||
self.dom.get_by_ref(r).is_some_and(|instance|instance.class=="Workspace")
|
||||
)?,
|
||||
game:self.dom.root_ref(),
|
||||
})
|
||||
}
|
||||
pub fn convert_into_place(&mut self)->Services{
|
||||
//snapshot root instances
|
||||
let children=self.dom.root().children().to_owned();
|
||||
|
||||
//insert services
|
||||
let game=self.dom.root_ref();
|
||||
let workspace=self.dom.insert(game,
|
||||
InstanceBuilder::new("Workspace")
|
||||
.with_child(InstanceBuilder::new("Terrain"))
|
||||
);
|
||||
self.dom.insert(game,InstanceBuilder::new("Lighting"));
|
||||
|
||||
//transfer original root instances into workspace
|
||||
for instance in children{
|
||||
self.dom.transfer_within(instance,workspace);
|
||||
}
|
||||
|
||||
Services{
|
||||
game,
|
||||
workspace,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Services{
|
||||
pub game:Ref,
|
||||
pub workspace:Ref,
|
||||
}
|
||||
|
@ -1,9 +1,5 @@
|
||||
pub mod script;
|
||||
pub mod context;
|
||||
|
||||
pub mod runner;
|
||||
|
||||
pub type Result<T>=core::result::Result<T,script::Error>;
|
||||
pub mod context;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
@ -4,7 +4,22 @@ use super::vector3::Vector3;
|
||||
pub struct CFrame(pub(crate)glam::Affine3A);
|
||||
|
||||
impl CFrame{
|
||||
pub fn new(x:f32,y:f32,z:f32)->Self{
|
||||
pub fn new(
|
||||
x:f32,y:f32,z:f32,
|
||||
xx:f32,yx:f32,zx:f32,
|
||||
xy:f32,yy:f32,zy:f32,
|
||||
xz:f32,yz:f32,zz:f32,
|
||||
)->Self{
|
||||
Self(glam::Affine3A::from_mat3_translation(
|
||||
glam::mat3(
|
||||
glam::vec3(xx,yx,zx),
|
||||
glam::vec3(xy,yy,zy),
|
||||
glam::vec3(xz,yz,zz)
|
||||
),
|
||||
glam::vec3(x,y,z)
|
||||
))
|
||||
}
|
||||
pub fn point(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{
|
||||
@ -12,6 +27,104 @@ impl CFrame{
|
||||
}
|
||||
}
|
||||
|
||||
fn vec3_to_glam(v:glam::Vec3A)->rbx_types::Vector3{
|
||||
rbx_types::Vector3::new(v.x,v.y,v.z)
|
||||
}
|
||||
fn vec3_from_glam(v:rbx_types::Vector3)->glam::Vec3A{
|
||||
glam::vec3a(v.x,v.y,v.z)
|
||||
}
|
||||
|
||||
impl Into<rbx_types::CFrame> for CFrame{
|
||||
fn into(self)->rbx_types::CFrame{
|
||||
rbx_types::CFrame::new(
|
||||
vec3_to_glam(self.0.translation),
|
||||
rbx_types::Matrix3::new(
|
||||
vec3_to_glam(self.0.matrix3.x_axis),
|
||||
vec3_to_glam(self.0.matrix3.y_axis),
|
||||
vec3_to_glam(self.0.matrix3.z_axis),
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
impl From<rbx_types::CFrame> for CFrame{
|
||||
fn from(value:rbx_types::CFrame)->Self{
|
||||
CFrame(glam::Affine3A{
|
||||
matrix3:glam::mat3a(
|
||||
vec3_from_glam(value.orientation.x),
|
||||
vec3_from_glam(value.orientation.y),
|
||||
vec3_from_glam(value.orientation.z),
|
||||
),
|
||||
translation:vec3_from_glam(value.position)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
|
||||
let cframe_table=lua.create_table()?;
|
||||
|
||||
//CFrame.new
|
||||
cframe_table.raw_set("new",
|
||||
lua.create_function(|_,tuple:(
|
||||
mlua::Value,mlua::Value,Option<f32>,
|
||||
Option<f32>,Option<f32>,Option<f32>,
|
||||
Option<f32>,Option<f32>,Option<f32>,
|
||||
Option<f32>,Option<f32>,Option<f32>,
|
||||
)|match tuple{
|
||||
//CFrame.new(pos)
|
||||
(
|
||||
mlua::Value::UserData(pos),mlua::Value::Nil,None,
|
||||
None,None,None,
|
||||
None,None,None,
|
||||
None,None,None,
|
||||
)=>{
|
||||
let pos:Vector3=pos.take()?;
|
||||
Ok(CFrame::point(pos.0.x,pos.0.y,pos.0.z))
|
||||
},
|
||||
//TODO: CFrame.new(pos,look)
|
||||
(
|
||||
mlua::Value::UserData(pos),mlua::Value::UserData(look),None,
|
||||
None,None,None,
|
||||
None,None,None,
|
||||
None,None,None,
|
||||
)=>{
|
||||
let _pos:Vector3=pos.take()?;
|
||||
let _look:Vector3=look.take()?;
|
||||
Err(mlua::Error::runtime("Not yet implemented"))
|
||||
},
|
||||
//CFrame.new(x,y,z)
|
||||
(
|
||||
mlua::Value::Number(x),mlua::Value::Number(y),Some(z),
|
||||
None,None,None,
|
||||
None,None,None,
|
||||
None,None,None,
|
||||
)=>Ok(CFrame::point(x as f32,y as f32,z)),
|
||||
//CFrame.new(x,y,z,xx,yx,zx,xy,yy,zy,xz,yz,zz)
|
||||
(
|
||||
mlua::Value::Number(x),mlua::Value::Number(y),Some(z),
|
||||
Some(xx),Some(yx),Some(zx),
|
||||
Some(xy),Some(yy),Some(zy),
|
||||
Some(xz),Some(yz),Some(zz),
|
||||
)=>Ok(CFrame::new(x as f32,y as f32,z,
|
||||
xx,yx,zx,
|
||||
xy,yy,zy,
|
||||
xz,yz,zz,
|
||||
)),
|
||||
_=>Err(mlua::Error::runtime("Invalid arguments"))
|
||||
})?
|
||||
)?;
|
||||
|
||||
//CFrame.Angles
|
||||
cframe_table.raw_set("Angles",
|
||||
lua.create_function(|_,(x,y,z):(f32,f32,f32)|
|
||||
Ok(CFrame::angles(x,y,z))
|
||||
)?
|
||||
)?;
|
||||
|
||||
globals.set("CFrame",cframe_table)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl mlua::UserData for CFrame{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
||||
//CFrame.p
|
||||
@ -19,7 +132,20 @@ impl mlua::UserData for CFrame{
|
||||
}
|
||||
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||
//methods.add_method("area",|_,this,()|Ok(this.length*this.width));
|
||||
methods.add_method("components",|_,this,()|Ok((
|
||||
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,
|
||||
)));
|
||||
|
||||
//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)));
|
||||
@ -41,11 +167,5 @@ impl mlua::UserData for CFrame{
|
||||
);
|
||||
}
|
||||
}
|
||||
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!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type_from_lua_userdata!(CFrame);
|
||||
|
68
src/runner/color3.rs
Normal file
68
src/runner/color3.rs
Normal file
@ -0,0 +1,68 @@
|
||||
#[derive(Clone,Copy)]
|
||||
pub struct Color3{
|
||||
r:f32,
|
||||
g:f32,
|
||||
b:f32,
|
||||
}
|
||||
impl Color3{
|
||||
pub const fn new(r:f32,g:f32,b:f32)->Self{
|
||||
Self{r,g,b}
|
||||
}
|
||||
}
|
||||
impl Into<rbx_types::Color3> for Color3{
|
||||
fn into(self)->rbx_types::Color3{
|
||||
rbx_types::Color3::new(self.r,self.g,self.b)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
|
||||
let color3_table=lua.create_table()?;
|
||||
|
||||
color3_table.raw_set("new",
|
||||
lua.create_function(|_,(r,g,b):(f32,f32,f32)|
|
||||
Ok(Color3::new(r,g,b))
|
||||
)?
|
||||
)?;
|
||||
color3_table.raw_set("fromRGB",
|
||||
lua.create_function(|_,(r,g,b):(u8,u8,u8)|
|
||||
Ok(Color3::new(r as f32/255.0,g as f32/255.0,b as f32/255.0))
|
||||
)?
|
||||
)?;
|
||||
|
||||
globals.set("Color3",color3_table)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
fn lerp(lhs:f32,rhs:f32,t:f32)->f32{
|
||||
lhs+(rhs-lhs)*t
|
||||
}
|
||||
|
||||
impl mlua::UserData for Color3{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
||||
fields.add_field_method_get("r",|_,this|Ok(this.r));
|
||||
fields.add_field_method_set("r",|_,this,val|{
|
||||
this.r=val;
|
||||
Ok(())
|
||||
});
|
||||
fields.add_field_method_get("g",|_,this|Ok(this.g));
|
||||
fields.add_field_method_set("g",|_,this,val|{
|
||||
this.g=val;
|
||||
Ok(())
|
||||
});
|
||||
fields.add_field_method_get("b",|_,this|Ok(this.b));
|
||||
fields.add_field_method_set("b",|_,this,val|{
|
||||
this.b=val;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||
methods.add_method("Lerp",|_,this,(other,t):(Self,f32)|
|
||||
Ok(Color3::new(
|
||||
lerp(this.r,other.r,t),
|
||||
lerp(this.g,other.g,t),
|
||||
lerp(this.b,other.b,t),
|
||||
))
|
||||
)
|
||||
}
|
||||
}
|
||||
type_from_lua_userdata!(Color3);
|
61
src/runner/enum.rs
Normal file
61
src/runner/enum.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use mlua::IntoLua;
|
||||
|
||||
#[derive(Clone,Copy)]
|
||||
pub struct Enum(u32);
|
||||
pub struct EnumItems;
|
||||
pub struct EnumItem<'a>{
|
||||
ed:&'a rbx_reflection::EnumDescriptor<'a>,
|
||||
}
|
||||
|
||||
impl Into<rbx_types::Enum> for Enum{
|
||||
fn into(self)->rbx_types::Enum{
|
||||
rbx_types::Enum::from_u32(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> EnumItem<'a>{
|
||||
const fn new(ed:&'a rbx_reflection::EnumDescriptor)->Self{
|
||||
Self{ed}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_globals(_lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
|
||||
globals.set("Enum",EnumItems)
|
||||
}
|
||||
|
||||
impl mlua::UserData for EnumItem<'_>{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
|
||||
}
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(EnumItem<'lua>,mlua::String)|{
|
||||
match this.ed.items.get(val.to_str()?){
|
||||
Some(&id)=>Enum(id).into_lua(lua),
|
||||
None=>mlua::Value::Nil.into_lua(lua),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
type_from_lua_userdata_lua_lifetime!(EnumItem);
|
||||
|
||||
impl mlua::UserData for EnumItems{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
|
||||
}
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(_,val):(Self,mlua::String)|{
|
||||
let db=rbx_reflection_database::get();
|
||||
match db.enums.get(val.to_str()?){
|
||||
Some(ed)=>EnumItem::new(ed).into_lua(lua),
|
||||
None=>mlua::Value::Nil.into_lua(lua),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
type_from_lua_userdata!(EnumItems);
|
||||
|
||||
impl mlua::UserData for Enum{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
|
||||
}
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(_methods:&mut M){
|
||||
}
|
||||
}
|
||||
type_from_lua_userdata!(Enum);
|
@ -1,36 +1,66 @@
|
||||
use rbx_dom_weak::WeakDom;
|
||||
use std::collections::{hash_map::Entry,HashMap};
|
||||
|
||||
use mlua::{IntoLua,IntoLuaMulti};
|
||||
use rbx_types::Ref;
|
||||
use rbx_dom_weak::{InstanceBuilder,WeakDom};
|
||||
|
||||
use super::vector3::Vector3;
|
||||
|
||||
pub struct Instance{
|
||||
referent:rbx_types::Ref,
|
||||
/// A store of created functions for each Roblox class.
|
||||
/// Functions are created the first time they are accessed and stored in this data structure.
|
||||
#[derive(Default)]
|
||||
struct ClassFunctions<'a>{
|
||||
classes:HashMap<&'static str,//ClassName
|
||||
HashMap<&'static str,//Function name
|
||||
mlua::Function<'a>
|
||||
>
|
||||
>
|
||||
}
|
||||
|
||||
impl From<crate::script::Script> for Instance{
|
||||
fn from(value:crate::script::Script)->Self{
|
||||
Self{referent:value.script}
|
||||
}
|
||||
fn place_id<'lua>(lua:&'lua mlua::Lua,tuple:mlua::MultiValue<'lua>)->mlua::Result<mlua::MultiValue<'lua>>{
|
||||
0.into_lua_multi(lua)
|
||||
}
|
||||
type FPointer=for<'lua> fn(&'lua mlua::Lua,mlua::MultiValue<'lua>)->mlua::Result<mlua::MultiValue<'lua>>;
|
||||
static CLASS_FUNCTION_DATABASE:phf::Map<&str,phf::Map<&str,FPointer>>=phf::phf_map!{
|
||||
"DataModel"=>phf::phf_map!{
|
||||
"GetService"=>place_id as FPointer,
|
||||
}
|
||||
};
|
||||
|
||||
impl Instance{
|
||||
pub const fn new(referent:rbx_types::Ref)->Self{
|
||||
Self{referent}
|
||||
}
|
||||
pub fn get<'a>(&'a self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
pub fn get_mut<'a>(&'a self,dom:&'a mut WeakDom)->mlua::Result<&'a mut rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref_mut(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
|
||||
//class functions store
|
||||
lua.set_app_data::<ClassFunctions<'static>>(ClassFunctions::default());
|
||||
|
||||
let instance_table=lua.create_table()?;
|
||||
|
||||
//Instance.new
|
||||
instance_table.raw_set("new",
|
||||
lua.create_function(|lua,(class_name,parent):(mlua::String,Option<Instance>)|{
|
||||
let class_name_str=class_name.to_str()?;
|
||||
let parent=parent.ok_or(mlua::Error::runtime("Nil Parent not yet supported"))?;
|
||||
dom_mut(lua,|dom|{
|
||||
//TODO: Nil instances
|
||||
Ok(Instance::new(dom.insert(parent.referent,InstanceBuilder::new(class_name_str))))
|
||||
})
|
||||
})?
|
||||
)?;
|
||||
|
||||
globals.set("Instance",instance_table)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// LMAO look at this function!
|
||||
fn dom<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->mlua::Result<T>{
|
||||
fn dom_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->mlua::Result<T>{
|
||||
let mut dom=lua.app_data_mut::<&'static mut WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?;
|
||||
f(&mut *dom)
|
||||
}
|
||||
fn class_functions_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut ClassFunctions<'_>)->mlua::Result<T>)->mlua::Result<T>{
|
||||
let mut dom=lua.app_data_mut::<ClassFunctions<'static>>().ok_or(mlua::Error::runtime("ClassFunctions missing"))?;
|
||||
f(&mut *dom)
|
||||
}
|
||||
|
||||
fn coerce_float(value:&mlua::Value)->Option<f32>{
|
||||
fn coerce_float32(value:&mlua::Value)->Option<f32>{
|
||||
match value{
|
||||
&mlua::Value::Integer(i)=>Some(i as f32),
|
||||
&mlua::Value::Number(f)=>Some(f as f32),
|
||||
@ -38,25 +68,117 @@ fn coerce_float(value:&mlua::Value)->Option<f32>{
|
||||
}
|
||||
}
|
||||
|
||||
fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->String{
|
||||
let mut full_name=instance.name.clone();
|
||||
let mut pref=instance.parent();
|
||||
while let Some(parent)=dom.get_by_ref(pref){
|
||||
full_name.insert(0,'.');
|
||||
full_name.insert_str(0,parent.name.as_str());
|
||||
pref=parent.parent();
|
||||
}
|
||||
full_name
|
||||
}
|
||||
//helper function for script
|
||||
pub fn get_name_source(lua:&mlua::Lua,script:Instance)->Result<(String,String),mlua::Error>{
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=script.get(dom)?;
|
||||
let source=match instance.properties.get("Source"){
|
||||
Some(rbx_dom_weak::types::Variant::String(s))=>s.clone(),
|
||||
_=>Err(mlua::Error::external("Missing script.Source"))?,
|
||||
};
|
||||
Ok((get_full_name(dom,instance),source))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn find_first_child<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,name:&str)->Option<&'a rbx_dom_weak::Instance>{
|
||||
instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.name==name)
|
||||
}
|
||||
pub fn find_first_descendant<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,name:&str)->Option<&'a rbx_dom_weak::Instance>{
|
||||
dom.descendants_of(instance.referent()).find(|&inst|inst.name==name)
|
||||
}
|
||||
|
||||
pub fn find_first_child_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,class:&str)->Option<&'a rbx_dom_weak::Instance>{
|
||||
instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.class==class)
|
||||
}
|
||||
pub fn find_first_descendant_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,class:&str)->Option<&'a rbx_dom_weak::Instance>{
|
||||
dom.descendants_of(instance.referent()).find(|&inst|inst.class==class)
|
||||
}
|
||||
|
||||
#[derive(Clone,Copy)]
|
||||
pub struct Instance{
|
||||
referent:Ref,
|
||||
}
|
||||
impl Instance{
|
||||
pub const fn new(referent:Ref)->Self{
|
||||
Self{referent}
|
||||
}
|
||||
pub fn get<'a>(&self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
pub fn get_mut<'a>(&self,dom:&'a mut WeakDom)->mlua::Result<&'a mut rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref_mut(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
}
|
||||
type_from_lua_userdata!(Instance);
|
||||
|
||||
//TODO: update rbx_reflection and use dom.superclasses_iter
|
||||
pub struct SuperClassIter<'a> {
|
||||
database: &'a rbx_reflection::ReflectionDatabase<'a>,
|
||||
descriptor: Option<&'a rbx_reflection::ClassDescriptor<'a>>,
|
||||
}
|
||||
impl<'a> SuperClassIter<'a> {
|
||||
fn next_descriptor(&self) -> Option<&'a rbx_reflection::ClassDescriptor<'a>> {
|
||||
let superclass = self.descriptor?.superclass.as_ref()?;
|
||||
self.database.classes.get(superclass)
|
||||
}
|
||||
}
|
||||
impl<'a> Iterator for SuperClassIter<'a> {
|
||||
type Item = &'a rbx_reflection::ClassDescriptor<'a>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let next_descriptor = self.next_descriptor();
|
||||
std::mem::replace(&mut self.descriptor, next_descriptor)
|
||||
}
|
||||
}
|
||||
|
||||
impl mlua::UserData for Instance{
|
||||
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
|
||||
fields.add_field_method_get("Parent",|lua,this|{
|
||||
dom(lua,|dom|{
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=this.get(dom)?;
|
||||
Ok(Instance::new(instance.parent()))
|
||||
})
|
||||
});
|
||||
fields.add_field_method_set("Parent",|lua,this,val:Self|{
|
||||
dom(lua,|dom|{
|
||||
dom.transfer_within(this.referent,val.referent);
|
||||
fields.add_field_method_set("Parent",|lua,this,val:Option<Instance>|{
|
||||
let parent=val.ok_or(mlua::Error::runtime("Nil Parent not yet supported"))?;
|
||||
dom_mut(lua,|dom|{
|
||||
dom.transfer_within(this.referent,parent.referent);
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
fields.add_field_method_get("Name",|lua,this|{
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=this.get(dom)?;
|
||||
Ok(instance.name.clone())
|
||||
})
|
||||
});
|
||||
fields.add_field_method_set("Name",|lua,this,val:mlua::String|{
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=this.get_mut(dom)?;
|
||||
//Why does this need to be cloned?
|
||||
instance.name=val.to_str()?.to_owned();
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
fields.add_field_method_get("ClassName",|lua,this|{
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=this.get(dom)?;
|
||||
Ok(instance.class.clone())
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
|
||||
methods.add_method("GetChildren",|lua,this,_:()|
|
||||
dom(lua,|dom|{
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=this.get(dom)?;
|
||||
let children:Vec<_>=instance
|
||||
.children()
|
||||
@ -67,8 +189,39 @@ impl mlua::UserData for Instance{
|
||||
Ok(children)
|
||||
})
|
||||
);
|
||||
let ffc=|lua,this:&Self,(name,search_descendants):(mlua::String,Option<bool>)|{
|
||||
let name_str=name.to_str()?;
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=this.get(dom)?;
|
||||
Ok(
|
||||
match search_descendants.unwrap_or(false){
|
||||
true=>find_first_descendant(dom,instance,name_str),
|
||||
false=>find_first_child(dom,instance,name_str),
|
||||
}
|
||||
.map(|instance|
|
||||
Instance::new(instance.referent())
|
||||
)
|
||||
)
|
||||
})
|
||||
};
|
||||
methods.add_method("FindFirstChild",ffc);
|
||||
methods.add_method("WaitForChild",ffc);
|
||||
methods.add_method("FindFirstChildOfClass",|lua,this,(class,search_descendants):(mlua::String,Option<bool>)|{
|
||||
let class_str=class.to_str()?;
|
||||
dom_mut(lua,|dom|{
|
||||
Ok(
|
||||
match search_descendants.unwrap_or(false){
|
||||
true=>find_first_descendant_of_class(dom,this.get(dom)?,class_str),
|
||||
false=>find_first_child_of_class(dom,this.get(dom)?,class_str),
|
||||
}
|
||||
.map(|instance|
|
||||
Instance::new(instance.referent())
|
||||
)
|
||||
)
|
||||
})
|
||||
});
|
||||
methods.add_method("GetDescendants",|lua,this,_:()|
|
||||
dom(lua,|dom|{
|
||||
dom_mut(lua,|dom|{
|
||||
let children:Vec<_>=dom
|
||||
.descendants_of(this.referent)
|
||||
.map(|instance|
|
||||
@ -79,39 +232,138 @@ impl mlua::UserData for Instance{
|
||||
})
|
||||
);
|
||||
methods.add_method("IsA",|lua,this,classname:mlua::String|
|
||||
dom(lua,|dom|{
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=this.get(dom)?;
|
||||
Ok(crate::context::class_is_a(instance.class.as_str(),classname.to_str()?))
|
||||
})
|
||||
);
|
||||
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(Self,mlua::String,mlua::Value)|
|
||||
dom(lua,|dom|{
|
||||
let instance=this.get_mut(dom)?;
|
||||
let index_str=index.to_str()?;
|
||||
let class=rbx_reflection_database::get().classes.get(instance.class.as_str()).ok_or(mlua::Error::runtime("Class missing"))?;
|
||||
let property=class.default_properties.get(index_str).ok_or(mlua::Error::runtime(format!("Property '{index_str}' missing on class '{}'",class.name)))?;
|
||||
match property{
|
||||
rbx_types::Variant::Vector3(_)=>{
|
||||
let typed_value:Vector3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Vector3(typed_value.into()));
|
||||
},
|
||||
rbx_types::Variant::Float32(_)=>{
|
||||
let typed_value:f32=coerce_float(&value).ok_or(mlua::Error::runtime("Expected f32"))?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Float32(typed_value));
|
||||
},
|
||||
other=>println!("Unimplemented property type: {other:?}"),
|
||||
}
|
||||
methods.add_method("Destroy",|lua,this,()|
|
||||
dom_mut(lua,|dom|{
|
||||
dom.destroy(this.referent);
|
||||
Ok(())
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'lua> mlua::FromLua<'lua> for Instance{
|
||||
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
|
||||
match value{
|
||||
mlua::Value::UserData(ud)=>ud.take(),
|
||||
other=>Err(mlua::Error::runtime(format!("Expected Instance got {:?}",other))),
|
||||
}
|
||||
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,index):(Instance,mlua::String)|{
|
||||
let index_str=index.to_str()?;
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=this.get(dom)?;
|
||||
//Find existing property
|
||||
match instance.properties.get(index_str)
|
||||
//Find default value
|
||||
.or_else(||{
|
||||
let db=rbx_reflection_database::get();
|
||||
db.classes.get(instance.class.as_str()).and_then(|cd|
|
||||
db.find_default_property(cd,index_str)
|
||||
)
|
||||
})
|
||||
{
|
||||
Some(&rbx_types::Variant::CFrame(cf))=>return Ok(Into::<super::cframe::CFrame>::into(cf).into_lua(lua)),
|
||||
Some(&rbx_types::Variant::Vector3(v))=>return Ok(Into::<super::vector3::Vector3>::into(v).into_lua(lua)),
|
||||
_=>(),
|
||||
}
|
||||
//find a function with a matching name
|
||||
if let Some(ret)=class_functions_mut(lua,|cf|{
|
||||
let class_str=instance.class.as_str();
|
||||
let f=match CLASS_FUNCTION_DATABASE.get_entry(class_str){
|
||||
Some((&static_class_str,class_functions))=>{
|
||||
match cf.classes.entry(static_class_str){
|
||||
Entry::Occupied(mut occupied_entry)=>{
|
||||
match class_functions.get_entry(index_str){
|
||||
Some((&static_index_str,function_pointer))=>{
|
||||
match occupied_entry.get_mut().entry(static_index_str){
|
||||
Entry::Occupied(occupied_entry)=>{
|
||||
Some(occupied_entry.get().clone())
|
||||
},
|
||||
Entry::Vacant(vacant_entry)=>{
|
||||
Some(vacant_entry.insert(unsafe{core::mem::transmute(lua.create_function(function_pointer)?)}).clone())
|
||||
},
|
||||
}
|
||||
},
|
||||
None=>None,
|
||||
}
|
||||
},
|
||||
Entry::Vacant(vacant_entry)=>{
|
||||
match class_functions.get_entry(index_str){
|
||||
Some((&static_index_str,function_pointer))=>{
|
||||
let mut h=HashMap::new();
|
||||
h.entry(static_index_str).or_insert(unsafe{core::mem::transmute(lua.create_function(function_pointer)?)});
|
||||
vacant_entry.insert(h).get(static_index_str).map(|f|f.clone())
|
||||
},
|
||||
None=>None,
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
None=>None,
|
||||
};
|
||||
Ok(f.map(|f|{
|
||||
let f_static:mlua::Function::<'static>=unsafe{core::mem::transmute(f)};
|
||||
f_static
|
||||
}))
|
||||
})?{
|
||||
return Ok(ret.into_lua(lua));
|
||||
}
|
||||
//find a child with a matching name
|
||||
Ok(
|
||||
find_first_child(dom,instance,index_str)
|
||||
.map(|instance|Instance::new(instance.referent()))
|
||||
.into_lua(lua)
|
||||
)
|
||||
})
|
||||
});
|
||||
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(Instance,mlua::String,mlua::Value)|{
|
||||
dom_mut(lua,|dom|{
|
||||
let instance=this.get_mut(dom)?;
|
||||
//println!("__newindex t={} i={index:?} v={value:?}",instance.name);
|
||||
let index_str=index.to_str()?;
|
||||
let db=rbx_reflection_database::get();
|
||||
let class=db.classes.get(instance.class.as_str()).ok_or(mlua::Error::runtime("Class missing"))?;
|
||||
let mut iter=SuperClassIter{
|
||||
database:db,
|
||||
descriptor:Some(class),
|
||||
};
|
||||
let property=iter.find_map(|cls|cls.properties.get(index_str)).ok_or(mlua::Error::runtime(format!("Property '{index_str}' missing on class '{}'",class.name)))?;
|
||||
match &property.data_type{
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::Vector3)=>{
|
||||
let typed_value:Vector3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Vector3(typed_value.into()));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::Float32)=>{
|
||||
let typed_value:f32=coerce_float32(&value).ok_or(mlua::Error::runtime("Expected f32"))?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Float32(typed_value));
|
||||
},
|
||||
rbx_reflection::DataType::Enum(enum_name)=>{
|
||||
let typed_value=match &value{
|
||||
&mlua::Value::Integer(int)=>Ok(rbx_types::Enum::from_u32(int as u32)),
|
||||
&mlua::Value::Number(num)=>Ok(rbx_types::Enum::from_u32(num as u32)),
|
||||
mlua::Value::String(s)=>{
|
||||
let e=db.enums.get(enum_name).ok_or(mlua::Error::runtime("Database DataType Enum name does not exist"))?;
|
||||
Ok(rbx_types::Enum::from_u32(*e.items.get(s.to_str()?).ok_or(mlua::Error::runtime("Invalid enum item"))?))
|
||||
},
|
||||
mlua::Value::UserData(any_user_data)=>{
|
||||
let e:super::r#enum::Enum=any_user_data.take()?;
|
||||
Ok(e.into())
|
||||
},
|
||||
_=>Err(mlua::Error::runtime("Expected Enum")),
|
||||
}?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Enum(typed_value));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::Color3)=>{
|
||||
let typed_value:super::color3::Color3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Color3"))?.take()?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Color3(typed_value.into()));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::Bool)=>{
|
||||
let typed_value=value.as_boolean().ok_or(mlua::Error::runtime("Expected boolean"))?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Bool(typed_value));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::String)=>{
|
||||
let typed_value=value.as_str().ok_or(mlua::Error::runtime("Expected boolean"))?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::String(typed_value.to_owned()));
|
||||
},
|
||||
other=>return Err(mlua::Error::runtime(format!("Unimplemented property type: {other:?}"))),
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
24
src/runner/macros.rs
Normal file
24
src/runner/macros.rs
Normal file
@ -0,0 +1,24 @@
|
||||
macro_rules! type_from_lua_userdata{
|
||||
($asd:ident)=>{
|
||||
impl<'lua> mlua::FromLua<'lua> for $asd{
|
||||
fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->Result<Self,mlua::Error>{
|
||||
match value{
|
||||
mlua::Value::UserData(ud)=>ud.take(),
|
||||
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($asd),other))),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! type_from_lua_userdata_lua_lifetime{
|
||||
($asd:ident)=>{
|
||||
impl<'lua> mlua::FromLua<'lua> for $asd<'lua>{
|
||||
fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->Result<Self,mlua::Error>{
|
||||
match value{
|
||||
mlua::Value::UserData(ud)=>ud.take(),
|
||||
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($asd),other))),
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -1,7 +1,11 @@
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
mod runner;
|
||||
|
||||
mod r#enum;
|
||||
mod color3;
|
||||
mod cframe;
|
||||
mod vector3;
|
||||
mod instance;
|
||||
pub mod instance;
|
||||
|
||||
pub use runner::Runner;
|
||||
pub use runner::{Runner,Error};
|
||||
|
@ -1,28 +1,27 @@
|
||||
use crate::context::Context;
|
||||
|
||||
use super::vector3::Vector3;
|
||||
use super::cframe::CFrame;
|
||||
|
||||
pub struct Runner{
|
||||
lua:mlua::Lua,
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub enum Error{
|
||||
Lua(mlua::Error),
|
||||
Script(crate::script::Error),
|
||||
/// If the lua.remove_app_data function fails
|
||||
RemoveAppData,
|
||||
Lua{
|
||||
source:String,
|
||||
error:mlua::Error
|
||||
},
|
||||
RustLua(mlua::Error),
|
||||
NoServices,
|
||||
}
|
||||
impl Error{
|
||||
pub fn print(self){
|
||||
impl std::fmt::Display for Error{
|
||||
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
||||
match self{
|
||||
Self::Lua(mlua::Error::RuntimeError(s))=>{
|
||||
println!("lua error: {s}");
|
||||
},
|
||||
other=>println!("{:?}",other),
|
||||
Self::Lua{source,error}=>write!(f,"lua error: source:\n{source}\n{error}"),
|
||||
Self::RustLua(error)=>write!(f,"rust-side lua error: {error}"),
|
||||
other=>write!(f,"{other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl std::error::Error for Error{}
|
||||
|
||||
fn init(lua:&mlua::Lua)->mlua::Result<()>{
|
||||
lua.sandbox(true)?;
|
||||
@ -30,62 +29,59 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{
|
||||
//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)?;
|
||||
}
|
||||
super::r#enum::set_globals(lua,&globals)?;
|
||||
super::color3::set_globals(lua,&globals)?;
|
||||
super::vector3::set_globals(lua,&globals)?;
|
||||
super::cframe::set_globals(lua,&globals)?;
|
||||
super::instance::set_globals(lua,&globals)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Runner{
|
||||
pub fn new()->mlua::Result<Self>{
|
||||
pub fn new()->Result<Self,Error>{
|
||||
let runner=Self{
|
||||
lua:mlua::Lua::new(),
|
||||
};
|
||||
init(&runner.lua)?;
|
||||
init(&runner.lua).map_err(Error::RustLua)?;
|
||||
Ok(runner)
|
||||
}
|
||||
pub fn run_script(&self,script:crate::script::Script,context:&mut Context)->Result<(),Error>{
|
||||
let yoink=script.name_source(context);
|
||||
pub fn runnable_context<'a>(self,context:&'a mut Context)->Result<Runnable<'a>,Error>{
|
||||
let services=context.find_services().ok_or(Error::NoServices)?;
|
||||
self.runnable_context_with_services(context,&services)
|
||||
}
|
||||
pub fn runnable_context_with_services<'a>(self,context:&'a mut Context,services:&crate::context::Services)->Result<Runnable<'a>,Error>{
|
||||
{
|
||||
let globals=self.lua.globals();
|
||||
globals.set("game",super::instance::Instance::new(services.game)).map_err(Error::RustLua)?;
|
||||
globals.set("workspace",super::instance::Instance::new(services.workspace)).map_err(Error::RustLua)?;
|
||||
}
|
||||
//this makes set_app_data shut up about the lifetime
|
||||
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
|
||||
let (name,source)=yoink.map_err(Error::Script)?;
|
||||
self.lua.globals().set("script",super::instance::Instance::from(script)).map_err(Error::Lua)?;
|
||||
self.lua.load(source)
|
||||
.set_name(name)
|
||||
.exec().map_err(Error::Lua)?;
|
||||
self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
|
||||
Ok(())
|
||||
Ok(Runnable{
|
||||
lua:self.lua,
|
||||
_lifetime:&std::marker::PhantomData
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//Runnable is the same thing but has context set, which it holds the lifetime for.
|
||||
pub struct Runnable<'a>{
|
||||
lua:mlua::Lua,
|
||||
_lifetime:&'a std::marker::PhantomData<()>
|
||||
}
|
||||
impl Runnable<'_>{
|
||||
pub fn drop_context(self)->Runner{
|
||||
self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
|
||||
Runner{
|
||||
lua:self.lua,
|
||||
}
|
||||
}
|
||||
pub fn run_script(&self,script:super::instance::Instance)->Result<(),Error>{
|
||||
let (name,source)=super::instance::get_name_source(&self.lua,script).map_err(Error::RustLua)?;
|
||||
self.lua.globals().set("script",script).map_err(Error::RustLua)?;
|
||||
self.lua.load(source.as_str())
|
||||
.set_name(name)
|
||||
.exec().map_err(|error|Error::Lua{source,error})
|
||||
}
|
||||
}
|
||||
|
@ -7,28 +7,49 @@ impl Vector3{
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
|
||||
let vector3_table=lua.create_table()?;
|
||||
|
||||
//Vector3.new
|
||||
vector3_table.raw_set("new",
|
||||
lua.create_function(|_,(x,y,z):(f32,f32,f32)|
|
||||
Ok(Vector3::new(x,y,z))
|
||||
)?
|
||||
)?;
|
||||
|
||||
globals.set("Vector3",vector3_table)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Into<rbx_types::Vector3> for Vector3{
|
||||
fn into(self)->rbx_types::Vector3{
|
||||
rbx_types::Vector3::new(self.0.x,self.0.y,self.0.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rbx_types::Vector3> for Vector3{
|
||||
fn from(value:rbx_types::Vector3)->Vector3{
|
||||
Vector3::new(value.x,value.y,value.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;
|
||||
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;
|
||||
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;
|
||||
fields.add_field_method_get("z",|_,this|Ok(this.0.z));
|
||||
fields.add_field_method_set("z",|_,this,val|{
|
||||
this.0.z=val;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@ -37,6 +58,17 @@ impl mlua::UserData for Vector3{
|
||||
//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::Div,|_,(this,val):(Self,mlua::Value)|{
|
||||
match val{
|
||||
mlua::Value::Integer(n)=>Ok(Self(this.0/(n as f32))),
|
||||
mlua::Value::Number(n)=>Ok(Self(this.0/(n as f32))),
|
||||
mlua::Value::UserData(ud)=>{
|
||||
let rhs:Vector3=ud.take()?;
|
||||
Ok(Self(this.0/rhs.0))
|
||||
},
|
||||
other=>Err(mlua::Error::runtime(format!("Attempt to divide Vector3 by {other:?}"))),
|
||||
}
|
||||
});
|
||||
methods.add_meta_function(mlua::MetaMethod::ToString,|_,this:Self|
|
||||
Ok(format!("Vector3.new({},{},{})",
|
||||
this.0.x,
|
||||
@ -47,11 +79,4 @@ impl mlua::UserData for Vector3{
|
||||
}
|
||||
}
|
||||
|
||||
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)=>ud.take(),
|
||||
other=>Err(mlua::Error::runtime(format!("Expected Vector3 got {:?}",other))),
|
||||
}
|
||||
}
|
||||
}
|
||||
type_from_lua_userdata!(Vector3);
|
||||
|
@ -1,37 +0,0 @@
|
||||
use rbx_dom_weak::types::Ref;
|
||||
|
||||
use crate::context::Context;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error{
|
||||
NoScript,
|
||||
NoSource,
|
||||
}
|
||||
|
||||
fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->String{
|
||||
let mut full_name=instance.name.clone();
|
||||
let mut pref=instance.parent();
|
||||
while let Some(parent)=dom.get_by_ref(pref){
|
||||
full_name.insert(0,'.');
|
||||
full_name.insert_str(0,parent.name.as_str());
|
||||
pref=parent.parent();
|
||||
}
|
||||
full_name
|
||||
}
|
||||
|
||||
pub struct Script{
|
||||
pub(crate)script:Ref,
|
||||
}
|
||||
impl Script{
|
||||
pub const fn new(script:Ref)->Self{
|
||||
Self{script}
|
||||
}
|
||||
pub fn name_source(&self,context:&Context)->Result<(String,String),Error>{
|
||||
let instance=context.dom.get_by_ref(self.script).ok_or(Error::NoScript)?;
|
||||
let source=match instance.properties.get("Source").ok_or(Error::NoSource)?{
|
||||
rbx_dom_weak::types::Variant::String(s)=>s.clone(),
|
||||
_=>Err(Error::NoSource)?,
|
||||
};
|
||||
Ok((get_full_name(&context.dom,instance),source))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user