From 24d70ee5b9ed230e36b628b6b3931a5533578b56 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Thu, 13 Mar 2025 18:07:07 +0100 Subject: clean up code again --- src/classes/texture2d.rs | 37 ++++++++++++++++++++----------------- src/classes/transform.rs | 21 ++++++--------------- 2 files changed, 26 insertions(+), 32 deletions(-) (limited to 'src/classes') diff --git a/src/classes/texture2d.rs b/src/classes/texture2d.rs index 147cb5d..752bfeb 100644 --- a/src/classes/texture2d.rs +++ b/src/classes/texture2d.rs @@ -1,10 +1,9 @@ use super::streaminginfo::StreamingInfo; use crate::object::{Value, parser::FromValue}; -use anyhow::{Result, bail}; +use anyhow::{Result, anyhow, bail}; use image::{DynamicImage, ImageBuffer, Luma, Rgb, Rgba}; use log::info; use serde::Serialize; -use std::mem::transmute; #[derive(Debug, Serialize)] pub struct Texture2D { @@ -23,25 +22,18 @@ impl FromValue for Texture2D { fn from_value(v: Value) -> Result { let mut fields = v.as_class("Texture2D").unwrap(); Ok(Texture2D { - width: fields.remove("m_Width").unwrap().as_i32().unwrap(), - height: fields.remove("m_Height").unwrap().as_i32().unwrap(), - mip_count: fields.remove("m_MipCount").unwrap().as_i32().unwrap(), - texture_dimension: fields - .remove("m_TextureDimension") - .unwrap() - .as_i32() - .unwrap(), - format: unsafe { - transmute::<_, TextureFormat>( - fields.remove("m_TextureFormat").unwrap().as_i32().unwrap(), - ) - }, - name: fields.remove("m_Name").unwrap().as_string().unwrap(), + width: fields.field("m_Width")?, + height: fields.field("m_Height")?, + mip_count: fields.field("m_MipCount")?, + texture_dimension: fields.field("m_TextureDimension")?, + format: fields.field("m_TextureFormat")?, + name: fields.field("m_Name")?, + stream_data: fields.field("m_StreamData")?, image_data: fields.remove("image data").unwrap().as_typeless().unwrap(), - stream_data: fields.remove("m_StreamData").unwrap().parse().unwrap(), }) } } + impl Texture2D { pub fn to_image(&self) -> Result { let w = self.width as usize; @@ -142,6 +134,17 @@ impl Texture2D { } } +impl FromValue for TextureFormat { + fn from_value(v: Value) -> Result { + let x = v.as_i32().ok_or(anyhow!("expected i32 TextureFormat"))?; + if x < 72 { + Ok(unsafe { std::mem::transmute(x) }) + } else { + bail!("TextureFormat out of range") + } + } +} + #[allow(non_camel_case_types)] #[repr(i32)] #[derive(Debug, Serialize, PartialEq, Clone, Copy)] diff --git a/src/classes/transform.rs b/src/classes/transform.rs index d31a7d6..2ec515a 100644 --- a/src/classes/transform.rs +++ b/src/classes/transform.rs @@ -18,28 +18,19 @@ impl FromValue for Transform { fn from_value(v: Value) -> Result { let mut fields = v.as_class("Transform").unwrap(); Ok(Self { + father: fields.field("m_Father")?, + gameobject: fields.field("m_GameObject")?, + local_position: fields.field("m_LocalPosition")?, + local_rotation: fields.field("m_LocalRotation")?, + local_scale: fields.field("m_LocalScale")?, children: fields .remove("m_Children") .unwrap() - .as_class("vector") - .unwrap() - .remove("Array") - .unwrap() - .as_array() + .as_vector() .unwrap() .into_iter() .map(|e| PPtr::from_value(e).unwrap().cast()) .collect(), - father: fields - .remove("m_Father") - .unwrap() - .parse::() - .unwrap() - .cast(), - gameobject: fields.remove("m_GameObject").unwrap().parse().unwrap(), - local_position: fields.remove("m_LocalPosition").unwrap().parse().unwrap(), - local_rotation: fields.remove("m_LocalRotation").unwrap().parse().unwrap(), - local_scale: fields.remove("m_LocalScale").unwrap().parse().unwrap(), }) } } -- cgit v1.2.3-70-g09d2