108 lines
2.5 KiB
Rust
Raw Normal View History

use std::io::Read;
use rbx_dom_weak::WeakDom;
mod rbx;
2024-03-13 13:34:06 -07:00
mod mesh;
2024-01-30 17:41:16 -08:00
mod primitives;
2024-03-13 13:34:06 -07:00
pub mod data{
pub struct RobloxMeshBytes(Vec<u8>);
impl RobloxMeshBytes{
pub fn new(bytes:Vec<u8>)->Self{
Self(bytes)
}
pub(crate) fn cursor(self)->std::io::Cursor<Vec<u8>>{
std::io::Cursor::new(self.0)
}
}
}
pub struct Model{
dom:WeakDom,
}
impl Model{
fn new(dom:WeakDom)->Self{
Self{dom}
}
2024-10-03 20:22:45 -07:00
pub fn into_place(self)->Place{
2024-10-04 16:32:34 -07:00
let Self{mut dom}=self;
let context=roblox_emulator::context::Context::from_mut(&mut dom);
let services=context.convert_into_place();
2024-10-03 20:22:45 -07:00
Place{dom,services}
}
}
impl AsRef<WeakDom> for Model{
fn as_ref(&self)->&WeakDom{
&self.dom
}
}
pub struct Place{
dom:WeakDom,
2024-10-04 16:32:34 -07:00
services:roblox_emulator::context::Services,
}
impl Place{
2024-10-04 16:32:34 -07:00
fn new(dom:WeakDom)->Option<Self>{
let context=roblox_emulator::context::Context::from_ref(&dom);
Some(Self{
services:context.find_services()?,
dom,
})
}
2024-10-03 20:22:45 -07:00
pub fn run_scripts(&mut self){
let Place{dom,services}=self;
2024-09-17 18:27:56 -07:00
let runner=roblox_emulator::runner::Runner::new().unwrap();
2024-10-03 20:22:45 -07:00
let context=roblox_emulator::context::Context::from_mut(dom);
2024-10-03 19:35:52 -07:00
let scripts=context.scripts();
2024-10-04 16:32:34 -07:00
let runnable=runner.runnable_context_with_services(context,services).unwrap();
2024-10-03 19:35:52 -07:00
for script in scripts{
if let Err(e)=runnable.run_script(script){
2024-09-21 15:09:40 -07:00
println!("runner error: {e}");
2024-09-17 18:27:56 -07:00
}
}
}
}
impl AsRef<WeakDom> for Place{
fn as_ref(&self)->&WeakDom{
&self.dom
}
}
2024-02-13 21:27:28 -08:00
#[derive(Debug)]
2024-02-13 21:27:28 -08:00
pub enum ReadError{
RbxBinary(rbx_binary::DecodeError),
RbxXml(rbx_xml::DecodeError),
Io(std::io::Error),
UnknownFileFormat,
}
2024-02-13 21:27:28 -08:00
impl std::fmt::Display for ReadError{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for ReadError{}
pub fn read<R:Read>(input:R)->Result<Model,ReadError>{
let mut buf=std::io::BufReader::new(input);
2024-02-13 21:27:28 -08:00
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(Model::new).map_err(ReadError::RbxBinary),
b"<roblox "=>rbx_xml::from_reader_default(buf).map(Model::new).map_err(ReadError::RbxXml),
2024-02-13 21:27:28 -08:00
_=>Err(ReadError::UnknownFileFormat),
}
}
2024-02-13 21:27:28 -08:00
//ConvertError
2024-03-13 13:34:06 -07:00
pub fn convert<AcquireRenderConfigId,AcquireMeshId>(
dom:impl AsRef<WeakDom>,
2024-03-13 13:34:06 -07:00
acquire_render_config_id:AcquireRenderConfigId,
acquire_mesh_id:AcquireMeshId
)->rbx::PartialMap1
where
AcquireRenderConfigId:FnMut(Option<&str>)->strafesnet_common::model::RenderConfigId,
AcquireMeshId:FnMut(&str)->strafesnet_common::model::MeshId,
{
rbx::convert(&dom.as_ref(),acquire_render_config_id,acquire_mesh_id)
2024-09-17 18:27:56 -07:00
}