12 Commits

4 changed files with 84 additions and 175 deletions

2
Cargo.lock generated
View File

@@ -402,7 +402,7 @@ dependencies = [
[[package]]
name = "roblox_emulator"
version = "0.4.4"
version = "0.4.1"
dependencies = [
"glam",
"mlua",

View File

@@ -1,6 +1,6 @@
[package]
name = "roblox_emulator"
version = "0.4.4"
version = "0.4.1"
edition = "2021"
repository = "https://git.itzana.me/StrafesNET/roblox_emulator"
license = "MIT OR Apache-2.0"

View File

@@ -60,19 +60,10 @@ impl Context{
//insert services
let game=self.dom.root_ref();
let terrain_bldr=InstanceBuilder::new("Terrain");
let workspace=self.dom.insert(game,
InstanceBuilder::new("Workspace")
//Set Workspace.Terrain property equal to Terrain
.with_property("Terrain",terrain_bldr.referent())
.with_child(terrain_bldr)
.with_child(InstanceBuilder::new("Terrain"))
);
{
//Lowercase and upper case workspace property!
let game=self.dom.root_mut();
game.properties.insert("workspace".to_owned(),rbx_types::Variant::Ref(workspace));
game.properties.insert("Workspace".to_owned(),rbx_types::Variant::Ref(workspace));
}
self.dom.insert(game,InstanceBuilder::new("Lighting"));
//transfer original root instances into workspace

View File

@@ -1,6 +1,6 @@
use std::collections::{hash_map::Entry,HashMap};
use mlua::{FromLua,FromLuaMulti,IntoLua,IntoLuaMulti};
use mlua::{FromLuaMulti,IntoLua,IntoLuaMulti};
use rbx_types::Ref;
use rbx_dom_weak::{InstanceBuilder,WeakDom};
@@ -10,6 +10,8 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
//class functions store
lua.set_app_data(ClassFunctions::default());
lua.register_userdata_type(<Instance as mlua::UserData>::register)?;
let instance_table=lua.create_table()?;
//Instance.new
@@ -237,29 +239,16 @@ impl mlua::UserData for Instance{
let class=db.classes.get(instance.class.as_str()).ok_or(mlua::Error::runtime("Class missing"))?;
//Find existing property
match instance.properties.get(index_str)
.cloned()
//Find default value
.or_else(||db.find_default_property(class,index_str).cloned())
//Find virtual property
.or_else(||{
SuperClassIter{
database:db,
descriptor:Some(class),
}
.find_map(|class|
find_virtual_property(&instance.properties,class,index_str)
)
})
.or_else(||db.find_default_property(class,index_str))
{
Some(rbx_types::Variant::Int32(val))=>return val.into_lua(lua),
Some(rbx_types::Variant::Int64(val))=>return val.into_lua(lua),
Some(rbx_types::Variant::Float32(val))=>return val.into_lua(lua),
Some(rbx_types::Variant::Float64(val))=>return val.into_lua(lua),
Some(rbx_types::Variant::Ref(val))=>return Instance::new(val).into_lua(lua),
Some(rbx_types::Variant::CFrame(cf))=>return Into::<super::cframe::CFrame>::into(cf).into_lua(lua),
Some(rbx_types::Variant::Vector3(v))=>return Into::<super::vector3::Vector3>::into(v).into_lua(lua),
None=>(),
other=>return Err(mlua::Error::runtime(format!("Instance.__index Unsupported property type instance={} index={index_str} value={other:?}",instance.name))),
Some(&rbx_types::Variant::Int32(val))=>return Ok(val.into_lua(lua)),
Some(&rbx_types::Variant::Int64(val))=>return Ok(val.into_lua(lua)),
Some(&rbx_types::Variant::Float32(val))=>return Ok(val.into_lua(lua)),
Some(&rbx_types::Variant::Float64(val))=>return Ok(val.into_lua(lua)),
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)),
other=>println!("instance.properties.get(i)={other:?}"),
}
//find a function with a matching name
if let Some(function)=class_functions_mut(lua,|cf|{
@@ -267,19 +256,24 @@ impl mlua::UserData for Instance{
database:db,
descriptor:Some(class),
};
iter.find_map(|class|
cf.get_or_create_class_methods(&class.name)
.and_then(|mut class_methods|
class_methods.get_or_create_function(lua,index_str).transpose()
)
).transpose()
Ok(loop{
match iter.next(){
Some(class)=>match cf.get_or_create_class_function(lua,&class.name,index_str)?{
Some(function)=>break Some(function),
None=>(),
},
None=>break None,
}
})
})?{
return function.into_lua(lua);
return Ok(function.into_lua(lua));
}
//find a child with a matching name
find_first_child(dom,instance,index_str)
.map(|instance|Instance::new(instance.referent()))
.into_lua(lua)
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)|{
@@ -339,46 +333,29 @@ impl mlua::UserData for Instance{
}
}
/// A class function definition shorthand.
macro_rules! cf{
($f:expr)=>{
|lua,mut args|{
let this=Instance::from_lua(args.pop_front().unwrap_or(mlua::Value::Nil),lua)?;
$f(lua,this,FromLuaMulti::from_lua_multi(args,lua)?)?.into_lua_multi(lua)
fn get_service(lua:&mlua::Lua,tuple:mlua::MultiValue)->mlua::Result<mlua::MultiValue>{
let (_game,service):(Instance,mlua::String)=FromLuaMulti::from_lua_multi(tuple,lua)?;
dom_mut(lua,|dom|{
match &*service.to_str()?{
"Lighting"=>{
let referent=find_first_child_of_class(dom,dom.root(),"Lighting")
.map(|instance|instance.referent())
.unwrap_or_else(||
dom.insert(dom.root_ref(),InstanceBuilder::new("Lighting"))
);
Instance::new(referent).into_lua_multi(lua)
},
other=>Err::<mlua::MultiValue,_>(mlua::Error::runtime(format!("Service '{other}' not supported"))),
}
};
})
}
type ClassFunctionPointer=fn(&mlua::Lua,mlua::MultiValue)->mlua::Result<mlua::MultiValue>;
// TODO: use macros to define these with better organization
type FPointer=fn(&mlua::Lua,mlua::MultiValue)->mlua::Result<mlua::MultiValue>;
/// A double hash map of function pointers.
/// The class tree is walked by the Instance.__index metamethod to find available class methods.
type CFD=phf::Map<&'static str,// Class name
phf::Map<&'static str,// Method name
ClassFunctionPointer
>
>;
static CLASS_FUNCTION_DATABASE:CFD=phf::phf_map!{
static CLASS_FUNCTION_DATABASE:phf::Map<&str,phf::Map<&str,FPointer>>=phf::phf_map!{
"DataModel"=>phf::phf_map!{
"GetService"=>cf!(|lua,_this,service:mlua::String|{
dom_mut(lua,|dom|{
//dom.root_ref()==this.referent ?
match &*service.to_str()?{
"Lighting"=>{
let referent=find_first_child_of_class(dom,dom.root(),"Lighting")
.map(|instance|instance.referent())
.unwrap_or_else(||
dom.insert(dom.root_ref(),InstanceBuilder::new("Lighting"))
);
Ok(Instance::new(referent))
},
other=>Err::<Instance,_>(mlua::Error::runtime(format!("Service '{other}' not supported"))),
}
})
}),
},
"Terrain"=>phf::phf_map!{
"FillBlock"=>cf!(|_lua,_,_:(super::cframe::CFrame,Vector3,super::r#enum::Enum)|mlua::Result::Ok(()))
},
"GetService"=>get_service as FPointer,
}
};
/// A store of created functions for each Roblox class.
@@ -386,108 +363,49 @@ static CLASS_FUNCTION_DATABASE:CFD=phf::phf_map!{
#[derive(Default)]
struct ClassFunctions{
classes:HashMap<&'static str,//ClassName
HashMap<&'static str,//Method name
HashMap<&'static str,//Function name
mlua::Function
>
>
}
impl ClassFunctions{
/// return self.classes[class] or create the ClassMethods and then return it
fn get_or_create_class_methods(&mut self,class:&str)->Option<ClassMethods>{
// Use get_entry to get the &'static str keys of the database
/// Someone please rewrite this, all it's supposed to do is
/// return self.classes[class][index] or create the function in the hashmap and then return it
fn get_or_create_class_function(&mut self,lua:&mlua::Lua,class:&str,index:&str)->mlua::Result<Option<mlua::Function>>{
// Use get_entry to get the &'static str key of the database
// and use it as a key for the classes hashmap
CLASS_FUNCTION_DATABASE.get_entry(class)
.map(|(&static_class_str,method_pointers)|
ClassMethods{
method_pointers,
methods:self.classes.entry(static_class_str)
.or_insert_with(||HashMap::new()),
let f=match CLASS_FUNCTION_DATABASE.get_entry(class){
Some((&static_class_str,class_functions))=>{
match self.classes.entry(static_class_str){
Entry::Occupied(mut occupied_entry)=>{
match class_functions.get_entry(index){
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(lua.create_function(function_pointer)?).clone())
},
}
},
None=>None,
}
},
Entry::Vacant(vacant_entry)=>{
match class_functions.get_entry(index){
Some((&static_index_str,function_pointer))=>{
let mut h=HashMap::new();
h.entry(static_index_str).or_insert(lua.create_function(function_pointer)?);
vacant_entry.insert(h).get(static_index_str).map(|f|f.clone())
},
None=>None,
}
},
}
)
}
}
struct ClassMethods<'a>{
method_pointers:&'static phf::Map<&'static str,ClassFunctionPointer>,
methods:&'a mut HashMap<&'static str,mlua::Function>,
}
impl ClassMethods<'_>{
/// return self.methods[index] or create the function in the hashmap and then return it
fn get_or_create_function(&mut self,lua:&mlua::Lua,index:&str)->mlua::Result<Option<mlua::Function>>{
Ok(match self.method_pointers.get_entry(index){
Some((&static_index_str,&function_pointer))=>Some(
match self.methods.entry(static_index_str){
Entry::Occupied(entry)=>entry.get().clone(),
Entry::Vacant(entry)=>entry.insert(
lua.create_function(function_pointer)?
).clone(),
}
),
None=>None,
})
}
}
/// A virtual property pointer definition shorthand.
type VirtualPropertyFunctionPointer=fn(&rbx_types::Variant)->Option<rbx_types::Variant>;
type VirtualPropertyFunctionPointerMut=fn(&mut rbx_types::Variant,rbx_types::Variant)->Option<()>;
const fn vpp(
property:&'static str,
access:VirtualPropertyFunctionPointer,
access_mut:VirtualPropertyFunctionPointerMut,
)->VirtualProperty{
VirtualProperty{
property,
access,
access_mut,
}
}
struct VirtualProperty{
property:&'static str,// Source property name
access:VirtualPropertyFunctionPointer,
access_mut:VirtualPropertyFunctionPointerMut,
}
type VPD=phf::Map<&'static str,// Class name
phf::Map<&'static str,// Virtual property name
VirtualProperty
>
>;
static VIRTUAL_PROPERTY_DATABASE:VPD=phf::phf_map!{
"BasePart"=>phf::phf_map!{
"Position"=>vpp(
"CFrame",
//Get
|c|{
let c=match c{
rbx_types::Variant::CFrame(c)=>c,
_=>return None,//fail silently and ungracefully
};
Some(rbx_types::Variant::Vector3(c.position))
},
//Set
|c,p|{
let (c,p)=match (c,p){
(rbx_types::Variant::CFrame(c),rbx_types::Variant::Vector3(p))=>(c,p),
_=>return None,//fail silently and ungracefully
};
c.position=p;
Some(())
}
),
},
};
fn find_virtual_property(
properties:&HashMap<String,rbx_types::Variant>,
class:&rbx_reflection::ClassDescriptor,
index:&str
)->Option<rbx_types::Variant>{
//Find virtual property
let class_virtual_properties=VIRTUAL_PROPERTY_DATABASE.get(&class.name)?;
let virtual_property=class_virtual_properties.get(index)?;
//Get source property
let variant=properties.get(virtual_property.property)?;
//Transform Source property with provided function
(virtual_property.access)(variant)
None=>None,
};
Ok(f)
}
}