Compare commits

..

9 Commits

3 changed files with 38 additions and 38 deletions

2
Cargo.lock generated
View File

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

View File

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

View File

@ -96,42 +96,6 @@ impl ScriptConnection{
}
}
fn wait_thread(lua:&mlua::Lua,this:ScriptSignal)->Result<(),mlua::Error>{
Ok(this.wait(lua.current_thread()))
}
// This is used to avoid calling coroutine.yield from the rust side.
const LUA_WAIT:&str=
"local coroutine_yield=coroutine.yield
local wait_thread=wait_thread
return function(signal)
wait_thread(signal)
return coroutine_yield()
end";
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
let coroutine_table=globals.get::<mlua::Table>("coroutine")?;
let wait_thread=lua.create_function(wait_thread)?;
//create wait function environment
let wait_env=lua.create_table()?;
wait_env.raw_set("coroutine",coroutine_table)?;
wait_env.raw_set("wait_thread",wait_thread)?;
//construct wait function from Lua code
let wait=lua.load(LUA_WAIT)
.set_name("wait")
.set_environment(wait_env)
.call::<mlua::Function>(())?;
lua.register_userdata_type::<ScriptSignal>(|reg|{
reg.add_field("Wait",wait);
mlua::UserData::register(reg);
})?;
Ok(())
}
impl mlua::UserData for ScriptSignal{
fn add_methods<M:mlua::UserDataMethods<Self>>(methods:&mut M){
methods.add_method("Connect",|_lua,this,f:mlua::Function|
@ -170,3 +134,39 @@ impl mlua::UserData for ScriptConnection{
});
}
}
fn wait_thread(lua:&mlua::Lua,this:ScriptSignal)->Result<(),mlua::Error>{
Ok(this.wait(lua.current_thread()))
}
// This is used to avoid calling coroutine.yield from the rust side.
const LUA_WAIT:&str=
"local coroutine_yield=coroutine.yield
local wait_thread=wait_thread
return function(signal)
wait_thread(signal)
return coroutine_yield()
end";
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
let coroutine_table=globals.get::<mlua::Table>("coroutine")?;
let wait_thread=lua.create_function(wait_thread)?;
//create wait function environment
let wait_env=lua.create_table()?;
wait_env.raw_set("coroutine",coroutine_table)?;
wait_env.raw_set("wait_thread",wait_thread)?;
//construct wait function from Lua code
let wait=lua.load(LUA_WAIT)
.set_name("wait")
.set_environment(wait_env)
.call::<mlua::Function>(())?;
lua.register_userdata_type::<ScriptSignal>(|reg|{
reg.add_field("Wait",wait);
mlua::UserData::register(reg);
})?;
Ok(())
}