Skip to content

Commit

Permalink
Merge pull request #13 from ScanMountGoat/master
Browse files Browse the repository at this point in the history
update binrw and support more sections
  • Loading branch information
jam1garner authored Aug 29, 2022
2 parents 2d4294d + b074b02 commit 643859c
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 168 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ required-features = ["cli"]

[dependencies]
modular-bitfield = "0.11"
binrw = { git = "https://github.com/jam1garner/binrw", rev = "e8ae07ec" }
binwrite = "0.2.1"
binrw = "0.9.2"

# serde support
serde = { version = "1", features = ["derive"], optional = true }
Expand Down
83 changes: 55 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
use binrw::{prelude::*, punctuated::Punctuated, NullString, derive_binread};
use binwrite::BinWrite;
use std::path::Path;
use binrw::{binread, prelude::*, punctuated::Punctuated, NullString, VecArgs};
use core::fmt;
use std::path::Path;
use writer::c_bool;

#[cfg(feature = "serde_support")]
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

mod writer;

mod line_flags;
pub use line_flags::LineFlags;

pub fn read_punctuated<T: BinRead<Args = ()>, R: binrw::io::Read + binrw::io::Seek>(
reader: &mut R,
options: &binrw::ReadOptions,
args: VecArgs<()>,
) -> BinResult<Vec<T>> {
Punctuated::<T, u8>::separated(reader, options, args).map(Punctuated::into_values)
}

#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(BinRead, Debug)]
#[br(big, magic = b"\x00\x00\x00\x01\x0D\x01\x4C\x56\x44\x31")]
#[br(big, magic = b"\x00\x00\x00\x01\x0D\x01LVD\x31")]
pub struct LvdFile {
pub collisions: Section<Collision>,
pub spawns: Section<Spawn>,
Expand All @@ -26,18 +34,18 @@ pub struct LvdFile {
pub unk3: UnsupportedSection,
pub fs_area_cam: UnsupportedSection,
pub fs_cam_limit: UnsupportedSection,
pub damage_shapes: UnsupportedSection,
pub damage_shapes: Section<DamageShape>,
pub item_spawners: Section<ItemSpawner>,
pub ptrainer_ranges: Section<PokemonTrainerRange>, // version 13 only
pub ptrainer_platforms: Section<PokemonTrainerPlatform>, // version 13 only
pub general_shapes: UnsupportedSection,
pub general_shapes: Section<GeneralShape>,
pub general_points: Section<Point>,
pub unk4: UnsupportedSection,
pub unk5: UnsupportedSection,
pub unk6: UnsupportedSection,
pub unk7: UnsupportedSection,
pub shrunken_camera_boundary: Section<Bounds>, // version 13 only
pub shrunken_blast_zone: Section<Bounds>, // version 13 only
pub shrunken_blast_zone: Section<Bounds>, // version 13 only
}

#[derive(BinRead, Debug)]
Expand All @@ -60,7 +68,7 @@ pub struct LvdEntry {
pub bone_name: String,
}

#[derive_binread]
#[binread]
#[derive(Debug)]
#[br(magic = b"\x04\x04\x01\x01\x77\x35\xBB\x75\x00\x00\x00\x02")]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
Expand All @@ -69,45 +77,43 @@ pub struct Collision {
pub col_flags: ColFlags,
#[br(temp, pad_before = 1)]
pub vertex_count: u32,
#[br(pad_before = 1, parse_with = Punctuated::<Vector2, u8>::separated, map = Punctuated::into_values, count = vertex_count)]
#[br(pad_before = 1, parse_with = read_punctuated, count = vertex_count)]
pub vertices: Vec<Vector2>,
#[br(temp, pad_before = 1)]
pub normal_count: u32,
#[br(pad_before = 1, parse_with = Punctuated::<Vector2, u8>::separated, map = Punctuated::into_values, count = normal_count)]
#[br(pad_before = 1, parse_with = read_punctuated, count = normal_count)]
pub normals: Vec<Vector2>,
#[br(temp, pad_before = 1)]
pub cliff_count: u32,
#[br(count = cliff_count)]
pub cliffs: Vec<CollisionCliff>,
#[br(temp, pad_before = 1)]
pub line_count: u32,
#[br(pad_before = 1, parse_with = Punctuated::<CollisionMaterial, u8>::separated, map = Punctuated::into_values, count = line_count)]
#[br(pad_before = 1, parse_with = read_punctuated, count = line_count)]
pub materials: Vec<CollisionMaterial>,
#[br(temp, pad_before = 1)]
pub unk_count: u32,
#[br(count = unk_count)]
pub unknowns: Vec<UnknownEntry>,
}

use writer::c_bool as to_c_bool;

#[derive(BinRead, BinWrite, Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct ColFlags {
#[br(map = cbool)]
#[binwrite(preprocessor(to_c_bool))]
#[binwrite(map(c_bool))]
pub flag1: bool,

#[br(map = cbool)]
#[binwrite(preprocessor(to_c_bool))]
#[binwrite(map(c_bool))]
pub rig_col: bool,

#[br(map = cbool)]
#[binwrite(preprocessor(to_c_bool))]
#[binwrite(map(c_bool))]
pub flag3: bool,

#[br(map = cbool)]
#[binwrite(preprocessor(to_c_bool))]
#[binwrite(map(c_bool))]
pub drop_through: bool,
}

Expand Down Expand Up @@ -181,6 +187,27 @@ pub enum GroundCollAttr {
GROUND_COLL_ATTR_JACK_MEMENTOES = 43,
}

#[derive(BinRead, Debug)]
#[br(magic = b"\x01\x04\x01\x01\x77\x35\xBB\x75\x00\x00\x00\x02")]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct DamageShape {
pub entry: LvdEntry,
#[br(pad_before = 1)]
pub unk1: u32,
#[br(pad_after = 1)]
pub unk2: [f32; 8],
}

#[derive(BinRead, Debug)]
#[br(magic = b"\x01\x04\x01\x01\x77\x35\xBB\x75\x00\x00\x00\x02")]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct GeneralShape {
pub entry: LvdEntry,
#[br(pad_before = 1)]
pub unk1: u32,
pub shape: LvdShape,
}

#[derive(BinRead, Debug)]
#[br(magic = b"\x02\x04\x01\x01\x77\x35\xBB\x75\x00\x00\x00\x02")]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -215,7 +242,7 @@ pub struct Bounds {
pub bottom: f32,
}

#[derive_binread]
#[binread]
#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[br(magic = b"\x01\x04\x01\x01\x77\x35\xBB\x75\x00\x00\x00\x02")]
Expand All @@ -226,11 +253,11 @@ pub struct ItemSpawner {
pub unk: u8,
#[br(temp, pad_before = 1)]
pub section_count: u32,
#[br(pad_before = 1, parse_with = Punctuated::<LvdShape, u8>::separated, map = Punctuated::into_values, count = section_count)]
#[br(pad_before = if section_count > 0 { 1 } else {0 }, parse_with = read_punctuated, count = section_count)]
pub sections: Vec<LvdShape>,
}

#[derive_binread]
#[binread]
#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[br(magic = b"\x04\x04\x01\x01\x77\x35\xBB\x75\x00\x00\x00\x02")]
Expand All @@ -242,7 +269,7 @@ pub struct PokemonTrainerRange {
pub boundary_max: Vector3,
#[br(temp, pad_before = 1)]
pub trainer_count: u32,
#[br(pad_before = 1, parse_with = Punctuated::<Vector3, u8>::separated, map = Punctuated::into_values, count = trainer_count)]
#[br(pad_before = if trainer_count > 0 { 1 } else { 0 }, parse_with = read_punctuated, count = trainer_count)]
pub trainers: Vec<Vector3>,
#[br(pad_before = 1, pad_size_to = 0x40, map = NullString::into_string)]
pub platform_name: String,
Expand Down Expand Up @@ -272,7 +299,7 @@ pub struct Point {
pub pos: Vector3,
}

#[derive_binread]
#[binread]
#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub enum LvdShape {
Expand All @@ -282,7 +309,7 @@ pub enum LvdShape {
y: f32,
#[br(temp, pad_before = 8)]
unk: u8,
#[br(temp)]
#[br(temp, pad_before = 1)]
point_count: u32,
},
#[br(magic = b"\x03\0\0\0\x02")]
Expand All @@ -292,7 +319,7 @@ pub enum LvdShape {
radius: f32,
#[br(temp, pad_before = 4)]
unk: u8,
#[br(temp)]
#[br(temp, pad_after = 1)]
point_count: u32,
},
#[br(magic = b"\x03\0\0\0\x03")]
Expand All @@ -303,14 +330,14 @@ pub enum LvdShape {
top: f32,
#[br(temp)]
unk: u8,
#[br(temp)]
#[br(temp, pad_before = 1)]
point_count: u32,
},
#[br(magic = b"\x03\0\0\0\x04")]
Path {
#[br(temp, pad_before = 0x12)]
point_count: u32,
#[br(pad_before = 1, parse_with = Punctuated::<Vector2, u8>::separated, map = Punctuated::into_values, count = point_count)]
#[br(pad_before = 1, parse_with = read_punctuated, count = point_count)]
points: Vec<Vector2>,
},
Invalid {
Expand All @@ -326,7 +353,7 @@ pub struct UnsupportedSection {
pub count: u32,
}

#[derive_binread]
#[binread]
#[derive(Debug)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde_support", serde(transparent))]
Expand Down
90 changes: 49 additions & 41 deletions src/line_flags.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use modular_bitfield::prelude::*;
use binwrite::{BinWrite, WriterOption};
use binrw::BinRead;
use binrw::{BinWrite, WriteOptions};
use modular_bitfield::prelude::*;

use std::io::{Write, self};
use std::io::Write;

#[cfg(feature = "serde_support")]
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

fn from_bytes(mut x: [u8; 4]) -> LineFlags {
x.reverse();
Expand All @@ -15,9 +15,10 @@ fn from_bytes(mut x: [u8; 4]) -> LineFlags {
#[bitfield]
#[derive(BinRead, Debug, Clone, Copy)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde_support", serde(
from = "LineFlagsSerde", into = "LineFlagsSerde"
))]
#[cfg_attr(
feature = "serde_support",
serde(from = "LineFlagsSerde", into = "LineFlagsSerde")
)]
#[br(map = from_bytes)]
pub struct LineFlags {
pub length_zero: bool,
Expand Down Expand Up @@ -55,10 +56,17 @@ pub struct LineFlags {
}

impl BinWrite for LineFlags {
fn write_options<W: Write>(&self, writer: &mut W, _: &WriterOption) -> io::Result<()> {
type Args = ();

fn write_options<W: Write>(
&self,
writer: &mut W,
_: &WriteOptions,
_: Self::Args,
) -> Result<(), binrw::Error> {
let mut bytes = self.into_bytes();
bytes.reverse();
writer.write_all(&bytes)
writer.write_all(&bytes).map_err(Into::into)
}
}

Expand Down Expand Up @@ -103,38 +111,38 @@ struct LineFlagsSerde {
impl From<LineFlagsSerde> for LineFlags {
fn from(lfs: LineFlagsSerde) -> Self {
LineFlags::new()
.with_length_zero(lfs.length_zero)
.with_pacman_final_ignore(lfs.pacman_final_ignore)
.with_fall(lfs.fall)
.with_ignore_ray_check(lfs.ignore_ray_check)
.with_dive(lfs.dive)
.with_unpaintable(lfs.unpaintable)
.with_item(lfs.item)
.with_ignore_fighter_other(lfs.ignore_fighter_other)
.with_right(lfs.right)
.with_left(lfs.left)
.with_upper(lfs.upper)
.with_under(lfs.under)
.with_not_attach(lfs.not_attach)
.with_throughable(lfs.throughable)
.with_hang_l(lfs.hang_l)
.with_hang_r(lfs.hang_r)
.with_ignore_link_from_left(lfs.ignore_link_from_left)
.with_cloud(lfs.cloud)
.with_ignore_link_from_right(lfs.ignore_link_from_right)
.with_not_expand_near_search(lfs.not_expand_near_search)
.with_ignore(lfs.ignore)
.with_breakable(lfs.breakable)
.with_immediate_relanding_ban(lfs.immediate_relanding_ban)
.with_ignore_line_type1(lfs.ignore_line_type1)
.with_pickel_block(lfs.pickel_block)
.with_deceleration(lfs.deceleration)
.with_virtual_hit_line_up(lfs.virtual_hit_line_up)
.with_virtual_hit_line_left(lfs.virtual_hit_line_left)
.with_virtual_hit_line_right(lfs.virtual_hit_line_right)
.with_virtual_hit_line_down(lfs.virtual_hit_line_down)
.with_virtual_wall_hit_line(lfs.virtual_wall_hit_line)
.with_ignore_boss(lfs.ignore_boss)
.with_length_zero(lfs.length_zero)
.with_pacman_final_ignore(lfs.pacman_final_ignore)
.with_fall(lfs.fall)
.with_ignore_ray_check(lfs.ignore_ray_check)
.with_dive(lfs.dive)
.with_unpaintable(lfs.unpaintable)
.with_item(lfs.item)
.with_ignore_fighter_other(lfs.ignore_fighter_other)
.with_right(lfs.right)
.with_left(lfs.left)
.with_upper(lfs.upper)
.with_under(lfs.under)
.with_not_attach(lfs.not_attach)
.with_throughable(lfs.throughable)
.with_hang_l(lfs.hang_l)
.with_hang_r(lfs.hang_r)
.with_ignore_link_from_left(lfs.ignore_link_from_left)
.with_cloud(lfs.cloud)
.with_ignore_link_from_right(lfs.ignore_link_from_right)
.with_not_expand_near_search(lfs.not_expand_near_search)
.with_ignore(lfs.ignore)
.with_breakable(lfs.breakable)
.with_immediate_relanding_ban(lfs.immediate_relanding_ban)
.with_ignore_line_type1(lfs.ignore_line_type1)
.with_pickel_block(lfs.pickel_block)
.with_deceleration(lfs.deceleration)
.with_virtual_hit_line_up(lfs.virtual_hit_line_up)
.with_virtual_hit_line_left(lfs.virtual_hit_line_left)
.with_virtual_hit_line_right(lfs.virtual_hit_line_right)
.with_virtual_hit_line_down(lfs.virtual_hit_line_down)
.with_virtual_wall_hit_line(lfs.virtual_wall_hit_line)
.with_ignore_boss(lfs.ignore_boss)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::path::PathBuf;
use std::fs;
use std::path::PathBuf;

use lvd::LvdFile;
use clap::Parser;
use lvd::LvdFile;

#[derive(Parser)]
struct Args {
Expand All @@ -17,7 +17,7 @@ fn main() {
match out_file.extension().map(|x| x.to_str()).flatten() {
Some("lvd") => out_file.set_extension("yaml"),
Some("yaml") | Some("yml") => out_file.set_extension("lvd"),
_ => true
_ => true,
};
out_file
});
Expand Down
Loading

0 comments on commit 643859c

Please sign in to comment.