aboutsummaryrefslogtreecommitdiff
path: root/src/classes
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-03-13 18:07:07 +0100
committermetamuffin <metamuffin@disroot.org>2025-03-13 18:07:07 +0100
commit24d70ee5b9ed230e36b628b6b3931a5533578b56 (patch)
treec0b636101bba41e74b968685d80b85f83a49dddb /src/classes
parent70d22e5162afa1b81f976acd1db534834010d3b8 (diff)
downloadunity-tools-24d70ee5b9ed230e36b628b6b3931a5533578b56.tar
unity-tools-24d70ee5b9ed230e36b628b6b3931a5533578b56.tar.bz2
unity-tools-24d70ee5b9ed230e36b628b6b3931a5533578b56.tar.zst
clean up code again
Diffstat (limited to 'src/classes')
-rw-r--r--src/classes/texture2d.rs37
-rw-r--r--src/classes/transform.rs21
2 files changed, 26 insertions, 32 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)]
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<Self> {
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::<PPtr>()
- .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(),
})
}
}