Compare commits
2 Commits
c162bb34d7
...
61b9aaba84
Author | SHA1 | Date | |
---|---|---|---|
61b9aaba84 | |||
22d6087b3c |
@ -33,7 +33,6 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{
|
|||||||
|
|
||||||
#[cfg(feature="run-service")]
|
#[cfg(feature="run-service")]
|
||||||
crate::scheduler::set_globals(lua,&globals)?;
|
crate::scheduler::set_globals(lua,&globals)?;
|
||||||
super::script_signal::set_globals(lua,&globals)?;
|
|
||||||
super::r#enum::set_globals(lua,&globals)?;
|
super::r#enum::set_globals(lua,&globals)?;
|
||||||
super::color3::set_globals(lua,&globals)?;
|
super::color3::set_globals(lua,&globals)?;
|
||||||
super::vector3::set_globals(lua,&globals)?;
|
super::vector3::set_globals(lua,&globals)?;
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
use std::{cell::RefCell,rc::Rc};
|
use std::{cell::RefCell,rc::Rc};
|
||||||
|
|
||||||
use mlua::UserDataFields;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct FunctionList{
|
struct FunctionList{
|
||||||
functions:Vec<mlua::Function>,
|
functions:Vec<mlua::Function>,
|
||||||
@ -43,9 +41,10 @@ pub struct ScriptSignal{
|
|||||||
// Emulate the garbage roblox api.
|
// Emulate the garbage roblox api.
|
||||||
// ScriptConnection should not exist.
|
// ScriptConnection should not exist.
|
||||||
// :Disconnect should be a method on ScriptSignal, and this would be avoided entirely.
|
// :Disconnect should be a method on ScriptSignal, and this would be avoided entirely.
|
||||||
connections:RcFunctionList,
|
callbacks:RcFunctionList,
|
||||||
once:RcFunctionList,
|
once:RcFunctionList,
|
||||||
wait:Rc<RefCell<Vec<mlua::Thread>>>,
|
wait:Rc<RefCell<Vec<mlua::Thread>>>,
|
||||||
|
wait_function:std::cell::OnceCell<mlua::Function>,
|
||||||
}
|
}
|
||||||
pub struct ScriptConnection{
|
pub struct ScriptConnection{
|
||||||
connection:RcFunctionList,
|
connection:RcFunctionList,
|
||||||
@ -54,13 +53,14 @@ pub struct ScriptConnection{
|
|||||||
impl ScriptSignal{
|
impl ScriptSignal{
|
||||||
pub fn new()->Self{
|
pub fn new()->Self{
|
||||||
Self{
|
Self{
|
||||||
connections:RcFunctionList::new(),
|
callbacks:RcFunctionList::new(),
|
||||||
once:RcFunctionList::new(),
|
once:RcFunctionList::new(),
|
||||||
wait:Rc::new(RefCell::new(Vec::new())),
|
wait:Rc::new(RefCell::new(Vec::new())),
|
||||||
|
wait_function:std::cell::OnceCell::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn fire(&self,args:&mlua::MultiValue){
|
pub fn fire(&self,args:&mlua::MultiValue){
|
||||||
self.connections.fire(args);
|
self.callbacks.fire(args);
|
||||||
//Replace the FunctionList with an empty one and drop the borrow
|
//Replace the FunctionList with an empty one and drop the borrow
|
||||||
let once=std::mem::replace(&mut *self.once.functions.borrow_mut(),FunctionList::new());
|
let once=std::mem::replace(&mut *self.once.functions.borrow_mut(),FunctionList::new());
|
||||||
once.fire(args);
|
once.fire(args);
|
||||||
@ -73,9 +73,9 @@ impl ScriptSignal{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn connect(&self,function:mlua::Function)->ScriptConnection{
|
pub fn connect(&self,function:mlua::Function)->ScriptConnection{
|
||||||
self.connections.functions.borrow_mut().functions.push(function.clone());
|
self.callbacks.functions.borrow_mut().functions.push(function.clone());
|
||||||
ScriptConnection{
|
ScriptConnection{
|
||||||
connection:self.connections.clone(),
|
connection:self.callbacks.clone(),
|
||||||
function,
|
function,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,8 +109,13 @@ return function()
|
|||||||
return coroutine_yield()
|
return coroutine_yield()
|
||||||
end";
|
end";
|
||||||
|
|
||||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
impl mlua::UserData for ScriptSignal{
|
||||||
let coroutine_table=globals.get::<mlua::Table>("coroutine")?;
|
fn add_fields<F:mlua::UserDataFields<Self>>(fields:&mut F){
|
||||||
|
fields.add_field_method_get("Wait",|lua,this|{
|
||||||
|
Ok(match this.wait_function.get(){
|
||||||
|
Some(f)=>f.clone(),
|
||||||
|
None=>{
|
||||||
|
let coroutine_table=lua.globals().get::<mlua::Table>("coroutine")?;
|
||||||
let wait_thread=lua.create_function(wait_thread)?;
|
let wait_thread=lua.create_function(wait_thread)?;
|
||||||
|
|
||||||
//create wait function environment
|
//create wait function environment
|
||||||
@ -124,14 +129,12 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
|||||||
.set_environment(wait_env)
|
.set_environment(wait_env)
|
||||||
.call::<mlua::Function>(())?;
|
.call::<mlua::Function>(())?;
|
||||||
|
|
||||||
lua.register_userdata_type::<ScriptSignal>(|reg|{
|
this.wait_function.set(wait.clone()).unwrap();
|
||||||
reg.add_field("Wait",wait);
|
wait
|
||||||
})?;
|
}
|
||||||
|
})
|
||||||
Ok(())
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
impl mlua::UserData for ScriptSignal{
|
|
||||||
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
|
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
|
||||||
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))
|
||||||
|
Loading…
Reference in New Issue
Block a user