Compare commits
1 Commits
master
...
virtual-pr
Author | SHA1 | Date | |
---|---|---|---|
e799f6a094 |
@ -1,2 +0,0 @@
|
||||
[registries.strafesnet]
|
||||
index = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/"
|
77
Cargo.lock
generated
77
Cargo.lock
generated
@ -16,9 +16,9 @@ checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.4.0"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
@ -40,9 +40,9 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||
|
||||
[[package]]
|
||||
name = "blake3"
|
||||
version = "1.5.5"
|
||||
version = "1.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8ee0c1824c4dea5b5f81736aff91bae041d2c07ee1192bec91054e10e3e601e"
|
||||
checksum = "d82033247fd8e890df8f740e407ad4d038debb9eb1f40533fffb32e7d17dc6f7"
|
||||
dependencies = [
|
||||
"arrayref",
|
||||
"arrayvec",
|
||||
@ -53,9 +53,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "bstr"
|
||||
version = "1.11.0"
|
||||
version = "1.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a68f1f47cdf0ec8ee4b941b2eee2a80cb796db73118c0dd09ac63fbe405be22"
|
||||
checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"serde",
|
||||
@ -69,9 +69,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.1"
|
||||
version = "1.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47"
|
||||
checksum = "2d74707dde2ba56f86ae90effb3b43ddd369504387e718014de010cec7959800"
|
||||
dependencies = [
|
||||
"shlex",
|
||||
]
|
||||
@ -88,12 +88,6 @@ version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.15"
|
||||
@ -107,9 +101,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "glam"
|
||||
version = "0.29.2"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc46dd3ec48fdd8e693a98d2b8bafae273a2d54c1de02a2a7e3d57d501f39677"
|
||||
checksum = "c28091a37a5d09b555cb6628fd954da299b536433834f5b8e59eba78e0cbbf8a"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
@ -119,9 +113,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.166"
|
||||
version = "0.2.158"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2ccc108bbc0b1331bd061864e7cd823c0cab660bbe6970e66e2c0614decde36"
|
||||
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
@ -145,9 +139,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luau0-src"
|
||||
version = "0.11.2+luau653"
|
||||
version = "0.10.3+luau640"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02313a53daf1fae25e82f7e7ca56180b72d1f08c514426672877cd957298201c"
|
||||
checksum = "2f39d12b514a676c943990cfbe6200fedcb9c293c8c9219d29be512a6969be92"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
@ -160,12 +154,11 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "mlua"
|
||||
version = "0.10.1"
|
||||
version = "0.10.0-beta.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ae9546e4a268c309804e8bbb7526e31cbfdedca7cd60ac1b987d0b212e0d876"
|
||||
checksum = "200dd4c8e5f81416d43a023b9921c3cbf01d828094b23a90b26826c3840ba4f3"
|
||||
dependencies = [
|
||||
"bstr",
|
||||
"either",
|
||||
"libloading",
|
||||
"mlua-sys",
|
||||
"num-traits",
|
||||
@ -175,9 +168,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mlua-sys"
|
||||
version = "0.6.5"
|
||||
version = "0.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "efa6bf1a64f06848749b7e7727417f4ec2121599e2a10ef0a8a3888b0e9a5a0d"
|
||||
checksum = "ebe026d6bd1583a9cf9080e189030ddaea7e6f5f0deb366a8e26f8a26c4135b8"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cfg-if",
|
||||
@ -267,9 +260,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.31"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
|
||||
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
@ -282,9 +275,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.92"
|
||||
version = "1.0.86"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
|
||||
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
@ -409,7 +402,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "roblox_emulator"
|
||||
version = "0.4.7"
|
||||
version = "0.4.4"
|
||||
dependencies = [
|
||||
"glam",
|
||||
"mlua",
|
||||
@ -434,18 +427,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.215"
|
||||
version = "1.0.210"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f"
|
||||
checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.215"
|
||||
version = "1.0.210"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0"
|
||||
checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -472,9 +465,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.89"
|
||||
version = "2.0.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e"
|
||||
checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -483,18 +476,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.69"
|
||||
version = "1.0.63"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
|
||||
checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.69"
|
||||
version = "1.0.63"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
||||
checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -503,9 +496,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.14"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
||||
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
|
@ -1,19 +1,15 @@
|
||||
[package]
|
||||
name = "roblox_emulator"
|
||||
version = "0.4.7"
|
||||
version = "0.4.4"
|
||||
edition = "2021"
|
||||
repository = "https://git.itzana.me/StrafesNET/roblox_emulator"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "Run embedded Luau scripts which manipulate the DOM."
|
||||
authors = ["Rhys Lloyd <krakow20@gmail.com>"]
|
||||
|
||||
[features]
|
||||
default=["run-service"]
|
||||
run-service=[]
|
||||
|
||||
[dependencies]
|
||||
glam = "0.29.0"
|
||||
mlua = { version = "0.10.1", features = ["luau"] }
|
||||
mlua = { version = "0.10.0-beta", features = ["luau"] }
|
||||
phf = { version = "0.11.2", features = ["macros"] }
|
||||
rbx_dom_weak = { version = "2.7.0", registry = "strafesnet" }
|
||||
rbx_reflection = { version = "4.7.0", registry = "strafesnet" }
|
||||
|
@ -1,7 +1,5 @@
|
||||
pub mod runner;
|
||||
pub mod context;
|
||||
#[cfg(feature="run-service")]
|
||||
pub(crate) mod scheduler;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
@ -1,31 +0,0 @@
|
||||
#[derive(Clone,Copy)]
|
||||
pub struct ColorSequence{}
|
||||
impl ColorSequence{
|
||||
pub const fn new()->Self{
|
||||
Self{}
|
||||
}
|
||||
}
|
||||
impl Into<rbx_types::ColorSequence> for ColorSequence{
|
||||
fn into(self)->rbx_types::ColorSequence{
|
||||
rbx_types::ColorSequence{
|
||||
keypoints:Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
||||
let number_sequence_table=lua.create_table()?;
|
||||
|
||||
number_sequence_table.raw_set("new",
|
||||
lua.create_function(|_,_:mlua::MultiValue|
|
||||
Ok(ColorSequence::new())
|
||||
)?
|
||||
)?;
|
||||
|
||||
globals.set("ColorSequence",number_sequence_table)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl mlua::UserData for ColorSequence{}
|
||||
type_from_lua_userdata!(ColorSequence);
|
@ -4,12 +4,11 @@ use mlua::{FromLua,FromLuaMulti,IntoLua,IntoLuaMulti};
|
||||
use rbx_types::Ref;
|
||||
use rbx_dom_weak::{InstanceBuilder,WeakDom};
|
||||
|
||||
use crate::runner::vector3::Vector3;
|
||||
use super::vector3::Vector3;
|
||||
|
||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
||||
//class functions store
|
||||
lua.set_app_data(ClassMethodsStore::default());
|
||||
lua.set_app_data(InstanceValueStore::default());
|
||||
lua.set_app_data(ClassFunctions::default());
|
||||
|
||||
let instance_table=lua.create_table()?;
|
||||
|
||||
@ -17,7 +16,7 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
||||
instance_table.raw_set("new",
|
||||
lua.create_function(|lua,(class_name,parent):(mlua::String,Option<Instance>)|{
|
||||
let class_name_str=&*class_name.to_str()?;
|
||||
let parent=parent.ok_or_else(||mlua::Error::runtime("Nil Parent not yet supported"))?;
|
||||
let parent=parent.ok_or(mlua::Error::runtime("Nil Parent not yet supported"))?;
|
||||
dom_mut(lua,|dom|{
|
||||
//TODO: Nil instances
|
||||
Ok(Instance::new(dom.insert(parent.referent,InstanceBuilder::new(class_name_str))))
|
||||
@ -31,10 +30,14 @@ pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
||||
}
|
||||
|
||||
// LMAO look at this function!
|
||||
pub fn dom_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->mlua::Result<T>{
|
||||
let mut dom=lua.app_data_mut::<&'static mut WeakDom>().ok_or_else(||mlua::Error::runtime("DataModel missing"))?;
|
||||
fn dom_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->mlua::Result<T>{
|
||||
let mut dom=lua.app_data_mut::<&'static mut WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?;
|
||||
f(&mut *dom)
|
||||
}
|
||||
fn class_functions_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut ClassFunctions)->mlua::Result<T>)->mlua::Result<T>{
|
||||
let mut cf=lua.app_data_mut::<ClassFunctions>().ok_or(mlua::Error::runtime("ClassFunctions missing"))?;
|
||||
f(&mut *cf)
|
||||
}
|
||||
|
||||
fn coerce_float32(value:&mlua::Value)->Option<f32>{
|
||||
match value{
|
||||
@ -89,10 +92,10 @@ impl Instance{
|
||||
Self{referent}
|
||||
}
|
||||
pub fn get<'a>(&self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref(self.referent).ok_or_else(||mlua::Error::runtime("Instance missing"))
|
||||
dom.get_by_ref(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
pub fn get_mut<'a>(&self,dom:&'a mut WeakDom)->mlua::Result<&'a mut rbx_dom_weak::Instance>{
|
||||
dom.get_by_ref_mut(self.referent).ok_or_else(||mlua::Error::runtime("Instance missing"))
|
||||
dom.get_by_ref_mut(self.referent).ok_or(mlua::Error::runtime("Instance missing"))
|
||||
}
|
||||
}
|
||||
type_from_lua_userdata!(Instance);
|
||||
@ -125,7 +128,7 @@ impl mlua::UserData for Instance{
|
||||
})
|
||||
});
|
||||
fields.add_field_method_set("Parent",|lua,this,val:Option<Instance>|{
|
||||
let parent=val.ok_or_else(||mlua::Error::runtime("Nil Parent not yet supported"))?;
|
||||
let parent=val.ok_or(mlua::Error::runtime("Nil Parent not yet supported"))?;
|
||||
dom_mut(lua,|dom|{
|
||||
dom.transfer_within(this.referent,parent.referent);
|
||||
Ok(())
|
||||
@ -231,7 +234,7 @@ impl mlua::UserData for Instance{
|
||||
let instance=this.get(dom)?;
|
||||
//println!("__index t={} i={index:?}",instance.name);
|
||||
let db=rbx_reflection_database::get();
|
||||
let class=db.classes.get(instance.class.as_str()).ok_or_else(||mlua::Error::runtime("Class missing"))?;
|
||||
let class=db.classes.get(instance.class.as_str()).ok_or(mlua::Error::runtime("Class missing"))?;
|
||||
//Find existing property
|
||||
match instance.properties.get(index_str)
|
||||
.cloned()
|
||||
@ -253,36 +256,26 @@ impl mlua::UserData for Instance{
|
||||
Some(rbx_types::Variant::Float32(val))=>return val.into_lua(lua),
|
||||
Some(rbx_types::Variant::Float64(val))=>return val.into_lua(lua),
|
||||
Some(rbx_types::Variant::Ref(val))=>return Instance::new(val).into_lua(lua),
|
||||
Some(rbx_types::Variant::CFrame(cf))=>return Into::<crate::runner::cframe::CFrame>::into(cf).into_lua(lua),
|
||||
Some(rbx_types::Variant::Vector3(v))=>return Into::<crate::runner::vector3::Vector3>::into(v).into_lua(lua),
|
||||
Some(rbx_types::Variant::CFrame(cf))=>return Into::<super::cframe::CFrame>::into(cf).into_lua(lua),
|
||||
Some(rbx_types::Variant::Vector3(v))=>return Into::<super::vector3::Vector3>::into(v).into_lua(lua),
|
||||
None=>(),
|
||||
other=>return Err(mlua::Error::runtime(format!("Instance.__index Unsupported property type instance={} index={index_str} value={other:?}",instance.name))),
|
||||
}
|
||||
//find a function with a matching name
|
||||
if let Some(function)=class_methods_store_mut(lua,|cf|{
|
||||
if let Some(function)=class_functions_mut(lua,|cf|{
|
||||
let mut iter=SuperClassIter{
|
||||
database:db,
|
||||
descriptor:Some(class),
|
||||
};
|
||||
iter.find_map(|class|{
|
||||
let mut class_methods=cf.get_or_create_class_methods(&class.name)?;
|
||||
class_methods.get_or_create_function(lua,index_str)
|
||||
.transpose()
|
||||
}).transpose()
|
||||
iter.find_map(|class|
|
||||
cf.get_or_create_class_methods(&class.name)
|
||||
.and_then(|mut class_methods|
|
||||
class_methods.get_or_create_function(lua,index_str).transpose()
|
||||
)
|
||||
).transpose()
|
||||
})?{
|
||||
return function.into_lua(lua);
|
||||
}
|
||||
|
||||
//find or create an associated userdata object
|
||||
if let Some(value)=instance_value_store_mut(lua,|ivs|{
|
||||
//TODO: walk class tree somehow
|
||||
match ivs.get_or_create_instance_values(&instance){
|
||||
Some(mut instance_values)=>instance_values.get_or_create_value(lua,index_str),
|
||||
None=>Ok(None)
|
||||
}
|
||||
})?{
|
||||
return value.into_lua(lua);
|
||||
}
|
||||
//find a child with a matching name
|
||||
find_first_child(dom,instance,index_str)
|
||||
.map(|instance|Instance::new(instance.referent()))
|
||||
@ -295,19 +288,19 @@ impl mlua::UserData for Instance{
|
||||
//println!("__newindex t={} i={index:?} v={value:?}",instance.name);
|
||||
let index_str=&*index.to_str()?;
|
||||
let db=rbx_reflection_database::get();
|
||||
let class=db.classes.get(instance.class.as_str()).ok_or_else(||mlua::Error::runtime("Class missing"))?;
|
||||
let class=db.classes.get(instance.class.as_str()).ok_or(mlua::Error::runtime("Class missing"))?;
|
||||
let mut iter=SuperClassIter{
|
||||
database:db,
|
||||
descriptor:Some(class),
|
||||
};
|
||||
let property=iter.find_map(|cls|cls.properties.get(index_str)).ok_or_else(||mlua::Error::runtime(format!("Property '{index_str}' missing on class '{}'",class.name)))?;
|
||||
let property=iter.find_map(|cls|cls.properties.get(index_str)).ok_or(mlua::Error::runtime(format!("Property '{index_str}' missing on class '{}'",class.name)))?;
|
||||
match &property.data_type{
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::Vector3)=>{
|
||||
let typed_value:Vector3=*value.as_userdata().ok_or_else(||mlua::Error::runtime("Expected Userdata"))?.borrow()?;
|
||||
let typed_value:Vector3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Userdata"))?.take()?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Vector3(typed_value.into()));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::Float32)=>{
|
||||
let typed_value:f32=coerce_float32(&value).ok_or_else(||mlua::Error::runtime("Expected f32"))?;
|
||||
let typed_value:f32=coerce_float32(&value).ok_or(mlua::Error::runtime("Expected f32"))?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Float32(typed_value));
|
||||
},
|
||||
rbx_reflection::DataType::Enum(enum_name)=>{
|
||||
@ -315,11 +308,11 @@ impl mlua::UserData for Instance{
|
||||
&mlua::Value::Integer(int)=>Ok(rbx_types::Enum::from_u32(int as u32)),
|
||||
&mlua::Value::Number(num)=>Ok(rbx_types::Enum::from_u32(num as u32)),
|
||||
mlua::Value::String(s)=>{
|
||||
let e=db.enums.get(enum_name).ok_or_else(||mlua::Error::runtime("Database DataType Enum name does not exist"))?;
|
||||
Ok(rbx_types::Enum::from_u32(*e.items.get(&*s.to_str()?).ok_or_else(||mlua::Error::runtime("Invalid enum item"))?))
|
||||
let e=db.enums.get(enum_name).ok_or(mlua::Error::runtime("Database DataType Enum name does not exist"))?;
|
||||
Ok(rbx_types::Enum::from_u32(*e.items.get(&*s.to_str()?).ok_or(mlua::Error::runtime("Invalid enum item"))?))
|
||||
},
|
||||
mlua::Value::UserData(any_user_data)=>{
|
||||
let e:crate::runner::r#enum::Enum=*any_user_data.borrow()?;
|
||||
let e:super::r#enum::Enum=any_user_data.take()?;
|
||||
Ok(e.into())
|
||||
},
|
||||
_=>Err(mlua::Error::runtime("Expected Enum")),
|
||||
@ -327,25 +320,17 @@ impl mlua::UserData for Instance{
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Enum(typed_value));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::Color3)=>{
|
||||
let typed_value:crate::runner::color3::Color3=*value.as_userdata().ok_or_else(||mlua::Error::runtime("Expected Color3"))?.borrow()?;
|
||||
let typed_value:super::color3::Color3=value.as_userdata().ok_or(mlua::Error::runtime("Expected Color3"))?.take()?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Color3(typed_value.into()));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::Bool)=>{
|
||||
let typed_value=value.as_boolean().ok_or_else(||mlua::Error::runtime("Expected boolean"))?;
|
||||
let typed_value=value.as_boolean().ok_or(mlua::Error::runtime("Expected boolean"))?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::Bool(typed_value));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::String)=>{
|
||||
let typed_value=value.as_str().ok_or_else(||mlua::Error::runtime("Expected boolean"))?;
|
||||
let typed_value=value.as_str().ok_or(mlua::Error::runtime("Expected boolean"))?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::String(typed_value.to_owned()));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::NumberSequence)=>{
|
||||
let typed_value:crate::runner::number_sequence::NumberSequence=*value.as_userdata().ok_or_else(||mlua::Error::runtime("Expected NumberSequence"))?.borrow()?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::NumberSequence(typed_value.into()));
|
||||
},
|
||||
rbx_reflection::DataType::Value(rbx_types::VariantType::ColorSequence)=>{
|
||||
let typed_value:crate::runner::color_sequence::ColorSequence=*value.as_userdata().ok_or_else(||mlua::Error::runtime("Expected ColorSequence"))?.borrow()?;
|
||||
instance.properties.insert(index_str.to_owned(),rbx_types::Variant::ColorSequence(typed_value.into()));
|
||||
},
|
||||
other=>return Err(mlua::Error::runtime(format!("Unimplemented property type: {other:?}"))),
|
||||
}
|
||||
Ok(())
|
||||
@ -377,13 +362,12 @@ static CLASS_FUNCTION_DATABASE:CFD=phf::phf_map!{
|
||||
"GetService"=>cf!(|lua,_this,service:mlua::String|{
|
||||
dom_mut(lua,|dom|{
|
||||
//dom.root_ref()==this.referent ?
|
||||
let service=&*service.to_str()?;
|
||||
match service{
|
||||
"Lighting"|"RunService"=>{
|
||||
let referent=find_first_child_of_class(dom,dom.root(),service)
|
||||
match &*service.to_str()?{
|
||||
"Lighting"=>{
|
||||
let referent=find_first_child_of_class(dom,dom.root(),"Lighting")
|
||||
.map(|instance|instance.referent())
|
||||
.unwrap_or_else(||
|
||||
dom.insert(dom.root_ref(),InstanceBuilder::new(service))
|
||||
dom.insert(dom.root_ref(),InstanceBuilder::new("Lighting"))
|
||||
);
|
||||
Ok(Instance::new(referent))
|
||||
},
|
||||
@ -393,21 +377,21 @@ static CLASS_FUNCTION_DATABASE:CFD=phf::phf_map!{
|
||||
}),
|
||||
},
|
||||
"Terrain"=>phf::phf_map!{
|
||||
"FillBlock"=>cf!(|_lua,_,_:(crate::runner::cframe::CFrame,Vector3,crate::runner::r#enum::Enum)|mlua::Result::Ok(()))
|
||||
"FillBlock"=>cf!(|_lua,_,_:(super::cframe::CFrame,Vector3,super::r#enum::Enum)|mlua::Result::Ok(()))
|
||||
},
|
||||
};
|
||||
|
||||
/// A store of created functions for each Roblox class.
|
||||
/// Functions are created the first time they are accessed and stored in this data structure.
|
||||
#[derive(Default)]
|
||||
struct ClassMethodsStore{
|
||||
struct ClassFunctions{
|
||||
classes:HashMap<&'static str,//ClassName
|
||||
HashMap<&'static str,//Method name
|
||||
mlua::Function
|
||||
>
|
||||
>
|
||||
}
|
||||
impl ClassMethodsStore{
|
||||
impl ClassFunctions{
|
||||
/// return self.classes[class] or create the ClassMethods and then return it
|
||||
fn get_or_create_class_methods(&mut self,class:&str)->Option<ClassMethods>{
|
||||
// Use get_entry to get the &'static str keys of the database
|
||||
@ -442,25 +426,25 @@ impl ClassMethods<'_>{
|
||||
})
|
||||
}
|
||||
}
|
||||
fn class_methods_store_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut ClassMethodsStore)->mlua::Result<T>)->mlua::Result<T>{
|
||||
let mut cf=lua.app_data_mut::<ClassMethodsStore>().ok_or_else(||mlua::Error::runtime("ClassMethodsStore missing"))?;
|
||||
f(&mut *cf)
|
||||
}
|
||||
|
||||
/// A virtual property pointer definition shorthand.
|
||||
type VirtualPropertyFunctionPointer=fn(&rbx_types::Variant)->Option<rbx_types::Variant>;
|
||||
type VirtualPropertyFunctionPointerMut=fn(&mut rbx_types::Variant,rbx_types::Variant)->Option<()>;
|
||||
const fn vpp(
|
||||
property:&'static str,
|
||||
pointer:VirtualPropertyFunctionPointer,
|
||||
access:VirtualPropertyFunctionPointer,
|
||||
access_mut:VirtualPropertyFunctionPointerMut,
|
||||
)->VirtualProperty{
|
||||
VirtualProperty{
|
||||
property,
|
||||
pointer,
|
||||
access,
|
||||
access_mut,
|
||||
}
|
||||
}
|
||||
struct VirtualProperty{
|
||||
property:&'static str,// Source property name
|
||||
pointer:VirtualPropertyFunctionPointer,
|
||||
access:VirtualPropertyFunctionPointer,
|
||||
access_mut:VirtualPropertyFunctionPointerMut,
|
||||
}
|
||||
type VPD=phf::Map<&'static str,// Class name
|
||||
phf::Map<&'static str,// Virtual property name
|
||||
@ -469,13 +453,26 @@ type VPD=phf::Map<&'static str,// Class name
|
||||
>;
|
||||
static VIRTUAL_PROPERTY_DATABASE:VPD=phf::phf_map!{
|
||||
"BasePart"=>phf::phf_map!{
|
||||
"Position"=>vpp("CFrame",|c:&rbx_types::Variant|{
|
||||
let c=match c{
|
||||
rbx_types::Variant::CFrame(c)=>c,
|
||||
_=>return None,//fail silently and ungracefully
|
||||
};
|
||||
Some(rbx_types::Variant::Vector3(c.position))
|
||||
}),
|
||||
"Position"=>vpp(
|
||||
"CFrame",
|
||||
//Get
|
||||
|c|{
|
||||
let c=match c{
|
||||
rbx_types::Variant::CFrame(c)=>c,
|
||||
_=>return None,//fail silently and ungracefully
|
||||
};
|
||||
Some(rbx_types::Variant::Vector3(c.position))
|
||||
},
|
||||
//Set
|
||||
|c,p|{
|
||||
let (c,p)=match (c,p){
|
||||
(rbx_types::Variant::CFrame(c),rbx_types::Variant::Vector3(p))=>(c,p),
|
||||
_=>return None,//fail silently and ungracefully
|
||||
};
|
||||
c.position=p;
|
||||
Some(())
|
||||
}
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
@ -492,66 +489,5 @@ fn find_virtual_property(
|
||||
let variant=properties.get(virtual_property.property)?;
|
||||
|
||||
//Transform Source property with provided function
|
||||
(virtual_property.pointer)(variant)
|
||||
}
|
||||
|
||||
// lazy-loaded per-instance userdata values
|
||||
// This whole thing is a bad idea and a garbage collection nightmare.
|
||||
// TODO: recreate rbx_dom_weak with my own instance type that owns this data.
|
||||
type CreateUserData=fn(&mlua::Lua)->mlua::Result<mlua::AnyUserData>;
|
||||
type LUD=phf::Map<&'static str,// Class name
|
||||
phf::Map<&'static str,// Value name
|
||||
CreateUserData
|
||||
>
|
||||
>;
|
||||
static LAZY_USER_DATA:LUD=phf::phf_map!{
|
||||
"RunService"=>phf::phf_map!{
|
||||
"RenderStepped"=>|lua|{
|
||||
lua.create_any_userdata(crate::runner::script_signal::ScriptSignal::new())
|
||||
},
|
||||
},
|
||||
};
|
||||
#[derive(Default)]
|
||||
pub struct InstanceValueStore{
|
||||
values:HashMap<Ref,
|
||||
HashMap<&'static str,
|
||||
mlua::AnyUserData
|
||||
>
|
||||
>,
|
||||
}
|
||||
pub struct InstanceValues<'a>{
|
||||
named_values:&'static phf::Map<&'static str,CreateUserData>,
|
||||
values:&'a mut HashMap<&'static str,mlua::AnyUserData>,
|
||||
}
|
||||
impl InstanceValueStore{
|
||||
pub fn get_or_create_instance_values(&mut self,instance:&rbx_dom_weak::Instance)->Option<InstanceValues>{
|
||||
LAZY_USER_DATA.get(instance.class.as_str())
|
||||
.map(|named_values|
|
||||
InstanceValues{
|
||||
named_values,
|
||||
values:self.values.entry(instance.referent())
|
||||
.or_insert_with(||HashMap::new()),
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
impl InstanceValues<'_>{
|
||||
pub fn get_or_create_value(&mut self,lua:&mlua::Lua,index:&str)->mlua::Result<Option<mlua::AnyUserData>>{
|
||||
Ok(match self.named_values.get_entry(index){
|
||||
Some((&static_index_str,&function_pointer))=>Some(
|
||||
match self.values.entry(static_index_str){
|
||||
Entry::Occupied(entry)=>entry.get().clone(),
|
||||
Entry::Vacant(entry)=>entry.insert(
|
||||
function_pointer(lua)?
|
||||
).clone(),
|
||||
}
|
||||
),
|
||||
None=>None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn instance_value_store_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut InstanceValueStore)->mlua::Result<T>)->mlua::Result<T>{
|
||||
let mut cf=lua.app_data_mut::<InstanceValueStore>().ok_or_else(||mlua::Error::runtime("InstanceValueStore missing"))?;
|
||||
f(&mut *cf)
|
||||
(virtual_property.access)(variant)
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
pub mod instance;
|
||||
pub use instance::Instance;
|
@ -7,8 +7,5 @@ mod color3;
|
||||
mod cframe;
|
||||
mod vector3;
|
||||
pub mod instance;
|
||||
mod script_signal;
|
||||
mod color_sequence;
|
||||
mod number_sequence;
|
||||
|
||||
pub use runner::{Runner,Runnable,Error};
|
||||
pub use runner::{Runner,Error};
|
||||
|
@ -1,31 +0,0 @@
|
||||
#[derive(Clone,Copy)]
|
||||
pub struct NumberSequence{}
|
||||
impl NumberSequence{
|
||||
pub const fn new()->Self{
|
||||
Self{}
|
||||
}
|
||||
}
|
||||
impl Into<rbx_types::NumberSequence> for NumberSequence{
|
||||
fn into(self)->rbx_types::NumberSequence{
|
||||
rbx_types::NumberSequence{
|
||||
keypoints:Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
||||
let number_sequence_table=lua.create_table()?;
|
||||
|
||||
number_sequence_table.raw_set("new",
|
||||
lua.create_function(|_,_:mlua::MultiValue|
|
||||
Ok(NumberSequence::new())
|
||||
)?
|
||||
)?;
|
||||
|
||||
globals.set("NumberSequence",number_sequence_table)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl mlua::UserData for NumberSequence{}
|
||||
type_from_lua_userdata!(NumberSequence);
|
@ -1,6 +1,4 @@
|
||||
use crate::context::Context;
|
||||
#[cfg(feature="run-service")]
|
||||
use crate::scheduler::scheduler_mut;
|
||||
|
||||
pub struct Runner{
|
||||
lua:mlua::Lua,
|
||||
@ -31,16 +29,11 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{
|
||||
//global environment
|
||||
let globals=lua.globals();
|
||||
|
||||
#[cfg(feature="run-service")]
|
||||
crate::scheduler::set_globals(lua,&globals)?;
|
||||
super::script_signal::set_globals(lua,&globals)?;
|
||||
super::r#enum::set_globals(lua,&globals)?;
|
||||
super::color3::set_globals(lua,&globals)?;
|
||||
super::vector3::set_globals(lua,&globals)?;
|
||||
super::cframe::set_globals(lua,&globals)?;
|
||||
super::instance::instance::set_globals(lua,&globals)?;
|
||||
super::number_sequence::set_globals(lua,&globals)?;
|
||||
super::color_sequence::set_globals(lua,&globals)?;
|
||||
super::instance::set_globals(lua,&globals)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -65,8 +58,6 @@ impl Runner{
|
||||
}
|
||||
//this makes set_app_data shut up about the lifetime
|
||||
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
|
||||
#[cfg(feature="run-service")]
|
||||
self.lua.set_app_data::<crate::scheduler::Scheduler>(crate::scheduler::Scheduler::default());
|
||||
Ok(Runnable{
|
||||
lua:self.lua,
|
||||
_lifetime:&std::marker::PhantomData
|
||||
@ -82,62 +73,15 @@ pub struct Runnable<'a>{
|
||||
impl Runnable<'_>{
|
||||
pub fn drop_context(self)->Runner{
|
||||
self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
|
||||
#[cfg(feature="run-service")]
|
||||
self.lua.remove_app_data::<crate::scheduler::Scheduler>();
|
||||
Runner{
|
||||
lua:self.lua,
|
||||
}
|
||||
}
|
||||
pub fn run_script(&self,script:super::instance::Instance)->Result<(),Error>{
|
||||
let (name,source)=super::instance::instance::get_name_source(&self.lua,script).map_err(Error::RustLua)?;
|
||||
self.lua.globals().raw_set("script",script).map_err(Error::RustLua)?;
|
||||
let f=self.lua.load(source.as_str())
|
||||
.set_name(name).into_function().map_err(Error::RustLua)?;
|
||||
// TODO: set_environment without losing the ability to print from Lua
|
||||
let thread=self.lua.create_thread(f).map_err(Error::RustLua)?;
|
||||
thread.resume::<mlua::MultiValue>(()).map_err(|error|Error::Lua{source,error})?;
|
||||
// wait() is called from inside Lua and goes to a rust function that schedules the thread and then yields
|
||||
// No need to schedule the thread here
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(feature="run-service")]
|
||||
pub fn has_scheduled_threads(&self)->Result<bool,mlua::Error>{
|
||||
scheduler_mut(&self.lua,|scheduler|
|
||||
Ok(scheduler.has_scheduled_threads())
|
||||
)
|
||||
}
|
||||
#[cfg(feature="run-service")]
|
||||
pub fn game_tick(&self)->Result<(),mlua::Error>{
|
||||
if let Some(threads)=scheduler_mut(&self.lua,|scheduler|Ok(scheduler.tick_threads()))?{
|
||||
for thread in threads{
|
||||
//TODO: return dt and total run time
|
||||
let result=thread.resume::<mlua::MultiValue>((1.0/30.0,0.0))
|
||||
.map_err(|error|Error::Lua{source:"source unavailable".to_owned(),error});
|
||||
match result{
|
||||
Ok(_)=>(),
|
||||
Err(e)=>println!("game_tick Error: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(feature="run-service")]
|
||||
pub fn run_service_step(&self)->Result<(),mlua::Error>{
|
||||
let render_stepped=super::instance::instance::dom_mut(&self.lua,|dom|{
|
||||
let run_service=super::instance::instance::find_first_child_of_class(dom,dom.root(),"RunService").ok_or_else(||mlua::Error::runtime("RunService missing"))?;
|
||||
super::instance::instance::instance_value_store_mut(&self.lua,|instance_value_store|{
|
||||
//unwrap because I trust my find_first_child_of_class function to
|
||||
let mut instance_values=instance_value_store.get_or_create_instance_values(run_service).ok_or_else(||mlua::Error::runtime("RunService InstanceValues missing"))?;
|
||||
let render_stepped=instance_values.get_or_create_value(&self.lua,"RenderStepped")?;
|
||||
//let stepped=instance_values.get_or_create_value(&self.lua,"Stepped")?;
|
||||
//let heartbeat=instance_values.get_or_create_value(&self.lua,"Heartbeat")?;
|
||||
Ok(render_stepped)
|
||||
})
|
||||
})?;
|
||||
if let Some(render_stepped)=render_stepped{
|
||||
let signal:&super::script_signal::ScriptSignal=&*render_stepped.borrow()?;
|
||||
signal.fire(&mlua::MultiValue::new());
|
||||
}
|
||||
Ok(())
|
||||
let (name,source)=super::instance::get_name_source(&self.lua,script).map_err(Error::RustLua)?;
|
||||
self.lua.globals().set("script",script).map_err(Error::RustLua)?;
|
||||
self.lua.load(source.as_str())
|
||||
.set_name(name)
|
||||
.exec().map_err(|error|Error::Lua{source,error})
|
||||
}
|
||||
}
|
||||
|
@ -1,172 +0,0 @@
|
||||
use std::{cell::RefCell,rc::Rc};
|
||||
|
||||
use mlua::UserDataFields;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct FunctionList{
|
||||
functions:Vec<mlua::Function>,
|
||||
}
|
||||
impl FunctionList{
|
||||
pub fn new()->Self{
|
||||
Self{
|
||||
functions: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
|
||||
for function in self.functions{
|
||||
//wee let's allocate for our function calls
|
||||
if let Err(e)=function.call::<mlua::MultiValue>(args.clone()){
|
||||
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.
|
||||
connections:RcFunctionList,
|
||||
once:RcFunctionList,
|
||||
wait:Rc<RefCell<Vec<mlua::Thread>>>,
|
||||
}
|
||||
pub struct ScriptConnection{
|
||||
connection:RcFunctionList,
|
||||
function:mlua::Function,
|
||||
}
|
||||
impl ScriptSignal{
|
||||
pub fn new()->Self{
|
||||
Self{
|
||||
connections:RcFunctionList::new(),
|
||||
once:RcFunctionList::new(),
|
||||
wait:Rc::new(RefCell::new(Vec::new())),
|
||||
}
|
||||
}
|
||||
pub fn fire(&self,args:&mlua::MultiValue){
|
||||
self.connections.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{
|
||||
self.connections.functions.borrow_mut().functions.push(function.clone());
|
||||
ScriptConnection{
|
||||
connection:self.connections.clone(),
|
||||
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{
|
||||
pub fn position(&self)->Option<usize>{
|
||||
self.connection.functions.borrow().functions.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))
|
||||
);
|
||||
methods.add_method("Once",|_lua,this,f:mlua::Function|
|
||||
Ok(this.once(f))
|
||||
);
|
||||
// Fire is not allowed to be called from Lua
|
||||
// methods.add_method("Fire",|_lua,this,args:mlua::MultiValue|
|
||||
// Ok(this.fire(args))
|
||||
// );
|
||||
}
|
||||
}
|
||||
impl mlua::FromLua for ScriptSignal{
|
||||
fn from_lua(value:mlua::Value,_lua:&mlua::Lua)->Result<Self,mlua::Error>{
|
||||
match value{
|
||||
mlua::Value::UserData(ud)=>Ok(ud.borrow::<Self>()?.clone()),
|
||||
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!(ScriptSignal),other))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.connection.functions.borrow_mut().functions.remove(index);
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
106
src/scheduler.rs
106
src/scheduler.rs
@ -1,106 +0,0 @@
|
||||
pub use tick::Tick;
|
||||
mod tick{
|
||||
#[derive(Clone,Copy,Default,Hash,PartialEq,Eq,PartialOrd,Ord)]
|
||||
pub struct Tick(u64);
|
||||
impl std::ops::Add<u64> for Tick{
|
||||
type Output=Self;
|
||||
fn add(self,rhs:u64)->Self::Output{
|
||||
Self(self.0+rhs)
|
||||
}
|
||||
}
|
||||
impl std::ops::Sub<u64> for Tick{
|
||||
type Output=Self;
|
||||
fn sub(self,rhs:u64)->Self::Output{
|
||||
Self(self.0-rhs)
|
||||
}
|
||||
}
|
||||
impl std::ops::AddAssign<u64> for Tick{
|
||||
fn add_assign(&mut self,rhs:u64){
|
||||
self.0+=rhs;
|
||||
}
|
||||
}
|
||||
impl std::ops::SubAssign<u64> for Tick{
|
||||
fn sub_assign(&mut self,rhs:u64){
|
||||
self.0-=rhs;
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub struct Scheduler{
|
||||
tick:Tick,
|
||||
schedule:std::collections::HashMap<Tick,Vec<mlua::Thread>>,
|
||||
}
|
||||
|
||||
impl Scheduler{
|
||||
pub fn has_scheduled_threads(&self)->bool{
|
||||
!self.schedule.is_empty()
|
||||
}
|
||||
pub fn schedule_thread(&mut self,delay:u64,thread:mlua::Thread){
|
||||
self.schedule.entry(self.tick+delay.max(1))
|
||||
.or_insert(Vec::new())
|
||||
.push(thread);
|
||||
}
|
||||
pub fn tick_threads(&mut self)->Option<Vec<mlua::Thread>>{
|
||||
self.tick+=1;
|
||||
self.schedule.remove(&self.tick)
|
||||
}
|
||||
}
|
||||
|
||||
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_else(||mlua::Error::runtime("Scheduler missing"))?;
|
||||
f(&mut *scheduler)
|
||||
}
|
||||
|
||||
fn schedule_thread(lua:&mlua::Lua,dt:mlua::Value)->Result<(),mlua::Error>{
|
||||
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(())
|
||||
})
|
||||
}
|
||||
|
||||
// This is used to avoid calling coroutine.yield from the rust side.
|
||||
const LUA_WAIT:&str=
|
||||
"local coroutine_yield=coroutine.yield
|
||||
local schedule_thread=schedule_thread
|
||||
return function(dt)
|
||||
schedule_thread(dt)
|
||||
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 schedule_thread=lua.create_function(schedule_thread)?;
|
||||
|
||||
//create wait function environment
|
||||
let wait_env=lua.create_table()?;
|
||||
wait_env.raw_set("coroutine",coroutine_table)?;
|
||||
wait_env.raw_set("schedule_thread",schedule_thread)?;
|
||||
|
||||
//construct wait function from Lua code
|
||||
let wait=lua.load(LUA_WAIT)
|
||||
.set_name("wait")
|
||||
.set_environment(wait_env)
|
||||
.call::<mlua::Function>(())?;
|
||||
|
||||
globals.raw_set("wait",wait)?;
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue
Block a user