wip: rewrite ScriptSignal

This commit is contained in:
Quaternions 2024-10-18 11:09:00 -07:00
parent ded3cb4700
commit 22d6087b3c

View File

@ -1,44 +1,96 @@
use std::{cell::RefCell,rc::Rc}; use std::{cell::RefCell,rc::Rc};
#[derive(Clone)] #[derive(Clone)]
pub struct ScriptSignal{ struct FunctionList{
// Emulate the garbage roblox api. functions:Vec<mlua::Function>,
// 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{ impl FunctionList{
signal:ScriptSignal,
function:mlua::Function,
}
impl ScriptSignal{
pub fn new()->Self{ pub fn new()->Self{
Self{ Self{
callbacks:Rc::new(RefCell::new(Vec::new())), functions:Vec::new(),
} }
} }
// This eats the Lua error // This eats the Lua error
pub fn fire(&self,args:mlua::MultiValue){ pub fn fire(self,args:&mlua::MultiValue){
// Make a copy of the list in case Lua attempts to modify it during the loop // 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 self.functions{
for function in functions{
//wee let's allocate for our function calls //wee let's allocate for our function calls
if let Err(e)=function.call::<mlua::MultiValue>(args.clone()){ if let Err(e)=function.call::<mlua::MultiValue>(args.clone()){
println!("Script Signal Error: {e}"); println!("Script Signal Error: {e}");
} }
} }
} }
}
#[derive(Clone)]
struct RcFunctionList{
functions:Rc<RefCell<FunctionList>>,
}
impl RcFunctionList{
pub fn new()->Self{
Self{
functions:Rc::new(RefCell::new(FunctionList::new())),
}
}
pub fn fire(&self,args:&mlua::MultiValue){
// Make a copy of the list in case Lua attempts to modify it during the loop
self.functions.borrow().clone().fire(args)
}
}
#[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:RcFunctionList,
once:RcFunctionList,
wait:Rc<RefCell<Vec<mlua::Thread>>>,
}
pub struct ScriptConnection{
connection:RcFunctionList,
function:mlua::Function,
}
impl ScriptSignal{
pub fn new()->Self{
Self{
callbacks:RcFunctionList::new(),
once:RcFunctionList::new(),
wait:Rc::new(RefCell::new(Vec::new())),
}
}
pub fn fire(&self,args:&mlua::MultiValue){
self.callbacks.fire(args);
//Replace the FunctionList with an empty one and drop the borrow
let once=std::mem::replace(&mut *self.once.functions.borrow_mut(),FunctionList::new());
once.fire(args);
//resume threads waiting for this signal
let threads=std::mem::replace(&mut *self.wait.borrow_mut(),Vec::new());
for thread in threads{
if let Err(e)=thread.resume::<mlua::MultiValue>(args.clone()){
println!("Script Signal thread resume Error: {e}");
}
}
}
pub fn connect(&self,function:mlua::Function)->ScriptConnection{ pub fn connect(&self,function:mlua::Function)->ScriptConnection{
self.callbacks.borrow_mut().push(function.clone()); self.callbacks.functions.borrow_mut().functions.push(function.clone());
ScriptConnection{ ScriptConnection{
signal:self.clone(), connection:self.callbacks.clone(),
function, function,
} }
} }
pub fn once(&self,function:mlua::Function)->ScriptConnection{
self.once.functions.borrow_mut().functions.push(function.clone());
ScriptConnection{
connection:self.once.clone(),
function,
}
}
pub fn wait(&self,thread:mlua::Thread){
self.wait.borrow_mut().push(thread);
}
} }
impl ScriptConnection{ impl ScriptConnection{
pub fn position(&self)->Option<usize>{ pub fn position(&self)->Option<usize>{
self.signal.callbacks.borrow().iter().position(|function|function==&self.function) self.connection.functions.borrow().functions.iter().position(|function|function==&self.function)
} }
} }
@ -47,6 +99,13 @@ impl mlua::UserData for ScriptSignal{
methods.add_method("Connect",|_lua,this,f:mlua::Function| methods.add_method("Connect",|_lua,this,f:mlua::Function|
Ok(this.connect(f)) Ok(this.connect(f))
); );
methods.add_method("Once",|_lua,this,f:mlua::Function|
Ok(this.once(f))
);
methods.add_method("Wait",|lua,this,()|
Ok(this.wait(lua.current_thread()))
todo!("coroutine.yield");
);
// Fire is not allowed to be called from Lua // Fire is not allowed to be called from Lua
// methods.add_method("Fire",|_lua,this,args:mlua::MultiValue| // methods.add_method("Fire",|_lua,this,args:mlua::MultiValue|
// Ok(this.fire(args)) // Ok(this.fire(args))
@ -71,7 +130,7 @@ impl mlua::UserData for ScriptConnection{
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){ fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
methods.add_method("Disconnect",|_,this,_:()|{ methods.add_method("Disconnect",|_,this,_:()|{
if let Some(index)=this.position(){ if let Some(index)=this.position(){
this.signal.callbacks.borrow_mut().remove(index); this.connection.functions.borrow_mut().functions.remove(index);
} }
Ok(()) Ok(())
}); });