Skip to content

Commit

Permalink
Make ByteStream metadata generic
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed Mar 8, 2024
1 parent c48e284 commit 58cdc02
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 127 deletions.
4 changes: 2 additions & 2 deletions procedural/src/byte/convertable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn derive_for_struct(
let from = implement_from.then(|| {
quote! {
impl #impl_generics crate::loaders::FromBytes for #name #type_generics #where_clause {
fn from_bytes(byte_stream: &mut crate::loaders::ByteStream) -> Result<Self, Box<crate::loaders::ConversionError>> {
fn from_bytes<META>(byte_stream: &mut crate::loaders::ByteStream<META>) -> Result<Self, Box<crate::loaders::ConversionError>> {
let base_offset = byte_stream.get_offset();
#(#from_bytes_implementations)*
Ok(#instanciate)
Expand Down Expand Up @@ -88,7 +88,7 @@ fn derive_for_enum(
let from = add_from.then(|| {
quote! {
impl #impl_generics crate::loaders::FromBytes for #name #type_generics #where_clause {
fn from_bytes(byte_stream: &mut ByteStream) -> Result<Self, Box<crate::loaders::ConversionError>> {
fn from_bytes<META>(byte_stream: &mut ByteStream<META>) -> Result<Self, Box<crate::loaders::ConversionError>> {
match crate::loaders::conversion_result::<Self, _>(#numeric_type::from_bytes(byte_stream))? as usize {
#( #indices => Ok(Self::#values), )*
invalid => Err(crate::loaders::ConversionError::from_message(format!("invalid enum variant {}", invalid))),
Expand Down
9 changes: 7 additions & 2 deletions procedural/src/byte/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ pub fn byte_convertable_helper(data_struct: DataStruct) -> (Vec<TokenStream>, Ve
let from_implementation = match version_function {
Some(function) => {
quote! {
let #field_variable = match byte_stream.get_version().#function {
let #field_variable = match byte_stream
.get_metadata::<Self, Option<crate::loaders::InternalVersion>>()?
.ok_or(crate::loaders::ConversionError::from_message("version not set"))?
.#function {
true => Some(#from_implementation),
false => None,
};
Expand All @@ -169,7 +172,9 @@ pub fn byte_convertable_helper(data_struct: DataStruct) -> (Vec<TokenStream>, Ve
to_bytes_implementations.push(to_implementation);

if is_version {
from_bytes_implementations.push(quote!(byte_stream.set_version(#field_variable);));
from_bytes_implementations.push(
quote!(*byte_stream.get_metadata_mut::<Self, Option<crate::loaders::InternalVersion>>()? = Some(InternalVersion::from(#field_variable));),
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion procedural/src/byte/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn derive_incoming_packet_struct(
const IS_PING: bool = #is_ping;
const HEADER: u16 = #signature;

fn from_bytes(byte_stream: &mut crate::loaders::ByteStream) -> Result<Self, Box<crate::loaders::ConversionError>> {
fn from_bytes<META>(byte_stream: &mut crate::loaders::ByteStream<META>) -> Result<Self, Box<crate::loaders::ConversionError>> {

let base_offset = byte_stream.get_offset();
#(#from_bytes_implementations)*
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Transform {
}

impl FromBytes for Transform {
fn from_bytes(byte_stream: &mut crate::loaders::ByteStream) -> Result<Self, Box<ConversionError>> {
fn from_bytes<META>(byte_stream: &mut crate::loaders::ByteStream<META>) -> Result<Self, Box<ConversionError>> {
let mut position = conversion_result::<Self, _>(<Vector3<f32>>::from_bytes(byte_stream))?;
let rotation = conversion_result::<Self, _>(<Vector3<f32>>::from_bytes(byte_stream))?;
let scale = conversion_result::<Self, _>(<Vector3<f32>>::from_bytes(byte_stream))?;
Expand Down
2 changes: 1 addition & 1 deletion src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use self::event::*;
pub use self::layout::*;
pub use self::provider::StateProvider;
pub use self::settings::InterfaceSettings;
pub use self::state::{ValueState, Remote, TrackedState, TrackedStateTake};
pub use self::state::{Remote, TrackedState, TrackedStateTake, ValueState};
pub use self::theme::{GameTheme, InterfaceTheme};
use self::theme::{Main, Menu, ThemeSelector, Themes};
pub use self::windows::*;
Expand Down
2 changes: 1 addition & 1 deletion src/inventory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use vulkano::image::view::ImageView;

pub use self::hotbar::Hotbar;
pub use self::skills::{Skill, SkillTree};
use crate::interface::{ValueState, Remote, TrackedState};
use crate::interface::{Remote, TrackedState, ValueState};
use crate::loaders::{GameFileLoader, ScriptLoader, TextureLoader};
use crate::network::{EquipPosition, ItemId, ItemIndex};

Expand Down
3 changes: 2 additions & 1 deletion src/loaders/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use derive_new::new;
use procedural::*;
use vulkano::image::view::ImageView;

use super::version::InternalVersion;
use super::Sprite;
#[cfg(feature = "debug")]
use crate::debug::*;
Expand Down Expand Up @@ -275,7 +276,7 @@ impl ActionLoader {
let timer = Timer::new_dynamic(format!("load actions from {MAGENTA}{path}{NONE}"));

let bytes = game_file_loader.get(&format!("data\\sprite\\{path}"))?;
let mut byte_stream = ByteStream::new(&bytes);
let mut byte_stream: ByteStream<Option<InternalVersion>> = ByteStream::without_metadata(&bytes);

if <[u8; 2]>::from_bytes(&mut byte_stream).unwrap() != [b'A', b'C'] {
return Err(format!("failed to read magic number from {path}"));
Expand Down
6 changes: 3 additions & 3 deletions src/loaders/archive/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ impl Archive for NativeArchive {
// while being able to read without buffering the entire file.
let mut file_header_buffer = [0u8; UNPACKED_SIZE_OF_ARCHIVEHEADER];
file.read_exact(&mut file_header_buffer).unwrap();
let file_header = Header::from_bytes(&mut ByteStream::new(&file_header_buffer)).unwrap();
let file_header = Header::from_bytes(&mut ByteStream::<()>::without_metadata(&file_header_buffer)).unwrap();
file_header.validate_version();

let _ = file.seek(SeekFrom::Current(file_header.get_file_table_offset() as i64)).unwrap();
let mut file_table_buffer = [0u8; UNPACKED_SIZE_OF_FILETABLE];

file.read_exact(&mut file_table_buffer).unwrap();
let file_table = AssetTable::from_bytes(&mut ByteStream::new(&file_table_buffer)).unwrap();
let file_table = AssetTable::from_bytes(&mut ByteStream::<()>::without_metadata(&file_table_buffer)).unwrap();

let mut compressed_file_table_buffer = vec![0u8; file_table.get_compressed_size()];
file.read_exact(&mut compressed_file_table_buffer).unwrap();
let (decompressed, _checksum) = decompress(&compressed_file_table_buffer, Format::Zlib).unwrap();

let file_count = file_header.get_file_count();

let mut file_table_byte_stream = ByteStream::new(&decompressed);
let mut file_table_byte_stream = ByteStream::<()>::without_metadata(&decompressed);
let mut assets = HashMap::with_capacity(file_count);

for _index in 0..file_count {
Expand Down
16 changes: 14 additions & 2 deletions src/loaders/convertable/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub enum ConversionErrorType {
ByteStreamTooShort { type_name: &'static str },
DataTooBig { type_name: &'static str },
IncorrectMetadata { type_name: &'static str },
Specific { message: String },
}

Expand All @@ -12,7 +13,7 @@ pub struct ConversionError {
}

impl ConversionError {
pub(super) fn from_error_type(error_type: ConversionErrorType) -> Box<Self> {
pub fn from_error_type(error_type: ConversionErrorType) -> Box<Self> {
Box::new(Self {
error_type,
stack: Vec::new(),
Expand Down Expand Up @@ -43,7 +44,18 @@ impl std::fmt::Debug for ConversionError {
write!(formatter, "byte stream too short while parsing {} in {}", type_name, stack)
}
ConversionErrorType::DataTooBig { type_name } => {
write!(formatter, "data is too big for the available space {} in {}", type_name, stack)
write!(
formatter,
"data is too big for the available space for {} in {}",
type_name, stack
)
}
ConversionErrorType::IncorrectMetadata { type_name } => {
write!(
formatter,
"the metadata associated to the byte stream is incorrect for {} in {}",
type_name, stack
)
}
ConversionErrorType::Specific { message } => write!(formatter, "{} in {}", message, stack),
}
Expand Down
Loading

0 comments on commit 58cdc02

Please sign in to comment.