Skip to content

Commit

Permalink
Remove serialize_bits in favor of experimental SerializeBits
Browse files Browse the repository at this point in the history
  • Loading branch information
widberg committed Sep 26, 2023
1 parent 88b6658 commit a8b16aa
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 127 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

53 changes: 0 additions & 53 deletions bff-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,56 +111,3 @@ fn impl_from_object_to_shadow_class(input: &BffClassMacroInput) -> proc_macro2::
}
}
}

// bilge serialization is incorrect
#[proc_macro_attribute]
pub fn serialize_bits(_attr: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let name_str = LitStr::new(&name.to_string(), name.span());

let data = if let syn::Data::Struct(data) = &input.data {
data
} else {
unimplemented!();
};

let field_count = data.fields.len();

let fields = data.fields.iter().filter(|f| {
if let Some(name) = &f.ident {
name != "reserved" && name != "_reserved" && name != "padding" && name != "_padding"
} else {
true
}
});

let serialize_fields = fields.map(|f| {
let name = &f.ident;
let name_str = if let Some(n) = name {
LitStr::new(&n.to_string(), n.span())
} else {
unimplemented!()
};
quote! {
state.serialize_field(#name_str, &self.#name().value())?;
}
});

quote! {
#input
impl serde::Serialize for #name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;
use serde::Serialize;
let mut state = serializer.serialize_struct(#name_str, #field_count)?;
#(#serialize_fields)*
state.end()
}
}
}
.into()
}
6 changes: 3 additions & 3 deletions bff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ edition = "2021"

[dependencies]
ascii = { version = "1.1.0", features = ["serde"] }
bff-derive = { path = "../bff-derive", version = "*" }
bilge = "0.2.0"
bff-derive = { path = "../bff-derive" }
bilge = { git = "https://github.com/widberg/bilge.git", features = [ "serde" ] }
binrw = "0.12.0"
derive_more = "0.99.17"
indexmap = { version = "2.0.0", features = ["serde"] }
num-traits = "0.2.15"
serde = { version = "1.0.160", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde-big-array = "0.5.1"

[dev-dependencies]
Expand Down
7 changes: 2 additions & 5 deletions bff/src/class/binary/v1_381_67_09_pc.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;

use crate::class::trivial_class::TrivialClass;
use crate::link_header::ResourceObjectLinkHeader;

#[serialize_bits]
#[bitsize(32)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct LookupDescription {
horizon: u12,
altitudes_index: u20,
}

#[serialize_bits]
#[bitsize(8)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct AltitudePack {
odd: u4,
even: u4,
Expand Down
7 changes: 2 additions & 5 deletions bff/src/class/material/v1_381_67_09_pc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;
Expand All @@ -8,9 +7,8 @@ use crate::link_header::ResourceObjectLinkHeader;
use crate::math::{Vec2f, RGB};
use crate::name::Name;

#[serialize_bits]
#[bitsize(32)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct MaterialEnabledBitmaps {
diffuse: u1,
unused0: u1,
Expand All @@ -24,9 +22,8 @@ struct MaterialEnabledBitmaps {
padding: u23,
}

#[serialize_bits]
#[bitsize(32)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct MaterialRdrFlags {
padding0: u5,
transparency: u1,
Expand Down
4 changes: 1 addition & 3 deletions bff/src/class/material_anim/v1_381_67_09_pc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;
Expand All @@ -15,9 +14,8 @@ use crate::keyframer::{
use crate::link_header::ResourceObjectLinkHeader;
use crate::name::Name;

#[serialize_bits]
#[bitsize(8)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct MaterialAnimFlags {
fl_mat_play: u1,
fl_mat_played: u1,
Expand Down
7 changes: 2 additions & 5 deletions bff/src/class/mesh/v1_381_67_09_pc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;
Expand Down Expand Up @@ -133,9 +132,8 @@ enum Vertex {
},
}

#[serialize_bits]
#[bitsize(32)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct D3DFlags {
d3d_pool_default: u1,
d3d_pool_managed: u1,
Expand Down Expand Up @@ -183,9 +181,8 @@ struct Unused1 {
unused6: u32,
}

#[serialize_bits]
#[bitsize(32)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct VertexGroupFlags {
padding: u2,
visible: u1,
Expand Down
26 changes: 2 additions & 24 deletions bff/src/class/mesh_data/v1_381_67_09_pc.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;

use crate::class::trivial_class::TrivialClass;
use crate::link_header::ResourceObjectLinkHeader;

#[serialize_bits]
#[bitsize(32)]
#[derive(BinRead, DebugBits)]
struct ObjectDatasFlags {
fl_objectdatas_hide: u1,
fl_objectdatas_code_control: u1,
fl_objectdatas_cloned: u1,
fl_objectdatas_skinned: u1,
fl_objectdatas_morphed: u1,
fl_objectdatas_vreflect: u1,
fl_objectdatas_hide_shadow: u1,
fl_objectdatas_static_shadow: u1,
fl_objectdatas_vp0_hide: u1,
fl_objectdatas_vp1_hide: u1,
fl_objectdatas_vp2_hide: u1,
fl_objectdatas_vp3_hide: u1,
fl_objectdatas_last: u1,
padding: u19,
}
use crate::link_header::{ ObjectDatasFlagsV1_381_67_09PC, ResourceObjectLinkHeader };

#[derive(BinRead, Debug, Serialize)]
#[br(import(_link_header: &ResourceObjectLinkHeader))]
pub struct MeshDataBodyV1_381_67_09PC {
flags: ObjectDatasFlags,
flags: ObjectDatasFlagsV1_381_67_09PC,
zero0: u32,
zero1: u32,
zero2: u32,
Expand Down
4 changes: 1 addition & 3 deletions bff/src/class/particles/v1_381_67_09_pc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;
Expand All @@ -15,9 +14,8 @@ use crate::link_header::ObjectLinkHeaderV1_381_67_09PC;
use crate::math::{Mat4f, Vec3f};
use crate::name::Name;

#[serialize_bits]
#[bitsize(32)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct ParticlesEmitterFlags {
fl_particles_loop: u1,
fl_particles_lock_h: u1,
Expand Down
4 changes: 1 addition & 3 deletions bff/src/class/sound/v1_291_03_06_pc.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;

use crate::class::trivial_class::TrivialClass;

#[serialize_bits]
#[bitsize(16)]
#[derive(DebugBits, BinRead)]
#[derive(DebugBits, BinRead, SerializeBits)]
struct SoundFlags {
paused: u1,
looping: u1,
Expand Down
4 changes: 1 addition & 3 deletions bff/src/class/sound/v1_381_67_09_pc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;

use crate::class::trivial_class::TrivialClass;
use crate::name::Name;

#[serialize_bits]
#[bitsize(16)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct SoundFlags {
paused: u1,
looping: u1,
Expand Down
4 changes: 1 addition & 3 deletions bff/src/class/surface/v1_06_63_02_pc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;
Expand Down Expand Up @@ -136,9 +135,8 @@ struct SeadIndex {
hit_patch_count: u32,
}

#[serialize_bits]
#[bitsize(16)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct ShouldDrawRelated {
index_in_draw_info_array: u3,
shift_amount_for_bit: u5,
Expand Down
4 changes: 1 addition & 3 deletions bff/src/class/surface/v1_291_03_06_pc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;
Expand Down Expand Up @@ -90,9 +89,8 @@ struct Edge {
t: [u16; 2],
}

#[serialize_bits]
#[bitsize(16)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct ShouldDrawRelated {
index_in_draw_info_array: u3,
shift_amount_for_bit: u5,
Expand Down
4 changes: 1 addition & 3 deletions bff/src/class/surface/v1_381_67_09_pc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;
Expand Down Expand Up @@ -41,9 +40,8 @@ struct Edge {
t: [u16; 2],
}

#[serialize_bits]
#[bitsize(16)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct ShouldDrawBitfield {
index_in_draw_info_array: u3,
shift_amount_for_bit: u5,
Expand Down
7 changes: 2 additions & 5 deletions bff/src/link_header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bff_derive::serialize_bits;
use bilge::prelude::*;
use binrw::BinRead;
use serde::Serialize;
Expand All @@ -12,9 +11,8 @@ pub struct ResourceObjectLinkHeader {
link_name: Name,
}

#[serialize_bits]
#[bitsize(32)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
pub struct ObjectDatasFlagsV1_381_67_09PC {
fl_objectdatas_hide: u1,
fl_objectdatas_code_control: u1,
Expand All @@ -32,9 +30,8 @@ pub struct ObjectDatasFlagsV1_381_67_09PC {
padding: u19,
}

#[serialize_bits]
#[bitsize(32)]
#[derive(BinRead, DebugBits)]
#[derive(BinRead, DebugBits, SerializeBits)]
pub struct ObjectFlagsV1_381_67_09PC {
fl_object_init: u1,
fl_object_max_bsphere: u1,
Expand Down

0 comments on commit a8b16aa

Please sign in to comment.