An unofficial collection of APIs used in FreeJam games and mods
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.3KB

  1. use std::io::{Read, Write};
  2. use std::any::Any;
  3. /// Standard trait for parsing Techblox game save data.
  4. pub trait Parsable {
  5. /// Process information from raw data.
  6. fn parse(reader: &mut dyn Read) -> std::io::Result<Self> where Self: Sized;
  7. /// Convert struct data back into raw bytes
  8. fn dump(&self, writer: &mut dyn Write) -> std::io::Result<usize>;
  9. }
  10. /// Entity descriptor containing serialized components.
  11. pub trait SerializedEntityDescriptor: Parsable {
  12. /// Count of entity components that this descriptor contains
  13. fn serialized_components() -> u8 where Self: Sized;
  14. /// Components that this entity is comprised of
  15. fn components<'a>(&'a self) -> Vec<&'a dyn SerializedEntityComponent>;
  16. /// Components that this entity is comprised of, for modification
  17. fn components_mut<'a>(&'a mut self) -> Vec<&'a mut dyn SerializedEntityComponent>;
  18. /// Hash of descriptor name
  19. fn hash_name(&self) -> u32;
  20. /// Hash of descriptor name
  21. fn hash(s: &str) -> u32 where Self: Sized {
  22. crate::techblox::hashname(s)
  23. }
  24. }
  25. /// Serializable entity component.
  26. /// Components are the atomic unit of entities.
  27. pub trait SerializedEntityComponent: Parsable + Any {
  28. /// Raw size of struct, in bytes.
  29. fn size() -> usize where Self: Sized {
  30. std::mem::size_of::<Self>()
  31. }
  32. }