mindustry logic execution, map- and schematic- parsing and rendering
Make BlockRegistry more generic
KosmosPrime 2023-01-22
parent 4bf0385 · commit 55772e1
-rw-r--r--src/block/mod.rs65
-rw-r--r--src/data/schematic.rs1
-rw-r--r--src/main.rs1
-rw-r--r--src/registry.rs50
4 files changed, 63 insertions, 54 deletions
diff --git a/src/block/mod.rs b/src/block/mod.rs
index 98c41a3..9e81442 100644
--- a/src/block/mod.rs
+++ b/src/block/mod.rs
@@ -1,13 +1,12 @@
use std::any::Any;
use std::borrow::Cow;
-use std::collections::HashMap;
-use std::collections::hash_map::Entry;
use std::error::Error;
use std::fmt;
use crate::access::BoxAccess;
use crate::data::GridPos;
use crate::data::dynamic::{DynData, DynType};
+use crate::registry::RegistryEntry;
pub mod base;
pub mod content;
@@ -164,11 +163,6 @@ impl Block
Self{name, logic}
}
- pub fn get_name(&self) -> &str
- {
- &self.name
- }
-
pub fn get_size(&self) -> u8
{
self.logic.get_size()
@@ -209,6 +203,14 @@ impl fmt::Debug for Block
}
}
+impl RegistryEntry for Block
+{
+ fn get_name(&self) -> &str
+ {
+ &self.name
+ }
+}
+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Rotation
{
@@ -294,53 +296,8 @@ impl From<Rotation> for u8
}
}
-pub struct BlockRegistry<'l>
-{
- blocks: HashMap<&'l str, &'l Block>,
-}
-
-impl<'l> BlockRegistry<'l>
-{
- pub fn new() -> Self
- {
- Self{blocks: HashMap::new()}
- }
-
- pub fn register(&mut self, block: &'l Block) -> Result<&'l Block, RegisterError<'l>>
- {
- match self.blocks.entry(&block.name)
- {
- Entry::Occupied(e) => Err(RegisterError(e.get())),
- Entry::Vacant(e) => Ok(*e.insert(block)),
- }
- }
-
- pub fn get(&self, name: &str) -> Option<&'l Block>
- {
- self.blocks.get(name).map(|&r| r)
- }
-}
-
-#[derive(Clone, Copy, Debug)]
-pub struct RegisterError<'l>(pub &'l Block);
-
-impl<'l> AsRef<HashMap<&'l str, &'l Block>> for BlockRegistry<'l>
-{
- fn as_ref(&self) -> &HashMap<&'l str, &'l Block>
- {
- &self.blocks
- }
-}
-
-impl<'l> fmt::Display for RegisterError<'l>
-{
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
- {
- write!(f, "block {:?} already exists", self.0.get_name())
- }
-}
-
-impl<'l> Error for RegisterError<'l> {}
+pub type BlockRegistry<'l> = crate::registry::Registry<'l, Block>;
+pub type RegisterError<'l> = crate::registry::RegisterError<'l, Block>;
macro_rules!make_register
{
diff --git a/src/data/schematic.rs b/src/data/schematic.rs
index 928b7e4..ea0d335 100644
--- a/src/data/schematic.rs
+++ b/src/data/schematic.rs
@@ -12,6 +12,7 @@ use crate::block::{self, Block, BlockRegistry, Rotation};
use crate::data::{self, DataRead, DataWrite, GridPos, Serializer};
use crate::data::base64;
use crate::data::dynamic::{self, DynData, DynSerializer};
+use crate::registry::RegistryEntry;
pub const MAX_DIMENSION: u16 = 128;
pub const MAX_BLOCKS: u32 = 128 * 128;
diff --git a/src/main.rs b/src/main.rs
index 0a097b0..17f2d2c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,7 @@ pub mod fluid;
pub mod item;
pub mod logic;
pub mod modifier;
+pub mod registry;
pub mod team;
pub mod unit;
diff --git a/src/registry.rs b/src/registry.rs
new file mode 100644
index 0000000..a22682d
--- /dev/null
+++ b/src/registry.rs
@@ -0,0 +1,50 @@
+use std::any::type_name;
+use std::collections::HashMap;
+use std::collections::hash_map::Entry;
+use std::error::Error;
+use std::fmt;
+
+pub trait RegistryEntry
+{
+ fn get_name(&self) -> &str;
+}
+
+pub struct Registry<'l, E: RegistryEntry + fmt::Debug + 'static>
+{
+ by_name: HashMap<&'l str, &'l E>,
+}
+
+impl<'l, E: RegistryEntry + fmt::Debug + 'static> Registry<'l, E>
+{
+ pub fn new() -> Self
+ {
+ Self{by_name: HashMap::new()}
+ }
+
+ pub fn register(&mut self, val: &'l E) -> Result<&'l E, RegisterError<'l, E>>
+ {
+ match self.by_name.entry(&val.get_name())
+ {
+ Entry::Occupied(e) => Err(RegisterError(e.get())),
+ Entry::Vacant(e) => Ok(e.insert(val)),
+ }
+ }
+
+ pub fn get(&self, name: &str) -> Option<&'l E>
+ {
+ self.by_name.get(name).map(|&r| r)
+ }
+}
+
+#[derive(Clone, Copy, Debug)]
+pub struct RegisterError<'l, E: RegistryEntry + fmt::Debug + 'static>(pub &'l E);
+
+impl<'l, E: RegistryEntry + fmt::Debug + 'static> fmt::Display for RegisterError<'l, E>
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
+ {
+ write!(f, "{} {:?} already exists", type_name::<E>(), self.0.get_name())
+ }
+}
+
+impl<'l, E: RegistryEntry + fmt::Debug + 'static> Error for RegisterError<'l, E> {}