make a distinction between Model and Place

This commit is contained in:
Quaternions 2024-10-03 19:35:59 -07:00
parent d5a3b797da
commit 35c19abaf1

View File

@ -1,4 +1,5 @@
use std::io::Read;
use rbx_dom_weak::WeakDom;
mod rbx;
mod mesh;
@ -16,9 +17,29 @@ pub mod data{
}
}
pub struct Dom(rbx_dom_weak::WeakDom);
impl Dom{
pub fn run_scripts(&mut self){
pub struct Model{
dom:WeakDom,
}
impl Model{
fn new(dom:WeakDom)->Self{
Self{dom}
}
pub fn into_place(self)->(Place,roblox_emulator::place::Services){
let (dom,services)=roblox_emulator::place::new_place_with_instances_in_workspace(self.dom);
(Place{dom},services)
}
}
impl AsRef<WeakDom> for Model{
fn as_ref(&self)->&WeakDom{
&self.dom
}
}
pub struct Place{
dom:WeakDom,
}
impl Place{
pub fn run_scripts(&mut self,services:roblox_emulator::place::Services){
let runner=roblox_emulator::runner::Runner::new().unwrap();
let context=roblox_emulator::context::Context::from_mut(&mut self.dom);
let scripts=context.scripts();
@ -30,6 +51,11 @@ impl Dom{
}
}
}
impl AsRef<WeakDom> for Place{
fn as_ref(&self)->&WeakDom{
&self.dom
}
}
#[derive(Debug)]
pub enum ReadError{
@ -45,12 +71,12 @@ impl std::fmt::Display for ReadError{
}
impl std::error::Error for ReadError{}
pub fn read<R:Read>(input:R)->Result<Dom,ReadError>{
pub fn read<R:Read>(input:R)->Result<Model,ReadError>{
let mut buf=std::io::BufReader::new(input);
let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?;
match &peek[0..8]{
b"<roblox!"=>rbx_binary::from_reader(buf).map(Dom).map_err(ReadError::RbxBinary),
b"<roblox "=>rbx_xml::from_reader_default(buf).map(Dom).map_err(ReadError::RbxXml),
b"<roblox!"=>rbx_binary::from_reader(buf).map(Model::new).map_err(ReadError::RbxBinary),
b"<roblox "=>rbx_xml::from_reader_default(buf).map(Model::new).map_err(ReadError::RbxXml),
_=>Err(ReadError::UnknownFileFormat),
}
}
@ -58,7 +84,7 @@ pub fn read<R:Read>(input:R)->Result<Dom,ReadError>{
//ConvertError
pub fn convert<AcquireRenderConfigId,AcquireMeshId>(
dom:&Dom,
dom:impl AsRef<WeakDom>,
acquire_render_config_id:AcquireRenderConfigId,
acquire_mesh_id:AcquireMeshId
)->rbx::PartialMap1
@ -66,5 +92,5 @@ where
AcquireRenderConfigId:FnMut(Option<&str>)->strafesnet_common::model::RenderConfigId,
AcquireMeshId:FnMut(&str)->strafesnet_common::model::MeshId,
{
rbx::convert(&dom.0,acquire_render_config_id,acquire_mesh_id)
rbx::convert(&dom.as_ref(),acquire_render_config_id,acquire_mesh_id)
}