Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user