use super::*;
use crate::{ArchiveBuilder, ListfileOption};
use tempfile::TempDir;
fn create_test_archive(dir: &Path, name: &str, files: &[(&str, &[u8])]) -> PathBuf {
let path = dir.join(name);
let mut builder = ArchiveBuilder::new().listfile_option(ListfileOption::Generate);
for (filename, data) in files {
builder = builder.add_file_data(data.to_vec(), filename);
}
builder.build(&path).unwrap();
path
}
#[test]
fn test_wow_loading_order() {
let temp = TempDir::new().unwrap();
let common_file = "Interface/FrameXML/UIParent.lua";
let locale_file = "Sound/Music/ZoneMusic/Forest.mp3";
let patch_file = "DBFilesClient/Spell.dbc";
let common_mpq = create_test_archive(
temp.path(),
"common.MPQ",
&[
(common_file, b"common version"),
(locale_file, b"common locale"),
(patch_file, b"common patch"),
],
);
let locale_mpq = create_test_archive(
temp.path(),
"locale-enUS.MPQ",
&[
(locale_file, b"locale version"), ],
);
let patch_mpq = create_test_archive(
temp.path(),
"patch.MPQ",
&[
(common_file, b"patch version"), (patch_file, b"patch version"), ],
);
let patch2_mpq = create_test_archive(
temp.path(),
"patch-2.MPQ",
&[
(patch_file, b"patch2 version"), ],
);
let mut chain = PatchChain::new();
chain.add_archive(&common_mpq, 0).unwrap(); chain.add_archive(&locale_mpq, 100).unwrap(); chain.add_archive(&patch_mpq, 1000).unwrap(); chain.add_archive(&patch2_mpq, 1001).unwrap();
assert_eq!(
chain.read_file(common_file).unwrap(),
b"patch version", "Common file should come from patch"
);
assert_eq!(
chain.read_file(locale_file).unwrap(),
b"locale version", "Locale file should come from locale archive"
);
assert_eq!(
chain.read_file(patch_file).unwrap(),
b"patch2 version", "Patched file should come from latest patch"
);
}
#[test]
fn test_locale_patch_priority() {
let temp = TempDir::new().unwrap();
let locale = "enUS";
let test_file = "Interface/AddOns/Blizzard_AuctionUI/Blizzard_AuctionUI.lua";
std::fs::create_dir_all(temp.path().join(locale)).unwrap();
let patch_mpq = create_test_archive(temp.path(), "patch.MPQ", &[(test_file, b"general patch")]);
let locale_patch = create_test_archive(
&temp.path().join(locale),
&format!("patch-{locale}.MPQ"),
&[(test_file, b"locale patch")],
);
let mut chain = PatchChain::new();
chain.add_archive(&patch_mpq, 10).unwrap(); chain.add_archive(&locale_patch, 20).unwrap();
assert_eq!(
chain.read_file(test_file).unwrap(),
b"locale patch",
"Locale patch should override general patch"
);
}
#[test]
fn test_expansion_loading_order() {
let temp = TempDir::new().unwrap();
let test_file = "World/Maps/Northrend/Northrend.wdt";
let common = create_test_archive(temp.path(), "common.MPQ", &[(test_file, b"common")]);
let expansion = create_test_archive(temp.path(), "expansion.MPQ", &[(test_file, b"expansion")]);
let lichking = create_test_archive(temp.path(), "lichking.MPQ", &[(test_file, b"lichking")]);
let mut chain = PatchChain::new();
chain.add_archive(&common, 0).unwrap();
chain.add_archive(&expansion, 1).unwrap();
chain.add_archive(&lichking, 2).unwrap();
assert_eq!(
chain.read_file(test_file).unwrap(),
b"lichking",
"Expansion archives should override in order"
);
}
#[test]
fn test_custom_patch_priority() {
let temp = TempDir::new().unwrap();
let test_file = "Interface/AddOns/MyAddon/Core.lua";
let patch3 = create_test_archive(
temp.path(),
"patch-3.MPQ",
&[(test_file, b"official patch 3")],
);
let patch4 = create_test_archive(
temp.path(),
"patch-4.MPQ",
&[(test_file, b"custom patch 4")],
);
let patch_x = create_test_archive(
temp.path(),
"patch-x.MPQ",
&[(test_file, b"custom patch x")],
);
let mut chain = PatchChain::new();
chain.add_archive(&patch3, 3).unwrap();
chain.add_archive(&patch4, 4).unwrap();
assert_eq!(
chain.read_file(test_file).unwrap(),
b"custom patch 4",
"Higher numbered patches should override"
);
chain.add_archive(&patch_x, 100).unwrap();
assert_eq!(
chain.read_file(test_file).unwrap(),
b"custom patch x",
"Letter patches with high priority should override"
);
}