Skip to content

Commit

Permalink
Implement Skeleton Track Set
Browse files Browse the repository at this point in the history
  • Loading branch information
anarelion committed Oct 31, 2023
1 parent e7c066d commit 17b91cd
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ authors = ["Anarelion <[email protected]>"]
edition = "2021"

[dependencies]
bitbybit = "1.2"
bytes = "1.0"
thiserror = "1.0"
tracing = "0.1"
Expand Down
2 changes: 2 additions & 0 deletions src/wld/fragments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod t54_mesh;
mod t5_texture_list_wrapper;
mod t45_dm_sprite_ref;
mod t17_skeleton_ref;
mod t16_skeleton;

pub use t18_track_def::WldTrackDef;
pub use t19_track::WldTrack;
Expand All @@ -21,3 +22,4 @@ pub use t54_mesh::WldMesh;
pub use t5_texture_list_wrapper::WldTextureRef;
pub use t45_dm_sprite_ref::WldDmSpriteRef;
pub use t17_skeleton_ref::WldSkeletonRef;
pub use t16_skeleton::WldSkeleton;
138 changes: 138 additions & 0 deletions src/wld/fragments/t16_skeleton.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use bitbybit::bitfield;
use bytes::{Buf, Bytes};
use std::fmt::{Debug, Formatter};
use std::rc::Rc;

use crate::utils::count;
use crate::wld::names::WldNames;
use crate::Decoder;

#[derive(Clone, Debug)]
pub struct WldSkeleton {
pub name: Option<String>,
pub flags: WldSkeletonFlags,
pub num_dags: u32,
pub collision_volume_reference: u32,
pub centre_offset: Option<(u32, u32, u32)>,
pub bounding_radius: Option<f32>,
pub dags: Vec<WldSkeletonDag>,
pub num_attached_skins: Option<u32>,
pub dm_sprites: Vec<u32>,
pub link_skin_updates_to_dag_index: Vec<u32>,
}

#[bitfield(u32)]
pub struct WldSkeletonFlags {
#[bit(0, r)]
pub has_center_offset: bool, // 0x01
#[bit(1, r)]
pub has_bounding_radius: bool, // 0x02
#[bit(9, r)]
pub unknown: bool, // 0x200
}

#[derive(Clone, Debug)]
pub struct WldSkeletonDag {
pub name: Option<String>,
pub flags: u32,
pub track_ref: u32,
pub mesh_or_sprite_ref: u32,
pub num_sub_dags: u32,
pub sub_dags: Vec<u32>,
}

impl Decoder for WldSkeleton {
type Settings = Rc<WldNames>;

fn new(input: &mut Bytes, settings: Self::Settings) -> Result<Self, crate::EQFilesError>
where
Self: Sized,
{
let name = settings.get_name(input);
let flags = WldSkeletonFlags::new_with_raw_value(input.get_u32_le());
let num_dags = input.get_u32_le();
let collision_volume_reference = input.get_u32_le();
let centre_offset = if flags.has_center_offset() {
Some((input.get_u32_le(), input.get_u32_le(), input.get_u32_le()))
} else {
None
};
let bounding_radius = if flags.has_bounding_radius() {
Some(input.get_f32_le())
} else {
None
};

let dags = count(input, num_dags as usize, settings, WldSkeletonDag::new)?;

let num_attached_skins = if flags.unknown() {
Some(input.get_u32_le())
} else {
None
};

let mut dm_sprites = Vec::new();
let mut link_skin_updates_to_dag_index = Vec::new();
if num_attached_skins.is_some() {
for _ in 0..num_attached_skins.unwrap() {
dm_sprites.push(input.get_u32_le());
}

for _ in 0..num_attached_skins.unwrap() {
link_skin_updates_to_dag_index.push(input.get_u32_le());
}
}

Ok(Self {
name,
flags,
num_dags,
collision_volume_reference,
centre_offset,
bounding_radius,
dags,
num_attached_skins,
dm_sprites,
link_skin_updates_to_dag_index,
})
}
}

impl Decoder for WldSkeletonDag {
type Settings = Rc<WldNames>;

fn new(input: &mut Bytes, settings: Self::Settings) -> Result<Self, crate::EQFilesError>
where
Self: Sized,
{
let name = settings.get_name(input);
let flags = input.get_u32_le();

let track_ref = input.get_u32_le();
let mesh_or_sprite_ref = input.get_u32_le();
let num_sub_dags = input.get_u32_le();
let mut sub_dags = Vec::with_capacity(num_sub_dags as usize);
for _ in 0..num_sub_dags {
sub_dags.push(input.get_u32_le());
}

Ok(Self {
name,
flags,
track_ref,
mesh_or_sprite_ref,
num_sub_dags,
sub_dags,
})
}
}

impl Debug for WldSkeletonFlags {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WldSkeletonFlags")
.field("has_center_offset", &self.has_center_offset())
.field("has_bounding_radius", &self.has_bounding_radius())
.field("unknown", &self.unknown())
.finish()
}
}
9 changes: 8 additions & 1 deletion src/wld/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct WldFile {
pub t3: HashMap<u32, WldTextureFilename>,
pub t4: HashMap<u32, WldTextureList>,
pub t5: HashMap<u32, WldTextureRef>,
pub t16: HashMap<u32, WldSkeleton>,
pub t17: HashMap<u32, WldSkeletonRef>,
pub t18: HashMap<u32, WldTrackDef>,
pub t19: HashMap<u32, WldTrack>,
Expand Down Expand Up @@ -59,6 +60,7 @@ impl Decoder for WldFile {
let t3 = extract_fragments(&raw_fragments, 3, names.clone());
let t4 = extract_fragments(&raw_fragments, 4, names.clone());
let t5 = extract_fragments(&raw_fragments, 5, names.clone());
let t16 = extract_fragments(&raw_fragments, 16, names.clone());
let t17 = extract_fragments(&raw_fragments, 17, names.clone());
let t18 = extract_fragments(&raw_fragments, 18, names.clone());
let t19 = extract_fragments(&raw_fragments, 19, names.clone());
Expand All @@ -70,7 +72,7 @@ impl Decoder for WldFile {
let remaining: HashSet<u32> = raw_fragments
.iter()
.map(|(_, frag)| frag.0)
.filter(|a| ![3, 4, 5, 17, 18, 19, 20, 45, 48, 49, 54].contains(a))
.filter(|a| ![3, 4, 5, 16, 17, 18, 19, 20, 45, 48, 49, 54].contains(a))
.collect();
if !remaining.is_empty() {
info!("{:?}", remaining);
Expand All @@ -82,6 +84,7 @@ impl Decoder for WldFile {
t3,
t4,
t5,
t16,
t17,
t18,
t19,
Expand Down Expand Up @@ -138,6 +141,10 @@ impl WldFile {
self.t17.get(&index).unwrap().clone()
}

pub fn get_skeleton(&self, index: u32) -> WldSkeleton {
self.t16.get(&index).unwrap().clone()
}

pub fn get_fragment_type(&self, index:u32) -> u32 {
self.raw_fragments.get(&index).unwrap().0
}
Expand Down

0 comments on commit 17b91cd

Please sign in to comment.