place with services

This commit is contained in:
Quaternions 2024-10-03 17:19:47 -07:00
parent f5e9287798
commit fb38f1076d
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{
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)}

View File

@ -1,5 +1,6 @@
pub mod context;
pub mod place;
pub mod runner;
pub mod context;
#[cfg(test)]
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

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

View File

@ -44,6 +44,11 @@ impl Runner{
Ok(runner)
}
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
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
Ok(Runnable{