use crate::{DecodeError, Format, Image, Plan};
use std::sync::Arc;
use unityrs_core::limits::{ChargeError, Meter, MeteredVec, heap_cost};
mod object;
pub use object::images;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ArrayFields {
pub graphics_format: i32,
pub width: i32,
pub height: i32,
pub depth: i32,
pub data_size: u64,
pub platform: i32,
}
const _: () = crate::send_sync::<ArrayFields>();
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum ArrayError {
#[error("invalid Texture2DArray field {0}")]
Field(&'static str),
#[error(transparent)]
Parse(#[from] unityrs_core::walk::ParseError),
#[error(transparent)]
Resource(#[from] unityrs_core::Error),
#[error("unknown GraphicsFormat {0}")]
UnknownGraphicsFormat(i32),
#[error("unmapped GraphicsFormat {0}")]
UnmappedGraphicsFormat(i32),
#[error("unsupported Texture2DArray platform {0}")]
UnsupportedPlatform(i32),
#[error("invalid Texture2DArray slice layout")]
Layout,
#[error("unsupported array TextureFormat {0}")]
UnsupportedFormat(i32),
#[error(transparent)]
Decode(#[from] DecodeError),
#[error(transparent)]
Charge(#[from] ChargeError),
}
const _: () = crate::send_sync::<ArrayError>();
pub fn unitypy_texture_format(value: i32) -> Result<i32, ArrayError> {
let format = match value {
1 | 5 => 63,
2 | 6 => 62,
3 | 7 => 3,
4 | 8 => 4,
9..=12 => 75 + value - 9,
13 | 21 | 29 => 9,
14 | 22 | 30 => 72,
15 | 23 | 31 => 73,
16 | 24 | 32 => 74,
17 | 25 | 33 => 79,
18 | 26 | 34 => 80,
19 | 27 | 35 => 81,
20 | 28 | 36 => 82,
56 | 58 => 8,
57 | 59 => 14,
96 | 97 => 10,
98 | 99 => 11,
100 | 101 => 12,
102 => 26,
104 => 27,
106 => 24,
108 | 109 => 25,
110 | 111 => 30,
112 | 113 => 32,
114 | 115 => 31,
116 | 117 => 33,
118 => 34,
119 | 120 => 45,
121 | 122 => 46,
123 | 124 => 47,
125 => 41,
126 => 42,
127 => 43,
128 => 44,
129 | 130 => 54,
131 | 132 => 55,
133 | 134 => 56,
135 | 136 => 57,
137 | 138 => 58,
139 | 140 => 59,
141 => 21,
_ => {
return Err(
if matches!(value, 0..=52 | 56..=86 | 90..=141 | 145..=151) {
ArrayError::UnmappedGraphicsFormat(value)
} else {
ArrayError::UnknownGraphicsFormat(value)
},
);
}
};
Ok(format)
}
pub fn texture_format(value: i32) -> Result<i32, ArrayError> {
match value {
13 => Ok(63),
14 => Ok(62),
15 => Ok(3),
16 => Ok(4),
17..=20 => Ok(75 + value - 17),
145..=150 => Ok(66 + value - 145),
_ => unitypy_texture_format(value),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ArrayPlan {
pub slice_count: usize,
pub slice_bytes: usize,
pub image: Plan,
pub peak_charge: u64,
}
const _: () = crate::send_sync::<ArrayPlan>();
fn slice_plan(format: i32, len: usize, w: i32, h: i32) -> Result<Plan, ArrayError> {
if w == 0 || h == 0 {
return Ok(crate::plan(Format::Rgba32, len, w, h)?);
}
let f = Format::from_texture_format(format).ok_or(ArrayError::UnsupportedFormat(format))?;
Ok(crate::plan(f, len, w, h)?)
}
pub fn plan(fields: &ArrayFields, data_len: usize) -> Result<ArrayPlan, ArrayError> {
if matches!(fields.platform, crate::SWITCH | crate::XBOX360) {
return Err(ArrayError::UnsupportedPlatform(fields.platform));
}
let format = texture_format(fields.graphics_format)?;
let depth = u64::try_from(fields.depth)
.ok()
.filter(|n| *n > 0)
.ok_or(ArrayError::Layout)?;
let step = fields.data_size / depth;
if step == 0 {
return Err(ArrayError::Layout);
}
let count = fields.data_size.div_ceil(step);
let end = (count - 1).checked_mul(step).ok_or(ArrayError::Layout)?;
let last_len = (data_len as u64).saturating_sub(end).min(step);
let image = slice_plan(
format,
usize::try_from(last_len).map_err(|_| ArrayError::Layout)?,
fields.width,
fields.height,
)?;
let slice_count = usize::try_from(count).map_err(|_| ArrayError::Layout)?;
let slice_bytes = usize::try_from(step).map_err(|_| ArrayError::Layout)?;
let table = count
.checked_mul(std::mem::size_of::<Image>() as u64)
.ok_or(ArrayError::Layout)?;
let peak_charge = image
.charge()
.checked_mul(count)
.and_then(|n| n.checked_add(heap_cost(table)))
.and_then(|n| n.checked_add(image.scratch))
.ok_or(ArrayError::Layout)?;
Ok(ArrayPlan {
slice_count,
slice_bytes,
image,
peak_charge,
})
}
pub fn decode(
fields: &ArrayFields,
inline: &[u8],
resource: Option<&[u8]>,
meter: &Arc<Meter>,
) -> Result<MeteredVec<Image>, ArrayError> {
let data = if inline.is_empty() {
resource.unwrap_or(inline)
} else {
inline
};
let p = plan(fields, data.len())?;
let format = texture_format(fields.graphics_format)?;
meter.count_decode(p.slice_count as u64)?;
let mut images = MeteredVec::with_capacity(meter, p.slice_count)?;
for i in 0..p.slice_count {
let start = i.checked_mul(p.slice_bytes).ok_or(ArrayError::Layout)?;
let end = start.saturating_add(p.slice_bytes).min(data.len());
let input = data.get(start..end).unwrap_or_default();
let img = if fields.width == 0 || fields.height == 0 {
crate::decode(Format::Rgba32, input, fields.width, fields.height, meter)?
} else {
let f =
Format::from_texture_format(format).ok_or(ArrayError::UnsupportedFormat(format))?;
crate::decode(f, input, fields.width, fields.height, meter)?
};
images.push(img)?;
}
Ok(images)
}