Context::get_or_create_service
This commit is contained in:
parent
cfa24c98a6
commit
0aed94568e
@ -46,9 +46,23 @@ impl Context{
|
||||
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>{
|
||||
//nil instances isn't real, it doesn't exist in real roblox places
|
||||
None
|
||||
Some(Services{
|
||||
workspace:self.get_service("Workspace")?,
|
||||
nil:self.get_service("Nil")?,
|
||||
game:self.dom.root_ref(),
|
||||
})
|
||||
}
|
||||
pub fn convert_into_place(&mut self)->Services{
|
||||
//snapshot root instances
|
||||
@ -56,13 +70,15 @@ impl Context{
|
||||
|
||||
//insert services
|
||||
let game=self.dom.root_ref();
|
||||
let workspace=self.get_or_create_service("Workspace",|context|{
|
||||
let terrain_bldr=InstanceBuilder::new("Terrain");
|
||||
let workspace=self.dom.insert(game,
|
||||
context.dom.insert(game,
|
||||
InstanceBuilder::new("Workspace")
|
||||
//Set Workspace.Terrain property equal to Terrain
|
||||
.with_property("Terrain",terrain_bldr.referent())
|
||||
.with_child(terrain_bldr)
|
||||
);
|
||||
)
|
||||
});
|
||||
{
|
||||
//Lowercase and upper case workspace property!
|
||||
let game=self.dom.root_mut();
|
||||
@ -73,11 +89,13 @@ impl Context{
|
||||
// Special nonexistent class that holds instances parented to nil,
|
||||
// which can still be referenced by scripts.
|
||||
// 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|
|
||||
context.dom.insert(
|
||||
game,InstanceBuilder::new("Nil")
|
||||
.with_referent(
|
||||
<Ref as std::str::FromStr>::from_str("ffffffffffffffffffffffffffffffff").unwrap()
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
//transfer original root instances into workspace
|
||||
|
Loading…
Reference in New Issue
Block a user