gmflib: add serde, expose read error type

main
q3k 2022-04-15 16:49:55 +00:00
parent 73f6caaf48
commit 8318a1d48b
3 changed files with 55 additions and 52 deletions

View File

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
serde = { version = "1.0", features = ["derive"] }
gmfmacros = { path = "../gmfmacros" } gmfmacros = { path = "../gmfmacros" }

View File

@ -3,3 +3,4 @@ mod gma;
pub mod types; pub mod types;
pub use types::*; pub use types::*;
pub use gmi::ReadError as GMIReadError;

View File

@ -1,6 +1,7 @@
use std::{io, string}; use std::{io, string};
use gmfmacros::{GMISerializable, GMASerializable}; use gmfmacros::{GMISerializable, GMASerializable};
use serde::Serialize;
use crate::{ use crate::{
gmi, gma, gmi, gma,
@ -9,7 +10,7 @@ use crate::{
gma::Serializable as GMASerializable, gma::Serializable as GMASerializable,
}; };
#[derive(Debug)] #[derive(Debug,Serialize)]
pub struct GMF { pub struct GMF {
pub version: u32, pub version: u32,
pub model_type: ModelType, pub model_type: ModelType,
@ -36,7 +37,7 @@ impl GMF {
impl gmi::Serializable for GMF { impl gmi::Serializable for GMF {
fn read<R: io::Read>(r: &mut gmi::ReadStream<R>) -> gmi::ReadResult<Self> { fn read<R: io::Read>(r: &mut gmi::ReadStream<R>) -> gmi::ReadResult<Self> {
if r.bytes(3)? != b"GMI".to_vec() { if r.bytes(3)? != b"GMI".to_vec() {
return Err(r.error("invaid magic")); return Err(r.error("invalid magic"));
} }
let version: u32 = r.read("version")?; let version: u32 = r.read("version")?;
if version != 3 { if version != 3 {
@ -72,7 +73,7 @@ impl gma::Serializable for GMF {
} }
} }
#[derive(Debug,GMISerializable)] #[derive(Debug,GMISerializable,Serialize)]
pub struct Color { pub struct Color {
pub r: u8, pub r: u8,
pub g: u8, pub g: u8,
@ -92,7 +93,7 @@ impl gma::Atom for Color {
} }
#[derive(Debug,GMISerializable)] #[derive(Debug,GMISerializable,Serialize)]
pub enum MapKind { pub enum MapKind {
Ambient = 0, Ambient = 0,
Diffuse = 1, Diffuse = 1,
@ -131,26 +132,26 @@ impl gma::Serializable for MapKind {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub enum MapType { pub enum MapType {
Explicit = 0, Explicit = 0,
Spherical = 1, Spherical = 1,
Screen = 4, Screen = 4,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub enum BitmapFilter { pub enum BitmapFilter {
Pyramidal = 0, Pyramidal = 0,
SAT = 1, SAT = 1,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub enum ModelType { pub enum ModelType {
#[gma_value("Basic Model")] #[gma_value("Basic Model")]
BasicModel = 1, BasicModel = 1,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged(1,2)] #[gmi_tagged(1,2)]
pub struct Scene { pub struct Scene {
pub filename: String, pub filename: String,
@ -164,7 +165,7 @@ pub struct Scene {
pub ambient: Color, pub ambient: Color,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged(7, 2)] #[gmi_tagged(7, 2)]
#[gma_name("MATERIAL_LIST")] #[gma_name("MATERIAL_LIST")]
pub struct MaterialList { pub struct MaterialList {
@ -172,7 +173,7 @@ pub struct MaterialList {
pub materials: Vec<Material>, pub materials: Vec<Material>,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged(8, 2)] #[gmi_tagged(8, 2)]
pub struct Material { pub struct Material {
#[gma_name("MATERIAL_REF_NO")] #[gma_name("MATERIAL_REF_NO")]
@ -197,13 +198,13 @@ pub struct Material {
pub sub: Option<MaterialList>, pub sub: Option<MaterialList>,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub enum Shading { pub enum Shading {
Other = 0, Other = 0,
Blinn = 0xc, Blinn = 0xc,
} }
#[derive(Debug, GMISerializable)] #[derive(Debug, GMISerializable,Serialize)]
pub enum Falloff { pub enum Falloff {
In = 0, In = 0,
InTwoSided = 1, InTwoSided = 1,
@ -224,14 +225,14 @@ impl gma::Serializable for Falloff {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub enum XPType { pub enum XPType {
Other = 0, Other = 0,
Filter = 1, Filter = 1,
Additive = 3, Additive = 3,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged(14, 2)] #[gmi_tagged(14, 2)]
#[gma_name("TEXTURE_LIST")] #[gma_name("TEXTURE_LIST")]
pub struct TextureList { pub struct TextureList {
@ -239,7 +240,7 @@ pub struct TextureList {
pub textures: Vec<Texture>, pub textures: Vec<Texture>,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged(15, 4)] #[gmi_tagged(15, 4)]
pub struct Texture { pub struct Texture {
#[gma_name("MAP_NAME")] #[gma_name("MAP_NAME")]
@ -286,7 +287,7 @@ pub struct Texture {
pub sub: Option<TextureList>, pub sub: Option<TextureList>,
} }
#[derive(Debug)] #[derive(Debug,Serialize)]
pub struct ObjectName(String); pub struct ObjectName(String);
impl gmi::Serializable for ObjectName { impl gmi::Serializable for ObjectName {
@ -305,7 +306,7 @@ impl gma::Serializable for ObjectName {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged_nolen(18,2)] #[gmi_tagged_nolen(18,2)]
#[gma_name("OBJECT_LIST")] #[gma_name("OBJECT_LIST")]
pub struct ObjectList { pub struct ObjectList {
@ -313,7 +314,7 @@ pub struct ObjectList {
pub objects: Vec<Object>, pub objects: Vec<Object>,
} }
#[derive(Debug)] #[derive(Debug,Serialize)]
pub enum Object { pub enum Object {
Geometry(GeometryObject), Geometry(GeometryObject),
Camera(CameraObject), Camera(CameraObject),
@ -361,7 +362,7 @@ impl gma::Serializable for Object {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gma_name("GEOMOBJECT")] #[gma_name("GEOMOBJECT")]
pub struct GeometryObject { pub struct GeometryObject {
#[gma_name("NODE_NAME")] #[gma_name("NODE_NAME")]
@ -375,7 +376,7 @@ pub struct GeometryObject {
pub mesh: Mesh, pub mesh: Mesh,
} }
#[derive(Debug,GMISerializable)] #[derive(Debug,GMISerializable,Serialize)]
pub struct CameraObject { pub struct CameraObject {
pub name: String, pub name: String,
pub tm1: TransformMatrix, pub tm1: TransformMatrix,
@ -408,12 +409,12 @@ impl gma::Serializable for CameraObject {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub enum CameraType { pub enum CameraType {
Target = 0, Target = 0,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged(17, 2)] #[gmi_tagged(17, 2)]
#[gma_name("NODE_TM")] #[gma_name("NODE_TM")]
pub struct TransformMatrix { pub struct TransformMatrix {
@ -429,7 +430,7 @@ pub struct TransformMatrix {
pub row3: [f32; 3], pub row3: [f32; 3],
} }
#[derive(Debug, GMISerializable)] #[derive(Debug, GMISerializable,Serialize)]
#[gmi_tagged_nolen(16, 4)] #[gmi_tagged_nolen(16, 4)]
pub struct Mesh { pub struct Mesh {
pub time: u32, pub time: u32,
@ -506,7 +507,7 @@ impl gma::Serializable for Mesh {
macro_rules! specialized_vec { macro_rules! specialized_vec {
($newname:ident, $elemname:ident, $header:expr, $format:expr) => { ($newname:ident, $elemname:ident, $header:expr, $format:expr) => {
#[derive(Debug)] #[derive(Debug,Serialize)]
pub struct $newname(Vec<$elemname>); pub struct $newname(Vec<$elemname>);
impl gmi::Serializable for $newname { impl gmi::Serializable for $newname {
fn read<R: io::Read>(r: &mut gmi::ReadStream<R>) -> gmi::ReadResult<Self> { fn read<R: io::Read>(r: &mut gmi::ReadStream<R>) -> gmi::ReadResult<Self> {
@ -613,14 +614,14 @@ impl MeshNormalList {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub struct Point { pub struct Point {
pub x: f32, pub x: f32,
pub y: f32, pub y: f32,
pub z: f32, pub z: f32,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub struct Face { pub struct Face {
pub a: u32, pub a: u32,
pub b: u32, pub b: u32,
@ -628,14 +629,14 @@ pub struct Face {
pub mtlid: u32, pub mtlid: u32,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub struct TFace { pub struct TFace {
pub a: u32, pub a: u32,
pub b: u32, pub b: u32,
pub c: u32, pub c: u32,
} }
#[derive(Debug,GMISerializable)] #[derive(Debug,GMISerializable,Serialize)]
pub struct FaceNormal { pub struct FaceNormal {
pub face: [f32; 3], pub face: [f32; 3],
pub vertex: [[f32; 3]; 3], pub vertex: [[f32; 3]; 3],
@ -647,7 +648,7 @@ impl gma::Serializable for FaceNormal {
} }
} }
#[derive(Debug, GMISerializable,GMASerializable)] #[derive(Debug, GMISerializable,GMASerializable,Serialize)]
pub struct TextureChannel { pub struct TextureChannel {
pub unk1: u32, pub unk1: u32,
pub tvertex_count: u32, pub tvertex_count: u32,
@ -664,7 +665,7 @@ pub struct TextureChannel {
pub tfaces: MeshTFaceList, pub tfaces: MeshTFaceList,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gma_name("LIGHT")] #[gma_name("LIGHT")]
pub struct LightObject { pub struct LightObject {
#[gma_name("NODE_NAME")] #[gma_name("NODE_NAME")]
@ -689,19 +690,19 @@ pub struct LightObject {
pub use_far_attenuation: FarAttenuation, pub use_far_attenuation: FarAttenuation,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub enum LightType { pub enum LightType {
Omni = 0, Omni = 0,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub enum LightShadows { pub enum LightShadows {
Off = 0, Off = 0,
Mapped = 1, Mapped = 1,
Raytraced = 2, Raytraced = 2,
} }
#[derive(Debug,GMASerializable)] #[derive(Debug,GMASerializable,Serialize)]
pub enum LightSpotShape { pub enum LightSpotShape {
Circle = 0, Circle = 0,
} }
@ -712,7 +713,7 @@ impl gmi::Serializable for LightSpotShape {
} }
} }
#[derive(Debug)] #[derive(Debug,Serialize)]
pub struct FarAttenuation(u32); pub struct FarAttenuation(u32);
impl gmi::Serializable for FarAttenuation { impl gmi::Serializable for FarAttenuation {
@ -729,7 +730,7 @@ impl gma::Serializable for FarAttenuation {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gma_name("GMID_ATTACHMENTPT")] #[gma_name("GMID_ATTACHMENTPT")]
pub struct AttachmentPointObject { pub struct AttachmentPointObject {
#[gma_name("NODE_NAME")] #[gma_name("NODE_NAME")]
@ -739,7 +740,7 @@ pub struct AttachmentPointObject {
pub user_data: String, pub user_data: String,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gma_name("GMID_HAVOK_CONSTRAINTSOLVER")] #[gma_name("GMID_HAVOK_CONSTRAINTSOLVER")]
pub struct ConstraintSolverObject { pub struct ConstraintSolverObject {
#[gma_name("NODE_NAME")] #[gma_name("NODE_NAME")]
@ -751,7 +752,7 @@ pub struct ConstraintSolverObject {
pub constraints: Option<ConstraintList>, pub constraints: Option<ConstraintList>,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged_nolen(44, 2)] #[gmi_tagged_nolen(44, 2)]
#[gma_name("GMID_HAVOK_CONSTRAINT_LIST")] #[gma_name("GMID_HAVOK_CONSTRAINT_LIST")]
pub struct ConstraintList { pub struct ConstraintList {
@ -759,7 +760,7 @@ pub struct ConstraintList {
pub constraints: Vec<Constraint>, pub constraints: Vec<Constraint>,
} }
#[derive(Debug)] #[derive(Debug,Serialize)]
pub enum Constraint { pub enum Constraint {
Hinge(HingeConstraint), Hinge(HingeConstraint),
PointToPoint(PointToPointConstraint), PointToPoint(PointToPointConstraint),
@ -787,7 +788,7 @@ impl gma::Serializable for Constraint {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gma_name("GMID_HAVOK_HINGE_CONSTRAINT")] #[gma_name("GMID_HAVOK_HINGE_CONSTRAINT")]
pub struct HingeConstraint { pub struct HingeConstraint {
#[gma_name("NODE_NAME")] #[gma_name("NODE_NAME")]
@ -811,7 +812,7 @@ pub struct HingeConstraint {
pub angle_limits: [f32; 2], pub angle_limits: [f32; 2],
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gma_name("GMID_HAVOK_POINTTOPOINT")] #[gma_name("GMID_HAVOK_POINTTOPOINT")]
pub struct PointToPointConstraint { pub struct PointToPointConstraint {
#[gma_name("NODE_NAME")] #[gma_name("NODE_NAME")]
@ -824,7 +825,7 @@ pub struct PointToPointConstraint {
pub points: PTPPoints, pub points: PTPPoints,
} }
#[derive(Debug,GMISerializable)] #[derive(Debug,GMISerializable,Serialize)]
pub struct PTPPoints { pub struct PTPPoints {
a: Point, a: Point,
b: Point, b: Point,
@ -839,7 +840,7 @@ impl gma::Serializable for PTPPoints {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gma_name("GMID_HAVOK_SIMOBJECT")] #[gma_name("GMID_HAVOK_SIMOBJECT")]
pub struct SimulationObject { pub struct SimulationObject {
#[gma_name("NODE_NAME")] #[gma_name("NODE_NAME")]
@ -872,7 +873,7 @@ pub struct SimulationObject {
pub collision_pairs: u32, pub collision_pairs: u32,
} }
#[derive(Debug)] #[derive(Debug,Serialize)]
pub struct Gravity(Point); pub struct Gravity(Point);
impl gmi::Serializable for Gravity { impl gmi::Serializable for Gravity {
@ -889,7 +890,7 @@ impl gma::Serializable for Gravity {
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gma_name("GMID_HAVOK_RBCOLLECTION")] #[gma_name("GMID_HAVOK_RBCOLLECTION")]
pub struct RBCollectionObject { pub struct RBCollectionObject {
#[gma_name("NODE_NAME")] #[gma_name("NODE_NAME")]
@ -903,7 +904,7 @@ pub struct RBCollectionObject {
pub disabled_collision_pairs: Option<DisabledCollisionPairList>, pub disabled_collision_pairs: Option<DisabledCollisionPairList>,
} }
#[derive(Debug,GMISerializable)] #[derive(Debug,GMISerializable,Serialize)]
#[gmi_tagged_nolen(33, 2)] #[gmi_tagged_nolen(33, 2)]
pub struct RigidBodyList { pub struct RigidBodyList {
pub rigidbodies: Vec<RigidBody>, pub rigidbodies: Vec<RigidBody>,
@ -922,7 +923,7 @@ impl gma::Serializable for RigidBodyList {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged_nolen(32, 4)] #[gmi_tagged_nolen(32, 4)]
#[gma_name("GMID_HAVOK_RIGIDBODY")] #[gma_name("GMID_HAVOK_RIGIDBODY")]
pub struct RigidBody { pub struct RigidBody {
@ -956,7 +957,7 @@ pub struct RigidBody {
pub children: NestedRigidBodyList, pub children: NestedRigidBodyList,
} }
#[derive(Debug)] #[derive(Debug,Serialize)]
pub struct NestedRigidBodyList(Option<RigidBodyList>); pub struct NestedRigidBodyList(Option<RigidBodyList>);
impl gmi::Serializable for NestedRigidBodyList { impl gmi::Serializable for NestedRigidBodyList {
@ -974,13 +975,13 @@ impl gma::Serializable for NestedRigidBodyList {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
pub enum GeoType { pub enum GeoType {
Standard = 0, Standard = 0,
Plane = 1, Plane = 1,
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gmi_tagged_nolen(51, 2)] #[gmi_tagged_nolen(51, 2)]
#[gma_name("GMID_HAVOK_DIS_COLLISION_PAIRS")] #[gma_name("GMID_HAVOK_DIS_COLLISION_PAIRS")]
pub struct DisabledCollisionPairList { pub struct DisabledCollisionPairList {
@ -988,7 +989,7 @@ pub struct DisabledCollisionPairList {
pub pairs: Vec<DisabledCollisionPair>, pub pairs: Vec<DisabledCollisionPair>,
} }
#[derive(Debug,GMISerializable)] #[derive(Debug,GMISerializable,Serialize)]
pub struct DisabledCollisionPair { pub struct DisabledCollisionPair {
pub a: String, pub a: String,
pub b: String, pub b: String,
@ -1001,7 +1002,7 @@ impl gma::Serializable for DisabledCollisionPair {
} }
} }
#[derive(Debug,GMISerializable,GMASerializable)] #[derive(Debug,GMISerializable,GMASerializable,Serialize)]
#[gma_name("GMID_HAVOK_ANGULAR_DASHPOT")] #[gma_name("GMID_HAVOK_ANGULAR_DASHPOT")]
pub struct AngularDashpotObject { pub struct AngularDashpotObject {
#[gma_name("NODE_NAME")] #[gma_name("NODE_NAME")]
@ -1021,7 +1022,7 @@ pub struct AngularDashpotObject {
pub quaternion: Quaternion, pub quaternion: Quaternion,
} }
#[derive(Debug)] #[derive(Debug,Serialize)]
pub struct Quaternion([f32; 4]); pub struct Quaternion([f32; 4]);
impl gmi::Serializable for Quaternion { impl gmi::Serializable for Quaternion {