From 50dea192071dc3f2f496ef2752ac5d55e945ea47 Mon Sep 17 00:00:00 2001 From: Adam Gausmann Date: Thu, 8 Jul 2021 12:45:27 -0500 Subject: [PATCH] Update to official Blotter v5 format --- README.md | 4 ++-- src/lib.rs | 35 ++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5eb44a9..d757a2e 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,6 @@ A Rust parser for the Blotter file format. -Current version: [Blotter v5 Draft 0][spec] +Current version: [Blotter v5][spec] -[spec]: https://gist.github.com/JimmyCushnie/7dae698c92a900f2e530668740c20ca2 +[spec]: https://gist.github.com/JimmyCushnie/bebea37a21acbb6e669589967f9512a2 diff --git a/src/lib.rs b/src/lib.rs index 1b74776..d4a8f7c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ type ComponentId = LittleEndian; type ComponentAddress = LittleEndian; type Int = LittleEndian; type Float = LittleEndian; +type Version = [Int; 4]; #[derive(Debug, Clone, Copy, PartialEq, Encode, Decode)] #[declio(id_type = "u8")] @@ -21,10 +22,13 @@ enum SaveType { pub struct BlotterFile { header: Header, save_version: u8, - game_version: [Int; 4], + game_version: Version, save_type: SaveType, components_len: Int, wires_len: Int, + mods_len: Int, + #[declio(ctx = "Len(mods_len.0.try_into()?)")] + mods: Vec, component_ids_len: Int, #[declio(ctx = "Len(component_ids_len.0.try_into()?)")] component_ids: Vec, @@ -52,6 +56,12 @@ struct Text { value: String, } +#[derive(Debug, Encode, Decode)] +struct Mod { + mod_id: Text, + version: Version, +} + #[derive(Debug, Encode, Decode)] struct ComponentIdMapping { numeric_id: ComponentId, @@ -60,8 +70,6 @@ struct ComponentIdMapping { #[derive(Debug, Encode, Decode)] struct Input { - #[declio(with = "zero_one")] - exclusive: bool, circuit_state_id: Int, } @@ -77,23 +85,32 @@ struct Component { type_id: ComponentId, position: [Float; 3], rotation: [Float; 4], - inputs_len: u8, - #[declio(ctx = "Len((*inputs_len).try_into()?)")] + inputs_len: Int, + #[declio(ctx = "Len(inputs_len.0.try_into()?)")] inputs: Vec, - outputs_len: u8, - #[declio(ctx = "Len((*outputs_len).try_into()?)")] + outputs_len: Int, + #[declio(ctx = "Len(outputs_len.0.try_into()?)")] outputs: Vec, custom_data_len: Int, - #[declio(ctx = "Len(custom_data_len.0.try_into()?)")] + #[declio(ctx = "Len(len_or_neg1(*custom_data_len)?)")] custom_data: Vec, } +fn len_or_neg1(len: Int) -> Result { + // From the spec: + // > If the component has no custom data, -1 may be written instead of 0 + match len.0 { + -1 => Ok(0), + other => other.try_into(), + } +} + #[derive(Debug, Encode, Decode)] struct PegAddress { #[declio(with = "zero_one")] is_input: bool, component: ComponentAddress, - index: u8, + index: Int, } #[derive(Debug, Encode, Decode)]