diff --git a/Cargo.toml b/Cargo.toml index 40d5063..d2ae33c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] \ No newline at end of file +[dependencies] +bitvec = "1.0" \ No newline at end of file diff --git a/src/misc/dense_store.rs b/src/misc/dense_store.rs index 8afbce2..32123ef 100644 --- a/src/misc/dense_store.rs +++ b/src/misc/dense_store.rs @@ -13,6 +13,10 @@ impl DenseStore { Self { items: Vec::new() } } + pub fn len(&self) -> usize { + self.items.len() + } + pub fn get(&self, index: Index) -> Option<&T> { self.items.get(index.into_raw()) } diff --git a/src/sandbox/mod.rs b/src/sandbox/mod.rs index ddfc797..d11c392 100644 --- a/src/sandbox/mod.rs +++ b/src/sandbox/mod.rs @@ -2,6 +2,8 @@ mod serialize; +use bitvec::vec::BitVec; + use crate::{ latest::ModInfo, misc::{ @@ -20,6 +22,7 @@ pub struct Sandbox { components: ObjectStore, wires: ObjectStore, nets: DenseStore, + net_states: BitVec, next_type: u16, component_types: HashMap, @@ -40,6 +43,7 @@ impl Sandbox { components: ObjectStore::new(), wires: ObjectStore::new(), nets: DenseStore::new(), + net_states: BitVec::new(), next_type: component_types.values().copied().max().unwrap_or(0), component_types, @@ -270,6 +274,9 @@ impl Sandbox { } fn make_net(&mut self) -> NetId { + assert_eq!(self.nets.len(), self.net_states.len()); + + self.net_states.push(false); NetId(self.nets.insert(NetInfo { wires: HashSet::new(), pegs: HashSet::new(), @@ -287,6 +294,11 @@ impl Sandbox { for peg_id in &renamed.pegs.clone() { self.get_peg_mut(peg_id).unwrap().net_id = NetId(rename.dest); } + + // Perform the same swap-remove in net_states: + self.net_states.swap_remove(rename.dest.into_raw()); + + assert_eq!(self.nets.len(), self.net_states.len()); Some(net) } else { None diff --git a/src/sandbox/serialize.rs b/src/sandbox/serialize.rs index d879fe3..6be59bc 100644 --- a/src/sandbox/serialize.rs +++ b/src/sandbox/serialize.rs @@ -1,5 +1,7 @@ //! Conversions between the Sandbox editor and Blotter serialization types. +use bitvec::vec::BitVec; + use crate::latest as blotter; use std::collections::{HashMap, HashSet}; @@ -101,6 +103,9 @@ impl From<&super::Sandbox> for blotter::BlotterFile { components.push(ser.serialize_component(component_id, component)) } + let mut states = sandbox.net_states.clone(); + states.set_uninitialized(false); + Self { game_version: GAME_VERSION, save_type: blotter::SaveType::World, @@ -120,7 +125,7 @@ impl From<&super::Sandbox> for blotter::BlotterFile { .map(|(_id, wire)| ser.serialize_wire(wire)) .collect(), circuit_states: blotter::CircuitStates::WorldFormat { - circuit_states: vec![], //TODO + circuit_states: states.into_vec(), }, } } @@ -218,6 +223,19 @@ impl From<&blotter::BlotterFile> for super::Sandbox { file.mods.clone(), ); + match &file.circuit_states { + blotter::CircuitStates::WorldFormat { circuit_states } => { + sandbox.net_states = BitVec::from_slice(circuit_states); + for _ in 0..8 * circuit_states.len() { + sandbox.nets.insert(super::NetInfo { + wires: HashSet::new(), + pegs: HashSet::new(), + }); + } + } + _ => panic!("unsupported circuit state format"), + } + for component in &file.components { let id = sandbox.insert_component(de.deserialize_component(component)); de.register_component(component.address, id); @@ -230,8 +248,6 @@ impl From<&blotter::BlotterFile> for super::Sandbox { .unwrap(); } - //TODO load circuit states - sandbox } }