From 2b7a1b344fe2e37442324fad045affc3b54af553 Mon Sep 17 00:00:00 2001 From: widberg Date: Sat, 30 Sep 2023 12:19:50 -0400 Subject: [PATCH] Implement TryFromVersionPlatform<&TrivialClass> for Object --- bff/src/class/trivial_class.rs | 91 ++++++++++++++++++++-------------- bff/src/object.rs | 8 +-- 2 files changed, 59 insertions(+), 40 deletions(-) diff --git a/bff/src/class/trivial_class.rs b/bff/src/class/trivial_class.rs index 7ed83d6..02759dc 100644 --- a/bff/src/class/trivial_class.rs +++ b/bff/src/class/trivial_class.rs @@ -4,69 +4,88 @@ use binrw::{BinRead, BinWrite}; use serde::{Deserialize, Serialize}; use crate::error::Error; +use crate::name::Name; use crate::object::Object; use crate::platforms::Platform; use crate::traits::TryFromVersionPlatform; use crate::versions::Version; -trait TyEq {} - -impl TyEq for (T, T) {} - #[derive(Debug, Serialize, Deserialize)] -pub struct TrivialClass -where - for<'a> LinkHeaderType: BinRead + BinWrite + Serialize + 'a, - for<'a> ::Args<'a>: Default, - for<'a> ::Args<'a>: Default, - - for<'a> BodyType: BinRead = (&'a LinkHeaderType,)> + BinWrite + Serialize + 'a, -{ - link_header: LinkHeaderType, - body: BodyType, +pub struct TrivialClass { + pub name: Name, + pub class_name: Name, + pub link_header: LinkHeaderType, + pub body: BodyType, } -impl TrivialClass +impl TryFromVersionPlatform<&Object> + for TrivialClass where - for<'a> LinkHeaderType: BinRead + BinWrite + Serialize + 'a, + for<'a> LinkHeaderType: BinRead + 'a, for<'a> ::Args<'a>: Default, - for<'a> ::Args<'a>: Default, - for<'a> BodyType: BinRead = (&'a LinkHeaderType,)> + BinWrite + Serialize + 'a, + for<'a> BodyType: BinRead = (&'a LinkHeaderType,)> + 'a, { - pub fn link_header(&self) -> &LinkHeaderType { - &self.link_header - } + type Error = Error; - pub fn body(&self) -> &BodyType { - &self.body + fn try_from_version_platform( + object: &Object, + _version: Version, + platform: Platform, + ) -> Result { + let mut link_header_cursor = Cursor::new(object.link_header()); + let mut body_cursor = Cursor::new(object.body()); + let link_header = LinkHeaderType::read_options( + &mut link_header_cursor, + platform.into(), + ::Args::default(), + )?; + let body = BodyType::read_options(&mut body_cursor, platform.into(), (&link_header,))?; + Ok(Self { + name: object.name, + class_name: object.class_name, + link_header, + body, + }) } } -impl TryFromVersionPlatform<&Object> - for TrivialClass +impl TryFromVersionPlatform<&TrivialClass> + for Object where - for<'a> LinkHeaderType: BinRead + BinWrite + Serialize + 'a, - for<'a> ::Args<'a>: Default, + for<'a> LinkHeaderType: BinWrite + 'a, for<'a> ::Args<'a>: Default, - for<'a> BodyType: BinRead = (&'a LinkHeaderType,)> + BinWrite + Serialize + 'a, + for<'a> BodyType: BinWrite + 'a, + for<'a> ::Args<'a>: Default, { type Error = Error; fn try_from_version_platform( - object: &Object, + class: &TrivialClass, _version: Version, platform: Platform, ) -> Result { - let mut header_cursor = Cursor::new(object.link_header()); - let mut body_cursor = Cursor::new(object.body()); - let link_header = LinkHeaderType::read_options( - &mut header_cursor, + let mut link_header_cursor = Cursor::new(Vec::new()); + let mut body_cursor = Cursor::new(Vec::new()); + LinkHeaderType::write_options( + &class.link_header, + &mut link_header_cursor, platform.into(), - ::Args::default(), + ::Args::default(), )?; - let body = BodyType::read_options(&mut body_cursor, platform.into(), (&link_header,))?; - Ok(Self { link_header, body }) + BodyType::write_options( + &class.body, + &mut body_cursor, + platform.into(), + ::Args::default(), + )?; + Ok(Self { + compress: false, + name: class.name, + class_name: class.class_name, + link_header: link_header_cursor.into_inner(), + body: body_cursor.into_inner(), + }) } } diff --git a/bff/src/object.rs b/bff/src/object.rs index 354b40f..1fcaeff 100644 --- a/bff/src/object.rs +++ b/bff/src/object.rs @@ -39,12 +39,12 @@ pub struct Object { compressed_size: u32, #[br(calc = compressed_size != 0)] #[bw(ignore)] - compress: bool, - class_name: Name, - name: Name, + pub compress: bool, + pub class_name: Name, + pub name: Name, #[br(count = link_header_size)] #[serde(skip_serializing)] - link_header: Vec, + pub link_header: Vec, #[br(parse_with = body_parser, args(decompressed_size, compressed_size))] #[serde(skip_serializing)] pub body: Vec,