use std::fmt;
use crate::format::{Format, texture_format_name};
use crate::formats::Formats;
pub const XBOX360: i32 = 11;
pub const SWITCH: i32 = 38;
pub const XBOX_SWAP_FORMATS: [i32; 5] = [7, 10, 28, 12, 29];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TextureFields<'a> {
pub texture_format: i32,
pub width: i32,
pub height: i32,
pub platform: i32,
pub platform_blob: Option<&'a [u8]>,
}
const _: () = crate::send_sync::<TextureFields<'static>>();
impl TextureFields<'_> {
#[must_use]
pub fn image_mode(&self) -> Option<crate::Mode> {
if self.width == 0 || self.height == 0 {
return Some(crate::Mode::Rgba);
}
let format = if is_switch_swizzled(self.platform, self.platform_blob) {
match self.texture_format {
3 => 4,
8 => 14,
other => other,
}
} else {
self.texture_format
};
Format::from_texture_format(format).map(Format::mode)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Selection {
Export(Format),
Skip(Skip),
}
const _: () = crate::send_sync::<Selection>();
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Skip {
EmptyImage,
UnsupportedFormat(i32),
UnsupportedPlatform(SkippedPlatform),
}
const _: () = crate::send_sync::<Skip>();
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SkippedPlatform {
Switch,
Xbox360,
}
const _: () = crate::send_sync::<SkippedPlatform>();
impl fmt::Display for Skip {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyImage => f.write_str("empty_image"),
Self::UnsupportedFormat(value) => match texture_format_name(*value) {
Some(name) => write!(f, "unsupported_format({name})"),
None => write!(f, "unsupported_format({value})"),
},
Self::UnsupportedPlatform(SkippedPlatform::Switch) => {
f.write_str("unsupported_platform(switch)")
}
Self::UnsupportedPlatform(SkippedPlatform::Xbox360) => {
f.write_str("unsupported_platform(xbox360)")
}
}
}
}
#[must_use]
pub fn is_switch_swizzled(platform: i32, platform_blob: Option<&[u8]>) -> bool {
if platform != SWITCH {
return false;
}
match platform_blob.and_then(|blob| blob.get(8..12)) {
Some(gobs) => gobs.iter().any(|&b| b != 0),
None => false,
}
}
#[must_use]
pub fn select(texture: &TextureFields<'_>) -> Selection {
select_in(texture, Formats::V1, (0, 0))
}
#[must_use]
pub fn select_in(
texture: &TextureFields<'_>,
formats: Formats,
unity_version: (u16, u16),
) -> Selection {
if texture.width == 0 || texture.height == 0 {
return Selection::Skip(Skip::EmptyImage);
}
if texture.texture_format == 6 || (texture.texture_format == 21 && texture.width % 2 != 0) {
return Selection::Skip(Skip::UnsupportedFormat(texture.texture_format));
}
let Some(format) = Format::from_texture_format(texture.texture_format)
.filter(|f| formats.contains(*f))
.map(|f| match f {
Format::Dxt1Crunched if unity_version >= (2017, 3) => Format::UnityDxt1Crunched,
Format::Dxt5Crunched if unity_version >= (2017, 3) => Format::UnityDxt5Crunched,
other => other,
})
else {
return Selection::Skip(Skip::UnsupportedFormat(texture.texture_format));
};
let switch_format = match texture.texture_format {
3 => 4,
8 => 14,
other => other,
};
if is_switch_swizzled(texture.platform, texture.platform_blob)
&& crate::platform::switch_block(switch_format).is_none()
{
return Selection::Skip(Skip::UnsupportedPlatform(SkippedPlatform::Switch));
}
Selection::Export(format)
}
#[must_use]
pub fn select_crunch(texture: &TextureFields<'_>, unity_version: (u16, u16)) -> Selection {
select_in(
texture,
Formats::V1
.with(Format::Dxt1Crunched)
.with(Format::Dxt5Crunched)
.with(Format::EtcRgb4Crunched)
.with(Format::Etc2Rgba8Crunched),
unity_version,
)
}