From 67beb6d06263b05d2cff426cb5ed2b8423f2fd95 Mon Sep 17 00:00:00 2001 From: Adam Gausmann Date: Mon, 28 Jun 2021 23:56:19 -0500 Subject: [PATCH] Initial commit --- .gitignore | 2 + Cargo.toml | 9 +++++ README.md | 7 ++++ src/lib.rs | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5626a92 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "blotter" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +declio = "0.2.0" diff --git a/README.md b/README.md new file mode 100644 index 0000000..5eb44a9 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Blotter.rs + +A Rust parser for the Blotter file format. + +Current version: [Blotter v5 Draft 0][spec] + +[spec]: https://gist.github.com/JimmyCushnie/7dae698c92a900f2e530668740c20ca2 diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7435d12 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,114 @@ +use declio::ctx::Len; +use declio::util::{magic_bytes, LittleEndian}; +use declio::{Decode, Encode}; +use std::convert::TryInto; + +const WORLD_SAVE_TYPE: u8 = 1; +const SUBASSEMBLY_SAVE_TYPE: u8 = 2; + +type ComponentId = LittleEndian; +type ComponentAddress = LittleEndian; +type Int = LittleEndian; +type Float = LittleEndian; + +#[derive(Debug, Encode, Decode)] +pub struct BlotterFile { + header: Header, + save_version: u8, + game_version: [Int; 4], + save_type: u8, + components_len: Int, + wires_len: Int, + component_ids_len: Int, + #[declio(ctx = "Len(component_ids_len.0.try_into()?)")] + component_ids: Vec, + #[declio(ctx = "Len(components_len.0.try_into()?)")] + components: Vec, + #[declio(ctx = "Len(wires_len.0.try_into()?)")] + wires: Vec, + #[declio(ctx = "*save_type")] + circuit_states: CircuitStates, + footer: Footer, +} + +magic_bytes! { + #[derive(Debug)] + Header(b"Logic world save"); + + #[derive(Debug)] + Footer(b"redstone sux lol"); +} + +#[derive(Debug, Encode, Decode)] +struct Text { + len: Int, + #[declio(with = "declio::util::utf8", ctx = "Len(len.0.try_into()?)")] + value: String, +} + +#[derive(Debug, Encode, Decode)] +struct ComponentIdMapping { + numeric_id: ComponentId, + text_id: Text, +} + +#[derive(Debug, Encode, Decode)] +struct Input { + exclusive: u8, + circuit_state_id: Int, +} + +#[derive(Debug, Encode, Decode)] +struct Output { + circuit_state_id: Int, +} + +#[derive(Debug, Encode, Decode)] +struct Component { + address: ComponentAddress, + parent: ComponentAddress, + type_id: ComponentId, + position: [Float; 3], + rotation: [Float; 4], + inputs_len: u8, + #[declio(ctx = "Len((*inputs_len).try_into()?)")] + inputs: Vec, + outputs_len: u8, + #[declio(ctx = "Len((*outputs_len).try_into()?)")] + outputs: Vec, + custom_data_len: Int, + #[declio(ctx = "Len(custom_data_len.0.try_into()?)")] + custom_data: Vec, +} + +#[derive(Debug, Encode, Decode)] +struct PegAddress { + is_input: u8, + component: ComponentAddress, + index: u8, +} + +#[derive(Debug, Encode, Decode)] +struct Wire { + peg_1: PegAddress, + peg_2: PegAddress, + circuit_state_id: Int, + rotation: Float, +} + +#[derive(Debug, Encode, Decode)] +#[declio(ctx = "save_type: u8", id_expr = "save_type")] +enum CircuitStates { + #[declio(id = "WORLD_SAVE_TYPE")] + WorldFormat { + len: Int, + #[declio(ctx = "Len(len.0.try_into()?)")] + circuit_states: Vec, + }, + #[declio(id = "SUBASSEMBLY_SAVE_TYPE")] + SubassemblyFormat { + len: Int, + #[declio(ctx = "Len(len.0.try_into()?)")] + on_states: Vec, + }, +}