Compare commits
6 Commits
73dca41859
...
471b1fd254
Author | SHA1 | Date | |
---|---|---|---|
471b1fd254 | |||
dbb7092e34 | |||
aaa0f160d5 | |||
6f7e3ff53f | |||
3dd46258b2 | |||
da169ade70 |
@ -8,7 +8,8 @@ use super::vector3::Vector3;
|
|||||||
|
|
||||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
||||||
//class functions store
|
//class functions store
|
||||||
lua.set_app_data(ClassFunctions::default());
|
lua.set_app_data(ClassMethodsStore::default());
|
||||||
|
lua.set_app_data(InstanceValueStore::default());
|
||||||
|
|
||||||
let instance_table=lua.create_table()?;
|
let instance_table=lua.create_table()?;
|
||||||
|
|
||||||
@ -30,14 +31,10 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LMAO look at this function!
|
// LMAO look at this function!
|
||||||
fn dom_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->mlua::Result<T>{
|
pub 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"))?;
|
let mut dom=lua.app_data_mut::<&'static mut WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?;
|
||||||
f(&mut *dom)
|
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 cf=lua.app_data_mut::<ClassFunctions>().ok_or(mlua::Error::runtime("ClassFunctions missing"))?;
|
|
||||||
f(&mut *cf)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn coerce_float32(value:&mlua::Value)->Option<f32>{
|
fn coerce_float32(value:&mlua::Value)->Option<f32>{
|
||||||
match value{
|
match value{
|
||||||
@ -262,7 +259,7 @@ impl mlua::UserData for Instance{
|
|||||||
other=>return Err(mlua::Error::runtime(format!("Instance.__index Unsupported property type instance={} index={index_str} value={other:?}",instance.name))),
|
other=>return Err(mlua::Error::runtime(format!("Instance.__index Unsupported property type instance={} index={index_str} value={other:?}",instance.name))),
|
||||||
}
|
}
|
||||||
//find a function with a matching name
|
//find a function with a matching name
|
||||||
if let Some(function)=class_functions_mut(lua,|cf|{
|
if let Some(function)=class_methods_store_mut(lua,|cf|{
|
||||||
let mut iter=SuperClassIter{
|
let mut iter=SuperClassIter{
|
||||||
database:db,
|
database:db,
|
||||||
descriptor:Some(class),
|
descriptor:Some(class),
|
||||||
@ -275,6 +272,17 @@ impl mlua::UserData for Instance{
|
|||||||
})?{
|
})?{
|
||||||
return function.into_lua(lua);
|
return function.into_lua(lua);
|
||||||
}
|
}
|
||||||
|
//find or create an associated userdata object
|
||||||
|
|
||||||
|
if let Some(value)=instance_value_store_mut(lua,|ivs|{
|
||||||
|
//TODO: walk class tree somehow
|
||||||
|
match ivs.get_or_create_instance_values(&instance){
|
||||||
|
Some(mut instance_values)=>instance_values.get_or_create_value(lua,index_str),
|
||||||
|
None=>Ok(None)
|
||||||
|
}
|
||||||
|
})?{
|
||||||
|
return value.into_lua(lua);
|
||||||
|
}
|
||||||
//find a child with a matching name
|
//find a child with a matching name
|
||||||
find_first_child(dom,instance,index_str)
|
find_first_child(dom,instance,index_str)
|
||||||
.map(|instance|Instance::new(instance.referent()))
|
.map(|instance|Instance::new(instance.referent()))
|
||||||
@ -369,12 +377,13 @@ static CLASS_FUNCTION_DATABASE:CFD=phf::phf_map!{
|
|||||||
"GetService"=>cf!(|lua,_this,service:mlua::String|{
|
"GetService"=>cf!(|lua,_this,service:mlua::String|{
|
||||||
dom_mut(lua,|dom|{
|
dom_mut(lua,|dom|{
|
||||||
//dom.root_ref()==this.referent ?
|
//dom.root_ref()==this.referent ?
|
||||||
match &*service.to_str()?{
|
let service=&*service.to_str()?;
|
||||||
"Lighting"=>{
|
match service{
|
||||||
let referent=find_first_child_of_class(dom,dom.root(),"Lighting")
|
"Lighting"|"RunService"=>{
|
||||||
|
let referent=find_first_child_of_class(dom,dom.root(),service)
|
||||||
.map(|instance|instance.referent())
|
.map(|instance|instance.referent())
|
||||||
.unwrap_or_else(||
|
.unwrap_or_else(||
|
||||||
dom.insert(dom.root_ref(),InstanceBuilder::new("Lighting"))
|
dom.insert(dom.root_ref(),InstanceBuilder::new(service))
|
||||||
);
|
);
|
||||||
Ok(Instance::new(referent))
|
Ok(Instance::new(referent))
|
||||||
},
|
},
|
||||||
@ -391,14 +400,14 @@ static CLASS_FUNCTION_DATABASE:CFD=phf::phf_map!{
|
|||||||
/// A store of created functions for each Roblox class.
|
/// A store of created functions for each Roblox class.
|
||||||
/// Functions are created the first time they are accessed and stored in this data structure.
|
/// Functions are created the first time they are accessed and stored in this data structure.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct ClassFunctions{
|
struct ClassMethodsStore{
|
||||||
classes:HashMap<&'static str,//ClassName
|
classes:HashMap<&'static str,//ClassName
|
||||||
HashMap<&'static str,//Method name
|
HashMap<&'static str,//Method name
|
||||||
mlua::Function
|
mlua::Function
|
||||||
>
|
>
|
||||||
>
|
>
|
||||||
}
|
}
|
||||||
impl ClassFunctions{
|
impl ClassMethodsStore{
|
||||||
/// return self.classes[class] or create the ClassMethods and then return it
|
/// return self.classes[class] or create the ClassMethods and then return it
|
||||||
fn get_or_create_class_methods(&mut self,class:&str)->Option<ClassMethods>{
|
fn get_or_create_class_methods(&mut self,class:&str)->Option<ClassMethods>{
|
||||||
// Use get_entry to get the &'static str keys of the database
|
// Use get_entry to get the &'static str keys of the database
|
||||||
@ -433,6 +442,10 @@ impl ClassMethods<'_>{
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn class_methods_store_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut ClassMethodsStore)->mlua::Result<T>)->mlua::Result<T>{
|
||||||
|
let mut cf=lua.app_data_mut::<ClassMethodsStore>().ok_or(mlua::Error::runtime("ClassMethodsStore missing"))?;
|
||||||
|
f(&mut *cf)
|
||||||
|
}
|
||||||
|
|
||||||
/// A virtual property pointer definition shorthand.
|
/// A virtual property pointer definition shorthand.
|
||||||
type VirtualPropertyFunctionPointer=fn(&rbx_types::Variant)->Option<rbx_types::Variant>;
|
type VirtualPropertyFunctionPointer=fn(&rbx_types::Variant)->Option<rbx_types::Variant>;
|
||||||
@ -481,3 +494,64 @@ fn find_virtual_property(
|
|||||||
//Transform Source property with provided function
|
//Transform Source property with provided function
|
||||||
(virtual_property.pointer)(variant)
|
(virtual_property.pointer)(variant)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lazy-loaded per-instance userdata values
|
||||||
|
// This whole thing is a bad idea and a garbage collection nightmare.
|
||||||
|
// TODO: recreate rbx_dom_weak with my own instance type that owns this data.
|
||||||
|
type CreateUserData=fn(&mlua::Lua)->mlua::Result<mlua::AnyUserData>;
|
||||||
|
type LUD=phf::Map<&'static str,// Class name
|
||||||
|
phf::Map<&'static str,// Value name
|
||||||
|
CreateUserData
|
||||||
|
>
|
||||||
|
>;
|
||||||
|
static LAZY_USER_DATA:LUD=phf::phf_map!{
|
||||||
|
"RunService"=>phf::phf_map!{
|
||||||
|
"RenderStepped"=>|lua|{
|
||||||
|
lua.create_any_userdata(super::script_signal::ScriptSignal::new())
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct InstanceValueStore{
|
||||||
|
values:HashMap<Ref,
|
||||||
|
HashMap<&'static str,
|
||||||
|
mlua::AnyUserData
|
||||||
|
>
|
||||||
|
>,
|
||||||
|
}
|
||||||
|
pub struct InstanceValues<'a>{
|
||||||
|
named_values:&'static phf::Map<&'static str,CreateUserData>,
|
||||||
|
values:&'a mut HashMap<&'static str,mlua::AnyUserData>,
|
||||||
|
}
|
||||||
|
impl InstanceValueStore{
|
||||||
|
pub fn get_or_create_instance_values(&mut self,instance:&rbx_dom_weak::Instance)->Option<InstanceValues>{
|
||||||
|
LAZY_USER_DATA.get(instance.class.as_str())
|
||||||
|
.map(|named_values|
|
||||||
|
InstanceValues{
|
||||||
|
named_values,
|
||||||
|
values:self.values.entry(instance.referent())
|
||||||
|
.or_insert_with(||HashMap::new()),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl InstanceValues<'_>{
|
||||||
|
pub fn get_or_create_value(&mut self,lua:&mlua::Lua,index:&str)->mlua::Result<Option<mlua::AnyUserData>>{
|
||||||
|
Ok(match self.named_values.get_entry(index){
|
||||||
|
Some((&static_index_str,&function_pointer))=>Some(
|
||||||
|
match self.values.entry(static_index_str){
|
||||||
|
Entry::Occupied(entry)=>entry.get().clone(),
|
||||||
|
Entry::Vacant(entry)=>entry.insert(
|
||||||
|
function_pointer(lua)?
|
||||||
|
).clone(),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
None=>None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn instance_value_store_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut InstanceValueStore)->mlua::Result<T>)->mlua::Result<T>{
|
||||||
|
let mut cf=lua.app_data_mut::<InstanceValueStore>().ok_or(mlua::Error::runtime("InstanceValueStore missing"))?;
|
||||||
|
f(&mut *cf)
|
||||||
|
}
|
||||||
|
@ -7,7 +7,8 @@ mod color3;
|
|||||||
mod cframe;
|
mod cframe;
|
||||||
mod vector3;
|
mod vector3;
|
||||||
pub mod instance;
|
pub mod instance;
|
||||||
mod number_sequence;
|
mod script_signal;
|
||||||
mod color_sequence;
|
mod color_sequence;
|
||||||
|
mod number_sequence;
|
||||||
|
|
||||||
pub use runner::{Runner,Runnable,Error};
|
pub use runner::{Runner,Runnable,Error};
|
||||||
|
@ -120,4 +120,23 @@ impl Runnable<'_>{
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
#[cfg(feature="run-service")]
|
||||||
|
pub fn run_service_step(&self)->Result<(),mlua::Error>{
|
||||||
|
let render_stepped=super::instance::dom_mut(&self.lua,|dom|{
|
||||||
|
let run_service=super::instance::find_first_child_of_class(dom,dom.root(),"RunService").ok_or_else(||mlua::Error::runtime("RunService missing"))?;
|
||||||
|
super::instance::instance_value_store_mut(&self.lua,|instance_value_store|{
|
||||||
|
//unwrap because I trust my find_first_child_of_class function to
|
||||||
|
let mut instance_values=instance_value_store.get_or_create_instance_values(run_service).unwrap();
|
||||||
|
let render_stepped=instance_values.get_or_create_value(&self.lua,"RenderStepped")?;
|
||||||
|
//let stepped=instance_values.get_or_create_value(&self.lua,"Stepped")?;
|
||||||
|
//let heartbeat=instance_values.get_or_create_value(&self.lua,"Heartbeat")?;
|
||||||
|
Ok(render_stepped)
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
if let Some(render_stepped)=render_stepped{
|
||||||
|
let signal:&super::script_signal::ScriptSignal=&*render_stepped.borrow()?;
|
||||||
|
signal.fire(mlua::MultiValue::new());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
79
src/runner/script_signal.rs
Normal file
79
src/runner/script_signal.rs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
use std::{cell::RefCell,rc::Rc};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ScriptSignal{
|
||||||
|
// Emulate the garbage roblox api.
|
||||||
|
// ScriptConnection should not exist.
|
||||||
|
// :Disconnect should be a method on ScriptSignal, and this would be avoided entirely.
|
||||||
|
callbacks:Rc<RefCell<Vec<mlua::Function>>>,
|
||||||
|
}
|
||||||
|
pub struct ScriptConnection{
|
||||||
|
signal:ScriptSignal,
|
||||||
|
function:mlua::Function,
|
||||||
|
}
|
||||||
|
impl ScriptSignal{
|
||||||
|
pub fn new()->Self{
|
||||||
|
Self{
|
||||||
|
callbacks:Rc::new(RefCell::new(Vec::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// This eats the Lua error
|
||||||
|
pub fn fire(&self,args:mlua::MultiValue){
|
||||||
|
// Make a copy of the list in case Lua attempts to modify it during the loop
|
||||||
|
let functions=self.callbacks.borrow().clone();
|
||||||
|
for function in functions{
|
||||||
|
//wee let's allocate for our function calls
|
||||||
|
if let Err(e)=function.call::<mlua::MultiValue>(args.clone()){
|
||||||
|
println!("Script Signal Error: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn connect(&self,function:mlua::Function)->ScriptConnection{
|
||||||
|
self.callbacks.borrow_mut().push(function.clone());
|
||||||
|
ScriptConnection{
|
||||||
|
signal:self.clone(),
|
||||||
|
function,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl ScriptConnection{
|
||||||
|
pub fn position(&self)->Option<usize>{
|
||||||
|
self.signal.callbacks.borrow().iter().position(|function|function==&self.function)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl mlua::UserData for ScriptSignal{
|
||||||
|
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
|
||||||
|
methods.add_method("Connect",|_lua,this,f:mlua::Function|
|
||||||
|
Ok(this.connect(f))
|
||||||
|
);
|
||||||
|
// Fire is not allowed to be called from Lua
|
||||||
|
// methods.add_method("Fire",|_lua,this,args:mlua::MultiValue|
|
||||||
|
// Ok(this.fire(args))
|
||||||
|
// );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl mlua::FromLua for ScriptSignal{
|
||||||
|
fn from_lua(value:mlua::Value,_lua:&mlua::Lua)->Result<Self,mlua::Error>{
|
||||||
|
match value{
|
||||||
|
mlua::Value::UserData(ud)=>Ok(ud.borrow::<Self>()?.clone()),
|
||||||
|
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!(ScriptSignal),other))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl mlua::UserData for ScriptConnection{
|
||||||
|
fn add_fields<F:mlua::UserDataFields<Self>>(fields:&mut F){
|
||||||
|
fields.add_field_method_get("Connected",|_,this|{
|
||||||
|
Ok(this.position().is_some())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
|
||||||
|
methods.add_method("Disconnect",|_,this,_:()|{
|
||||||
|
if let Some(index)=this.position(){
|
||||||
|
this.signal.callbacks.borrow_mut().remove(index);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -61,8 +61,8 @@ pub fn scheduler_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut crate::scheduler::S
|
|||||||
let mut scheduler=lua.app_data_mut::<crate::scheduler::Scheduler>().ok_or(mlua::Error::runtime("Scheduler missing"))?;
|
let mut scheduler=lua.app_data_mut::<crate::scheduler::Scheduler>().ok_or(mlua::Error::runtime("Scheduler missing"))?;
|
||||||
f(&mut *scheduler)
|
f(&mut *scheduler)
|
||||||
}
|
}
|
||||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
|
||||||
let schedule_thread=lua.create_function(|lua,dt:mlua::Value|{
|
fn schedule_thread(lua:&mlua::Lua,dt:mlua::Value)->Result<(),mlua::Error>{
|
||||||
let delay=match dt{
|
let delay=match dt{
|
||||||
mlua::Value::Integer(i)=>i.max(0) as u64*60,
|
mlua::Value::Integer(i)=>i.max(0) as u64*60,
|
||||||
mlua::Value::Number(f)=>{
|
mlua::Value::Number(f)=>{
|
||||||
@ -85,21 +85,32 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
|||||||
scheduler.schedule_thread(delay.max(2),lua.current_thread());
|
scheduler.schedule_thread(delay.max(2),lua.current_thread());
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
})?;
|
}
|
||||||
let wait_env=lua.create_table()?;
|
|
||||||
wait_env.raw_set("coroutine",globals.get::<mlua::Table>("coroutine")?)?;
|
// This is used to avoid calling coroutine.yield from the rust side.
|
||||||
wait_env.raw_set("schedule_thread",schedule_thread)?;
|
const LUA_WAIT:&str=
|
||||||
let wait=lua.load("
|
"local coroutine_yield=coroutine.yield
|
||||||
local coroutine_yield=coroutine.yield
|
|
||||||
local schedule_thread=schedule_thread
|
local schedule_thread=schedule_thread
|
||||||
return function(dt)
|
return function(dt)
|
||||||
schedule_thread(dt)
|
schedule_thread(dt)
|
||||||
return coroutine_yield()
|
return coroutine_yield()
|
||||||
end
|
end";
|
||||||
")
|
|
||||||
|
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
||||||
|
let coroutine_table=globals.get::<mlua::Table>("coroutine")?;
|
||||||
|
let schedule_thread=lua.create_function(schedule_thread)?;
|
||||||
|
|
||||||
|
//create wait function environment
|
||||||
|
let wait_env=lua.create_table()?;
|
||||||
|
wait_env.raw_set("coroutine",coroutine_table)?;
|
||||||
|
wait_env.raw_set("schedule_thread",schedule_thread)?;
|
||||||
|
|
||||||
|
//construct wait function from Lua code
|
||||||
|
let wait=lua.load(LUA_WAIT)
|
||||||
.set_name("wait")
|
.set_name("wait")
|
||||||
.set_environment(wait_env)
|
.set_environment(wait_env)
|
||||||
.call::<mlua::Function>(())?;
|
.call::<mlua::Function>(())?;
|
||||||
|
|
||||||
globals.raw_set("wait",wait)?;
|
globals.raw_set("wait",wait)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user