Compare commits

..

13 Commits

3 changed files with 38 additions and 38 deletions

2
Cargo.lock generated
View File

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

View File

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

View File

@ -96,6 +96,42 @@ 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{ 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|
@ -134,39 +170,3 @@ 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(())
}