From fb38f1076dcf0ff641e0c91622f8092bbeb925ad Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 3 Oct 2024 17:19:47 -0700 Subject: [PATCH] place with services --- src/context.rs | 18 +++++++++++------- src/lib.rs | 3 ++- src/place.rs | 37 +++++++++++++++++++++++++++++++++++++ src/runner/instance.rs | 3 +++ src/runner/runner.rs | 5 +++++ 5 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 src/place.rs diff --git a/src/context.rs b/src/context.rs index 91f3a69..9e0b05c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,4 +1,4 @@ -use rbx_dom_weak::{types::Ref,WeakDom}; +use rbx_dom_weak::{types::Ref,InstanceBuilder,WeakDom}; pub fn class_is_a(class:&str,superclass:&str)->bool{ class==superclass @@ -19,13 +19,17 @@ impl Context{ pub const fn new(dom:WeakDom)->Self{ Self{dom} } - pub fn script_singleton(source:String)->(Context,crate::runner::instance::Script){ - let dom=WeakDom::new( - rbx_dom_weak::InstanceBuilder::new("Script") - .with_property("Source",rbx_types::Variant::String(source)) + pub fn script_singleton(source:String)->(Context,crate::runner::instance::Script,crate::place::Services){ + let script=InstanceBuilder::new("Script") + .with_property("Source",rbx_types::Variant::String(source)); + let script_ref=script.referent(); + let (dom,services)=crate::place::new_place_with_instances_in_workspace( + WeakDom::new( + InstanceBuilder::new("DataModel") + .with_child(script) + ) ); - let script=crate::runner::instance::Script::new(dom.root_ref()); - (Context{dom},script) + (Context{dom},crate::runner::instance::Script::new(script_ref),services) } pub fn from_mut(dom:&mut WeakDom)->&mut Context{ unsafe{&mut *(dom as *mut WeakDom as *mut Context)} diff --git a/src/lib.rs b/src/lib.rs index f93d8c1..70c62b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ -pub mod context; +pub mod place; pub mod runner; +pub mod context; #[cfg(test)] mod tests; diff --git a/src/place.rs b/src/place.rs new file mode 100644 index 0000000..1fa0979 --- /dev/null +++ b/src/place.rs @@ -0,0 +1,37 @@ +use rbx_dom_weak::{InstanceBuilder,WeakDom}; +use rbx_types::Ref; + +pub struct Services{ + pub game:Ref, + pub workspace:Ref, +} + +pub fn new_place_with_instances_in_workspace(mut instance_dom:WeakDom)->(WeakDom,Services){ + //workspace + let workspace_bldr=InstanceBuilder::new("Workspace") + .with_child(InstanceBuilder::new("Terrain")); + let workspace=workspace_bldr.referent(); + + //game + let game_bldr= + InstanceBuilder::new("DataModel") + .with_child(workspace_bldr) + .with_child(InstanceBuilder::new("Lighting")); + let game=game_bldr.referent(); + + let mut dom=WeakDom::new(game_bldr); + + //put instances in workspace + let children=instance_dom.root().children().to_owned(); + for instance in children{ + instance_dom.transfer(instance,&mut dom,workspace); + } + + ( + dom, + Services{ + game, + workspace + } + ) +} diff --git a/src/runner/instance.rs b/src/runner/instance.rs index 88eec38..6face0b 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -185,6 +185,9 @@ impl DataModel{ } } +class!(Workspace); +class_composition!(Workspace,(Instance)); + class!(Lighting); class_composition!(Lighting,(Instance)); diff --git a/src/runner/runner.rs b/src/runner/runner.rs index 2d44869..df3c237 100644 --- a/src/runner/runner.rs +++ b/src/runner/runner.rs @@ -44,6 +44,11 @@ impl Runner{ Ok(runner) } pub fn runnable_context<'a>(self,context:&'a mut Context,services:crate::place::Services)->Result,Error>{ + { + let globals=self.lua.globals(); + globals.set("game",super::instance::DataModel::new(services.game)).map_err(Error::RustLua)?; + globals.set("workspace",super::instance::Workspace::new(services.workspace)).map_err(Error::RustLua)?; + } //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)}); Ok(Runnable{