Compare commits
7 Commits
03e9d989f8
...
8d99c788ef
Author | SHA1 | Date | |
---|---|---|---|
8d99c788ef | |||
43ab764b18 | |||
bffc5bb8f1 | |||
f8f659e8ce | |||
135103364d | |||
368ce7da9e | |||
13621022e1 |
2
.cargo/config.toml
Normal file
2
.cargo/config.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[registries.strafesnet]
|
||||
index = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -402,7 +402,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "roblox_emulator"
|
||||
version = "0.4.5"
|
||||
version = "0.4.6"
|
||||
dependencies = [
|
||||
"glam",
|
||||
"mlua",
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "roblox_emulator"
|
||||
version = "0.4.5"
|
||||
version = "0.4.6"
|
||||
edition = "2021"
|
||||
repository = "https://git.itzana.me/StrafesNET/roblox_emulator"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
@ -369,12 +369,13 @@ static CLASS_FUNCTION_DATABASE:CFD=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")
|
||||
let service=&*service.to_str()?;
|
||||
match service{
|
||||
"Lighting"|"RunService"=>{
|
||||
let referent=find_first_child_of_class(dom,dom.root(),service)
|
||||
.map(|instance|instance.referent())
|
||||
.unwrap_or_else(||
|
||||
dom.insert(dom.root_ref(),InstanceBuilder::new("Lighting"))
|
||||
dom.insert(dom.root_ref(),InstanceBuilder::new(service))
|
||||
);
|
||||
Ok(Instance::new(referent))
|
||||
},
|
||||
|
@ -7,7 +7,8 @@ mod color3;
|
||||
mod cframe;
|
||||
mod vector3;
|
||||
pub mod instance;
|
||||
mod number_sequence;
|
||||
mod script_signal;
|
||||
mod color_sequence;
|
||||
mod number_sequence;
|
||||
|
||||
pub use runner::{Runner,Error};
|
||||
pub use runner::{Runner,Runnable,Error};
|
||||
|
72
src/runner/script_signal.rs
Normal file
72
src/runner/script_signal.rs
Normal file
@ -0,0 +1,72 @@
|
||||
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 allocate_twice=self.callbacks.borrow().clone();
|
||||
for function in allocate_twice{
|
||||
//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))
|
||||
// );
|
||||
}
|
||||
}
|
||||
//type_from_lua_userdata!(ScriptSignal);
|
||||
|
||||
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(())
|
||||
});
|
||||
}
|
||||
}
|
@ -57,27 +57,34 @@ impl Scheduler{
|
||||
}
|
||||
}
|
||||
|
||||
fn coerce_float64(value:&mlua::Value)->Option<f64>{
|
||||
match value{
|
||||
&mlua::Value::Integer(i)=>Some(i as f64),
|
||||
&mlua::Value::Number(f)=>Some(f),
|
||||
_=>None,
|
||||
}
|
||||
}
|
||||
pub fn scheduler_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut crate::scheduler::Scheduler)->mlua::Result<T>)->mlua::Result<T>{
|
||||
let mut scheduler=lua.app_data_mut::<crate::scheduler::Scheduler>().ok_or(mlua::Error::runtime("Scheduler missing"))?;
|
||||
f(&mut *scheduler)
|
||||
}
|
||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
||||
let schedule_thread=lua.create_function(move|lua,dt:mlua::Value|{
|
||||
let delay=coerce_float64(&dt).ok_or(mlua::Error::runtime("Expected float"))?.max(0.0)*60.0;
|
||||
if delay<u64::MAX as f64{
|
||||
scheduler_mut(lua,|scheduler|{
|
||||
scheduler.schedule_thread((delay as u64).max(2),lua.current_thread());
|
||||
Ok(())
|
||||
}).unwrap();
|
||||
let schedule_thread=lua.create_function(|lua,dt:mlua::Value|{
|
||||
let delay=match dt{
|
||||
mlua::Value::Integer(i)=>i.max(0) as u64*60,
|
||||
mlua::Value::Number(f)=>{
|
||||
let delay=f.max(0.0)*60.0;
|
||||
match delay.classify(){
|
||||
std::num::FpCategory::Nan=>Err(mlua::Error::runtime("NaN"))?,
|
||||
// cases where the number is too large to schedule
|
||||
std::num::FpCategory::Infinite=>return Ok(()),
|
||||
std::num::FpCategory::Normal=>if (u64::MAX as f64)<delay{
|
||||
return Ok(());
|
||||
},
|
||||
_=>(),
|
||||
}
|
||||
delay as u64
|
||||
},
|
||||
mlua::Value::Nil=>0,
|
||||
_=>Err(mlua::Error::runtime("Expected float"))?,
|
||||
};
|
||||
scheduler_mut(lua,|scheduler|{
|
||||
scheduler.schedule_thread(delay.max(2),lua.current_thread());
|
||||
Ok(())
|
||||
})
|
||||
})?;
|
||||
let wait_env=lua.create_table()?;
|
||||
wait_env.raw_set("coroutine",globals.get::<mlua::Table>("coroutine")?)?;
|
||||
|
Loading…
Reference in New Issue
Block a user