From 48a648716da07c86453bd93571e8a7c12fcfc425 Mon Sep 17 00:00:00 2001 From: hal8174 Date: Sat, 5 Apr 2025 23:44:44 +0200 Subject: [PATCH] Get serialization parity for direct abstract blueprint serialization --- factorio-blueprint-generator/src/factory.rs | 4 +- factorio-blueprint/src/abstraction.rs | 12 +-- factorio-blueprint/src/abstraction/serde.rs | 99 +++++++++++++++++++++ 3 files changed, 108 insertions(+), 7 deletions(-) diff --git a/factorio-blueprint-generator/src/factory.rs b/factorio-blueprint-generator/src/factory.rs index b6e341f..a638c46 100644 --- a/factorio-blueprint-generator/src/factory.rs +++ b/factorio-blueprint-generator/src/factory.rs @@ -521,7 +521,7 @@ pub fn generate_factory, keys: HashMap, - wires: Vec<(EntityKey, u8, EntityKey, u8)>, + wires: HashSet<(EntityKey, u8, EntityKey, u8)>, } impl Blueprint { @@ -575,7 +577,7 @@ impl Blueprint { Self { entities: Vec::new(), keys: HashMap::new(), - wires: Vec::new(), + wires: HashSet::new(), } } @@ -622,7 +624,7 @@ impl Blueprint { } pub fn add_wire(&mut self, a: EntityKey, endpoint_a: u8, b: EntityKey, endpoint_b: u8) { - self.wires.push((a, endpoint_a, b, endpoint_b)); + self.wires.insert((a, endpoint_a, b, endpoint_b)); } pub fn add_blueprint(&mut self, other: Self) { diff --git a/factorio-blueprint/src/abstraction/serde.rs b/factorio-blueprint/src/abstraction/serde.rs index cbb87a2..aa72b2f 100644 --- a/factorio-blueprint/src/abstraction/serde.rs +++ b/factorio-blueprint/src/abstraction/serde.rs @@ -1,3 +1,5 @@ +use std::collections::{HashMap, HashSet}; + use factorio_core::prelude::Position; use serde::{ Serialize, @@ -30,10 +32,21 @@ impl Serialize for Blueprint { { let mut blueprint_object = serializer.serialize_map(None)?; + blueprint_object.serialize_entry("item", "blueprint")?; + blueprint_object.serialize_entry("version", &562949954666669u64)?; + blueprint_object.serialize_entry("label", "test")?; blueprint_object.serialize_entry("entities", &SerialzeEntitiesWrapper(&self.entities))?; + blueprint_object.serialize_entry( + "wires", + &SerializeWires { + keys: &self.keys, + wires: &self.wires, + }, + )?; + blueprint_object.end() } } @@ -63,6 +76,7 @@ impl Serialize for SerializeEntityWrapper<'_> { S: serde::Serializer, { let mut entity_map = serializer.serialize_map(None)?; + entity_map.serialize_entry("entity_number", &self.0)?; entity_map.serialize_entry("name", &self.1.get_name())?; @@ -73,6 +87,67 @@ impl Serialize for SerializeEntityWrapper<'_> { entity_map.serialize_entry("direction", &self.1.direction.get_index())?; } + match &self.1.entity { + super::EntityType::Belt(_beltspeed) => (), + super::EntityType::UndergroundBelt(_beltspeed, underground_type) => { + entity_map.serialize_entry( + "type", + match underground_type { + super::UndergroundType::Input => "input", + super::UndergroundType::Output => "output", + }, + )?; + } + super::EntityType::Splitter { + beltspeed: _, + input_priority_left, + output_priority_left, + filter: _, + } => { + if let Some(left) = input_priority_left { + entity_map.serialize_entry( + "input_priority", + match left { + true => "left", + false => "right", + }, + )?; + } + if let Some(left) = output_priority_left { + entity_map.serialize_entry( + "output_priority", + match left { + true => "left", + false => "right", + }, + )?; + } + } + super::EntityType::ElectricPole(_electric_pole_type) => (), + super::EntityType::Inserter { + inserter_type: _, + override_stack_size, + } => { + if let Some(stack_size) = override_stack_size { + entity_map.serialize_entry("override_stack_size", stack_size)?; + } + } + super::EntityType::Production { + name: _, + recipe, + size: _, + } => { + entity_map.serialize_entry("recipe", recipe)?; + } + super::EntityType::Rail { rail_type: _ } => (), + super::EntityType::Roboport => (), + super::EntityType::Unknown { + name: _, + size: _, + misc: _, + } => (), + } + entity_map.end() } } @@ -91,3 +166,27 @@ impl Serialize for SerializePosition { position_object.end() } } + +struct SerializeWires<'a> { + keys: &'a HashMap, + wires: &'a HashSet<(EntityKey, u8, EntityKey, u8)>, +} +impl Serialize for SerializeWires<'_> { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let mut wires_sequence = serializer.serialize_seq(Some(self.wires.len()))?; + + for (a, endpoint_a, b, endpoint_b) in self.wires { + wires_sequence.serialize_element(&( + self.keys[a] + 1, + endpoint_a, + self.keys[b] + 1, + endpoint_b, + ))?; + } + + wires_sequence.end() + } +}