196 lines
7.5 KiB
C++
196 lines
7.5 KiB
C++
#include "eterpack/eterpack.h"
|
|
#include <cassert>
|
|
#include <iostream>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <filesystem>
|
|
#include <cstring>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
static int g_failures = 0;
|
|
#define TEST_CHECK(cond) \
|
|
do { \
|
|
if (!(cond)) { \
|
|
std::cerr << "FAIL [" << __FILE__ << ":" << __LINE__ << "]: " #cond << std::endl; \
|
|
++g_failures; \
|
|
} \
|
|
} while(0)
|
|
|
|
void test_tea() {
|
|
std::cout << "[Test] TEA cipher..." << std::endl;
|
|
uint32_t key[4] = { 0x12345678, 0x9abcdef0, 0x0fedcba9, 0x87654321 };
|
|
std::string text = "SpeedTree & Metin2 CrossPlatform VFS TEA Test Data!!";
|
|
std::span<const uint8_t> data(reinterpret_cast<const uint8_t*>(text.data()), text.size());
|
|
|
|
auto encrypted = eterpack::tea_encrypt_bytes(data, key);
|
|
TEST_CHECK(encrypted.size() >= text.size());
|
|
TEST_CHECK(encrypted.size() % 8 == 0);
|
|
|
|
auto decrypted = eterpack::tea_decrypt_bytes(encrypted, key);
|
|
TEST_CHECK(decrypted.size() == encrypted.size());
|
|
std::string recovered(reinterpret_cast<const char*>(decrypted.data()), text.size());
|
|
TEST_CHECK(recovered == text);
|
|
}
|
|
|
|
void test_crc32() {
|
|
std::cout << "[Test] CRC32..." << std::endl;
|
|
// Known IEEE 802.3 CRC32
|
|
const char* str = "123456789";
|
|
uint32_t crc = eterpack::GetCRC32(str, 9);
|
|
TEST_CHECK(crc == 0xCBF43926);
|
|
|
|
// Case-insensitive CRC
|
|
uint32_t crc_lower = eterpack::GetCaseCRC32("d:/ymir work/tree/oak.spt", 24);
|
|
uint32_t crc_upper = eterpack::GetCaseCRC32("D:/YMIR WORK/TREE/OAK.SPT", 24);
|
|
TEST_CHECK(crc_lower == crc_upper);
|
|
}
|
|
|
|
void test_lzo_and_mcoz() {
|
|
std::cout << "[Test] LZO & MCOZ..." << std::endl;
|
|
std::string sample = "Metin2 high performance LZO decompression verification string 1234567890! "
|
|
"SpeedTree shaders and GR2 meshes are packed tightly.";
|
|
for (int i = 0; i < 5; ++i) {
|
|
sample += sample;
|
|
}
|
|
|
|
std::span<const uint8_t> input(reinterpret_cast<const uint8_t*>(sample.data()), sample.size());
|
|
|
|
// 1. Raw LZO
|
|
std::vector<uint8_t> comp;
|
|
TEST_CHECK(eterpack::LZO::instance().raw_compress(input.data(), input.size(), comp));
|
|
TEST_CHECK(comp.size() < input.size());
|
|
|
|
std::vector<uint8_t> decomp(input.size());
|
|
TEST_CHECK(eterpack::LZO::instance().raw_decompress(comp.data(), comp.size(), decomp.data(), decomp.size()));
|
|
TEST_CHECK(std::memcmp(input.data(), decomp.data(), input.size()) == 0);
|
|
|
|
// 2. MCOZ Plain
|
|
std::vector<uint8_t> mcoz_plain;
|
|
TEST_CHECK(eterpack::LZO::instance().compress_mcoz(input, mcoz_plain, nullptr));
|
|
std::vector<uint8_t> mcoz_plain_out;
|
|
TEST_CHECK(eterpack::LZO::instance().decompress_mcoz(mcoz_plain, mcoz_plain_out, nullptr));
|
|
TEST_CHECK(mcoz_plain_out.size() == input.size());
|
|
TEST_CHECK(std::memcmp(input.data(), mcoz_plain_out.data(), input.size()) == 0);
|
|
|
|
// 3. MCOZ Encrypted with default key
|
|
std::vector<uint8_t> mcoz_enc;
|
|
TEST_CHECK(eterpack::LZO::instance().compress_mcoz(input, mcoz_enc, eterpack::DEFAULT_ETERPACK_KEY));
|
|
std::vector<uint8_t> mcoz_enc_out;
|
|
TEST_CHECK(eterpack::LZO::instance().decompress_mcoz(mcoz_enc, mcoz_enc_out, eterpack::DEFAULT_ETERPACK_KEY));
|
|
TEST_CHECK(mcoz_enc_out.size() == input.size());
|
|
TEST_CHECK(std::memcmp(input.data(), mcoz_enc_out.data(), input.size()) == 0);
|
|
}
|
|
|
|
void test_archive_and_vfs() {
|
|
std::cout << "[Test] Archive Creation & VFS..." << std::endl;
|
|
std::string temp_dir = "test_pack_tmp";
|
|
fs::create_directories(temp_dir);
|
|
|
|
std::vector<std::pair<std::string, std::vector<uint8_t>>> files;
|
|
std::string file1_data = "Hello from SpeedTree texture diffuse.dds";
|
|
std::string file2_data = "Gr2 animation motion data frame buffer bytes";
|
|
std::string file3_data = "ItemProto binary table content";
|
|
|
|
files.emplace_back("d:/ymir work/tree/texture.dds",
|
|
std::vector<uint8_t>(file1_data.begin(), file1_data.end()));
|
|
files.emplace_back("d:/ymir work/pc/warrior/run.msa",
|
|
std::vector<uint8_t>(file2_data.begin(), file2_data.end()));
|
|
files.emplace_back("locale/item_proto",
|
|
std::vector<uint8_t>(file3_data.begin(), file3_data.end()));
|
|
|
|
std::string base_path = temp_dir + "/test_pack";
|
|
bool created = eterpack::EterPackArchive::create_archive(
|
|
base_path, files, true, eterpack::DEFAULT_ETERPACK_KEY,
|
|
eterpack::CompressionType::Compress, eterpack::DEFAULT_SECURITY_KEY
|
|
);
|
|
TEST_CHECK(created);
|
|
|
|
// Open and extract single archive
|
|
eterpack::EterPackArchive archive;
|
|
TEST_CHECK(archive.open(base_path));
|
|
TEST_CHECK(archive.get_entry_count() == 3);
|
|
TEST_CHECK(archive.has_file("d:/ymir work/tree/texture.dds"));
|
|
TEST_CHECK(archive.has_file("d:/ymir work/pc/warrior/run.msa"));
|
|
TEST_CHECK(archive.has_file("locale/item_proto"));
|
|
|
|
std::vector<uint8_t> out1;
|
|
TEST_CHECK(archive.extract("d:/ymir work/tree/texture.dds", out1));
|
|
TEST_CHECK(std::string(out1.begin(), out1.end()) == file1_data);
|
|
|
|
std::vector<uint8_t> out2;
|
|
TEST_CHECK(archive.extract("d:/ymir work/pc/warrior/run.msa", out2));
|
|
TEST_CHECK(std::string(out2.begin(), out2.end()) == file2_data);
|
|
|
|
// Mount in VFS
|
|
eterpack::EterPackVFS vfs;
|
|
TEST_CHECK(vfs.mount(base_path));
|
|
TEST_CHECK(vfs.exists("d:/ymir work/tree/texture.dds"));
|
|
TEST_CHECK(vfs.exists("D:/Ymir Work/Tree/Texture.dds")); // Case insensitive check
|
|
std::vector<uint8_t> vfs_out;
|
|
TEST_CHECK(vfs.read_file("d:/ymir work/tree/texture.dds", vfs_out));
|
|
TEST_CHECK(vfs_out == out1);
|
|
|
|
// Clean up
|
|
archive.close();
|
|
fs::remove_all(temp_dir);
|
|
}
|
|
|
|
void test_real_client_pack_if_present() {
|
|
std::cout << "[Test] Real 40250 Client/pack root check..." << std::endl;
|
|
std::vector<std::string> candidates = {
|
|
"../../../metin2-client/Client/pack/root",
|
|
"../../metin2-client/Client/pack/root",
|
|
"metin2-client/Client/pack/root",
|
|
"/Users/shen/Work/Code/Metin2/metin2-client/Client/pack/root"
|
|
};
|
|
|
|
std::string root_path;
|
|
for (const auto& c : candidates) {
|
|
if (fs::exists(c + ".eix") && fs::exists(c + ".epk")) {
|
|
root_path = c;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!root_path.empty()) {
|
|
eterpack::EterPackArchive real_archive;
|
|
bool ok = real_archive.open(root_path);
|
|
TEST_CHECK(ok);
|
|
if (ok) {
|
|
std::cout << " -> Successfully opened real root.eix/.epk with "
|
|
<< real_archive.get_entry_count() << " entries!" << std::endl;
|
|
TEST_CHECK(real_archive.get_entry_count() > 0);
|
|
|
|
// Test extracting an item from root.epk
|
|
std::vector<uint8_t> proto_bytes;
|
|
if (real_archive.has_file("20110915_ymir_item_proto")) {
|
|
bool ext = real_archive.extract("20110915_ymir_item_proto", proto_bytes);
|
|
TEST_CHECK(ext);
|
|
std::cout << " -> Extracted 20110915_ymir_item_proto, size = "
|
|
<< proto_bytes.size() << " bytes" << std::endl;
|
|
TEST_CHECK(proto_bytes.size() > 1000);
|
|
}
|
|
}
|
|
} else {
|
|
std::cout << " -> (Skipping real pack check: path not found)" << std::endl;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
std::cout << "=== Running eterpack Unit Tests ===" << std::endl;
|
|
test_tea();
|
|
test_crc32();
|
|
test_lzo_and_mcoz();
|
|
test_archive_and_vfs();
|
|
test_real_client_pack_if_present();
|
|
|
|
if (g_failures == 0) {
|
|
std::cout << "=== ALL eterpack TESTS PASSED ===" << std::endl;
|
|
return 0;
|
|
} else {
|
|
std::cerr << "=== " << g_failures << " eterpack TESTS FAILED ===" << std::endl;
|
|
return 1;
|
|
}
|
|
}
|