From 57cf820ac08a68d9866a72166980ccaee8d55e0f Mon Sep 17 00:00:00 2001 From: Adam Gausmann Date: Fri, 28 Oct 2022 22:54:14 -0500 Subject: [PATCH] Add data parser for circuit boards --- src/custom_data.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 49 insertions(+) create mode 100644 src/custom_data.rs diff --git a/src/custom_data.rs b/src/custom_data.rs new file mode 100644 index 0000000..87b2162 --- /dev/null +++ b/src/custom_data.rs @@ -0,0 +1,48 @@ +//! Parsers and serializers for vanilla component data. +//! +//! These are designed for Logic World 0.91.0 Preview 510 and may not work for +//! other game versions. + +use std::io::{Read, Write}; + +use crate::error::Error; +use crate::io::*; + +pub trait ComponentData: Sized { + const TYPE_STRING: &'static str; + + fn read(reader: &mut R) -> Result; + + fn write(&self, writer: &mut W) -> Result<(), Error>; +} + +#[derive(Debug)] +pub struct CircuitBoard { + pub color: [u8; 3], + pub size_x: u32, + pub size_z: u32, +} + +impl ComponentData for CircuitBoard { + const TYPE_STRING: &'static str = "MHG.CircuitBoard"; + + fn read(reader: &mut R) -> Result { + let color = read_array(|| read_u8(reader))?; + let size_x = read_u32(reader)?; + let size_z = read_u32(reader)?; + Ok(Self { + color, + size_x, + size_z, + }) + } + + fn write(&self, writer: &mut W) -> Result<(), Error> { + for x in self.color { + write_u8(x, writer)?; + } + write_u32(self.size_x, writer)?; + write_u32(self.size_z, writer)?; + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 17c88c5..07e8371 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ mod convert; +pub mod custom_data; pub mod error; pub(crate) mod io; pub mod v5;