Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 fix msbt #2

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "acridotheres-3ds"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
description = "Nintendo 3DS-specific file formats for Acridotheres"
license = "MIT"
Expand Down
6 changes: 3 additions & 3 deletions src/msbt/create.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::MsbtFileSource;
use super::{hashtables::padding_len, MsbtFileSource};
use dh::{recommended::*, Readable, Writable};
use std::io::Result;

Expand Down Expand Up @@ -58,7 +58,7 @@ pub fn create<'a>(
let pos = target.pos()?;
let labels_size = pos - table_pos;
target.write_u32le_at(labels_size_pos, labels_size as u32)?;
target.write_bytes(&vec![0xab; 16 - pos as usize % 16])?;
target.write_bytes(&vec![0xab; padding_len(pos) as usize])?;

// attributes section
if has_attr_section {
Expand Down Expand Up @@ -137,6 +137,6 @@ pub fn create<'a>(
let pos = target.pos()?;
let text_size = pos - table_pos;
target.write_u32le_at(text_size_pos, text_size as u32)?;
target.write_bytes(&vec![0xab; 16 - pos as usize % 16])?;
target.write_bytes(&vec![0xab; padding_len(pos) as usize])?;
Ok(())
}
13 changes: 11 additions & 2 deletions src/msbt/hashtables.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use dh::{recommended::*, Readable};
use std::{collections::HashMap, io::Result};

pub fn padding_len(pos: u64) -> u64 {
let res = pos % 16;
if res == 0 {
0
} else {
16 - res
}
}

// https://zeldamods.org/wiki/Msbt#Labels_Section
pub fn parse_label_block(
reader: &mut dyn Readable,
Expand Down Expand Up @@ -33,7 +42,7 @@ pub fn parse_label_block(

reader.to(table_start + size as u64)?;
let pos = reader.pos()?;
reader.jump(16 - pos as i64 % 16)?;
reader.jump(padding_len(pos) as i64)?;

Ok(labels)
}
Expand All @@ -45,7 +54,7 @@ pub fn parse_attr_block(reader: &mut dyn Readable, big_endian: bool) -> Result<(
let start = reader.pos()?;
reader.jump(size as i64)?;
let pos = reader.pos()?;
reader.jump(16 - pos as i64 % 16)?;
reader.jump(padding_len(pos) as i64)?;

Ok((start, size as u64))
}
Expand Down
12 changes: 12 additions & 0 deletions tests/msbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ fn msbt_secretary() {
assert!(string.starts_with("Sure thing!"));
}

#[test]
fn msbt_an_3p_fu() {
let mut reader = dh::file::open_r("tests/samples/AN_3P_Fu.msbt").unwrap();
let metadata = msbt::metadata(&mut reader).unwrap();
assert_eq!(metadata.files.len(), 56);

let mut target = dh::data::write_empty();
msbt::extract(&mut reader, &mut target, &metadata.files[31]).unwrap();
let string = String::from_utf8(dh::data::close(target)).unwrap();
assert!(string.starts_with("<{0003,0016,}>Oh!<{0007,0000,14000000}> That might work!"));
}

#[test]
fn custom_msbt() {
let mut file0 = dh::data::read_ref(b"Hello, world!");
Expand Down
Binary file added tests/samples/AN_3P_Fu.msbt
Binary file not shown.