Get serialization parity for direct abstract blueprint serialization

This commit is contained in:
hal8174 2025-04-05 23:44:44 +02:00
parent a5d3819114
commit 48a648716d
3 changed files with 108 additions and 7 deletions

View file

@ -521,7 +521,7 @@ pub fn generate_factory<L: Layouter, P: Pathfinder + Sync, R: Rng + SeedableRng
Some(true), Some(true),
Some("deconstruction-planner".to_string()), Some("deconstruction-planner".to_string()),
)); ));
blueprints.push(blueprint.clone()); blueprints.push(blueprint);
blocks.push(macro_block.clone()); blocks.push(macro_block.clone());
amount += input_connections[input_index].1.amount; amount += input_connections[input_index].1.amount;
input_index += 1; input_index += 1;
@ -556,7 +556,7 @@ pub fn generate_factory<L: Layouter, P: Pathfinder + Sync, R: Rng + SeedableRng
Some(true), Some(true),
None, None,
)); ));
blueprints.push(blueprint.clone()); blueprints.push(blueprint);
blocks.push(macro_block.clone()); blocks.push(macro_block.clone());
} else { } else {
intermediate_connections_output.insert( intermediate_connections_output.insert(

View file

@ -8,7 +8,10 @@ use factorio_core::{
quaterdirection::QuaterDirection, quaterdirection::QuaterDirection,
visualize::{Color, Visualization}, visualize::{Color, Visualization},
}; };
use std::{collections::HashMap, sync::atomic::AtomicUsize}; use std::{
collections::{HashMap, HashSet},
sync::atomic::AtomicUsize,
};
mod power_connection; mod power_connection;
mod roboports; mod roboports;
@ -563,11 +566,10 @@ pub struct EntityKey(usize);
static ENTITY_COUNTER: AtomicUsize = AtomicUsize::new(0); static ENTITY_COUNTER: AtomicUsize = AtomicUsize::new(0);
#[derive(Clone)]
pub struct Blueprint { pub struct Blueprint {
entities: Vec<(EntityKey, Entity)>, entities: Vec<(EntityKey, Entity)>,
keys: HashMap<EntityKey, usize>, keys: HashMap<EntityKey, usize>,
wires: Vec<(EntityKey, u8, EntityKey, u8)>, wires: HashSet<(EntityKey, u8, EntityKey, u8)>,
} }
impl Blueprint { impl Blueprint {
@ -575,7 +577,7 @@ impl Blueprint {
Self { Self {
entities: Vec::new(), entities: Vec::new(),
keys: HashMap::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) { 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) { pub fn add_blueprint(&mut self, other: Self) {

View file

@ -1,3 +1,5 @@
use std::collections::{HashMap, HashSet};
use factorio_core::prelude::Position; use factorio_core::prelude::Position;
use serde::{ use serde::{
Serialize, Serialize,
@ -30,10 +32,21 @@ impl Serialize for Blueprint {
{ {
let mut blueprint_object = serializer.serialize_map(None)?; 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("label", "test")?;
blueprint_object.serialize_entry("entities", &SerialzeEntitiesWrapper(&self.entities))?; blueprint_object.serialize_entry("entities", &SerialzeEntitiesWrapper(&self.entities))?;
blueprint_object.serialize_entry(
"wires",
&SerializeWires {
keys: &self.keys,
wires: &self.wires,
},
)?;
blueprint_object.end() blueprint_object.end()
} }
} }
@ -63,6 +76,7 @@ impl Serialize for SerializeEntityWrapper<'_> {
S: serde::Serializer, S: serde::Serializer,
{ {
let mut entity_map = serializer.serialize_map(None)?; let mut entity_map = serializer.serialize_map(None)?;
entity_map.serialize_entry("entity_number", &self.0)?; entity_map.serialize_entry("entity_number", &self.0)?;
entity_map.serialize_entry("name", &self.1.get_name())?; 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())?; 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() entity_map.end()
} }
} }
@ -91,3 +166,27 @@ impl Serialize for SerializePosition {
position_object.end() position_object.end()
} }
} }
struct SerializeWires<'a> {
keys: &'a HashMap<EntityKey, usize>,
wires: &'a HashSet<(EntityKey, u8, EntityKey, u8)>,
}
impl Serialize for SerializeWires<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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()
}
}