Context::get_or_create_service

This commit is contained in:
Quaternions 2024-10-14 12:15:39 -07:00
parent cfa24c98a6
commit 0aed94568e

View File

@ -46,9 +46,23 @@ impl Context{
self.superclass_iter("LuaSourceContainer").map(crate::runner::instance::Instance::new).collect() self.superclass_iter("LuaSourceContainer").map(crate::runner::instance::Instance::new).collect()
} }
fn get_service(&self,service:&str)->Option<Ref>{
self.dom.root().children().iter().find(|&&r|
self.dom.get_by_ref(r).is_some_and(|instance|instance.class==service)
).copied()
}
fn get_or_create_service(&mut self,service:&str,mut create:impl FnMut(&mut Context)->Ref)->Ref{
match self.get_service(service){
Some(referent)=>referent,
None=>create(self),
}
}
pub fn find_services(&self)->Option<Services>{ pub fn find_services(&self)->Option<Services>{
//nil instances isn't real, it doesn't exist in real roblox places Some(Services{
None workspace:self.get_service("Workspace")?,
nil:self.get_service("Nil")?,
game:self.dom.root_ref(),
})
} }
pub fn convert_into_place(&mut self)->Services{ pub fn convert_into_place(&mut self)->Services{
//snapshot root instances //snapshot root instances
@ -56,13 +70,15 @@ impl Context{
//insert services //insert services
let game=self.dom.root_ref(); let game=self.dom.root_ref();
let terrain_bldr=InstanceBuilder::new("Terrain"); let workspace=self.get_or_create_service("Workspace",|context|{
let workspace=self.dom.insert(game, let terrain_bldr=InstanceBuilder::new("Terrain");
InstanceBuilder::new("Workspace") context.dom.insert(game,
//Set Workspace.Terrain property equal to Terrain InstanceBuilder::new("Workspace")
.with_property("Terrain",terrain_bldr.referent()) //Set Workspace.Terrain property equal to Terrain
.with_child(terrain_bldr) .with_property("Terrain",terrain_bldr.referent())
); .with_child(terrain_bldr)
)
});
{ {
//Lowercase and upper case workspace property! //Lowercase and upper case workspace property!
let game=self.dom.root_mut(); let game=self.dom.root_mut();
@ -73,10 +89,12 @@ impl Context{
// Special nonexistent class that holds instances parented to nil, // Special nonexistent class that holds instances parented to nil,
// which can still be referenced by scripts. // which can still be referenced by scripts.
// Using a sentinel value as a ref because global variables are hard. // Using a sentinel value as a ref because global variables are hard.
let nil=self.dom.insert( let nil=self.get_or_create_service("Nil",|context|
game,InstanceBuilder::new("Nil") context.dom.insert(
.with_referent( game,InstanceBuilder::new("Nil")
<Ref as std::str::FromStr>::from_str("ffffffffffffffffffffffffffffffff").unwrap() .with_referent(
<Ref as std::str::FromStr>::from_str("ffffffffffffffffffffffffffffffff").unwrap()
)
) )
); );