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.

techblox_parsing.rs 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #[cfg(feature = "techblox")]
  2. use libfj::techblox;
  3. #[cfg(feature = "techblox")]
  4. use libfj::techblox::{SerializedEntityDescriptor, Parsable, blocks};
  5. #[cfg(feature = "techblox")]
  6. use std::io::Read;
  7. #[cfg(feature = "techblox")]
  8. use std::fs::File;
  9. #[cfg(feature = "techblox")]
  10. const GAMESAVE_PATH: &str = "tests/GameSave.Techblox";
  11. #[cfg(feature = "techblox")]
  12. const HASHNAMES: &[&str] = &[
  13. "StandardBlockEntityDescriptorV4",
  14. ];
  15. #[cfg(feature = "techblox")]
  16. #[test]
  17. fn techblox_gamesave_parse() -> Result<(), ()> {
  18. let mut f = File::open(GAMESAVE_PATH).map_err(|_| ())?;
  19. let mut buf = Vec::new();
  20. f.read_to_end(&mut buf).map_err(|_| ())?;
  21. let gs = techblox::GameSave::parse(&mut buf.as_slice()).map_err(|_| ())?;
  22. for i in 1..(gs.group_len as usize) {
  23. assert_eq!(gs.group_headers[i-1].hash, gs.group_headers[i].hash);
  24. //println!("#{} count {} vs {}", i, gs.group_headers[i-1].component_count, gs.group_headers[i].component_count);
  25. assert_eq!(gs.group_headers[i-1].component_count, gs.group_headers[i].component_count);
  26. }
  27. for i in 0..(gs.group_len as usize) {
  28. assert_eq!(gs.group_headers[i].component_count, techblox::BlockGroupEntity::serialized_components());
  29. }
  30. for i in 1..(gs.cube_len as usize) {
  31. //assert_eq!(gs.cube_headers[i-1].hash, gs.cube_headers[i].hash);
  32. //println!("#{} count {} vs {}", i, gs.cube_headers[i-1].component_count, gs.cube_headers[i].component_count);
  33. if gs.cube_headers[i-1].hash == gs.cube_headers[i].hash {
  34. assert_eq!(gs.group_headers[i-1].component_count, gs.group_headers[i].component_count);
  35. }
  36. }
  37. for i in 0..(gs.cube_len as usize) {
  38. assert!(gs.cube_headers[i].component_count >= blocks::BlockEntity::serialized_components());
  39. //println!("#{} components: {}", i, gs.cube_headers[i].component_count);
  40. }
  41. println!("{}", gs.to_string());
  42. Ok(())
  43. }
  44. #[allow(dead_code)]
  45. #[cfg(feature = "techblox")]
  46. //#[test]
  47. fn techblox_gamesave_brute_force() -> Result<(), ()> {
  48. // this is slow and not very important, so it's probably better to not test this
  49. let mut f = File::open(GAMESAVE_PATH).map_err(|_| ())?;
  50. let mut buf = Vec::new();
  51. f.read_to_end(&mut buf).map_err(|_| ())?;
  52. let gs = techblox::GameSave::parse(&mut buf.as_slice()).map_err(|_| ())?;
  53. println!("murmurhash3: {} -> {}", gs.group_headers[0].guess_name(), gs.group_headers[0].hash);
  54. Ok(())
  55. }
  56. #[cfg(feature = "techblox")]
  57. #[test]
  58. fn hash_tb_name() {
  59. for name in HASHNAMES {
  60. println!("MurmurHash3: {} -> {}", name, crate::techblox::EntityHeader::from_name(name, 0, 0, 0).hash);
  61. }
  62. }