256 lines
9.4 KiB
C++
256 lines
9.4 KiB
C++
#include "proto/proto.h"
|
|
#include "eterpack/lzo.h"
|
|
#include <iostream>
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <vector>
|
|
#include <filesystem>
|
|
|
|
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 append32(std::vector<uint8_t>& buf, uint32_t val) {
|
|
buf.push_back(val & 0xFF);
|
|
buf.push_back((val >> 8) & 0xFF);
|
|
buf.push_back((val >> 16) & 0xFF);
|
|
buf.push_back((val >> 24) & 0xFF);
|
|
}
|
|
|
|
void test_synthetic_item_proto_156() {
|
|
std::cout << "[Test] Synthetic item_proto (stride 156)..." << std::endl;
|
|
// Create 2 records with stride 156
|
|
uint32_t stride = 156;
|
|
std::vector<uint8_t> raw(stride * 2, 0);
|
|
|
|
// Record 0: vnum 100, range 5, name "TestSword", price 1500
|
|
auto r0 = raw.data();
|
|
*reinterpret_cast<uint32_t*>(r0 + 0) = 100;
|
|
*reinterpret_cast<uint32_t*>(r0 + 4) = 5; // range
|
|
std::memcpy(r0 + 8, "SwordInternal", 13);
|
|
std::memcpy(r0 + 33, "Test Sword", 10);
|
|
r0[58] = 1; // type WEAPON
|
|
r0[59] = 0; // subtype SWORD
|
|
r0[60] = 5; // weight
|
|
r0[61] = 2; // size
|
|
*reinterpret_cast<uint32_t*>(r0 + 78) = 1500; // buyPrice
|
|
*reinterpret_cast<uint32_t*>(r0 + 82) = 750; // sellPrice
|
|
// Limits
|
|
r0[86] = 1; *reinterpret_cast<int32_t*>(r0 + 87) = 30; // Level 30
|
|
// Applies
|
|
r0[96] = 7; *reinterpret_cast<int32_t*>(r0 + 97) = 15; // Attack speed +15
|
|
// Sockets
|
|
*reinterpret_cast<int32_t*>(r0 + 135) = 1;
|
|
*reinterpret_cast<int32_t*>(r0 + 139) = 2;
|
|
*reinterpret_cast<int32_t*>(r0 + 143) = -1;
|
|
|
|
// Record 1: vnum 200, range 0, name "TestArmor"
|
|
auto r1 = raw.data() + stride;
|
|
*reinterpret_cast<uint32_t*>(r1 + 0) = 200;
|
|
*reinterpret_cast<uint32_t*>(r1 + 4) = 0;
|
|
std::memcpy(r1 + 8, "ArmorInternal", 13);
|
|
std::memcpy(r1 + 33, "Test Armor", 10);
|
|
r1[58] = 2; // type ARMOR
|
|
*reinterpret_cast<uint32_t*>(r1 + 78) = 3000;
|
|
|
|
// Compress with MCOZ + TEA
|
|
std::vector<uint8_t> mcoz_blob;
|
|
bool comp = eterpack::LZO::instance().compress_mcoz(raw, mcoz_blob, eterpack::DEFAULT_ITEM_PROTO_KEY);
|
|
TEST_CHECK(comp);
|
|
|
|
// Build container MIPX
|
|
std::vector<uint8_t> file_bytes;
|
|
append32(file_bytes, 0x5850494d); // 'M', 'I', 'P', 'X'
|
|
append32(file_bytes, 1); // version
|
|
append32(file_bytes, stride); // stride 156
|
|
append32(file_bytes, 2); // elements
|
|
append32(file_bytes, static_cast<uint32_t>(mcoz_blob.size()));
|
|
file_bytes.insert(file_bytes.end(), mcoz_blob.begin(), mcoz_blob.end());
|
|
|
|
proto::ItemProto proto;
|
|
TEST_CHECK(proto.load(file_bytes));
|
|
TEST_CHECK(proto.size() == 2);
|
|
|
|
const auto* it100 = proto.find(100);
|
|
TEST_CHECK(it100 != nullptr);
|
|
if (it100) {
|
|
TEST_CHECK(it100->vnum == 100);
|
|
TEST_CHECK(it100->range == 5);
|
|
TEST_CHECK(it100->name == "SwordInternal");
|
|
TEST_CHECK(it100->locale_name == "Test Sword");
|
|
TEST_CHECK(it100->type == 1);
|
|
TEST_CHECK(it100->subtype == 0);
|
|
TEST_CHECK(it100->buy_price == 1500);
|
|
TEST_CHECK(it100->sell_price == 750);
|
|
TEST_CHECK(it100->limits[0].first == 1);
|
|
TEST_CHECK(it100->limits[0].second == 30);
|
|
TEST_CHECK(it100->applies[0].first == 7);
|
|
TEST_CHECK(it100->applies[0].second == 15);
|
|
TEST_CHECK(it100->sockets[0] == 1);
|
|
TEST_CHECK(it100->sockets[1] == 2);
|
|
TEST_CHECK(it100->sockets[2] == -1);
|
|
}
|
|
|
|
// Test range lookup: vnum 102 should resolve to base 100
|
|
const auto* it102 = proto.find(102);
|
|
TEST_CHECK(it102 == it100);
|
|
|
|
const auto* it200 = proto.find(200);
|
|
TEST_CHECK(it200 != nullptr);
|
|
if (it200) {
|
|
TEST_CHECK(it200->vnum == 200);
|
|
TEST_CHECK(it200->name == "ArmorInternal");
|
|
}
|
|
|
|
// 106 is out of range 100..105
|
|
TEST_CHECK(proto.find(106) == nullptr);
|
|
}
|
|
|
|
void test_synthetic_mob_proto_255() {
|
|
std::cout << "[Test] Synthetic mob_proto (stride 255)..." << std::endl;
|
|
uint32_t stride = 255;
|
|
std::vector<uint8_t> raw(stride, 0);
|
|
|
|
auto r = raw.data();
|
|
*reinterpret_cast<uint32_t*>(r + 0) = 101; // Wild Dog
|
|
std::memcpy(r + 4, "WildDogInternal", 15);
|
|
std::memcpy(r + 29, "Wild Dog", 8);
|
|
r[54] = 0; // type MONSTER
|
|
r[55] = 0; // rank PAWN
|
|
r[56] = 0; // battleType MELEE
|
|
r[57] = 1; // level 1
|
|
r[58] = 0; // size
|
|
*reinterpret_cast<uint32_t*>(r + 59) = 10; // goldMin
|
|
*reinterpret_cast<uint32_t*>(r + 63) = 25; // goldMax
|
|
*reinterpret_cast<uint32_t*>(r + 67) = 15; // exp
|
|
*reinterpret_cast<uint32_t*>(r + 71) = 120; // maxHP
|
|
r[75] = 12; // regenCycle
|
|
r[76] = 5; // regenPercent
|
|
*reinterpret_cast<uint16_t*>(r + 77) = 8; // def
|
|
*reinterpret_cast<uint32_t*>(r + 79) = 0; // aiFlag
|
|
*reinterpret_cast<uint32_t*>(r + 83) = 1; // raceFlag ANIMAL
|
|
r[91] = 5; // str
|
|
r[92] = 3; // dex
|
|
*reinterpret_cast<uint32_t*>(r + 95) = 12; // damageMin
|
|
*reinterpret_cast<uint32_t*>(r + 99) = 18; // damageMax
|
|
|
|
std::vector<uint8_t> mcoz_blob;
|
|
bool comp = eterpack::LZO::instance().compress_mcoz(raw, mcoz_blob, eterpack::DEFAULT_MOB_PROTO_KEY);
|
|
TEST_CHECK(comp);
|
|
|
|
std::vector<uint8_t> file_bytes;
|
|
append32(file_bytes, 0x54504d4d); // 'M', 'M', 'P', 'T'
|
|
append32(file_bytes, 1); // elements
|
|
append32(file_bytes, static_cast<uint32_t>(mcoz_blob.size()));
|
|
file_bytes.insert(file_bytes.end(), mcoz_blob.begin(), mcoz_blob.end());
|
|
|
|
proto::MobProto proto;
|
|
TEST_CHECK(proto.load(file_bytes));
|
|
TEST_CHECK(proto.size() == 1);
|
|
|
|
const auto* m101 = proto.find(101);
|
|
TEST_CHECK(m101 != nullptr);
|
|
if (m101) {
|
|
TEST_CHECK(m101->vnum == 101);
|
|
TEST_CHECK(m101->name == "WildDogInternal");
|
|
TEST_CHECK(m101->locale_name == "Wild Dog");
|
|
TEST_CHECK(m101->level == 1);
|
|
TEST_CHECK(m101->gold_min == 10);
|
|
TEST_CHECK(m101->gold_max == 25);
|
|
TEST_CHECK(m101->exp == 15);
|
|
TEST_CHECK(m101->max_hp == 120);
|
|
TEST_CHECK(m101->def == 8);
|
|
TEST_CHECK(m101->damage_min == 12);
|
|
TEST_CHECK(m101->damage_max == 18);
|
|
}
|
|
}
|
|
|
|
void test_real_item_and_mob_proto_if_present() {
|
|
std::cout << "[Test] Real asset files verification..." << std::endl;
|
|
std::vector<std::string> item_candidates = {
|
|
"40250/Server Client TMP4/Client/Eternexus/root/20110915_ymir_item_proto",
|
|
"../../40250/Server Client TMP4/Client/Eternexus/root/20110915_ymir_item_proto",
|
|
"../../../40250/Server Client TMP4/Client/Eternexus/root/20110915_ymir_item_proto",
|
|
"/Users/shen/Work/Code/Metin2/40250/Server Client TMP4/Client/Eternexus/root/20110915_ymir_item_proto"
|
|
};
|
|
|
|
for (const auto& path : item_candidates) {
|
|
if (fs::exists(path)) {
|
|
proto::ItemProto proto;
|
|
bool ok = proto.load_file(path);
|
|
TEST_CHECK(ok);
|
|
if (ok) {
|
|
std::cout << " -> Loaded real item_proto (" << path << ") with "
|
|
<< proto.size() << " records!" << std::endl;
|
|
TEST_CHECK(proto.size() >= 5000);
|
|
|
|
// Check vnum 10 (Sword+0 in Metin2)
|
|
const auto* sword = proto.find(10);
|
|
TEST_CHECK(sword != nullptr);
|
|
if (sword) {
|
|
std::cout << " Vnum 10: name='" << sword->name
|
|
<< "', locale='" << sword->locale_name
|
|
<< "', type=" << (int)sword->type
|
|
<< ", buyPrice=" << sword->buy_price << std::endl;
|
|
TEST_CHECK(sword->vnum == 10);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
std::vector<std::string> mob_candidates = {
|
|
"40250/Server Client TMP4/Client/Eternexus/locale_pl/locale/pl/mob_proto",
|
|
"../../40250/Server Client TMP4/Client/Eternexus/locale_pl/locale/pl/mob_proto",
|
|
"../../../40250/Server Client TMP4/Client/Eternexus/locale_pl/locale/pl/mob_proto",
|
|
"/Users/shen/Work/Code/Metin2/40250/Server Client TMP4/Client/Eternexus/locale_pl/locale/pl/mob_proto"
|
|
};
|
|
|
|
for (const auto& path : mob_candidates) {
|
|
if (fs::exists(path)) {
|
|
proto::MobProto proto;
|
|
bool ok = proto.load_file(path);
|
|
TEST_CHECK(ok);
|
|
if (ok) {
|
|
std::cout << " -> Loaded real mob_proto (" << path << ") with "
|
|
<< proto.size() << " records!" << std::endl;
|
|
TEST_CHECK(proto.size() >= 1000);
|
|
|
|
// Check vnum 101 (Wild Dog)
|
|
const auto* mob = proto.find(101);
|
|
TEST_CHECK(mob != nullptr);
|
|
if (mob) {
|
|
std::cout << " Vnum 101: name='" << mob->name
|
|
<< "', locale='" << mob->locale_name
|
|
<< "', level=" << (int)mob->level
|
|
<< ", maxHP=" << mob->max_hp << std::endl;
|
|
TEST_CHECK(mob->vnum == 101);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
std::cout << "=== Running proto Unit Tests ===" << std::endl;
|
|
test_synthetic_item_proto_156();
|
|
test_synthetic_mob_proto_255();
|
|
test_real_item_and_mob_proto_if_present();
|
|
|
|
if (g_failures == 0) {
|
|
std::cout << "=== ALL proto TESTS PASSED ===" << std::endl;
|
|
return 0;
|
|
} else {
|
|
std::cerr << "=== " << g_failures << " proto TESTS FAILED ===" << std::endl;
|
|
return 1;
|
|
}
|
|
}
|