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

Feat/param format #48

Merged
merged 20 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 19 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
10 changes: 9 additions & 1 deletion Cargo.lock

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

58 changes: 27 additions & 31 deletions crates/asset-server/src/types/msb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl FastPathAssetLoader for MsbAssetLoader {
let mut name = m
.expect("Could not get name bytes from model entry")
.name
.to_string_lossy();
.to_string();
if name.starts_with('m') {
let msb_name = load_context.asset_path().to_string();
name = format!(
Expand All @@ -109,15 +109,13 @@ impl FastPathAssetLoader for MsbAssetLoader {
.expect("Could not get point set from MSB")
.map(|p| {
let point = p.as_ref().expect("Could not get point entry from MSB");
load_context.labeled_asset_scope(point.name.to_string_lossy(), |_| {
MsbPointAsset {
name: point.name.to_string_lossy(),
position: Vec3::new(
point.position[0].get(),
point.position[1].get(),
point.position[2].get(),
),
}
load_context.labeled_asset_scope(point.name.to_string(), |_| MsbPointAsset {
name: point.name.to_string(),
position: Vec3::new(
point.position[0].get(),
point.position[1].get(),
point.position[2].get(),
),
})
})
.collect(),
Expand All @@ -133,28 +131,26 @@ impl FastPathAssetLoader for MsbAssetLoader {
}

Some(
load_context.labeled_asset_scope(part.name.to_string_lossy(), |_| {
MsbPartAsset {
name: part.name.to_string_lossy(),
transform: MsbAssetLoader::make_msb_transform(
Vec3::new(
part.position[0].get(),
part.position[1].get(),
part.position[2].get(),
),
Some(Vec3::new(
part.rotation[0].get(),
part.rotation[1].get(),
part.rotation[2].get(),
)),
Some(Vec3::new(
part.scale[0].get(),
part.scale[1].get(),
part.scale[2].get(),
)),
load_context.labeled_asset_scope(part.name.to_string(), |_| MsbPartAsset {
name: part.name.to_string(),
transform: MsbAssetLoader::make_msb_transform(
Vec3::new(
part.position[0].get(),
part.position[1].get(),
part.position[2].get(),
),
model: models[part.model_index.get() as usize].clone(),
}
Some(Vec3::new(
part.rotation[0].get(),
part.rotation[1].get(),
part.rotation[2].get(),
)),
Some(Vec3::new(
part.scale[0].get(),
part.scale[1].get(),
part.scale[2].get(),
)),
),
model: models[part.model_index.get() as usize].clone(),
}),
)
})
Expand Down
4 changes: 3 additions & 1 deletion crates/formats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ num-modular = "0.6"
rayon.workspace = true
rsa = "0.9"
thiserror.workspace = true
widestring = "1"
# Currently fetched from a fork until PR removing 'static bound on WStr
# and implementing Cow support is merged
utf16string = { git = "https://github.com/tremwil/utf16string.git", tag = "0.2.1", "version" = "0.2" }
zerocopy = { version = "0.7.32", features = ["derive"] }
zstd = "0.13"

Expand Down
42 changes: 10 additions & 32 deletions crates/formats/src/io_ext/widestring.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
use std::borrow::Cow;

use bytemuck::PodCastError;
use thiserror::Error;
use widestring::{U16Str, U16String};
use utf16string::WStr;
use zerocopy::ByteOrder;

#[derive(Debug, Error)]
pub enum ReadWidestringError {
#[error("Could not find end of string")]
NoEndFound,

#[error("Bytemuck could not cast input slice to a u16 slice")]
Cast,
#[error("String is not valid UTF-16")]
InvalidUTF16,
}

/// Reads a widestring from an input slice.
/// Attempts to read a widestring in-place and copies the string bytes
/// to aligned memory when that fails.
pub fn read_widestring(input: &[u8]) -> Result<Cow<'_, U16Str>, ReadWidestringError> {
/// Reads a null-terminated widestring from an input slice.
/// Fails if the null terminator cannot be found or if the input is invalid UTF-16.
pub fn read_wide_cstring<BO: ByteOrder>(input: &[u8]) -> Result<&WStr<BO>, ReadWidestringError> {
// Find the end of the input string. Unfortunately we need the end to be
// known as U16Str seems to behave inconsistently (sometimes yields garble
// at the end of a read string) when the slice doesn't end at the terminator.
let length = input
.chunks_exact(2)
.position(|bytes| bytes[0] == 0x0 && bytes[1] == 0x0)
.position(|bytes| bytes == [0, 0])
.ok_or(ReadWidestringError::NoEndFound)?;

// Create a view that has a proper end so we don't copy
// the entire input slice if required and so we don't have to deal with
// bytemuck freaking out over the end not being aligned
// Create a view that has a proper end
let string_bytes = &input[..length * 2];

Ok(match bytemuck::try_cast_slice::<u8, u16>(string_bytes) {
Ok(s) => Cow::Borrowed(U16Str::from_slice(s)),
Err(e) => {
// We should probably return the error if it isn't strictly
// about the alignment of the input.
if e != PodCastError::TargetAlignmentGreaterAndInputNotAligned {
return Err(ReadWidestringError::Cast);
}

let aligned_copy = string_bytes
.chunks(2)
.map(|a| u16::from_le_bytes([a[0], a[1]]))
.collect::<Vec<u16>>();

Cow::Owned(U16String::from_vec(aligned_copy))
}
})
WStr::from_utf16(string_bytes).map_err(|_e| ReadWidestringError::InvalidUTF16)
}
1 change: 1 addition & 0 deletions crates/formats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub mod flver;
pub mod io_ext;
pub mod matbin;
pub mod msb;
pub mod param;
pub mod tpf;
26 changes: 13 additions & 13 deletions crates/formats/src/matbin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{borrow::Cow, io};
use std::io;

use byteorder::LE;
use thiserror::Error;
use widestring::U16Str;
use utf16string::WStr;
use zerocopy::{FromBytes, FromZeroes, Ref, F32, U32, U64};

use crate::io_ext::{read_widestring, zerocopy::Padding, ReadWidestringError};
use crate::io_ext::{read_wide_cstring, zerocopy::Padding, ReadWidestringError};

#[derive(Debug, Error)]
pub enum MatbinError {
Expand Down Expand Up @@ -52,32 +52,32 @@ impl<'a> Matbin<'a> {
})
}

pub fn shader_path(&self) -> Result<Cow<'_, U16Str>, MatbinError> {
pub fn shader_path(&self) -> Result<&'_ WStr<LE>, MatbinError> {
let offset = self.header.shader_path_offset.get() as usize;
let bytes = &self.bytes[offset..];

Ok(read_widestring(bytes)?)
Ok(read_wide_cstring(bytes)?)
}

pub fn source_path(&self) -> Result<Cow<'_, U16Str>, MatbinError> {
pub fn source_path(&self) -> Result<&'_ WStr<LE>, MatbinError> {
let offset = self.header.source_path_offset.get() as usize;
let bytes = &self.bytes[offset..];

Ok(read_widestring(bytes)?)
Ok(read_wide_cstring(bytes)?)
}

pub fn samplers(&self) -> impl Iterator<Item = Result<SamplerIterElement, MatbinError>> {
self.samplers.iter().map(|e| {
let name = {
let offset = e.name_offset.get() as usize;
let bytes = &self.bytes[offset..];
read_widestring(bytes)
read_wide_cstring(bytes)
}?;

let path = {
let offset = e.path_offset.get() as usize;
let bytes = &self.bytes[offset..];
read_widestring(bytes)
read_wide_cstring(bytes)
}?;

Ok(SamplerIterElement { name, path })
Expand All @@ -89,7 +89,7 @@ impl<'a> Matbin<'a> {
let name = {
let offset = e.name_offset.get() as usize;
let bytes = &self.bytes[offset..];
read_widestring(bytes)
read_wide_cstring(bytes)
}?;

let value_slice = &self.bytes[e.value_offset.get() as usize..];
Expand All @@ -113,13 +113,13 @@ impl<'a> std::fmt::Debug for Matbin<'a> {
}

pub struct ParameterIterElement<'a> {
pub name: Cow<'a, U16Str>,
pub name: &'a WStr<LE>,
pub value: ParameterValue<'a>,
}

pub struct SamplerIterElement<'a> {
pub name: Cow<'a, U16Str>,
pub path: Cow<'a, U16Str>,
pub name: &'a WStr<LE>,
pub path: &'a WStr<LE>,
}

pub enum ParameterValue<'a> {
Expand Down
4 changes: 2 additions & 2 deletions crates/formats/src/msb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use self::{
event::EVENT_PARAM_ST, model::MODEL_PARAM_ST, parts::PARTS_PARAM_ST, point::POINT_PARAM_ST,
route::ROUTE_PARAM_ST,
};
use crate::io_ext::{read_widestring, ReadWidestringError};
use crate::io_ext::{read_wide_cstring, ReadWidestringError};

#[derive(Debug, Error)]
pub enum MsbError {
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<'a> Msb<'a> {

let name_offset = header.name_offset.get() as usize;

if read_widestring(&self.bytes[name_offset..])?.to_string_lossy() == T::NAME {
if read_wide_cstring::<LE>(&self.bytes[name_offset..])?.to_string() == T::NAME {
return Ok(offsets
.iter()
.map(|o| T::read_entry(&self.bytes[o.get() as usize..])));
Expand Down
10 changes: 4 additions & 6 deletions crates/formats/src/msb/event.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use std::borrow::Cow;

use byteorder::LE;
use widestring::U16Str;
use utf16string::WStr;
use zerocopy::{FromBytes, FromZeroes, F32, I16, I32, U16, U32, U64};

use super::{MsbError, MsbParam};
use crate::io_ext::{read_widestring, zerocopy::Padding};
use crate::io_ext::{read_wide_cstring, zerocopy::Padding};

#[derive(Debug)]
#[allow(unused, non_camel_case_types)]
pub struct EVENT_PARAM_ST<'a> {
name: Cow<'a, U16Str>,
name: &'a WStr<LE>,
event_index: U32<LE>,
event_type: I32<LE>,
id: U32<LE>,
Expand All @@ -23,7 +21,7 @@ impl<'a> MsbParam<'a> for EVENT_PARAM_ST<'a> {
fn read_entry(data: &'a [u8]) -> Result<Self, MsbError> {
let header = Header::ref_from_prefix(data).ok_or(MsbError::UnalignedValue)?;

let name = read_widestring(&data[header.name_offset.get() as usize..])?;
let name = read_wide_cstring(&data[header.name_offset.get() as usize..])?;

let event_data = EventData::from_type_and_slice(
header.event_type.get(),
Expand Down
14 changes: 6 additions & 8 deletions crates/formats/src/msb/model.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use std::borrow::Cow;

use byteorder::LE;
use widestring::U16Str;
use utf16string::WStr;
use zerocopy::{FromBytes, FromZeroes, U32, U64};

use super::{MsbError, MsbParam};
use crate::io_ext::read_widestring;
use crate::io_ext::read_wide_cstring;

#[derive(Debug)]
#[allow(unused, non_camel_case_types)]
pub struct MODEL_PARAM_ST<'a> {
pub name: Cow<'a, U16Str>,
pub name: &'a WStr<LE>,
model_type: U32<LE>,
id: U32<LE>,
sib_path: Cow<'a, U16Str>,
sib_path: &'a WStr<LE>,
instance_count: U32<LE>,
}

Expand All @@ -23,8 +21,8 @@ impl<'a> MsbParam<'a> for MODEL_PARAM_ST<'a> {
fn read_entry(data: &'a [u8]) -> Result<Self, MsbError> {
let header = Header::ref_from_prefix(data).ok_or(MsbError::UnalignedValue)?;

let name = read_widestring(&data[header.name_offset.get() as usize..])?;
let sib_path = read_widestring(&data[header.sib_path_offset.get() as usize..])?;
let name = read_wide_cstring(&data[header.name_offset.get() as usize..])?;
let sib_path = read_wide_cstring(&data[header.sib_path_offset.get() as usize..])?;

Ok(MODEL_PARAM_ST {
name,
Expand Down
14 changes: 6 additions & 8 deletions crates/formats/src/msb/parts.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use std::borrow::Cow;

use byteorder::LE;
use widestring::U16Str;
use utf16string::WStr;
use zerocopy::{FromBytes, FromZeroes, F32, I16, I32, U16, U32, U64};

use super::{MsbError, MsbParam};
use crate::io_ext::{read_widestring, zerocopy::Padding};
use crate::io_ext::{read_wide_cstring, zerocopy::Padding};

#[derive(Debug)]
#[allow(unused, non_camel_case_types)]
pub struct PARTS_PARAM_ST<'a> {
pub name: Cow<'a, U16Str>,
pub name: &'a WStr<LE>,
pub id: U32<LE>,
pub model_index: U32<LE>,
pub sib: Cow<'a, U16Str>,
pub sib: &'a WStr<LE>,
pub position: [F32<LE>; 3],
pub rotation: [F32<LE>; 3],
pub scale: [F32<LE>; 3],
Expand All @@ -32,8 +30,8 @@ impl<'a> MsbParam<'a> for PARTS_PARAM_ST<'a> {
fn read_entry(data: &'a [u8]) -> Result<Self, MsbError> {
let header = Header::ref_from_prefix(data).ok_or(MsbError::UnalignedValue)?;

let name = read_widestring(&data[header.name_offset.get() as usize..])?;
let sib = read_widestring(&data[header.sib_offset.get() as usize..])?;
let name = read_wide_cstring(&data[header.name_offset.get() as usize..])?;
let sib = read_wide_cstring(&data[header.sib_offset.get() as usize..])?;

let masking_behavior = MaskingBehavior::ref_from_prefix(
&data[header.masking_behavior_data_offset.get() as usize..],
Expand Down
Loading
Loading