From c0323d12dff90b3ea8492e2543d681a3a1943d12 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 4 Oct 2024 16:14:03 -0700 Subject: [PATCH] delete place.rs --- src/context.rs | 51 ++++++++++++++++++++++++++++++++++++++++++-------- src/lib.rs | 1 - src/place.rs | 47 ---------------------------------------------- 3 files changed, 43 insertions(+), 56 deletions(-) delete mode 100644 src/place.rs diff --git a/src/context.rs b/src/context.rs index 44458b9..91562bd 100644 --- a/src/context.rs +++ b/src/context.rs @@ -19,17 +19,16 @@ impl Context{ pub const fn new(dom:WeakDom)->Self{ Self{dom} } - pub fn script_singleton(source:String)->(Context,crate::runner::instance::Script,crate::place::Services){ + pub fn script_singleton(source:String)->(Context,crate::runner::instance::Script,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) - ) - ); - (Context{dom},crate::runner::instance::Script::new(script_ref),services) + let mut context=Self::new(WeakDom::new( + InstanceBuilder::new("DataModel") + .with_child(script) + )); + let services=context.convert_into_place(); + (context,crate::runner::instance::Script::new(script_ref),services) } pub fn from_ref(dom:&WeakDom)->&Context{ unsafe{&*(dom as *const WeakDom as *const Context)} @@ -46,4 +45,40 @@ impl Context{ pub fn scripts(&self)->Vec{ self.superclass_iter("LuaSourceContainer").map(crate::runner::instance::Script::new).collect() } + + pub fn find_services(&self)->Option{ + Some(Services{ + workspace:*self.dom.root().children().iter().find(|&&r| + self.dom.get_by_ref(r).is_some_and(|instance|instance.class=="Workspace") + )?, + game:self.dom.root_ref(), + }) + } + pub fn convert_into_place(&mut self)->Services{ + //snapshot root instances + let children=self.dom.root().children().to_owned(); + + //insert services + let game=self.dom.root_ref(); + let workspace=self.dom.insert(game, + InstanceBuilder::new("Workspace") + .with_child(InstanceBuilder::new("Terrain")) + ); + self.dom.insert(game,InstanceBuilder::new("Lighting")); + + //transfer original root instances into workspace + for instance in children{ + self.dom.transfer_within(instance,workspace); + } + + Services{ + game, + workspace, + } + } +} + +pub struct Services{ + pub game:Ref, + pub workspace:Ref, } diff --git a/src/lib.rs b/src/lib.rs index 70c62b9..b97eda2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -pub mod place; pub mod runner; pub mod context; diff --git a/src/place.rs b/src/place.rs deleted file mode 100644 index 6961609..0000000 --- a/src/place.rs +++ /dev/null @@ -1,47 +0,0 @@ -use rbx_dom_weak::{InstanceBuilder,WeakDom}; -use rbx_types::Ref; - -pub struct Services{ - pub game:Ref, - pub workspace:Ref, -} -impl Services{ - pub fn find_in(dom:&WeakDom)->Option{ - Some(Services{ - workspace:*dom.root().children().iter().find(|&&r| - dom.get_by_ref(r).is_some_and(|instance|instance.class=="Workspace") - )?, - game:dom.root_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 - } - ) -}