diff options
author | metamuffin <metamuffin@disroot.org> | 2025-03-13 18:07:07 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-03-13 18:07:07 +0100 |
commit | 24d70ee5b9ed230e36b628b6b3931a5533578b56 (patch) | |
tree | c0b636101bba41e74b968685d80b85f83a49dddb /src/classes/texture2d.rs | |
parent | 70d22e5162afa1b81f976acd1db534834010d3b8 (diff) | |
download | unity-tools-24d70ee5b9ed230e36b628b6b3931a5533578b56.tar unity-tools-24d70ee5b9ed230e36b628b6b3931a5533578b56.tar.bz2 unity-tools-24d70ee5b9ed230e36b628b6b3931a5533578b56.tar.zst |
clean up code again
Diffstat (limited to 'src/classes/texture2d.rs')
-rw-r--r-- | src/classes/texture2d.rs | 37 |
1 files changed, 20 insertions, 17 deletions
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<Self> { 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<DynamicImage> { let w = self.width as usize; @@ -142,6 +134,17 @@ impl Texture2D { } } +impl FromValue for TextureFormat { + fn from_value(v: Value) -> Result<Self> { + 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)] |