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.

256 lines
11KB

  1. #[cfg(feature = "techblox")]
  2. use libfj::techblox;
  3. #[cfg(feature = "techblox")]
  4. use libfj::techblox::{SerializedEntityDescriptor, Parsable, blocks, EntityHeader};
  5. #[cfg(feature = "techblox")]
  6. use std::io::{Read, Seek};
  7. #[cfg(feature = "techblox")]
  8. use std::fs::{File, OpenOptions};
  9. #[cfg(feature = "techblox")]
  10. use std::convert::AsRef;
  11. #[cfg(feature = "techblox")]
  12. const GAMESAVE_PATH: &str = "tests/GameSave.Techblox";
  13. #[cfg(feature = "techblox")]
  14. const GAMESAVE_PATH_OUT: &str = "tests/GameSave.out.Techblox";
  15. #[cfg(feature = "techblox")]
  16. const GAMESAVE_PATH_ALL: &str = "tests/All.Techblox";
  17. #[cfg(feature = "techblox")]
  18. const GAMESAVE_PATH_ALL_OUT: &str = "tests/All.out.Techblox";
  19. #[cfg(feature = "techblox")]
  20. const HASHNAMES: &[&str] = &[
  21. "BlockGroupEntityDescriptorV0",
  22. "StandardBlockEntityDescriptorV4",
  23. "BatteryEntityDescriptorV4",
  24. "MotorEntityDescriptorV7",
  25. "LeverEntityDescriptorV7",
  26. "ButtonEntityDescriptorV6",
  27. "JointBlockEntityDescriptorV3",
  28. "ServoEntityDescriptorV7",
  29. "PistonEntityDescriptorV6",
  30. "DampedSpringEntityDescriptorV5",
  31. "DampedAngularSpringEntityDescriptorV4",
  32. "SpawnPointEntityDescriptorV6",
  33. "BuildingSpawnPointEntityDescriptorV4",
  34. "TriggerEntityDescriptorV6",
  35. "PilotSeatEntityDescriptorV4",
  36. "PilotSeatEntityDescriptorV3",
  37. "TextBlockEntityDescriptorV4",
  38. "PassengerSeatEntityDescriptorV4",
  39. "PassengerSeatEntityDescriptorV3",
  40. "LogicBlockEntityDescriptorV1",
  41. "TyreEntityDescriptorV1",
  42. "ObjectIDEntityDescriptorV1",
  43. "MoverEntityDescriptorV1",
  44. "RotatorEntityDescriptorV1",
  45. "DamperEntityDescriptorV1",
  46. "AdvancedDamperEntityDescriptorV1",
  47. "CoMEntityDescriptor",
  48. "FilterBlockEntityDescriptorV1",
  49. "ConstrainerEntityDescriptorV1",
  50. "NumberToTextBlockEntityDescriptorV1",
  51. "CentreHudBlockEntityDescriptorV1",
  52. "ObjectiveHudBlockEntityDescriptorV1",
  53. "GameStatsHudBlockEntityDescriptorV1",
  54. "GameOverHudBlockEntityDescriptorV1",
  55. "TimerBlockEntityDescriptorV1",
  56. "BitBlockEntityDescriptorV2",
  57. "ConstantBlockEntityDescriptor",
  58. "CounterBlockEntityDescriptorV1",
  59. "SimpleSfxEntityDescriptorV1",
  60. "LoopedSfxEntityDescriptorV1",
  61. "MusicBlockEntityDescriptorV1",
  62. "ProjectileBlockEntityDescriptorV1",
  63. "DamagingSurfaceEntityDescriptorV1",
  64. "DestructionManagerEntityDescriptorV1",
  65. "ChunkDestructionBlockEntityDescriptorV1",
  66. "ClusterDestructionBlockEntityDescriptorV1",
  67. "PickupBlockEntityDescriptorV1",
  68. "PointLightEntityDescriptorV1",
  69. "SpotLightEntityDescriptorV1",
  70. "SunLightEntityDescriptorV1",
  71. "AmbientLightEntityDescriptorV1",
  72. "FogEntityDescriptorV1",
  73. "SkyEntityDescriptorV1",
  74. "SynchronizedWireBlockEntityDescriptor",
  75. "WheelRigEntityDescriptor",
  76. "WheelRigSteerableEntityDescriptor",
  77. "EngineBlockEntityDescriptor",
  78. "WireEntityDescriptorMock",
  79. "GlobalWireSettingsEntityDescriptor",
  80. "FlyCamEntityDescriptorV0",
  81. "CharacterCameraEntityDescriptorV1",
  82. ];
  83. #[cfg(feature = "techblox")]
  84. #[test]
  85. fn techblox_gamesave_parse() -> Result<(), ()> {
  86. let mut f = File::open(GAMESAVE_PATH).map_err(|_| ())?;
  87. let mut buf = Vec::new();
  88. f.read_to_end(&mut buf).map_err(|_| ())?;
  89. let gs = techblox::GameSave::parse(&mut buf.as_slice()).map_err(|_| ())?;
  90. for i in 1..(gs.group_len as usize) {
  91. assert_eq!(gs.group_headers[i-1].hash, gs.group_headers[i].hash);
  92. //println!("#{} count {} vs {}", i, gs.group_headers[i-1].component_count, gs.group_headers[i].component_count);
  93. assert_eq!(gs.group_headers[i-1].component_count, gs.group_headers[i].component_count);
  94. }
  95. for i in 0..(gs.group_len as usize) {
  96. assert_eq!(gs.group_headers[i].component_count, techblox::BlockGroupEntity::serialized_components());
  97. assert_eq!(gs.group_headers[i].hash, gs.cube_groups[i].hash_name());
  98. }
  99. for i in 1..(gs.cube_len as usize) {
  100. //assert_eq!(gs.cube_headers[i-1].hash, gs.cube_headers[i].hash);
  101. //println!("#{} count {} vs {}", i, gs.cube_headers[i-1].component_count, gs.cube_headers[i].component_count);
  102. if gs.cube_headers[i-1].hash == gs.cube_headers[i].hash {
  103. assert_eq!(gs.group_headers[i-1].component_count, gs.group_headers[i].component_count);
  104. }
  105. }
  106. for i in 0..(gs.cube_len as usize) {
  107. assert!(gs.cube_headers[i].component_count >= blocks::BlockEntity::serialized_components());
  108. //println!("#{} components: {}", i, gs.cube_headers[i].component_count);
  109. assert_eq!(gs.cube_headers[i].hash, gs.cube_entities[i].hash_name());
  110. }
  111. //println!("Parsed wire settings hash: {} obsolete? {}", gs.wire_settings_header.hash, gs.wire_settings_entity.settings_component.obsolete != 0);
  112. assert_eq!(gs.wire_settings_header.hash, EntityHeader::from_name("GlobalWireSettingsEntityDescriptor", 0, 0, 0).hash);
  113. assert_eq!(gs.wire_settings_header.hash, gs.wire_settings_entity.hash_name());
  114. //println!("Parsed Flycam hash: {}", gs.flycam_header.hash);
  115. assert_eq!(gs.flycam_header.hash, EntityHeader::from_name("FlyCamEntityDescriptorV0", 0, 0, 0).hash);
  116. assert_eq!(gs.flycam_header.hash, gs.flycam_entity.hash_name());
  117. //println!("Parsed Phycam hash: {}", gs.phycam_header.hash);
  118. assert_eq!(gs.phycam_header.hash, EntityHeader::from_name("CharacterCameraEntityDescriptorV1", 0, 0, 0).hash);
  119. assert_eq!(gs.phycam_header.hash, gs.phycam_entity.hash_name());
  120. println!("{}", gs.to_string());
  121. Ok(())
  122. }
  123. #[allow(dead_code)]
  124. #[cfg(feature = "techblox")]
  125. //#[test]
  126. fn techblox_gamesave_brute_force() -> Result<(), ()> {
  127. // this is slow and not very important, so it's probably better to not test this
  128. let mut f = File::open(GAMESAVE_PATH).map_err(|_| ())?;
  129. let mut buf = Vec::new();
  130. f.read_to_end(&mut buf).map_err(|_| ())?;
  131. let gs = techblox::GameSave::parse(&mut buf.as_slice()).map_err(|_| ())?;
  132. println!("murmurhash3: {} -> {}", gs.group_headers[0].guess_name(), gs.group_headers[0].hash);
  133. Ok(())
  134. }
  135. #[cfg(feature = "techblox")]
  136. #[test]
  137. fn hash_tb_name() {
  138. for name in HASHNAMES {
  139. println!("MurmurHash3: {} -> {}", name, crate::techblox::EntityHeader::from_name(name, 0, 0, 0).hash);
  140. }
  141. }
  142. #[cfg(feature = "techblox")]
  143. #[test]
  144. fn techblox_gamesave_perfect_parse() -> Result<(), ()> {
  145. let mut in_file = File::open(GAMESAVE_PATH_ALL).map_err(|_| ())?;
  146. let mut buf = Vec::new();
  147. in_file.read_to_end(&mut buf).map_err(|_| ())?;
  148. let gs = techblox::GameSave::parse(&mut buf.as_slice()).map_err(|_| ())?;
  149. let mut out_file = OpenOptions::new()
  150. .write(true)
  151. .truncate(true)
  152. .create(true)
  153. .open(GAMESAVE_PATH_OUT)
  154. .map_err(|_| ())?;
  155. gs.dump(&mut out_file).map_err(|_| ())?;
  156. assert_eq!(in_file.stream_position().unwrap(), out_file.stream_position().unwrap());
  157. Ok(())
  158. }
  159. #[cfg(feature = "techblox")]
  160. #[test]
  161. fn techblox_gamesave_parse_all() -> Result<(), ()> {
  162. let mut in_file = File::open(GAMESAVE_PATH_ALL).map_err(|_| ())?;
  163. let mut buf = Vec::new();
  164. in_file.read_to_end(&mut buf).map_err(|_| ())?;
  165. let gs = techblox::GameSave::parse(&mut buf.as_slice()).map_err(|_| ())?;
  166. // verify
  167. for i in 1..(gs.group_len as usize) {
  168. assert_eq!(gs.group_headers[i-1].hash, gs.group_headers[i].hash);
  169. //println!("#{} count {} vs {}", i, gs.group_headers[i-1].component_count, gs.group_headers[i].component_count);
  170. assert_eq!(gs.group_headers[i-1].component_count, gs.group_headers[i].component_count);
  171. }
  172. for i in 0..(gs.group_len as usize) {
  173. assert_eq!(gs.group_headers[i].component_count, techblox::BlockGroupEntity::serialized_components());
  174. assert_eq!(gs.group_headers[i].hash, gs.cube_groups[i].hash_name());
  175. /*let pos = format!("({}, {}, {})", gs.cube_groups[i].block_group_transform.block_group_grid_position.x, gs.cube_groups[i].block_group_transform.block_group_grid_position.y, gs.cube_groups[i].block_group_transform.block_group_grid_position.z);
  176. let rot = format!("({}, {}, {}, {})", gs.cube_groups[i].block_group_transform.block_group_grid_rotation.value.x, gs.cube_groups[i].block_group_transform.block_group_grid_rotation.value.y, gs.cube_groups[i].block_group_transform.block_group_grid_rotation.value.z,
  177. gs.cube_groups[i].block_group_transform.block_group_grid_rotation.value.w);
  178. println!("block id: {}, position: {}, rotation: {}", gs.cube_groups[i].saved_block_group_id.saved_block_group_id, pos, rot);*/
  179. }
  180. for i in 1..(gs.cube_len as usize) {
  181. //assert_eq!(gs.cube_headers[i-1].hash, gs.cube_headers[i].hash);
  182. //println!("#{} count {} vs {}", i, gs.cube_headers[i-1].component_count, gs.cube_headers[i].component_count);
  183. if gs.cube_headers[i-1].hash == gs.cube_headers[i].hash {
  184. assert_eq!(gs.group_headers[i-1].component_count, gs.group_headers[i].component_count);
  185. }
  186. }
  187. for i in 0..(gs.cube_len as usize) {
  188. assert!(gs.cube_headers[i].component_count >= blocks::BlockEntity::serialized_components());
  189. //println!("#{} components: {}", i, gs.cube_headers[i].component_count);
  190. assert_eq!(gs.cube_headers[i].hash, gs.cube_entities[i].hash_name());
  191. }
  192. //println!("Parsed wire settings hash: {} obsolete? {}", gs.wire_settings_header.hash, gs.wire_settings_entity.settings_component.obsolete != 0);
  193. assert_eq!(gs.wire_settings_header.hash, EntityHeader::from_name("GlobalWireSettingsEntityDescriptor", 0, 0, 0).hash);
  194. assert_eq!(gs.wire_settings_header.hash, gs.wire_settings_entity.hash_name());
  195. //println!("Parsed Flycam hash: {}", gs.flycam_header.hash);
  196. assert_eq!(gs.flycam_header.hash, EntityHeader::from_name("FlyCamEntityDescriptorV0", 0, 0, 0).hash);
  197. assert_eq!(gs.flycam_header.hash, gs.flycam_entity.hash_name());
  198. //println!("Parsed Phycam hash: {}", gs.phycam_header.hash);
  199. assert_eq!(gs.phycam_header.hash, EntityHeader::from_name("CharacterCameraEntityDescriptorV1", 0, 0, 0).hash);
  200. assert_eq!(gs.phycam_header.hash, gs.phycam_entity.hash_name());
  201. // write out
  202. let mut out_file = OpenOptions::new()
  203. .write(true)
  204. .truncate(true)
  205. .create(true)
  206. .open(GAMESAVE_PATH_ALL_OUT)
  207. .map_err(|_| ())?;
  208. gs.dump(&mut out_file).map_err(|_| ())?;
  209. assert_eq!(in_file.stream_position().unwrap(), out_file.stream_position().unwrap());
  210. Ok(())
  211. }
  212. #[cfg(feature = "techblox")]
  213. #[test]
  214. fn techblox_gamesave_block_groups() -> Result<(), ()> {
  215. let mut in_file = File::open(GAMESAVE_PATH_ALL).map_err(|_| ())?;
  216. let mut buf = Vec::new();
  217. in_file.read_to_end(&mut buf).map_err(|_| ())?;
  218. let gs = techblox::GameSave::parse(&mut buf.as_slice()).map_err(|_| ())?;
  219. for block_trait in &gs.cube_entities {
  220. let block: &blocks::BlockEntity = block_trait.as_ref().as_ref();
  221. //println!("Block @ ({}, {}, {})", block.pos_component.position.x, block.pos_component.position.y, block.pos_component.position.z);
  222. assert!(is_in_block_groups(block.group_component.current_block_group, &gs.cube_groups));
  223. }
  224. Ok(())
  225. }
  226. #[cfg(feature = "techblox")]
  227. fn is_in_block_groups(id: i32, block_groups: &Vec<techblox::BlockGroupEntity>) -> bool {
  228. for bg in block_groups {
  229. if bg.saved_block_group_id.saved_block_group_id == id {
  230. return true;
  231. }
  232. }
  233. false
  234. }