place with services

This commit is contained in:
Quaternions 2024-10-03 17:19:47 -07:00
parent 73b924d734
commit f71099b533
5 changed files with 58 additions and 8 deletions

View File

@ -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{ pub fn class_is_a(class:&str,superclass:&str)->bool{
class==superclass class==superclass
@ -19,13 +19,17 @@ impl Context{
pub const fn new(dom:WeakDom)->Self{ pub const fn new(dom:WeakDom)->Self{
Self{dom} Self{dom}
} }
pub fn script_singleton(source:String)->(Context,crate::runner::instance::Script){ pub fn script_singleton(source:String)->(Context,crate::runner::instance::Script,crate::place::Services){
let dom=WeakDom::new( let script=InstanceBuilder::new("Script")
rbx_dom_weak::InstanceBuilder::new("Script") .with_property("Source",rbx_types::Variant::String(source));
.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},crate::runner::instance::Script::new(script_ref),services)
(Context{dom},script)
} }
pub fn from_mut(dom:&mut WeakDom)->&mut Context{ pub fn from_mut(dom:&mut WeakDom)->&mut Context{
unsafe{&mut *(dom as *mut WeakDom as *mut Context)} unsafe{&mut *(dom as *mut WeakDom as *mut Context)}

View File

@ -1,5 +1,6 @@
pub mod context; pub mod place;
pub mod runner; pub mod runner;
pub mod context;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

37
src/place.rs Normal file
View File

@ -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
}
)
}

View File

@ -206,6 +206,9 @@ impl DataModel{
} }
} }
class!(Workspace);
class_composition!(Workspace,(Instance));
class!(Lighting); class!(Lighting);
class_composition!(Lighting,(Instance)); class_composition!(Lighting,(Instance));

View File

@ -45,6 +45,11 @@ impl Runner{
Ok(runner) Ok(runner)
} }
pub fn runnable_context<'a>(self,context:&'a mut Context,services:crate::place::Services)->Result<Runnable<'a>,Error>{ pub fn runnable_context<'a>(self,context:&'a mut Context,services:crate::place::Services)->Result<Runnable<'a>,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 //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)}); self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
Ok(Runnable{ Ok(Runnable{