Skip to content

Commit

Permalink
Fucked names impl part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
widberg committed Oct 11, 2023
1 parent 5da75c7 commit d22aceb
Show file tree
Hide file tree
Showing 12 changed files with 421 additions and 146 deletions.
9 changes: 5 additions & 4 deletions bff-cli/src/crc32.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::{self, BufRead, Read};

use bff::crc32::{asobo_alternate_options, asobo_options, kalisto_options};
use bff::crc32::{Asobo32, AsoboAlternate32, Kalisto32};
use bff::traits::NameHashFunction;
use clap::ValueEnum;

use crate::error::BffCliResult;
Expand Down Expand Up @@ -50,9 +51,9 @@ pub fn crc32(
) -> BffCliResult<()> {
let starting = *starting;
let hash_function = match algorithm {
Crc32Algorithm::Asobo => asobo_options,
Crc32Algorithm::AsoboAlternate => asobo_alternate_options,
Crc32Algorithm::Kalisto => kalisto_options,
Crc32Algorithm::Asobo => Asobo32::hash_options,
Crc32Algorithm::AsoboAlternate => AsoboAlternate32::hash_options,
Crc32Algorithm::Kalisto => Kalisto32::hash_options,
};

match (string, mode) {
Expand Down
11 changes: 6 additions & 5 deletions bff-cli/src/crc64.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::{self, BufRead, Read};

use bff::crc64::asobo_crc64_options;
use bff::crc64::Asobo64;
use bff::traits::NameHashFunction;
use clap::ValueEnum;

use crate::crc32::{CrcFormat, CrcMode};
Expand All @@ -11,13 +12,13 @@ pub enum Crc64Algorithm {
Asobo,
}

fn format_hash(hash: u64, format: &CrcFormat) -> String {
fn format_hash(hash: i64, format: &CrcFormat) -> String {
match format {
CrcFormat::Signed => {
format!("{}", hash as i64)
format!("{}", hash)
}
CrcFormat::Unsigned => {
format!("{}", hash)
format!("{}", hash as u64)
}
CrcFormat::Hexadecimal => {
format!("{:#08x}", hash)
Expand All @@ -34,7 +35,7 @@ pub fn crc64(
) -> BffCliResult<()> {
let starting = *starting;
let hash_function = match algorithm {
Crc64Algorithm::Asobo => asobo_crc64_options,
Crc64Algorithm::Asobo => Asobo64::hash_options,
};

match (string, mode) {
Expand Down
52 changes: 41 additions & 11 deletions bff-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,26 @@ pub fn bff_named_class(input: TokenStream) -> TokenStream {
let name = &input.ident;
let class_name = LitStr::new(format!("{}_Z", name).as_str(), name.span());

// This mess can go away once https://github.com/rust-lang/rust/issues/76001 is stabilized
quote! {
impl crate::traits::NamedClass for #name {
const NAME: crate::names::Name = crate::names::Name::new(crate::crc32::asobo(#class_name.as_bytes()));
const NAME_STR: &'static str = #class_name;
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()));
}

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()));
}

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()));
}

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()));
}

impl crate::traits::NamedClass<&'static str> for #name {
const NAME: &'static str = #class_name;
}
}
.into()
Expand Down Expand Up @@ -108,7 +124,13 @@ fn impl_from_object_to_shadow_class(input: &BffClassMacroInput) -> proc_macro2::
match (version.clone(), platform) {
#(#arms)*
_ => Err(
crate::error::UnimplementedClassError::new(object.name, <Self as crate::traits::NamedClass>::NAME, version, platform).into(),
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(),
),
}
}
Expand Down Expand Up @@ -194,7 +216,10 @@ fn impl_read_bigfile(input: &BffBigFileMacroInput) -> proc_macro2::TokenStream {
let body = &form.body;
quote! {
#(#attrs)*
#pat #guard => { <#body as BigFileRead>::read(reader, version, platform) }
#pat #guard => {
crate::names::names().lock().unwrap().name_type = <#body as BigFileIo>::name_type(version.clone(), platform);
<#body as BigFileIo>::read(reader, version, platform)
}
}
})
.collect::<Vec<_>>();
Expand All @@ -204,7 +229,7 @@ fn impl_read_bigfile(input: &BffBigFileMacroInput) -> proc_macro2::TokenStream {
use crate::versions::Version::*;
use crate::platforms::Platform::*;
use binrw::BinRead;
use crate::traits::BigFileRead;
use crate::traits::BigFileIo;
let endian: crate::Endian = platform.into();
let version: crate::versions::Version = crate::strings::FixedStringNull::<256>::read_be(reader)?.as_str().into();
match (version.clone(), platform) {
Expand All @@ -229,7 +254,10 @@ fn impl_write_bigfile(input: &BffBigFileMacroInput) -> proc_macro2::TokenStream
let body = &form.body;
quote! {
#(#attrs)*
#pat #guard => { <#body as BigFileWrite>::write(self, writer) }
#pat #guard => {
crate::names::names().lock().unwrap().name_type = <#body as BigFileIo>::name_type(version.clone(), platform);
<#body as BigFileIo>::write(self, writer)
}
}
})
.collect::<Vec<_>>();
Expand All @@ -239,11 +267,13 @@ fn impl_write_bigfile(input: &BffBigFileMacroInput) -> proc_macro2::TokenStream
use crate::versions::Version::*;
use crate::platforms::Platform::*;
use binrw::BinWrite;
use crate::traits::BigFileWrite;
let endian: crate::Endian = self.manifest.platform.into();
let version_string = self.manifest.version.to_string();
use crate::traits::BigFileIo;
let platform = self.manifest.platform;
let endian: crate::Endian = platform.into();
let version = &self.manifest.version;
let version_string = version.to_string();
crate::strings::FixedStringNull::<256>::write_be(&version_string.into(), writer)?;
match (self.manifest.version.clone(), self.manifest.platform) {
match (version.clone(), platform) {
#(#arms)*
(version, platform) => Err(crate::error::UnimplementedVersionPlatformError::new(version, platform).into()),
}
Expand Down
13 changes: 8 additions & 5 deletions bff/src/bigfile/v1_06_63_02_pc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ use crate::bigfile::v1_06_63_02_pc::pool::{
use crate::bigfile::BigFile;
use crate::helpers::{calculated_padded, write_align_to};
use crate::lz::compress_data_with_header_writer_internal;
use crate::names::Name;
use crate::names::NameType::Asobo32;
use crate::names::{Name, NameType};
use crate::platforms::Platform;
use crate::traits::{BigFileRead, BigFileWrite};
use crate::traits::BigFileIo;
use crate::versions::{Version, VersionTriple};
use crate::BffResult;

Expand Down Expand Up @@ -115,7 +116,7 @@ fn pool_parser(objects: &mut HashMap<Name, Resource>) -> BinResult<ManifestPool>

pub struct BigFileV1_06_63_02PC;

impl BigFileRead for BigFileV1_06_63_02PC {
impl BigFileIo for BigFileV1_06_63_02PC {
fn read<R: Read + Seek>(
reader: &mut R,
version: Version,
Expand Down Expand Up @@ -153,9 +154,7 @@ impl BigFileRead for BigFileV1_06_63_02PC {
objects,
})
}
}

impl BigFileWrite for BigFileV1_06_63_02PC {
fn write<W: Write + Seek>(bigfile: &BigFile, writer: &mut W) -> BffResult<()> {
let endian: Endian = bigfile.manifest.platform.into();

Expand Down Expand Up @@ -500,4 +499,8 @@ impl BigFileWrite for BigFileV1_06_63_02PC {
writer.seek(SeekFrom::Start(end))?;
Ok(())
}

fn name_type(_version: Version, _platform: Platform) -> NameType {
Asobo32
}
}
13 changes: 8 additions & 5 deletions bff/src/bigfile/v1_08_40_02_pc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ use crate::bigfile::v1_06_63_02_pc::header::BlockDescription;
use crate::bigfile::BigFile;
use crate::helpers::{calculated_padded, write_align_to};
use crate::lz::compress_data_with_header_writer_internal;
use crate::names::Name;
use crate::names::NameType::Asobo32;
use crate::names::{Name, NameType};
use crate::platforms::Platform;
use crate::traits::{BigFileRead, BigFileWrite};
use crate::traits::BigFileIo;
use crate::versions::{Version, VersionTriple};
use crate::{BffResult, Endian};

Expand Down Expand Up @@ -64,7 +65,7 @@ fn blocks_parser(

pub struct BigFileV1_08_40_02PC;

impl BigFileRead for BigFileV1_08_40_02PC {
impl BigFileIo for BigFileV1_08_40_02PC {
fn read<R: Read + Seek>(
reader: &mut R,
version: Version,
Expand Down Expand Up @@ -95,9 +96,7 @@ impl BigFileRead for BigFileV1_08_40_02PC {
objects,
})
}
}

impl BigFileWrite for BigFileV1_08_40_02PC {
fn write<W: Write + Seek>(bigfile: &BigFile, writer: &mut W) -> BffResult<()> {
let endian: Endian = bigfile.manifest.platform.into();

Expand Down Expand Up @@ -265,4 +264,8 @@ impl BigFileWrite for BigFileV1_08_40_02PC {
writer.seek(SeekFrom::Start(end))?;
Ok(())
}

fn name_type(_version: Version, _platform: Platform) -> NameType {
Asobo32
}
}
13 changes: 8 additions & 5 deletions bff/src/bigfile/v1_22_pc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use crate::bigfile::resource::ResourceData::Data;
use crate::bigfile::BigFile;
use crate::dynarray::DynArray;
use crate::helpers::write_align_to;
use crate::names::Name;
use crate::names::NameType::Kalisto32;
use crate::names::{Name, NameType};
use crate::platforms::Platform;
use crate::traits::{BigFileRead, BigFileWrite};
use crate::traits::BigFileIo;
use crate::versions::{Version, VersionTriple};
use crate::BffResult;

Expand Down Expand Up @@ -141,7 +142,7 @@ impl<const HAS_VERSION_TRIPLE: bool> From<BigFileV1_22PC<HAS_VERSION_TRIPLE>> fo
}
}

impl<const HAS_VERSION_TRIPLE: bool> BigFileRead for BigFileV1_22PC<HAS_VERSION_TRIPLE> {
impl<const HAS_VERSION_TRIPLE: bool> BigFileIo for BigFileV1_22PC<HAS_VERSION_TRIPLE> {
fn read<R: Read + Seek>(
reader: &mut R,
version: Version,
Expand All @@ -152,9 +153,7 @@ impl<const HAS_VERSION_TRIPLE: bool> BigFileRead for BigFileV1_22PC<HAS_VERSION_
BigFileV1_22PC::read_options(reader, endian, (version, platform))?;
Ok(bigfile.into())
}
}

impl<const HAS_VERSION_TRIPLE: bool> BigFileWrite for BigFileV1_22PC<HAS_VERSION_TRIPLE> {
fn write<W: Write + Seek>(bigfile: &BigFile, writer: &mut W) -> BffResult<()> {
let endian: Endian = bigfile.manifest.platform.into();

Expand Down Expand Up @@ -214,4 +213,8 @@ impl<const HAS_VERSION_TRIPLE: bool> BigFileWrite for BigFileV1_22PC<HAS_VERSION

Ok(())
}

fn name_type(_version: Version, _platform: Platform) -> NameType {
Kalisto32
}
}
Loading

0 comments on commit d22aceb

Please sign in to comment.