mindustry logic execution, map- and schematic- parsing and rendering
Add glue to get content from id
KosmosPrime 2023-01-18
parent 63c7a6a · commit 60babcb
-rw-r--r--src/content.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/content.rs b/src/content.rs
index ac72989..947f2f2 100644
--- a/src/content.rs
+++ b/src/content.rs
@@ -92,6 +92,35 @@ numeric_enum!
}
}
+macro_rules!gen_by_id
+{
+ ($target:path, $id:expr) =>
+ {
+ match <$target>::try_from($id)
+ {
+ Ok(v) => Ok(Box::new(v)),
+ Err(..) => Err($id),
+ }
+ };
+}
+
+impl Type
+{
+ pub fn get(&self, id: u16) -> Result<Box<dyn Content>, u16>
+ {
+ match self
+ {
+ Type::Item => gen_by_id!(crate::item::Type, id),
+ Type::Block => gen_by_id!(crate::block::content::Type, id),
+ Type::Fluid => gen_by_id!(crate::fluid::Type, id),
+ Type::Modifier => gen_by_id!(crate::modifier::Type, id),
+ Type::Unit => gen_by_id!(crate::unit::Type, id),
+ Type::Team => gen_by_id!(crate::team::Team, id),
+ _ => Ok(Box::new(Generic(*self, id))),
+ }
+ }
+}
+
pub trait Content
{
fn get_type(&self) -> Type;
@@ -100,3 +129,23 @@ pub trait Content
fn get_name(&self) -> &str;
}
+
+struct Generic(Type, u16);
+
+impl Content for Generic
+{
+ fn get_type(&self) -> Type
+ {
+ self.0
+ }
+
+ fn get_id(&self) -> u16
+ {
+ self.1
+ }
+
+ fn get_name(&self) -> &str
+ {
+ "<unknown>"
+ }
+}