Get serialization parity for direct abstract blueprint serialization
This commit is contained in:
parent
a5d3819114
commit
48a648716d
3 changed files with 108 additions and 7 deletions
|
|
@ -8,7 +8,10 @@ use factorio_core::{
|
|||
quaterdirection::QuaterDirection,
|
||||
visualize::{Color, Visualization},
|
||||
};
|
||||
use std::{collections::HashMap, sync::atomic::AtomicUsize};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
sync::atomic::AtomicUsize,
|
||||
};
|
||||
|
||||
mod power_connection;
|
||||
mod roboports;
|
||||
|
|
@ -563,11 +566,10 @@ pub struct EntityKey(usize);
|
|||
|
||||
static ENTITY_COUNTER: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Blueprint {
|
||||
entities: Vec<(EntityKey, Entity)>,
|
||||
keys: HashMap<EntityKey, usize>,
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -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<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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue