Initialize shared Metin2 SDK with rendering optimizations and tests

This commit is contained in:
shen
2026-10-11 17:52:55 -07:00
commit 86759bb726
147 changed files with 27272 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.20)
project(character LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(character STATIC
src/msm_data.cpp
src/character_assembly.cpp
)
target_include_directories(character PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
add_library(character::character ALIAS character)
option(CHARACTER_BUILD_TESTS "Build character SDK tests" ON)
if(CHARACTER_BUILD_TESTS)
add_executable(character_test tests/character_test.cpp)
target_link_libraries(character_test PRIVATE character)
add_test(NAME character_test COMMAND character_test)
endif()
+5
View File
@@ -0,0 +1,5 @@
#pragma once
#include "types.h"
#include "msm_data.h"
#include "character_assembly.h"
@@ -0,0 +1,54 @@
#pragma once
#include "types.h"
#include "msm_data.h"
#include <string>
#include <vector>
#include <memory>
#include <unordered_map>
namespace metin2::character {
struct AssembledPart {
uint32_t part_id = PART_MAIN;
std::string model_path;
std::vector<SkinMapping> skin_overrides;
std::string socket_bone;
float local_matrix[16] = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
};
class CharacterAssembly {
public:
CharacterAssembly(std::shared_ptr<const MsmData> msm);
~CharacterAssembly() = default;
// Equip / Appearance changes
bool SetArmorShape(uint32_t shape_index);
bool SetHair(uint32_t hair_index);
void SetWeapon(uint32_t part_id, const std::string& weapon_model, const std::string& custom_socket = "");
// Query active visual composition
const std::vector<AssembledPart>& GetAssembledParts() const { return m_parts; }
const AssembledPart* GetPart(uint32_t part_id) const;
// Calculate socket world matrix: out_world = bone_world * local_transform
static void ComputeSocketWorldTransform(const float bone_world[16], const float local_offset[3], float out_world[16]);
std::shared_ptr<const MsmData> GetMsm() const { return m_msm; }
private:
void RebuildAssembly();
std::shared_ptr<const MsmData> m_msm;
uint32_t m_current_shape_index = 0;
uint32_t m_current_hair_index = 0;
std::unordered_map<uint32_t, std::string> m_custom_weapons;
std::vector<AssembledPart> m_parts;
};
} // namespace metin2::character
+60
View File
@@ -0,0 +1,60 @@
#pragma once
#include "types.h"
#include <string>
#include <vector>
#include <unordered_map>
#include <map>
namespace metin2::character {
class MsmData {
public:
explicit MsmData(uint32_t race_id = 0);
~MsmData() = default;
// Load from official Metin2 .msm file or raw string content
bool LoadMsmFile(const std::string& file_path);
bool LoadMsmFromString(const std::string& content);
uint32_t GetRaceId() const { return m_race_id; }
const std::string& GetBaseModelFile() const { return m_base_model_file; }
const std::string& GetTreeFile() const { return m_tree_file; }
bool IsTree() const { return !m_tree_file.empty(); }
const std::string& GetMotionListFile() const { return m_motion_list_file; }
// Shape / Armor data query
const ShapeData* FindShape(uint32_t shape_index) const;
void SetShape(uint32_t shape_index, const ShapeData& shape);
// Hair data query
const HairData* FindHair(uint32_t hair_index) const;
void SetHair(uint32_t hair_index, const HairData& hair);
// Attaching sockets & bones
void RegisterAttachingBone(uint32_t part_id, const std::string& bone_name);
bool GetAttachingBone(uint32_t part_id, std::string& out_bone_name) const;
const std::vector<AttachingData>& GetAttachingDataList() const { return m_attaching_list; }
void AddAttachingData(const AttachingData& data) { m_attaching_list.push_back(data); }
// Motion registry
void RegisterMotion(uint16_t mode, uint16_t motion_name, const std::string& file_name, int percent = 100);
const std::vector<MotionEntry>* GetMotions(uint16_t mode, uint16_t motion_name) const;
private:
uint32_t m_race_id = 0;
std::string m_base_model_file;
std::string m_tree_file;
std::string m_motion_list_file;
std::unordered_map<uint32_t, ShapeData> m_shapes;
std::unordered_map<uint32_t, HairData> m_hairs;
std::unordered_map<uint32_t, std::string> m_attaching_bones;
std::vector<AttachingData> m_attaching_list;
// mode -> (motion_name -> vector of motions)
std::map<uint16_t, std::map<uint16_t, std::vector<MotionEntry>>> m_motions;
};
} // namespace metin2::character
+91
View File
@@ -0,0 +1,91 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
namespace metin2::character {
enum EParts {
PART_MAIN = 0,
PART_WEAPON = 1,
PART_HEAD = 2,
PART_WEAPON_LEFT = 3,
PART_HAIR = 4,
PART_SASH = 5,
PART_ARROW_TYPE = 6,
PART_MAX_NUM
};
enum EMotion {
NAME_NONE = 0,
NAME_WAIT = 1,
NAME_WALK = 2,
NAME_RUN = 3,
NAME_CHANGE_WEAPON = 4,
NAME_DAMAGE = 5,
NAME_DAMAGE_FLYING = 6,
NAME_STAND_UP = 7,
NAME_DAMAGE_BACK = 8,
NAME_DAMAGE_FLYING_BACK = 9,
NAME_STAND_UP_BACK = 10,
NAME_DEAD = 11,
NAME_DEAD_BACK = 12,
NAME_NORMAL_ATTACK = 13,
NAME_COMBO_ATTACK_1 = 14,
NAME_COMBO_ATTACK_2 = 15,
NAME_COMBO_ATTACK_3 = 16,
NAME_COMBO_ATTACK_4 = 17,
NAME_INTRO_WAIT = 22,
NAME_INTRO_SELECTED = 23,
NAME_INTRO_NOT_SELECTED = 24,
NAME_SPAWN = 25,
NAME_STOP = 32,
NAME_SKILL = 50,
NAME_MAX_NUM = 150
};
enum EAttachingDataType {
ATTACHING_DATA_TYPE_NONE = 0,
ATTACHING_DATA_TYPE_COLLISION_DATA = 1,
ATTACHING_DATA_TYPE_EFFECT = 2,
ATTACHING_DATA_TYPE_OBJECT = 3,
};
struct SkinMapping {
std::string src_file;
std::string dst_file;
};
struct ShapeData {
uint32_t shape_index = 0;
std::string model_file;
std::vector<SkinMapping> skins;
};
struct HairData {
uint32_t hair_index = 0;
std::string model_file;
std::vector<SkinMapping> skins;
};
struct AttachingData {
uint32_t type = ATTACHING_DATA_TYPE_NONE;
bool is_attaching = false;
uint32_t model_index = 0;
std::string bone_name;
// Local transform offset
float offset_x = 0.0f;
float offset_y = 0.0f;
float offset_z = 0.0f;
// Attached effect / collision data
std::string effect_file;
float collision_sphere_radius = 0.0f;
};
struct MotionEntry {
std::string file_name;
int percentage = 100;
};
} // namespace metin2::character
+95
View File
@@ -0,0 +1,95 @@
#include "character/character_assembly.h"
#include <algorithm>
namespace metin2::character {
CharacterAssembly::CharacterAssembly(std::shared_ptr<const MsmData> msm)
: m_msm(std::move(msm)) {
RebuildAssembly();
}
bool CharacterAssembly::SetArmorShape(uint32_t shape_index) {
m_current_shape_index = shape_index;
RebuildAssembly();
return true;
}
bool CharacterAssembly::SetHair(uint32_t hair_index) {
m_current_hair_index = hair_index;
RebuildAssembly();
return true;
}
void CharacterAssembly::SetWeapon(uint32_t part_id, const std::string& weapon_model, const std::string& custom_socket) {
if (weapon_model.empty()) {
m_custom_weapons.erase(part_id);
} else {
m_custom_weapons[part_id] = weapon_model;
}
RebuildAssembly();
}
const AssembledPart* CharacterAssembly::GetPart(uint32_t part_id) const {
for (const auto& part : m_parts) {
if (part.part_id == part_id) return &part;
}
return nullptr;
}
void CharacterAssembly::ComputeSocketWorldTransform(const float bone_world[16], const float local_offset[3], float out_world[16]) {
// Copy bone matrix
std::copy(bone_world, bone_world + 16, out_world);
// Apply local translation along bone world axes
out_world[12] += bone_world[0] * local_offset[0] + bone_world[4] * local_offset[1] + bone_world[8] * local_offset[2];
out_world[13] += bone_world[1] * local_offset[0] + bone_world[5] * local_offset[1] + bone_world[9] * local_offset[2];
out_world[14] += bone_world[2] * local_offset[0] + bone_world[6] * local_offset[1] + bone_world[10] * local_offset[2];
}
void CharacterAssembly::RebuildAssembly() {
m_parts.clear();
if (!m_msm) return;
// 1. Base Body / Armor Part
AssembledPart body;
body.part_id = PART_MAIN;
body.model_path = m_msm->GetBaseModelFile();
const ShapeData* shape = m_msm->FindShape(m_current_shape_index);
if (shape) {
if (!shape->model_file.empty()) {
body.model_path = shape->model_file;
}
body.skin_overrides = shape->skins;
}
m_parts.push_back(std::move(body));
// 2. Hair Part
if (m_current_hair_index > 0) {
const HairData* hair = m_msm->FindHair(m_current_hair_index);
if (hair && !hair->model_file.empty()) {
AssembledPart hair_part;
hair_part.part_id = PART_HAIR;
hair_part.model_path = hair->model_file;
hair_part.skin_overrides = hair->skins;
std::string bone;
if (m_msm->GetAttachingBone(PART_HAIR, bone)) {
hair_part.socket_bone = bone;
}
m_parts.push_back(std::move(hair_part));
}
}
// 3. Weapons
for (const auto& [part_id, model] : m_custom_weapons) {
AssembledPart weapon_part;
weapon_part.part_id = part_id;
weapon_part.model_path = model;
std::string bone;
if (m_msm->GetAttachingBone(part_id, bone)) {
weapon_part.socket_bone = bone;
}
m_parts.push_back(std::move(weapon_part));
}
}
} // namespace metin2::character
+231
View File
@@ -0,0 +1,231 @@
#include "character/msm_data.h"
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iostream>
namespace metin2::character {
namespace {
inline std::string Trim(const std::string& str) {
size_t first = str.find_first_not_of(" \t\r\n");
if (first == std::string::npos) return "";
size_t last = str.find_last_not_of(" \t\r\n");
return str.substr(first, (last - first + 1));
}
inline std::string ToLower(std::string str) {
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return str;
}
inline std::string NormalizePath(std::string path) {
for (char& c : path) {
if (c == '\\') c = '/';
else if (static_cast<unsigned char>(c) < 128) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
return path;
}
inline std::string ResolvePath(const std::string& path, const std::string& base_path) {
std::string norm = NormalizePath(path);
if (norm.empty()) return norm;
if (norm.find(':') != std::string::npos || norm.front() == '/') {
return norm; // Already absolute
}
if (!base_path.empty()) {
std::string base = NormalizePath(base_path);
if (!base.empty() && base.back() != '/') base += '/';
return base + norm;
}
return norm;
}
} // namespace
MsmData::MsmData(uint32_t race_id) : m_race_id(race_id) {
// Default standard attaching bone names (Warrior/Assassin/Sura/Shaman convention)
m_attaching_bones[PART_WEAPON] = "equip_right";
m_attaching_bones[PART_WEAPON_LEFT] = "equip_left";
m_attaching_bones[PART_HEAD] = "Bip01 Head";
m_attaching_bones[PART_HAIR] = "Bip01 Head";
m_attaching_bones[PART_SASH] = "Bip01 Spine2";
}
bool MsmData::LoadMsmFile(const std::string& file_path) {
std::ifstream file(file_path);
if (!file.is_open()) return false;
std::stringstream buffer;
buffer << file.rdbuf();
return LoadMsmFromString(buffer.str());
}
bool MsmData::LoadMsmFromString(const std::string& content) {
std::istringstream stream(content);
std::string raw_line;
std::vector<std::string> lines;
while (std::getline(stream, raw_line)) {
std::string trimmed = Trim(raw_line);
if (trimmed.empty() || trimmed[0] == '#') continue;
lines.push_back(trimmed);
}
std::vector<std::string> group_stack;
std::string current_base_path;
ShapeData current_shape;
HairData current_hair;
AttachingData current_attaching;
for (size_t i = 0; i < lines.size(); ++i) {
const std::string& l = lines[i];
if (l.find("Group ") == 0 || l.find("group ") == 0) {
std::string grp = ToLower(Trim(l.substr(6)));
group_stack.push_back(grp);
if (grp.find("shapedata") == 0 && grp != "shapedata") {
current_shape = ShapeData();
} else if (grp.find("hairdata") == 0 && grp != "hairdata") {
current_hair = HairData();
} else if (grp.find("attachingdata") == 0 && grp != "attachingdata") {
current_attaching = AttachingData();
}
continue;
}
if (l == "}") {
if (!group_stack.empty()) {
std::string top = group_stack.back();
group_stack.pop_back();
if (top.find("shapedata") == 0 && top != "shapedata") {
m_shapes[current_shape.shape_index] = current_shape;
} else if (top.find("hairdata") == 0 && top != "hairdata") {
m_hairs[current_hair.hair_index] = current_hair;
} else if (top.find("attachingdata") == 0 && top != "attachingdata") {
m_attaching_list.push_back(current_attaching);
} else if (top == "shapedata" || top == "hairdata") {
current_base_path.clear();
}
}
continue;
}
std::istringstream line_stream(l);
std::string key;
line_stream >> key;
key = ToLower(key);
std::string val;
line_stream >> std::ws;
std::getline(line_stream, val);
val = Trim(val);
if (val.size() >= 2 && val.front() == '"' && val.back() == '"') {
val = val.substr(1, val.size() - 2);
}
std::string active_group = group_stack.empty() ? "" : group_stack.back();
if (key == "basemodelfilename") {
m_base_model_file = NormalizePath(val);
} else if (key == "treefilename") {
m_tree_file = NormalizePath(val);
} else if (key == "motionlistfilename") {
m_motion_list_file = NormalizePath(val);
} else if (key == "specialpath" || key == "pathname") {
current_base_path = NormalizePath(val);
if (!current_base_path.empty() && current_base_path.back() != '/') current_base_path += '/';
} else if (key == "shapeindex") {
current_shape.shape_index = std::stoul(val);
} else if (key == "model" || key == "local_model") {
std::string resolved = (key == "local_model") ? NormalizePath(val) : ResolvePath(val, current_base_path);
if (active_group.find("shapedata") == 0) current_shape.model_file = resolved;
else if (active_group.find("hairdata") == 0) current_hair.model_file = resolved;
} else if (key == "sourceskin" || key == "local_sourceskin") {
std::string src = (key == "local_sourceskin") ? NormalizePath(val) : ResolvePath(val, current_base_path);
std::string dst;
if (i + 1 < lines.size()) {
std::istringstream next_stream(lines[i + 1]);
std::string next_key;
next_stream >> next_key;
std::string lower_next = ToLower(next_key);
if (lower_next == "targetskin" || lower_next == "local_targetskin") {
next_stream >> std::ws;
std::getline(next_stream, dst);
dst = Trim(dst);
if (dst.size() >= 2 && dst.front() == '"' && dst.back() == '"') {
dst = dst.substr(1, dst.size() - 2);
}
dst = (lower_next == "local_targetskin") ? NormalizePath(dst) : ResolvePath(dst, current_base_path);
i++;
}
}
if (active_group.find("shapedata") == 0) {
current_shape.skins.push_back({src, dst});
} else if (active_group.find("hairdata") == 0) {
current_hair.skins.push_back({src, dst});
}
} else if (key == "hairindex") {
current_hair.hair_index = std::stoul(val);
} else if (key == "attachingdatatype") {
current_attaching.type = std::stoul(val);
} else if (key == "isattaching") {
current_attaching.is_attaching = (std::stoi(val) != 0);
} else if (key == "attachingmodelindex") {
current_attaching.model_index = std::stoul(val);
} else if (key == "attachingbonename") {
current_attaching.bone_name = val;
}
}
return !m_base_model_file.empty() || !m_tree_file.empty();
}
const ShapeData* MsmData::FindShape(uint32_t shape_index) const {
auto it = m_shapes.find(shape_index);
return (it != m_shapes.end()) ? &it->second : nullptr;
}
void MsmData::SetShape(uint32_t shape_index, const ShapeData& shape) {
m_shapes[shape_index] = shape;
}
const HairData* MsmData::FindHair(uint32_t hair_index) const {
auto it = m_hairs.find(hair_index);
return (it != m_hairs.end()) ? &it->second : nullptr;
}
void MsmData::SetHair(uint32_t hair_index, const HairData& hair) {
m_hairs[hair_index] = hair;
}
void MsmData::RegisterAttachingBone(uint32_t part_id, const std::string& bone_name) {
m_attaching_bones[part_id] = bone_name;
}
bool MsmData::GetAttachingBone(uint32_t part_id, std::string& out_bone_name) const {
auto it = m_attaching_bones.find(part_id);
if (it != m_attaching_bones.end()) {
out_bone_name = it->second;
return true;
}
return false;
}
void MsmData::RegisterMotion(uint16_t mode, uint16_t motion_name, const std::string& file_name, int percent) {
m_motions[mode][motion_name].push_back({file_name, percent});
}
const std::vector<MotionEntry>* MsmData::GetMotions(uint16_t mode, uint16_t motion_name) const {
auto mode_it = m_motions.find(mode);
if (mode_it == m_motions.end()) return nullptr;
auto motion_it = mode_it->second.find(motion_name);
if (motion_it == mode_it->second.end()) return nullptr;
return &motion_it->second;
}
} // namespace metin2::character
+129
View File
@@ -0,0 +1,129 @@
#include "character/character.h"
#include <iostream>
#include <cassert>
#include <cmath>
using namespace metin2::character;
int main() {
std::cout << "[Test 1] Parse official MSM character script..." << std::endl;
const std::string sample_msm = R"(
ScriptType RaceDataFile
BaseActorData
{
BaseModelFileName "d:/ymir work/pc/warrior/warrior_novice.gr2"
MotionListFileName "d:/ymir work/pc/warrior/motlist.txt"
}
Group ShapeData
{
PathName "d:/ymir work/pc/warrior/"
ShapeDataCount 2
Group ShapeData00
{
ShapeIndex 0
Model "warrior_novice.gr2"
SourceSkin "warrior_novice.dds"
TargetSkin "warrior_novice_blue.dds"
}
Group ShapeData01
{
ShapeIndex 1
Model "warrior_4-1.gr2"
SourceSkin "warrior_4-1.dds"
TargetSkin "warrior_4-1_gold.dds"
}
}
Group HairData
{
PathName "d:/ymir work/pc/warrior/hair/"
HairDataCount 1
Group HairData01
{
HairIndex 101
Model "hair_01.gr2"
SourceSkin "hair_01.dds"
TargetSkin "hair_01_red.dds"
}
}
Group AttachingData
{
AttachingDataCount 1
Group AttachingData00
{
AttachingDataType 2
isAttaching 1
AttachingModelIndex 0
AttachingBoneName "equip_right"
}
}
)";
auto msm = std::make_shared<MsmData>(0);
bool ok = msm->LoadMsmFromString(sample_msm);
assert(ok);
assert(msm->GetBaseModelFile() == "d:/ymir work/pc/warrior/warrior_novice.gr2");
const ShapeData* s0 = msm->FindShape(0);
assert(s0 != nullptr);
assert(!s0->skins.empty());
assert(s0->skins[0].dst_file == "d:/ymir work/pc/warrior/warrior_novice_blue.dds");
const ShapeData* s1 = msm->FindShape(1);
assert(s1 != nullptr);
assert(s1->model_file == "d:/ymir work/pc/warrior/warrior_4-1.gr2");
const HairData* h = msm->FindHair(101);
assert(h != nullptr);
assert(h->model_file == "d:/ymir work/pc/warrior/hair/hair_01.gr2");
std::cout << "[Test 2] Character dynamic assembly & skin replacement..." << std::endl;
CharacterAssembly char_assembly(msm);
// Initial novice state
assert(char_assembly.GetAssembledParts().size() == 1);
assert(char_assembly.GetAssembledParts()[0].part_id == PART_MAIN);
// Equip armor shape 1
char_assembly.SetArmorShape(1);
assert(char_assembly.GetPart(PART_MAIN)->model_path == "d:/ymir work/pc/warrior/warrior_4-1.gr2");
// Equip hair 101
char_assembly.SetHair(101);
assert(char_assembly.GetPart(PART_HAIR) != nullptr);
assert(char_assembly.GetPart(PART_HAIR)->socket_bone == "Bip01 Head");
// Equip main weapon (sword)
char_assembly.SetWeapon(PART_WEAPON, "d:/ymir work/weapon/weapon_sword_01.gr2");
assert(char_assembly.GetPart(PART_WEAPON) != nullptr);
assert(char_assembly.GetPart(PART_WEAPON)->socket_bone == "equip_right");
std::cout << " Assembled part count: " << char_assembly.GetAssembledParts().size() << std::endl;
std::cout << "[Test 3] Socket world transform calculation..." << std::endl;
float bone_world[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
100.0f, 150.0f, 200.0f, 1.0f
};
float offset[3] = { 10.0f, 5.0f, -2.0f };
float socket_world[16];
CharacterAssembly::ComputeSocketWorldTransform(bone_world, offset, socket_world);
assert(std::abs(socket_world[12] - 110.0f) < 1e-4f);
assert(std::abs(socket_world[13] - 155.0f) < 1e-4f);
assert(std::abs(socket_world[14] - 198.0f) < 1e-4f);
std::cout << " Socket translation computed: ("
<< socket_world[12] << ", " << socket_world[13] << ", " << socket_world[14] << ")" << std::endl;
std::cout << "All Character SDK tests passed successfully!" << std::endl;
return 0;
}