Skip to content

Commit 70ce281

Browse files
Add unit tests for bundle packing
1 parent fcdb4ef commit 70ce281

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/bundle.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn align<T: Into<usize> + From<usize>>(value: T, alignment: T) -> T {
7373
aligned.into()
7474
}
7575

76-
#[derive(Debug)]
76+
#[derive(Debug, PartialEq, Eq)]
7777
struct LevelEnds {
7878
compressed_end: u32,
7979
uncompressed_end: u32,
@@ -85,7 +85,7 @@ const EXPECTED_PLAYER_VERSION: &str = "fusion-2.x.x";
8585
const EXPECTED_ENGINE_VERSION_BASE: &str = "2";
8686
const DEFAULT_ENGINE_VERSION: &str = "2.5.4b5";
8787

88-
#[derive(Debug)]
88+
#[derive(Debug, PartialEq, Eq)]
8989
pub struct AssetBundleHeader {
9090
signature: String,
9191
stream_version: u32,
@@ -335,6 +335,12 @@ impl std::fmt::Display for LevelFile {
335335
}
336336
}
337337
}
338+
impl PartialEq for LevelFile {
339+
fn eq(&self, other: &Self) -> bool {
340+
self.name == other.name && self.data == other.data
341+
}
342+
}
343+
impl Eq for LevelFile {}
338344
impl LevelFile {
339345
fn new(name: String, data: Vec<u8>) -> Self {
340346
Self {
@@ -349,6 +355,19 @@ impl LevelFile {
349355
struct Level {
350356
files: Vec<LevelFile>,
351357
}
358+
impl PartialEq for Level {
359+
fn eq(&self, other: &Self) -> bool {
360+
// Files may be out of order, that's fine
361+
for file in &self.files {
362+
let other_file = other.files.iter().find(|f| f.name == file.name);
363+
if other_file.is_none_or(|f| f.data != file.data) {
364+
return false;
365+
}
366+
}
367+
true
368+
}
369+
}
370+
impl Eq for Level {}
352371
impl Level {
353372
fn read<R: Read + BufRead>(reader: &mut R) -> Result<Self, Error> {
354373
let mut reader = Counter::new(BufReader::new(get_lzma_decoder(reader)?));
@@ -435,7 +454,7 @@ impl Level {
435454
}
436455
}
437456

438-
#[derive(Debug)]
457+
#[derive(Debug, PartialEq, Eq)]
439458
pub struct AssetBundle {
440459
levels: Vec<Level>,
441460
}

src/tests.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ async fn test_generate_manifest() {
8686
#[cfg(feature = "lzma")]
8787
#[tokio::test]
8888
async fn test_extract_bundle() {
89+
use crate::bundle::AssetBundle;
90+
8991
let bundle_path = "example_builds/compressed/good/Map_00_00.unity3d";
9092
let output_dir = TempDir::new();
9193

92-
let (_, bundle) = crate::bundle::AssetBundle::from_file(bundle_path).unwrap();
94+
let (_, bundle) = AssetBundle::from_file(bundle_path).unwrap();
9395
bundle.extract_files(output_dir.path()).unwrap();
9496

9597
let version = Version::from_manifest_file("example_manifest.json").unwrap();
@@ -100,3 +102,31 @@ async fn test_extract_bundle() {
100102
.unwrap();
101103
assert!(corrupted.is_empty());
102104
}
105+
106+
#[cfg(feature = "lzma")]
107+
#[tokio::test]
108+
async fn test_repack_bundle() {
109+
use crate::bundle::AssetBundle;
110+
111+
let bundle_path = "example_builds/compressed/good/Map_00_00.unity3d";
112+
let output_dir = TempDir::new();
113+
114+
let (_, og_bundle) = AssetBundle::from_file(bundle_path).unwrap();
115+
og_bundle.extract_files(output_dir.path()).unwrap();
116+
117+
let repacked_bundle = AssetBundle::from_directory(output_dir.path()).unwrap();
118+
assert!(og_bundle == repacked_bundle);
119+
}
120+
121+
#[cfg(feature = "lzma")]
122+
#[tokio::test]
123+
async fn test_pack_bundle() {
124+
use crate::bundle::AssetBundle;
125+
126+
let bundle_path = "example_builds/compressed/good/Map_00_00.unity3d";
127+
let unpacked_path = "example_builds/uncompressed/good/map_5f00_5f00_2eunity3d";
128+
129+
let (_, og_bundle) = AssetBundle::from_file(bundle_path).unwrap();
130+
let packed_bundle = AssetBundle::from_directory(unpacked_path).unwrap();
131+
assert!(og_bundle == packed_bundle);
132+
}

0 commit comments

Comments
 (0)