Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(proto LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_library(proto STATIC
|
||||
src/item_proto.cpp
|
||||
src/mob_proto.cpp
|
||||
)
|
||||
|
||||
target_include_directories(proto PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
|
||||
target_link_libraries(proto PUBLIC eterpack)
|
||||
|
||||
add_library(proto::proto ALIAS proto)
|
||||
|
||||
option(PROTO_BUILD_TESTS "Build proto SDK tests" ON)
|
||||
if(PROTO_BUILD_TESTS)
|
||||
add_executable(proto_test tests/proto_test.cpp)
|
||||
target_link_libraries(proto_test PRIVATE proto)
|
||||
add_test(NAME proto_test COMMAND proto_test)
|
||||
endif()
|
||||
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <array>
|
||||
#include <utility>
|
||||
#include <span>
|
||||
#include <filesystem>
|
||||
#include "eterpack/types.h"
|
||||
|
||||
namespace proto {
|
||||
|
||||
struct ItemProtoRecord {
|
||||
uint32_t vnum = 0;
|
||||
uint32_t range = 0; // vnum range for grouped items
|
||||
std::string name;
|
||||
std::string locale_name;
|
||||
|
||||
uint8_t type = 0;
|
||||
uint8_t subtype = 0;
|
||||
uint8_t weight = 0;
|
||||
uint8_t size = 1;
|
||||
|
||||
uint32_t anti_flags = 0;
|
||||
uint32_t flags = 0;
|
||||
uint32_t wear_flags = 0;
|
||||
uint32_t immune_flags = 0;
|
||||
|
||||
uint32_t buy_price = 0;
|
||||
uint32_t sell_price = 0;
|
||||
|
||||
std::array<std::pair<uint8_t, int32_t>, 2> limits{};
|
||||
std::array<std::pair<uint8_t, int32_t>, 3> applies{};
|
||||
std::array<int32_t, 6> values{};
|
||||
std::array<int32_t, 8> sockets{};
|
||||
|
||||
uint32_t refined_vnum = 0;
|
||||
uint16_t refine_set = 0;
|
||||
uint8_t alter_to_magic_percent = 0;
|
||||
uint8_t specular = 0;
|
||||
uint8_t gain_socket_percent = 0;
|
||||
};
|
||||
|
||||
class ItemProto {
|
||||
public:
|
||||
ItemProto() = default;
|
||||
|
||||
bool load(std::span<const uint8_t> data, const uint32_t key[4] = eterpack::DEFAULT_ITEM_PROTO_KEY);
|
||||
bool load_file(const std::filesystem::path& filepath, const uint32_t key[4] = eterpack::DEFAULT_ITEM_PROTO_KEY);
|
||||
|
||||
const ItemProtoRecord* find(uint32_t vnum) const;
|
||||
const std::vector<ItemProtoRecord>& get_records() const { return m_records; }
|
||||
size_t size() const { return m_records.size(); }
|
||||
bool empty() const { return m_records.empty(); }
|
||||
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::vector<ItemProtoRecord> m_records;
|
||||
std::unordered_map<uint32_t, size_t> m_vnum_map;
|
||||
std::vector<size_t> m_ranged_indices;
|
||||
};
|
||||
|
||||
} // namespace proto
|
||||
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <array>
|
||||
#include <span>
|
||||
#include <filesystem>
|
||||
#include "eterpack/types.h"
|
||||
|
||||
namespace proto {
|
||||
|
||||
struct MobSkillLevel {
|
||||
uint32_t vnum = 0;
|
||||
uint8_t level = 0;
|
||||
};
|
||||
|
||||
struct MobRecord {
|
||||
uint32_t vnum = 0;
|
||||
std::string name;
|
||||
std::string locale_name;
|
||||
|
||||
uint8_t type = 0;
|
||||
uint8_t rank = 0;
|
||||
uint8_t battle_type = 0;
|
||||
uint8_t level = 1;
|
||||
uint8_t size = 0;
|
||||
|
||||
uint32_t gold_min = 0;
|
||||
uint32_t gold_max = 0;
|
||||
uint32_t exp = 0;
|
||||
uint32_t max_hp = 0;
|
||||
uint8_t regen_cycle = 0;
|
||||
uint8_t regen_percent = 0;
|
||||
uint16_t def = 0;
|
||||
|
||||
uint32_t ai_flag = 0;
|
||||
uint32_t race_flag = 0;
|
||||
uint32_t immune_flag = 0;
|
||||
|
||||
uint8_t str = 0;
|
||||
uint8_t dex = 0;
|
||||
uint8_t con = 0;
|
||||
uint8_t intel = 0;
|
||||
|
||||
uint32_t damage_min = 0;
|
||||
uint32_t damage_max = 0;
|
||||
|
||||
int16_t attack_speed = 0;
|
||||
int16_t moving_speed = 0;
|
||||
uint8_t aggressive_hp_pct = 0;
|
||||
uint16_t aggressive_sight = 0;
|
||||
uint16_t attack_range = 0;
|
||||
|
||||
std::array<int8_t, 6> enchants{};
|
||||
std::array<int8_t, 11> resists{};
|
||||
|
||||
uint32_t resurrection_vnum = 0;
|
||||
uint32_t drop_item_vnum = 0;
|
||||
|
||||
uint8_t mount_capacity = 0;
|
||||
uint8_t on_click_type = 0;
|
||||
uint8_t empire = 0;
|
||||
std::string folder;
|
||||
float damage_multiply = 1.0f;
|
||||
uint32_t summon_vnum = 0;
|
||||
uint32_t drain_sp = 0;
|
||||
uint32_t monster_color = 0;
|
||||
uint32_t polymorph_item_vnum = 0;
|
||||
|
||||
std::array<MobSkillLevel, 5> skills{};
|
||||
uint8_t berserk_point = 0;
|
||||
uint8_t stone_skin_point = 0;
|
||||
uint8_t god_speed_point = 0;
|
||||
uint8_t death_blow_point = 0;
|
||||
uint8_t revive_point = 0;
|
||||
};
|
||||
|
||||
class MobProto {
|
||||
public:
|
||||
MobProto() = default;
|
||||
|
||||
bool load(std::span<const uint8_t> data, const uint32_t key[4] = eterpack::DEFAULT_MOB_PROTO_KEY);
|
||||
bool load_file(const std::filesystem::path& filepath, const uint32_t key[4] = eterpack::DEFAULT_MOB_PROTO_KEY);
|
||||
|
||||
const MobRecord* find(uint32_t vnum) const;
|
||||
const std::vector<MobRecord>& get_records() const { return m_records; }
|
||||
size_t size() const { return m_records.size(); }
|
||||
bool empty() const { return m_records.empty(); }
|
||||
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::vector<MobRecord> m_records;
|
||||
std::unordered_map<uint32_t, size_t> m_vnum_map;
|
||||
};
|
||||
|
||||
} // namespace proto
|
||||
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#include "proto/item_proto.h"
|
||||
#include "proto/mob_proto.h"
|
||||
@@ -0,0 +1,210 @@
|
||||
#include "proto/item_proto.h"
|
||||
#include "eterpack/lzo.h"
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include <bit>
|
||||
|
||||
namespace proto {
|
||||
|
||||
namespace {
|
||||
|
||||
inline uint16_t read16(std::span<const uint8_t> data, size_t offset) {
|
||||
if (offset + 2 > data.size()) return 0;
|
||||
return static_cast<uint16_t>(data[offset]) |
|
||||
(static_cast<uint16_t>(data[offset + 1]) << 8);
|
||||
}
|
||||
|
||||
inline uint32_t read32(std::span<const uint8_t> data, size_t offset) {
|
||||
if (offset + 4 > data.size()) return 0;
|
||||
return static_cast<uint32_t>(data[offset]) |
|
||||
(static_cast<uint32_t>(data[offset + 1]) << 8) |
|
||||
(static_cast<uint32_t>(data[offset + 2]) << 16) |
|
||||
(static_cast<uint32_t>(data[offset + 3]) << 24);
|
||||
}
|
||||
|
||||
inline int32_t read32_signed(std::span<const uint8_t> data, size_t offset) {
|
||||
return std::bit_cast<int32_t>(read32(data, offset));
|
||||
}
|
||||
|
||||
inline std::string read_str(std::span<const uint8_t> data, size_t offset, size_t max_len) {
|
||||
if (offset >= data.size()) return {};
|
||||
size_t len = 0;
|
||||
while (offset + len < data.size() && len < max_len && data[offset + len] != 0) {
|
||||
++len;
|
||||
}
|
||||
return std::string(reinterpret_cast<const char*>(data.data() + offset), len);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void ItemProto::clear() {
|
||||
m_records.clear();
|
||||
m_vnum_map.clear();
|
||||
m_ranged_indices.clear();
|
||||
}
|
||||
|
||||
bool ItemProto::load_file(const std::filesystem::path& filepath, const uint32_t key[4]) {
|
||||
std::ifstream file(filepath, std::ios::binary);
|
||||
if (!file.is_open()) return false;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
size_t size = static_cast<size_t>(file.tellg());
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> buffer(size);
|
||||
if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return load(buffer, key);
|
||||
}
|
||||
|
||||
bool ItemProto::load(std::span<const uint8_t> input, const uint32_t key[4]) {
|
||||
clear();
|
||||
if (input.size() < 12) return false;
|
||||
|
||||
uint32_t fourcc = read32(input, 0);
|
||||
size_t mcoz_offset = 0;
|
||||
uint32_t count = 0;
|
||||
uint32_t stride = 0;
|
||||
|
||||
if (fourcc == 0x5850494d) { // 'M', 'I', 'P', 'X'
|
||||
if (input.size() < 20) return false;
|
||||
uint32_t version = read32(input, 4);
|
||||
if (version != 1) return false;
|
||||
stride = read32(input, 8);
|
||||
count = read32(input, 12);
|
||||
// data_size at input[16]
|
||||
mcoz_offset = 20;
|
||||
} else if (fourcc == 0x5450494d) { // 'M', 'I', 'P', 'T'
|
||||
count = read32(input, 4);
|
||||
// data_size at input[8]
|
||||
mcoz_offset = 12;
|
||||
} else if (fourcc == eterpack::FOURCC_MCOZ) {
|
||||
// Raw MCOZ container without outer MIPX/MIPT header
|
||||
mcoz_offset = 0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mcoz_offset > input.size()) return false;
|
||||
|
||||
std::vector<uint8_t> raw;
|
||||
if (!eterpack::LZO::instance().decompress_mcoz(input.subspan(mcoz_offset), raw, key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (raw.empty()) return false;
|
||||
|
||||
if (stride == 0) {
|
||||
if (count > 0 && raw.size() % count == 0) {
|
||||
stride = static_cast<uint32_t>(raw.size() / count);
|
||||
} else {
|
||||
// Try standard strides
|
||||
for (uint32_t s : {156, 152, 176, 172, 178}) {
|
||||
if (raw.size() % s == 0) {
|
||||
stride = s;
|
||||
count = static_cast<uint32_t>(raw.size() / s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (count == 0 && stride > 0) {
|
||||
count = static_cast<uint32_t>(raw.size() / stride);
|
||||
}
|
||||
|
||||
if (stride != 152 && stride != 156 && stride != 172 && stride != 176 && stride != 178) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (count == 0 || raw.size() < static_cast<size_t>(count) * stride) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_records.reserve(count);
|
||||
size_t shift = (stride == 156 || stride == 176 || stride == 178) ? 4 : 0;
|
||||
bool has_8_sockets = (stride == 172 || stride == 176 || stride == 178);
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
auto row = std::span<const uint8_t>(raw).subspan(static_cast<size_t>(i) * stride, stride);
|
||||
ItemProtoRecord item;
|
||||
|
||||
item.vnum = read32(row, 0);
|
||||
if (shift) {
|
||||
item.range = read32(row, 4);
|
||||
}
|
||||
|
||||
item.name = read_str(row, 4 + shift, 24);
|
||||
item.locale_name = read_str(row, 29 + shift, 24);
|
||||
|
||||
item.type = row[54 + shift];
|
||||
item.subtype = row[55 + shift];
|
||||
item.weight = row[56 + shift];
|
||||
item.size = row[57 + shift];
|
||||
|
||||
item.anti_flags = read32(row, 58 + shift);
|
||||
item.flags = read32(row, 62 + shift);
|
||||
item.wear_flags = read32(row, 66 + shift);
|
||||
item.immune_flags = read32(row, 70 + shift);
|
||||
|
||||
item.buy_price = read32(row, 74 + shift);
|
||||
item.sell_price = read32(row, 78 + shift);
|
||||
|
||||
for (size_t j = 0; j < 2; ++j) {
|
||||
item.limits[j] = {
|
||||
row[82 + shift + 5 * j],
|
||||
read32_signed(row, 83 + shift + 5 * j)
|
||||
};
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < 3; ++j) {
|
||||
item.applies[j] = {
|
||||
row[92 + shift + 5 * j],
|
||||
read32_signed(row, 93 + shift + 5 * j)
|
||||
};
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < 6; ++j) {
|
||||
item.values[j] = read32_signed(row, 107 + shift + 4 * j);
|
||||
}
|
||||
|
||||
size_t socket_count = has_8_sockets ? 8 : 3;
|
||||
for (size_t j = 0; j < socket_count; ++j) {
|
||||
item.sockets[j] = read32_signed(row, 131 + shift + 4 * j);
|
||||
}
|
||||
|
||||
size_t tail_offset = 131 + shift + socket_count * 4;
|
||||
item.refined_vnum = read32(row, tail_offset);
|
||||
item.refine_set = read16(row, tail_offset + 4);
|
||||
item.alter_to_magic_percent = row[tail_offset + 6];
|
||||
item.specular = row[tail_offset + 7];
|
||||
item.gain_socket_percent = row[tail_offset + 8];
|
||||
|
||||
size_t idx = m_records.size();
|
||||
if (item.range > 0) {
|
||||
m_ranged_indices.push_back(idx);
|
||||
}
|
||||
m_vnum_map[item.vnum] = idx;
|
||||
m_records.push_back(std::move(item));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const ItemProtoRecord* ItemProto::find(uint32_t vnum) const {
|
||||
auto it = m_vnum_map.find(vnum);
|
||||
if (it != m_vnum_map.end()) {
|
||||
return &m_records[it->second];
|
||||
}
|
||||
|
||||
for (size_t idx : m_ranged_indices) {
|
||||
const auto& rec = m_records[idx];
|
||||
if (vnum > rec.vnum && vnum < rec.vnum + rec.range) {
|
||||
return &rec;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace proto
|
||||
@@ -0,0 +1,210 @@
|
||||
#include "proto/mob_proto.h"
|
||||
#include "eterpack/lzo.h"
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include <bit>
|
||||
|
||||
namespace proto {
|
||||
|
||||
namespace {
|
||||
|
||||
inline uint16_t read16(std::span<const uint8_t> data, size_t offset) {
|
||||
if (offset + 2 > data.size()) return 0;
|
||||
return static_cast<uint16_t>(data[offset]) |
|
||||
(static_cast<uint16_t>(data[offset + 1]) << 8);
|
||||
}
|
||||
|
||||
inline int16_t read16_signed(std::span<const uint8_t> data, size_t offset) {
|
||||
return std::bit_cast<int16_t>(read16(data, offset));
|
||||
}
|
||||
|
||||
inline uint32_t read32(std::span<const uint8_t> data, size_t offset) {
|
||||
if (offset + 4 > data.size()) return 0;
|
||||
return static_cast<uint32_t>(data[offset]) |
|
||||
(static_cast<uint32_t>(data[offset + 1]) << 8) |
|
||||
(static_cast<uint32_t>(data[offset + 2]) << 16) |
|
||||
(static_cast<uint32_t>(data[offset + 3]) << 24);
|
||||
}
|
||||
|
||||
inline float read_float(std::span<const uint8_t> data, size_t offset) {
|
||||
uint32_t val = read32(data, offset);
|
||||
return std::bit_cast<float>(val);
|
||||
}
|
||||
|
||||
inline std::string read_str(std::span<const uint8_t> data, size_t offset, size_t max_len) {
|
||||
if (offset >= data.size()) return {};
|
||||
size_t len = 0;
|
||||
while (offset + len < data.size() && len < max_len && data[offset + len] != 0) {
|
||||
++len;
|
||||
}
|
||||
return std::string(reinterpret_cast<const char*>(data.data() + offset), len);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void MobProto::clear() {
|
||||
m_records.clear();
|
||||
m_vnum_map.clear();
|
||||
}
|
||||
|
||||
bool MobProto::load_file(const std::filesystem::path& filepath, const uint32_t key[4]) {
|
||||
std::ifstream file(filepath, std::ios::binary);
|
||||
if (!file.is_open()) return false;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
size_t size = static_cast<size_t>(file.tellg());
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> buffer(size);
|
||||
if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return load(buffer, key);
|
||||
}
|
||||
|
||||
bool MobProto::load(std::span<const uint8_t> input, const uint32_t key[4]) {
|
||||
clear();
|
||||
if (input.size() < 12) return false;
|
||||
|
||||
uint32_t fourcc = read32(input, 0);
|
||||
size_t mcoz_offset = 0;
|
||||
uint32_t count = 0;
|
||||
|
||||
if (fourcc == 0x54504d4d) { // 'M', 'M', 'P', 'T'
|
||||
count = read32(input, 4);
|
||||
mcoz_offset = 12;
|
||||
} else if (fourcc == eterpack::FOURCC_MCOZ) {
|
||||
mcoz_offset = 0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mcoz_offset > input.size()) return false;
|
||||
|
||||
std::vector<uint8_t> raw;
|
||||
if (!eterpack::LZO::instance().decompress_mcoz(input.subspan(mcoz_offset), raw, key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (raw.empty()) return false;
|
||||
|
||||
uint32_t stride = 0;
|
||||
if (count > 0 && raw.size() % count == 0) {
|
||||
stride = static_cast<uint32_t>(raw.size() / count);
|
||||
} else {
|
||||
for (uint32_t s : {255, 256, 262, 263, 235}) {
|
||||
if (raw.size() % s == 0) {
|
||||
stride = s;
|
||||
count = static_cast<uint32_t>(raw.size() / s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (stride != 235 && stride != 255 && stride != 256 && stride != 262 && stride != 263) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (count == 0 || raw.size() < static_cast<size_t>(count) * stride) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t prefix_shift = (stride >= 262) ? 1 : 0;
|
||||
size_t resist_shift = (stride >= 262) ? 2 : ((stride == 256) ? 1 : 0);
|
||||
|
||||
m_records.reserve(count);
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
auto row = std::span<const uint8_t>(raw).subspan(static_cast<size_t>(i) * stride, stride);
|
||||
MobRecord m;
|
||||
|
||||
m.vnum = read32(row, 0);
|
||||
m.name = read_str(row, 4, 24);
|
||||
m.locale_name = read_str(row, 29, 24);
|
||||
|
||||
m.type = row[54];
|
||||
m.rank = row[55];
|
||||
m.battle_type = row[56];
|
||||
m.level = row[57];
|
||||
m.size = row[58 + prefix_shift];
|
||||
|
||||
m.gold_min = read32(row, 59 + prefix_shift);
|
||||
m.gold_max = read32(row, 63 + prefix_shift);
|
||||
m.exp = read32(row, 67 + prefix_shift);
|
||||
m.max_hp = read32(row, 71 + prefix_shift);
|
||||
m.regen_cycle = row[75 + prefix_shift];
|
||||
m.regen_percent = row[76 + prefix_shift];
|
||||
m.def = read16(row, 77 + prefix_shift);
|
||||
|
||||
m.ai_flag = read32(row, 79 + prefix_shift);
|
||||
m.race_flag = read32(row, 83 + prefix_shift);
|
||||
m.immune_flag = read32(row, 87 + prefix_shift);
|
||||
|
||||
m.str = row[91 + prefix_shift];
|
||||
m.dex = row[92 + prefix_shift];
|
||||
m.con = row[93 + prefix_shift];
|
||||
m.intel = row[94 + prefix_shift];
|
||||
|
||||
m.damage_min = read32(row, 95 + prefix_shift);
|
||||
m.damage_max = read32(row, 99 + prefix_shift);
|
||||
|
||||
m.attack_speed = read16_signed(row, 103 + prefix_shift);
|
||||
m.moving_speed = read16_signed(row, 105 + prefix_shift);
|
||||
m.aggressive_hp_pct = row[107 + prefix_shift];
|
||||
m.aggressive_sight = read16(row, 108 + prefix_shift);
|
||||
m.attack_range = read16(row, 110 + prefix_shift);
|
||||
|
||||
for (size_t j = 0; j < 6; ++j) {
|
||||
m.enchants[j] = static_cast<int8_t>(row[112 + prefix_shift + j]);
|
||||
}
|
||||
for (size_t j = 0; j < 11; ++j) {
|
||||
m.resists[j] = static_cast<int8_t>(row[118 + prefix_shift + j]);
|
||||
}
|
||||
|
||||
m.resurrection_vnum = read32(row, 129 + prefix_shift + resist_shift);
|
||||
m.drop_item_vnum = read32(row, 133 + prefix_shift + resist_shift);
|
||||
m.mount_capacity = row[137 + prefix_shift + resist_shift];
|
||||
m.on_click_type = row[138 + prefix_shift + resist_shift];
|
||||
m.empire = row[139 + prefix_shift + resist_shift];
|
||||
|
||||
m.folder = read_str(row, 140 + prefix_shift + resist_shift, 64);
|
||||
m.damage_multiply = read_float(row, 205 + prefix_shift + resist_shift);
|
||||
m.summon_vnum = read32(row, 209 + prefix_shift + resist_shift);
|
||||
m.drain_sp = read32(row, 213 + prefix_shift + resist_shift);
|
||||
m.monster_color = read32(row, 217 + prefix_shift + resist_shift);
|
||||
m.polymorph_item_vnum = read32(row, 221 + prefix_shift + resist_shift);
|
||||
|
||||
if (stride >= 255) {
|
||||
for (size_t j = 0; j < 5; ++j) {
|
||||
size_t off = 225 + prefix_shift + resist_shift + j * 5;
|
||||
m.skills[j].vnum = read32(row, off);
|
||||
m.skills[j].level = row[off + 4];
|
||||
}
|
||||
size_t pt_off = 250 + prefix_shift + resist_shift;
|
||||
if (pt_off + 5 <= stride) {
|
||||
m.berserk_point = row[pt_off];
|
||||
m.stone_skin_point = row[pt_off + 1];
|
||||
m.god_speed_point = row[pt_off + 2];
|
||||
m.death_blow_point = row[pt_off + 3];
|
||||
m.revive_point = row[pt_off + 4];
|
||||
}
|
||||
}
|
||||
|
||||
size_t idx = m_records.size();
|
||||
m_vnum_map[m.vnum] = idx;
|
||||
m_records.push_back(std::move(m));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const MobRecord* MobProto::find(uint32_t vnum) const {
|
||||
auto it = m_vnum_map.find(vnum);
|
||||
if (it != m_vnum_map.end()) {
|
||||
return &m_records[it->second];
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace proto
|
||||
@@ -0,0 +1,255 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user