AVA2 client Windows parity passes (gr2 BitKnit2/64-bit, GamePhase handlers, python modules)

- gr2: BitKnit2 decompression, 64-bit LE files, compressed curves
- net: remaining GamePhase handlers, generated GC packet table
- python: event/chat/chr/minimap/guild/quest/messenger/shop-search and
  other native modules ported against the AVA2 Windows client
- render: D3D8 fixed-function lighting in Gr2ModelRenderer, terrain
  without baked shade; cape/NPC equipment attachment
- tests + audits updated; docs/PORT-PLAN.md and WINDOWS-PARITY.md

Batch 4 (render pipeline: background.*, app.RenderGame/UpdateGame,
camera bindings, main.cpp fake-content removal) is still in progress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-08 10:43:01 +09:00
co-authored by Claude Opus 5.5
parent 9f748eb05a
commit 9077aa389a
145 changed files with 22692 additions and 6584 deletions
+455
View File
@@ -0,0 +1,455 @@
#include "RaceData.h"
#include "src/ava2_text_file.h"
#include "src/ava2_text_file_loader.h"
#include "src/eter_resource/ResourceManager.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
namespace ava2 {
namespace {
void TraceError(const std::string& text) { std::cerr << "[AVA2 TraceError] " << text << std::endl; }
// CFileNameHelper::StringPath: lower case + '\\' -> '/'. ChangeDosPath goes the other way, but the
// skin match (CGrannyMaterial::IsIn) compares StringPath forms, so the skin key is kept in that form.
std::string StringPath(std::string path) {
for (auto& 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;
}
bool IsGuildRace(unsigned race) { return (race >= 14000 && race < 15000) || race == 20043; }
bool IsNPCRace(unsigned race) { return race > 9000; }
bool IsNewPetRace(unsigned race) { return race >= 34001 && race <= 34150; } // NEW_PET_SYSTEM
bool IsNewMountRace(unsigned race) { return race >= 32000 && race <= 32100; }
} // namespace
bool ReadRaceResourceFile(const std::string& fileName, std::string* text) {
for (const auto& candidate : CResourceManager::GetCandidatePaths(CResourceManager::NormalizePath(fileName.c_str()))) {
std::ifstream in(candidate, std::ios::binary);
if (!in) continue;
std::ostringstream ss; ss << in.rdbuf();
*text = ss.str();
return true;
}
return false;
}
std::string ReadMotionFileName(const std::string& msaFileName) {
std::string text;
if (!ReadRaceResourceFile(msaFileName, &text)) return {};
TextFileLoader loader;
if (!loader.load(text)) return {};
std::string motion;
if (!loader.tokenString("motionfilename", &motion)) return {};
return motion;
}
bool RaceData::LoadRaceData(const std::string& fileName) {
std::string text;
if (!ReadRaceResourceFile(fileName, &text)) return false;
TextFileLoader loader;
loader.load(text);
loader.setTop();
loader.tokenString("basemodelfilename", &m_baseModelFileName);
loader.tokenString("treefilename", &m_treeFileName);
loader.tokenString("attributefilename", &m_attributeFileName);
loader.tokenString("smokebonename", &m_smokeBoneName);
loader.tokenString("motionlistfilename", &m_motionListFileName);
if (!m_treeFileName.empty()) m_treeFileName = StringPath(m_treeFileName);
if (auto* smoke = loader.tokenVector("smokefilename")) {
if (smoke->size() % 2 != 0) {
TraceError("SmokeFileName ArgCount[" + std::to_string(smoke->size()) + "]%2==0");
return false;
}
// Smoke effects (CEffectManager::RegisterEffect2) are not ported.
}
// The original `continue`s below skip SetParentNode(), so a ShapeDataNN without ShapeIndex (or
// without Model/Local_Model) leaves the cursor inside that child; kept as-is.
if (loader.setChildNode("shapedata")) {
std::string pathName;
int count = 0;
if (loader.tokenString("pathname", &pathName) && loader.tokenInteger("shapedatacount", &count)) {
for (unsigned i = 0; i < static_cast<unsigned>(count); ++i) {
if (!loader.setChildNode("shapedata", i)) continue;
loader.tokenString("specialpath", &pathName);
int shapeIndex = 0;
if (!loader.tokenInteger("shapeindex", &shapeIndex)) continue;
std::string model;
if (loader.tokenString("model", &model)) {
SetShapeModel(shapeIndex, pathName + model);
} else {
if (!loader.tokenString("local_model", &model)) continue;
SetShapeModel(shapeIndex, model);
}
std::string src, dst;
if (loader.tokenString("local_sourceskin", &src) && loader.tokenString("local_targetskin", &dst))
AppendShapeSkin(shapeIndex, 0, src, dst);
if (loader.tokenString("sourceskin", &src) && loader.tokenString("targetskin", &dst))
AppendShapeSkin(shapeIndex, 0, pathName + src, pathName + dst);
// ENABLE_SKIN_EXTENDED
for (unsigned n = 2; n <= 9; ++n) {
if (loader.tokenString("sourceskin" + std::to_string(n), &src) &&
loader.tokenString("targetskin" + std::to_string(n), &dst))
AppendShapeSkin(shapeIndex, 0, pathName + src, pathName + dst);
}
loader.setParent();
}
}
loader.setParent();
}
if (loader.setChildNode("hairdata")) {
std::string pathName;
int count = 0;
if (loader.tokenString("pathname", &pathName) && loader.tokenInteger("hairdatacount", &count)) {
for (unsigned i = 0; i < static_cast<unsigned>(count); ++i) {
if (!loader.setChildNode("hairdata", i)) continue;
loader.tokenString("specialpath", &pathName);
int hairIndex = 0;
if (!loader.tokenInteger("hairindex", &hairIndex)) continue;
std::string model, src, dst;
if (loader.tokenString("model", &model) && loader.tokenString("sourceskin", &src) &&
loader.tokenString("targetskin", &dst))
SetHairSkin(hairIndex, 0, pathName + model, pathName + src, pathName + dst);
loader.setParent();
}
}
loader.setParent();
}
// AttachingData (collision spheres, attached effects) is not ported.
return true;
}
void RaceData::SetShapeModel(uint32_t index, const std::string& model) { m_shapes[index].model = model; }
void RaceData::AppendShapeSkin(uint32_t index, uint32_t, const std::string& src, const std::string& dst) {
m_shapes[index].skins.push_back({StringPath(src), dst});
}
void RaceData::SetHairSkin(uint32_t index, uint32_t, const std::string& model, const std::string& src, const std::string& dst) {
auto& hair = m_hairs[index];
hair.skins.push_back({StringPath(src), dst});
hair.model = model;
}
const RaceData::Shape* RaceData::FindShape(uint32_t index) const {
auto it = m_shapes.find(index);
return it == m_shapes.end() ? nullptr : &it->second;
}
const RaceData::Shape* RaceData::FindHair(uint32_t index) const {
auto it = m_hairs.find(index);
if (it == m_hairs.end()) {
if (index != 0) TraceError("Hair number " + std::to_string(index) + " is not exist.");
return nullptr;
}
return &it->second;
}
bool RaceData::GetAttachingBoneName(uint32_t part, std::string* bone) const {
auto it = m_attachingBoneNames.find(part);
if (it == m_attachingBoneNames.end()) return false;
*bone = it->second;
return true;
}
bool RaceData::RegisterMotion(uint16_t mode, uint16_t name, const std::string& fileName, int percent) {
auto it = m_motionModes.find(mode);
if (it == m_motionModes.end()) {
TraceError("Failed getting motion mode data!");
return false;
}
it->second[name].push_back({fileName, percent});
return true;
}
const RaceData::MotionVectorMap* RaceData::GetMotionMode(uint16_t mode) const {
auto it = m_motionModes.find(mode);
return it == m_motionModes.end() ? nullptr : &it->second;
}
const std::vector<RaceData::Motion>* RaceData::GetMotions(uint16_t mode, uint32_t name) const {
const MotionVectorMap* motions = GetMotionMode(mode);
if (!motions) return nullptr;
auto it = motions->find(name);
return it == motions->end() ? nullptr : &it->second;
}
bool RaceData::GetMotionKey(uint16_t mode, uint16_t name, uint16_t* keyMode) const {
const MotionVectorMap* motions = GetMotionMode(mode);
if (!motions) return false;
if (motions->count(name)) { *keyMode = mode; return true; }
// MODE_HORSE_ONEHAND_SWORD..MODE_HORSE_BELL (10..15) fall back to MODE_HORSE (9).
const uint16_t generalMode = (mode >= 10 && mode <= 15) ? 9 : MODE_GENERAL;
const MotionVectorMap* general = GetMotionMode(generalMode);
if (!general || !general->count(name)) return false;
*keyMode = generalMode;
return true;
}
bool RaceData::SetMotionRandomWeight(uint16_t mode, uint16_t name, uint16_t subIndex, uint8_t percent) {
auto mit = m_motionModes.find(mode);
if (mit == m_motionModes.end()) return false;
auto it = mit->second.find(name);
if (it == mit->second.end()) {
// The original prints wMotionModeIndex where it means the motion index.
TraceError("CRaceData::SetMotionRandomWeight(wMotionModeIndex=" + std::to_string(mode) + ", wMotionIndex=" +
std::to_string(name) + ", wMotionSubIndex=" + std::to_string(subIndex) + ", byPercentage=" +
std::to_string(percent) + ") - Find Motion(wMotionIndex=" + std::to_string(mode) + ") FAILED");
return false;
}
if (subIndex >= it->second.size()) return false;
it->second[subIndex].percent = percent;
return true;
}
bool RaceData::GetNormalAttackIndex(uint16_t mode, uint16_t* motion) const {
auto it = m_normalAttack.find(mode);
if (it == m_normalAttack.end()) return false;
*motion = uint16_t(it->second); // stored in a DWORD map, returned as WORD
return true;
}
void RaceData::ReserveComboAttack(uint16_t mode, uint16_t comboType, uint32_t count) {
m_comboAttack.insert({(uint32_t(mode) << 16) | comboType, std::vector<uint32_t>(count)});
}
void RaceData::RegisterComboAttack(uint16_t mode, uint16_t comboType, uint32_t comboIndex, uint16_t motion) {
auto it = m_comboAttack.find((uint32_t(mode) << 16) | comboType);
if (it == m_comboAttack.end()) return;
if (comboIndex >= it->second.size()) {
TraceError("CRaceData::RegisterCombo - Strange combo index!");
return;
}
it->second[comboIndex] = motion;
}
const std::vector<uint32_t>* RaceData::GetComboData(uint16_t mode, uint16_t comboType) const {
auto it = m_comboAttack.find((uint32_t(mode) << 16) | comboType);
return it == m_comboAttack.end() ? nullptr : &it->second;
}
RaceManager& RaceManager::Instance() {
static RaceManager instance;
return instance;
}
RaceData* RaceManager::CreateRace(uint32_t race) {
if (m_races.count(race)) {
TraceError("RaceManager::CreateRace : Race " + std::to_string(race) + " already created");
return nullptr;
}
auto data = std::make_unique<RaceData>(race);
RaceData* raw = data.get();
m_races.emplace(race, std::move(data));
return raw;
}
RaceData* RaceManager::GetRaceDataPointer(uint32_t race) {
auto it = m_races.find(race);
if (it != m_races.end()) return it->second.get();
RaceData* loaded = LoadRace(race);
if (!loaded) {
TraceError("CRaceManager::GetRaceDataPointer: cannot load data by dwRaceIndex " + std::to_string(race));
return nullptr;
}
return loaded;
}
void RaceManager::SelectRace(uint32_t race) {
auto it = m_races.find(race);
if (it == m_races.end()) return; // assert(!"Failed to select race data!")
m_selected = it->second.get();
}
float RaceManager::GetRaceHeight(int race) const {
auto it = m_raceHeights.find(race);
return it == m_raceHeights.end() ? 0.0f : it->second;
}
std::string RaceManager::GetFullPathFileName(const std::string& name) const {
if (!name.empty() && name[0] == '.') return name;
return m_pathName + name;
}
void RaceManager::Destroy() { m_races.clear(); m_selected = nullptr; }
void RaceManager::GetRaceResourcePathes(uint32_t race, std::vector<std::string>* paths) {
auto push = [paths](std::initializer_list<const char*> names) { for (auto* n : names) paths->push_back(std::string("d:/ymir work/") + n + "/"); };
if (IsGuildRace(race)) {
push({"guild", "npc", "npc2", "npc_pet", "monster", "monster2"});
} else if (IsNPCRace(race)) {
if (race >= 30000) {
if (race == 34028 || race == 34029) push({"npc_pet", "npc2"});
else push({"npc2", "npc_pet"});
if (IsNewPetRace(race)) push({"npc_pet"});
if (IsNewMountRace(race)) push({"npc_mount"});
push({"npc", "monster", "monster2", "guild"});
} else {
push({"npc", "npc2", "npc_pet", "monster", "monster2", "guild"});
}
} else if (race == 8507 || race == 8510) {
push({"monster2", "monster", "npc", "npc2", "npc_pet", "guild"});
} else if (race > 8000) {
push({"monster", "monster2", "npc", "npc2", "npc_pet", "guild"});
} else if (race > 2000 || (race >= 1400 && race <= 1700)) {
push({"monster2", "monster", "npc", "npc2", "npc_pet", "guild"});
} else {
push({"monster", "monster2", "npc", "npc2", "npc_pet", "guild"});
}
}
RaceData* RaceManager::LoadRace(uint32_t race) {
auto nameIt = m_raceNames.find(race);
if (nameIt == m_raceNames.end() || nameIt->second.empty()) return nullptr;
const std::string& raceName = nameIt->second;
// LOAD_LOCAL_RESOURCE
if (raceName[0] == '#') {
std::string pathName = raceName.substr(1);
auto data = std::make_unique<RaceData>(race);
if (!data->LoadRaceData(pathName + "shape.msm")) {
TraceError("CRaceManager::RegisterRacePath(race=" + std::to_string(race) + ").LoadRaceData(" + pathName + "shape.msm)");
return nullptr;
}
LoadRaceMotionList(*data, pathName, pathName + "motlist.txt");
RaceData* raw = data.get();
m_races.emplace(race, std::move(data));
return raw;
}
std::vector<std::string> paths;
GetRaceResourcePathes(race, &paths);
auto data = std::make_unique<RaceData>(race);
for (size_t i = 0; i < paths.size(); ++i) {
std::string fullPath = paths[i];
auto src = m_raceSrcNames.find(raceName);
fullPath += src == m_raceSrcNames.end() ? raceName : src->second;
fullPath += "/";
std::string msm = fullPath + raceName + ".msm";
if (!data->LoadRaceData(msm)) {
if (i != paths.size() - 1) continue; // Tracenf "Will Find Another Path."
TraceError("CRaceManager::RegisterRacePath : RACE[" + std::to_string(race) + "] LOAD MSMFILE[" + msm + "] ERROR");
return nullptr;
}
LoadRaceMotionList(*data, fullPath, fullPath + data->GetMotionListFileName());
RaceData* raw = data.get();
m_races.emplace(race, std::move(data));
return raw;
}
TraceError("CRaceManager::RegisterRacePath : RACE[" + std::to_string(race) + "] HAVE NO PATH ERROR");
return nullptr;
}
bool RaceManager::LoadRaceMotionList(RaceData& race, const std::string& pathName, const std::string& motionListFileName) {
static const std::map<std::string, uint32_t> kTypes = {
{"SPAWN", RaceData::NAME_SPAWN},
{"WAIT", RaceData::NAME_WAIT}, {"WAIT1", RaceData::NAME_WAIT}, {"WAIT2", RaceData::NAME_WAIT},
{"WALK", RaceData::NAME_WALK}, {"WALK1", RaceData::NAME_WALK}, {"WALK2", RaceData::NAME_WALK},
{"RUN", RaceData::NAME_RUN}, {"RUN1", RaceData::NAME_RUN}, {"RUN2", RaceData::NAME_RUN},
{"STOP", RaceData::NAME_STOP}, {"DEAD", RaceData::NAME_DEAD},
{"COMBO_ATTACK", RaceData::NAME_COMBO_ATTACK_1}, {"COMBO_ATTACK1", RaceData::NAME_COMBO_ATTACK_2},
{"COMBO_ATTACK2", RaceData::NAME_COMBO_ATTACK_3},
{"NORMAL_ATTACK", RaceData::NAME_NORMAL_ATTACK}, {"NORMAL_ATTACK1", RaceData::NAME_NORMAL_ATTACK},
{"NORMAL_ATTACK2", RaceData::NAME_NORMAL_ATTACK},
{"FRONT_DAMAGE", RaceData::NAME_DAMAGE}, {"FRONT_DAMAGE1", RaceData::NAME_DAMAGE},
{"FRONT_DAMAGE2", RaceData::NAME_DAMAGE}, {"FRONT_DAMAGE3", RaceData::NAME_DAMAGE},
{"FRONT_DEAD", RaceData::NAME_DEAD}, {"FRONT_DEAD1", RaceData::NAME_DEAD}, {"FRONT_DEAD2", RaceData::NAME_DEAD},
{"FRONT_KNOCKDOWN", RaceData::NAME_DAMAGE_FLYING}, {"FRONT_KNOCKDOWN1", RaceData::NAME_DAMAGE_FLYING},
{"FRONT_STANDUP", RaceData::NAME_STAND_UP}, {"FRONT_STANDUP1", RaceData::NAME_STAND_UP},
{"BACK_DAMAGE", RaceData::NAME_DAMAGE_BACK}, {"BACK_DAMAGE1", RaceData::NAME_DAMAGE_BACK},
{"BACK_DEAD", RaceData::NAME_DEAD_BACK}, {"BACK_DEAD1", RaceData::NAME_DEAD_BACK}, {"BACK_DEAD2", RaceData::NAME_DEAD_BACK},
{"BACK_KNOCKDOWN", RaceData::NAME_DAMAGE_FLYING_BACK}, {"BACK_KNOCKDOWN1", RaceData::NAME_DAMAGE_FLYING_BACK},
{"BACK_STANDUP", RaceData::NAME_STAND_UP_BACK}, {"BACK_STANDUP1", RaceData::NAME_STAND_UP_BACK},
{"SPECIAL", RaceData::NAME_SPECIAL_1}, {"SPECIAL1", RaceData::NAME_SPECIAL_2}, {"SPECIAL2", RaceData::NAME_SPECIAL_3},
{"SPECIAL3", RaceData::NAME_SPECIAL_4}, {"SPECIAL4", RaceData::NAME_SPECIAL_5}, {"SPECIAL5", RaceData::NAME_SPECIAL_6},
{"SKILL1", RaceData::NAME_SKILL + 121}, {"SKILL2", RaceData::NAME_SKILL + 122}, {"SKILL3", RaceData::NAME_SKILL + 123},
{"SKILL4", RaceData::NAME_SKILL + 124}, {"SKILL5", RaceData::NAME_SKILL + 125},
};
std::string text;
if (!ReadRaceResourceFile(motionListFileName, &text)) return false;
// sscanf("%s %s %s %d") leaves the buffers untouched on a blank line, so such a line repeats the
// previous registration exactly like the original.
char mode[256] = "", type[256] = "", file[256] = "";
int percent = 0;
bool isSpawn = false;
std::string spawnFile, motionFile;
for (const auto& line : ava2TextLines(text)) {
std::sscanf(line.c_str(), "%255s %255s %255s %d", mode, type, file, &percent);
auto it = kTypes.find(type);
if (it == kTypes.end()) {
// WAIT4 / WAIT20 style names fall back to the name with 1..2 trailing characters cut.
const size_t cutLimit = 2;
bool found = false;
size_t len = std::strlen(type);
if (cutLimit < len + 1) {
for (size_t i = 1; i <= cutLimit; ++i) {
it = kTypes.find(std::string(type).substr(0, len - i));
if (it != kTypes.end()) { found = true; break; }
}
}
if (!found) continue;
}
motionFile = pathName + file;
race.RegisterMotionData(it->second, motionFile, percent);
if (it->second == RaceData::NAME_SPAWN) isSpawn = true;
else if (it->second == RaceData::NAME_DAMAGE) spawnFile = motionFile;
}
if (!isSpawn && !spawnFile.empty()) race.RegisterMotionData(RaceData::NAME_SPAWN, spawnFile, percent);
return true;
}
bool RaceManager::LoadGameNPC(const std::string& fileName) {
std::string text;
if (!ReadRaceResourceFile(fileName, &text)) return false;
auto strip = [](std::string s) {
size_t b = s.find_first_not_of(" \t"), e = s.find_last_not_of(" \t");
return b == std::string::npos ? std::string() : s.substr(b, e - b + 1);
};
for (const auto& line : ava2TextLines(text)) {
auto tokens = ava2TabTokens(line, false);
if (tokens.size() < 2) continue;
uint32_t vnum = static_cast<uint32_t>(std::strtoul(tokens[0].c_str(), nullptr, 10));
if (vnum) RegisterRaceName(vnum, strip(tokens[1]));
else if (tokens.size() >= 3) RegisterRaceSrcName(strip(tokens[1]), strip(tokens[2]));
}
return true;
}
void RaceManager::LoadPlayerRaces() {
// CPythonPlayerSettingsModule m_vecRaceData.
static const struct { const char* msm; const char* path; } kRaces[] = {
{"warrior_m.msm", "d:/ymir work/pc/warrior/"}, {"assassin_w.msm", "d:/ymir work/pc/assassin/"},
{"sura_m.msm", "d:/ymir work/pc/sura/"}, {"shaman_w.msm", "d:/ymir work/pc/shaman/"},
{"warrior_w.msm", "d:/ymir work/pc2/warrior/"}, {"assassin_m.msm", "d:/ymir work/pc2/assassin/"},
{"sura_w.msm", "d:/ymir work/pc2/sura/"}, {"shaman_m.msm", "d:/ymir work/pc2/shaman/"},
};
for (uint32_t i = 0; i < sizeof(kRaces) / sizeof(kRaces[0]); ++i) {
if (m_races.count(i)) continue;
RaceData* race = CreateRace(i);
if (!race) continue;
// The LoadRaceData result is ignored by CPythonPlayerSettingsModule::Load.
race->LoadRaceData(kRaces[i].msm);
// __LoadGameRace attaching bones (RaceToJob: race % 4 here).
switch (i % 4) {
case 0: race->RegisterAttachingBoneName(RaceData::PART_WEAPON, "equip_right_hand"); break;
case 1: case 3:
race->RegisterAttachingBoneName(RaceData::PART_WEAPON, "equip_right");
race->RegisterAttachingBoneName(RaceData::PART_WEAPON_LEFT, "equip_left");
break;
case 2: race->RegisterAttachingBoneName(RaceData::PART_WEAPON, "equip_right"); break;
}
race->RegisterAttachingBoneName(RaceData::PART_SASH, "Bip01 Spine2"); // ENABLE_SASH_SYSTEM
}
}
} // namespace ava2