Skip to content

Commit

Permalink
Add support for legacy class names
Browse files Browse the repository at this point in the history
Legacy class names do not have the _Z suffix and are all uppercase
i.e. SURFACE instead of Surface_Z.
  • Loading branch information
widberg committed Oct 12, 2023
1 parent 6191133 commit 552ac79
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
15 changes: 8 additions & 7 deletions bff-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,33 @@ pub fn bff_named_class(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let class_name = LitStr::new(format!("{}_Z", name).as_str(), name.span());
let class_name_legacy = LitStr::new(&name.to_string().to_uppercase(), name.span());

// This mess can go away once https://github.com/rust-lang/rust/issues/76001 is stabilized
quote! {
impl crate::traits::NamedClass<crate::names::NameAsobo32> for #name {
const NAME: crate::names::NameAsobo32 = crate::names::NameAsobo32::new(crate::crc32::asobo(#class_name.as_bytes()));
const NAME_LEGACY: crate::names::NameAsobo32 = crate::names::NameAsobo32::new(crate::crc32::asobo(#class_name_legacy.as_bytes()));
}

impl crate::traits::NamedClass<crate::names::NameAsoboAlternate32> for #name {
const NAME: crate::names::NameAsoboAlternate32 = crate::names::NameAsoboAlternate32::new(crate::crc32::asobo_alternate(#class_name.as_bytes()));
const NAME_LEGACY: crate::names::NameAsoboAlternate32 = crate::names::NameAsoboAlternate32::new(crate::crc32::asobo_alternate(#class_name_legacy.as_bytes()));
}

impl crate::traits::NamedClass<crate::names::NameKalisto32> for #name {
const NAME: crate::names::NameKalisto32 = crate::names::NameKalisto32::new(crate::crc32::kalisto(#class_name.as_bytes()));
const NAME_LEGACY: crate::names::NameKalisto32 = crate::names::NameKalisto32::new(crate::crc32::kalisto(#class_name_legacy.as_bytes()));
}

impl crate::traits::NamedClass<crate::names::NameAsobo64> for #name {
const NAME: crate::names::NameAsobo64 = crate::names::NameAsobo64::new(crate::crc64::asobo(#class_name.as_bytes()));
const NAME_LEGACY: crate::names::NameAsobo64 = crate::names::NameAsobo64::new(crate::crc64::asobo(#class_name_legacy.as_bytes()));
}

impl crate::traits::NamedClass<&'static str> for #name {
const NAME: &'static str = #class_name;
const NAME_LEGACY: &'static str = #class_name_legacy;
}
}
.into()
Expand Down Expand Up @@ -124,13 +130,8 @@ fn impl_from_object_to_shadow_class(input: &BffClassMacroInput) -> proc_macro2::
match (version.clone(), platform) {
#(#arms)*
_ => Err(
crate::error::UnimplementedClassError::new(object.name,
match crate::names::names().lock().unwrap().name_type {
crate::names::NameType::Asobo32 => <Self as crate::traits::NamedClass<crate::names::NameAsobo32>>::NAME.into(),
crate::names::NameType::AsoboAlternate32 => <Self as crate::traits::NamedClass<crate::names::NameAsoboAlternate32>>::NAME.into(),
crate::names::NameType::Kalisto32 => <Self as crate::traits::NamedClass<crate::names::NameKalisto32>>::NAME.into(),
crate::names::NameType::Asobo64 => <Self as crate::traits::NamedClass<crate::names::NameAsobo64>>::NAME.into(),
}, version, platform).into(),
// TODO: Pick the right name based on the algorithm and suffix for the current BigFile
crate::error::UnimplementedClassError::new(object.name, <Self as crate::traits::NamedClass<crate::names::NameAsobo32>>::NAME.into(), version, platform).into(),
),
}
}
Expand Down
13 changes: 8 additions & 5 deletions bff/src/macros/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ macro_rules! classes {
type Error = crate::error::Error;

fn try_from_version_platform(object: &crate::bigfile::resource::Resource, version: crate::versions::Version, platform: crate::platforms::Platform) -> crate::BffResult<Class> {
use crate::traits::NamedClass;
match object.class_name {
$(crate::names::Name::Asobo32(<$class as crate::traits::NamedClass<crate::names::NameAsobo32>>::NAME)
| crate::names::Name::AsoboAlternate32(<$class as crate::traits::NamedClass<crate::names::NameAsoboAlternate32>>::NAME)
| crate::names::Name::Kalisto32(<$class as crate::traits::NamedClass<crate::names::NameKalisto32>>::NAME)
| crate::names::Name::Asobo64(<$class as crate::traits::NamedClass<crate::names::NameAsobo64>>::NAME) => Ok(Box::new(<&crate::bigfile::resource::Resource as crate::traits::TryIntoVersionPlatform<$class>>::try_into_version_platform(object, version, platform)?).into()),)*
$(crate::names::Name::Asobo32($class::NAME) | crate::names::Name::Asobo32($class::NAME_LEGACY)
| crate::names::Name::AsoboAlternate32($class::NAME) | crate::names::Name::AsoboAlternate32($class::NAME_LEGACY)
| crate::names::Name::Kalisto32($class::NAME) | crate::names::Name::Kalisto32($class::NAME_LEGACY)
| crate::names::Name::Asobo64($class::NAME) | crate::names::Name::Asobo64($class::NAME_LEGACY)
=> Ok(Box::new(<&crate::bigfile::resource::Resource as crate::traits::TryIntoVersionPlatform<$class>>::try_into_version_platform(object, version, platform)?).into()),)*
_ => Err(crate::error::UnimplementedClassError::new(object.name, object.class_name, version, platform).into()),
}
}
Expand All @@ -32,7 +34,8 @@ macro_rules! classes {
}

pub fn class_names() -> Vec<&'static str> {
vec![$(<$class as crate::traits::NamedClass<&'static str>>::NAME,)*]
use crate::traits::NamedClass;
vec![$($class::NAME,$class::NAME_LEGACY,)*]
}
};
}
Expand Down
19 changes: 18 additions & 1 deletion bff/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ where

pub trait NamedClass<N> {
const NAME: N;
const NAME_LEGACY: N;
}

pub trait BigFileIo {
Expand Down Expand Up @@ -172,7 +173,23 @@ macro_rules! impl_referenced_names {
}
}

impl_referenced_names!((), bool, f32, f64, u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, String);
impl_referenced_names!(
(),
bool,
f32,
f64,
u8,
u16,
u32,
u64,
u128,
i8,
i16,
i32,
i64,
i128,
String
);

// this should be const https://github.com/rust-lang/rust/issues/67792
pub trait NameHashFunction {
Expand Down

0 comments on commit 552ac79

Please sign in to comment.