From fdcc1a362d3493b77c89066dde9fbe7caa9ae7f0 Mon Sep 17 00:00:00 2001 From: korenkonder Date: Sun, 30 Jun 2024 15:43:10 +0300 Subject: [PATCH] `MotHead`: moved code to `motion.cpp` and `motion.hpp` --- src/CRE/rob/motion.cpp | 222 +++++++++++++++++++++++++++++++++++++++ src/CRE/rob/motion.hpp | 5 + src/CRE/rob/rob.cpp | 229 ----------------------------------------- src/CRE/rob/rob.hpp | 9 -- src/ReDIVA/app.cpp | 2 - 5 files changed, 227 insertions(+), 240 deletions(-) diff --git a/src/CRE/rob/motion.cpp b/src/CRE/rob/motion.cpp index e5873607..eb412526 100644 --- a/src/CRE/rob/motion.cpp +++ b/src/CRE/rob/motion.cpp @@ -6,6 +6,28 @@ #include "motion.hpp" #include "../data.hpp" +struct MhdFile { + mothead* data; + uint32_t set; + std::string file_path; // Added + p_file_handler file_handler; + prj::shared_ptr alloc_handler; + int32_t load_count; + + MhdFile(); + virtual ~MhdFile(); + + bool CheckNotReady(); + void FreeData(); + void LoadFile(const char* path, const char* file, uint32_t set); + void ParseFile(const void* data); + mothead* ParseMothead(mothead_file* mhdsf, size_t data); + mothead_mot* ParseMotheadMot(mothead_mot_file* mhdsf, size_t data); + bool Unload(); + + static void ParseFileParent(MhdFile* mhd, const void* file_data, size_t size); +}; + struct MotFile { const motion_set_info* mot_set_info; prj::shared_ptr alloc_handler; @@ -25,8 +47,33 @@ struct MotFile { static void ParseFileParent(MotFile* mot, const void* file_data, size_t size); }; +static const mothead_mot mothead_mot_null; + +std::map mothead_storage_data; std::map motion_storage_data; +void motion_set_load_mothead(uint32_t set, std::string&& mdata_dir, const motion_database* mot_db) { + const motion_set_info* set_info = mot_db->get_motion_set_by_id(set); + if (!set_info) + return; + + std::string file; + file.assign("mothead_"); + file.append(set_info->name); + file.append(".bin"); + + std::string path; + path.assign("rom/rob/"); + if (data_list[DATA_AFT].check_file_exists(mdata_dir.c_str(), file.c_str())) + path.assign(mdata_dir); + + auto elem = mothead_storage_data.find(set); + if (elem == mothead_storage_data.end()) + elem = mothead_storage_data.insert({ set, {} }).first; + + elem->second.LoadFile(path.c_str(), file.c_str(), set); +} + void motion_set_load_motion(uint32_t set, std::string&& mdata_dir, const motion_database* mot_db) { const motion_set_info* set_info = mot_db->get_motion_set_by_id(set); if (!set_info) @@ -40,12 +87,45 @@ void motion_set_load_motion(uint32_t set, std::string&& mdata_dir, const motion_ elem->second.LoadFile(std::string(mdata_dir), set); } +void motion_set_unload_mothead(uint32_t set) { + auto elem = mothead_storage_data.find(set); + if (elem != mothead_storage_data.end() && elem->second.Unload()) + mothead_storage_data.erase(elem); +} + void motion_set_unload_motion(uint32_t set) { auto elem = motion_storage_data.find(set); if (elem != motion_storage_data.end() && elem->second.Unload()) motion_storage_data.erase(elem); } +bool mothead_storage_check_mhd_file_not_ready(uint32_t set_id) { + auto elem = mothead_storage_data.find(set_id); + if (elem != mothead_storage_data.end()) + return elem->second.CheckNotReady(); + return false; +} + +const mothead_mot* mothead_storage_get_mot_by_motion_id( + uint32_t motion_id, const motion_database* mot_db) { + uint32_t set_id = mot_db->get_motion_set_id_by_motion_id(motion_id); + if (set_id == -1) + return &mothead_mot_null; + + auto elem = mothead_storage_data.find(set_id); + if (elem == mothead_storage_data.end() || !elem->second.data) + return &mothead_mot_null; + + mothead* mhd = elem->second.data; + if ((int32_t)motion_id < mhd->first_mot_id || (int32_t)motion_id > mhd->last_mot_id) + return &mothead_mot_null; + + mothead_mot* mot = mhd->mots[(ssize_t)(int32_t)motion_id - mhd->first_mot_id]; + if (!mot) + return &mothead_mot_null; + return mot; +} + bool motion_storage_check_mot_file_not_ready(uint32_t set_id) { auto elem = motion_storage_data.find(set_id); if (elem != motion_storage_data.end()) @@ -93,11 +173,153 @@ const mot_set* motion_storage_get_motion_set(uint32_t set_id) { } void motion_init() { + mothead_storage_data = {}; motion_storage_data = {}; } void motion_free() { motion_storage_data.clear(); + mothead_storage_data.clear(); +} + +MhdFile::MhdFile() : data(), set(), load_count() { + FreeData(); +} + +MhdFile::~MhdFile() { + FreeData(); +} + +bool MhdFile::CheckNotReady() { + return file_handler.check_not_ready(); +} + +void MhdFile::FreeData() { + data = 0; + alloc_handler.reset(); + set = -1; + file_path.clear(); + file_handler.reset(); + load_count = 0; +} + +void MhdFile::LoadFile(const char* path, const char* file, uint32_t set) { + if (load_count > 0) { + load_count++; + return; + } + + this->set = set; + file_path.assign(path); + file_path.append(file); + if (file_handler.read_file(&data_list[DATA_AFT], path, file)) + file_handler.set_callback_data(0, (PFNFILEHANDLERCALLBACK*)ParseFileParent, this); + load_count = 1; +} + +void MhdFile::ParseFile(const void* data) { + alloc_handler = prj::shared_ptr(new prj::stack_allocator); + mothead_file* mhdf = (mothead_file*)((size_t)data + ((uint32_t*)data)[1]); + this->data = MhdFile::ParseMothead(mhdf, (size_t)mhdf); +} + +mothead* MhdFile::ParseMothead(mothead_file* mhdf, size_t data) { + mothead* mhd = alloc_handler->allocate(); + mhd->mot_set_id = mhdf->mot_set_id; + mhd->first_mot_id = mhdf->first_mot_id; + mhd->last_mot_id = mhdf->last_mot_id; + uint32_t* mot_offsets = (uint32_t*)(data + mhdf->mot_offsets_offset); + size_t mot_count = (size_t)mhdf->last_mot_id - mhdf->first_mot_id + 1; + mhd->mots = alloc_handler->allocate(mot_count); + mothead_mot** mots = mhd->mots; + for (; mot_count; mot_count--) { + *mots = 0; + if (*mot_offsets) + *mots = ParseMotheadMot((mothead_mot_file*)(data + *mot_offsets), data); + mots++; + mot_offsets++; + } + return mhd; +} + +mothead_mot* MhdFile::ParseMotheadMot(mothead_mot_file* mhdsf, size_t data) { + struct mothead_mot_data_file { + int32_t type; + uint32_t offset; + }; + + struct mothead_data_file { + mothead_data_type type; + int32_t frame; + uint32_t offset; + }; + + mothead_mot* mhdm = alloc_handler->allocate(); + mhdm->field_0.field_0 = mhdsf->field_0; + mhdm->field_0.field_4 = mhdsf->field_4; + mhdm->field_0.field_8 = mhdsf->field_8; + mhdm->field_0.field_C = mhdsf->field_C; + mhdm->field_10 = mhdsf->field_10; + mhdm->field_12 = mhdsf->field_12; + + if (mhdsf->mot_data_offset) { + mothead_mot_data_file* mot_data_file = (mothead_mot_data_file*)(data + mhdsf->mot_data_offset); + mothead_mot_data_file* mot_data_file_end = mot_data_file; + while ((mot_data_file_end++)->type >= 0); + + size_t count = mot_data_file_end - mot_data_file; + mhdm->mot_data = alloc_handler->allocate(count); + mothead_mot_data* mot_data = mhdm->mot_data; + for (size_t i = count; i; i--, mot_data_file++, mot_data++) { + mot_data->type = mot_data_file->type; + mot_data->data = (void*)(data + mot_data_file->offset); + } + } + + if (mhdsf->data_offset) { + mothead_data_file* data_file = (mothead_data_file*)(data + mhdsf->data_offset); + mothead_data_file* data_file_end = data_file; + while ((data_file_end++)->type >= 0); + + size_t count = data_file_end - data_file; + mhdm->data = alloc_handler->allocate(count); + mothead_data* _data = mhdm->data; + for (size_t i = count; i; i--, data_file++, _data++) { + _data->type = data_file->type; + _data->frame = data_file->frame; + _data->data = (void*)(data + data_file->offset); + } + } + + if (mhdsf->field_1C) { + uint32_t* v29 = (uint32_t*)(data + mhdsf->field_1C); + uint32_t* v30 = v29; + while (*v30++); + + size_t count = v30 - v29; + mhdm->field_28 = alloc_handler->allocate(count); + int64_t* v33 = mhdm->field_28; + for (size_t i = count; i; i--) + *v33++ = data + *v29++; + } + return mhdm; +} + +bool MhdFile::Unload() { + if (--load_count < 0) { + load_count = 0; + return true; + } + else if (load_count <= 0) { + FreeData(); + return true; + } + else + return false; +} + +void MhdFile::ParseFileParent(MhdFile* mhd, const void* file_data, size_t size) { + mhd->ParseFile(file_data); } MotFile::MotFile() : mot_set_info(), mot_set(), load_count() { diff --git a/src/CRE/rob/motion.hpp b/src/CRE/rob/motion.hpp index 0a3f325a..fe8e44b5 100644 --- a/src/CRE/rob/motion.hpp +++ b/src/CRE/rob/motion.hpp @@ -7,9 +7,14 @@ #include "rob.hpp" +extern void motion_set_load_mothead(uint32_t set, std::string&& mdata_dir, const motion_database* mot_db); extern void motion_set_load_motion(uint32_t set, std::string&& mdata_dir, const motion_database* mot_db); +extern void motion_set_unload_mothead(uint32_t set); extern void motion_set_unload_motion(uint32_t set); +extern bool mothead_storage_check_mhd_file_not_ready(uint32_t set_id); +extern const mothead_mot* mothead_storage_get_mot_by_motion_id( + uint32_t motion_id, const motion_database* mot_db); extern bool motion_storage_check_mot_file_not_ready(uint32_t set_id); extern const mot_data* motion_storage_get_mot_data(uint32_t motion_id, const motion_database* mot_db); extern float_t motion_storage_get_mot_data_frame_count(uint32_t motion_id, const motion_database* mot_db); diff --git a/src/CRE/rob/rob.cpp b/src/CRE/rob/rob.cpp index e30be66e..68f6c341 100644 --- a/src/CRE/rob/rob.cpp +++ b/src/CRE/rob/rob.cpp @@ -33,28 +33,6 @@ enum rob_sleeve_type : uint8_t { ROB_SLEEVE_LR = 0x03, }; -struct MhdFile { - mothead* data; - uint32_t set; - std::string file_path; // Added - p_file_handler file_handler; - prj::shared_ptr alloc_handler; - int32_t load_count; - - MhdFile(); - virtual ~MhdFile(); - - bool CheckNotReady(); - void FreeData(); - void LoadFile(const char* path, const char* file, uint32_t set); - void ParseFile(const void* data); - mothead* ParseMothead(mothead_file* mhdsf, size_t data); - mothead_mot* ParseMotheadMot(mothead_mot_file* mhdsf, size_t data); - bool Unload(); - - static void ParseFileParent(MhdFile* mhd, const void* file_data, size_t size); -}; - struct OpdMaker { struct Data { bool empty; @@ -1254,8 +1232,6 @@ static rob_manager_rob_impl* rob_manager_rob_impls2_get(TaskRobManager* rob_mgr) rob_chara* rob_chara_array; rob_chara_pv_data* rob_chara_pv_data_array; -std::map mothead_storage_data; - rob_manager_rob_impl rob_manager_rob_impls1[2]; bool rob_manager_rob_impls1_init = false; rob_manager_rob_impl rob_manager_rob_impls2[7]; @@ -1280,8 +1256,6 @@ static int32_t opd_maker_counter = 0; static int32_t pv_osage_manager_counter = 0; static int32_t rob_thread_parent_counter = 0; -static const mothead_mot mothead_mot_null; - static const mothead_func_struct mothead_func_array[] = { { mothead_func_0 , 1 }, { mothead_func_1 , 1 }, @@ -2337,34 +2311,6 @@ const float_t get_osage_gravity_const() { return 0.00299444468691945f; } -void motion_set_load_mothead(uint32_t set, std::string&& mdata_dir, const motion_database* mot_db) { - const motion_set_info* set_info = mot_db->get_motion_set_by_id(set); - if (!set_info) - return; - - std::string file; - file.assign("mothead_"); - file.append(set_info->name); - file.append(".bin"); - - std::string path; - path.assign("rom/rob/"); - if (data_list[DATA_AFT].check_file_exists(mdata_dir.c_str(), file.c_str())) - path.assign(mdata_dir); - - auto elem = mothead_storage_data.find(set); - if (elem == mothead_storage_data.end()) - elem = mothead_storage_data.insert({ set, {} }).first; - - elem->second.LoadFile(path.c_str(), file.c_str(), set); -} - -void motion_set_unload_mothead(uint32_t set) { - auto elem = mothead_storage_data.find(set); - if (elem != mothead_storage_data.end() && elem->second.Unload()) - mothead_storage_data.erase(elem); -} - const char* get_ram_osage_play_data_dir() { return "ram/osage_play_data"; } @@ -15813,41 +15759,6 @@ rob_chara::~rob_chara() { item_equip = 0; } -void mothead_storage_init() { - mothead_storage_data = {}; -} - -bool mothead_storage_check_mhd_file_not_ready(uint32_t set_id) { - auto elem = mothead_storage_data.find(set_id); - if (elem != mothead_storage_data.end()) - return elem->second.CheckNotReady(); - return false; -} - -const mothead_mot* mothead_storage_get_mot_by_motion_id( - uint32_t motion_id, const motion_database* mot_db) { - uint32_t set_id = mot_db->get_motion_set_id_by_motion_id(motion_id); - if (set_id == -1) - return &mothead_mot_null; - - auto elem = mothead_storage_data.find(set_id); - if (elem == mothead_storage_data.end() || !elem->second.data) - return &mothead_mot_null; - - mothead* mhd = elem->second.data; - if ((int32_t)motion_id < mhd->first_mot_id || (int32_t)motion_id > mhd->last_mot_id) - return &mothead_mot_null; - - mothead_mot* mot = mhd->mots[(ssize_t)(int32_t)motion_id - mhd->first_mot_id]; - if (!mot) - return &mothead_mot_null; - return mot; -} - -void mothead_storage_free() { - mothead_storage_data.clear(); -} - int32_t expression_id_to_mottbl_index(int32_t expression_id) { static const int32_t expression_id_to_mottbl_index_table[] = { 11, 15, 57, 19, 23, 25, 29, 33, @@ -15924,146 +15835,6 @@ int32_t mouth_anim_id_to_mottbl_index(int32_t mouth_anim_id) { return 131; } -MhdFile::MhdFile() : data(), set(), load_count() { - FreeData(); -} - -MhdFile::~MhdFile() { - FreeData(); -} - -bool MhdFile::CheckNotReady() { - return file_handler.check_not_ready(); -} - -void MhdFile::FreeData() { - data = 0; - alloc_handler.reset(); - set = -1; - file_path.clear(); - file_handler.reset(); - load_count = 0; -} - -void MhdFile::LoadFile(const char* path, const char* file, uint32_t set) { - if (load_count > 0) { - load_count++; - return; - } - - this->set = set; - file_path.assign(path); - file_path.append(file); - if (file_handler.read_file(&data_list[DATA_AFT], path, file)) - file_handler.set_callback_data(0, (PFNFILEHANDLERCALLBACK*)ParseFileParent, this); - load_count = 1; -} - -void MhdFile::ParseFile(const void* data) { - alloc_handler = prj::shared_ptr(new prj::stack_allocator); - mothead_file* mhdf = (mothead_file*)((size_t)data + ((uint32_t*)data)[1]); - this->data = MhdFile::ParseMothead(mhdf, (size_t)mhdf); -} - -mothead* MhdFile::ParseMothead(mothead_file* mhdf, size_t data) { - mothead* mhd = alloc_handler->allocate(); - mhd->mot_set_id = mhdf->mot_set_id; - mhd->first_mot_id = mhdf->first_mot_id; - mhd->last_mot_id = mhdf->last_mot_id; - uint32_t* mot_offsets = (uint32_t*)(data + mhdf->mot_offsets_offset); - size_t mot_count = (size_t)mhdf->last_mot_id - mhdf->first_mot_id + 1; - mhd->mots = alloc_handler->allocate(mot_count); - mothead_mot** mots = mhd->mots; - for (; mot_count; mot_count--) { - *mots = 0; - if (*mot_offsets) - *mots = ParseMotheadMot((mothead_mot_file*)(data + *mot_offsets), data); - mots++; - mot_offsets++; - } - return mhd; -} - -mothead_mot* MhdFile::ParseMotheadMot(mothead_mot_file* mhdsf, size_t data) { - struct mothead_mot_data_file { - int32_t type; - uint32_t offset; - }; - - struct mothead_data_file { - mothead_data_type type; - int32_t frame; - uint32_t offset; - }; - - mothead_mot* mhdm = alloc_handler->allocate(); - mhdm->field_0.field_0 = mhdsf->field_0; - mhdm->field_0.field_4 = mhdsf->field_4; - mhdm->field_0.field_8 = mhdsf->field_8; - mhdm->field_0.field_C = mhdsf->field_C; - mhdm->field_10 = mhdsf->field_10; - mhdm->field_12 = mhdsf->field_12; - - if (mhdsf->mot_data_offset) { - mothead_mot_data_file* mot_data_file = (mothead_mot_data_file*)(data + mhdsf->mot_data_offset); - mothead_mot_data_file* mot_data_file_end = mot_data_file; - while ((mot_data_file_end++)->type >= 0); - - size_t count = mot_data_file_end - mot_data_file; - mhdm->mot_data = alloc_handler->allocate(count); - mothead_mot_data* mot_data = mhdm->mot_data; - for (size_t i = count; i; i--, mot_data_file++, mot_data++) { - mot_data->type = mot_data_file->type; - mot_data->data = (void*)(data + mot_data_file->offset); - } - } - - if (mhdsf->data_offset) { - mothead_data_file* data_file = (mothead_data_file*)(data + mhdsf->data_offset); - mothead_data_file* data_file_end = data_file; - while ((data_file_end++)->type >= 0); - - size_t count = data_file_end - data_file; - mhdm->data = alloc_handler->allocate(count); - mothead_data* _data = mhdm->data; - for (size_t i = count; i; i--, data_file++, _data++) { - _data->type = data_file->type; - _data->frame = data_file->frame; - _data->data = (void*)(data + data_file->offset); - } - } - - if (mhdsf->field_1C) { - uint32_t* v29 = (uint32_t*)(data + mhdsf->field_1C); - uint32_t* v30 = v29; - while (*v30++); - - size_t count = v30 - v29; - mhdm->field_28 = alloc_handler->allocate(count); - int64_t* v33 = mhdm->field_28; - for (size_t i = count; i; i--) - *v33++ = data + *v29++; - } - return mhdm; -} - -bool MhdFile::Unload() { - if (--load_count < 0) { - load_count = 0; - return true; - } - else if (load_count <= 0) { - FreeData(); - return true; - } - else - return false; -} - -void MhdFile::ParseFileParent(MhdFile* mhd, const void* file_data, size_t size) { - mhd->ParseFile(file_data); -} - OpdMaker::Data::Data() : empty() { Reset(); } diff --git a/src/CRE/rob/rob.hpp b/src/CRE/rob/rob.hpp index 918f8254..79e37c69 100644 --- a/src/CRE/rob/rob.hpp +++ b/src/CRE/rob/rob.hpp @@ -3656,9 +3656,6 @@ extern const uint32_t* get_opd_motion_set_ids(); extern const float_t get_osage_gravity_const(); -extern void motion_set_load_mothead(uint32_t set, std::string&& mdata_dir, const motion_database* mot_db); -extern void motion_set_unload_mothead(uint32_t set); - extern const char* get_ram_osage_play_data_dir(); extern const char* get_ram_osage_play_data_tmp_dir(); extern const char* get_rom_osage_play_data_dir(); @@ -3763,12 +3760,6 @@ extern bool task_rob_manager_get_wait(int32_t chara_id); extern bool task_rob_manager_run_task(); extern bool task_rob_manager_del_task(); -extern void mothead_storage_init(); -extern bool mothead_storage_check_mhd_file_not_ready(uint32_t set_id); -extern const mothead_mot* mothead_storage_get_mot_by_motion_id( - uint32_t motion_id, const motion_database* mot_db); -extern void mothead_storage_free(); - extern int32_t expression_id_to_mottbl_index(int32_t expression_id); extern int32_t hand_anim_id_to_mottbl_index(int32_t hand_anim_id); extern int32_t look_anim_id_to_mottbl_index(int32_t look_anim_id); diff --git a/src/ReDIVA/app.cpp b/src/ReDIVA/app.cpp index 4a1659d5..25ccca1e 100644 --- a/src/ReDIVA/app.cpp +++ b/src/ReDIVA/app.cpp @@ -502,7 +502,6 @@ static render_context* render_context_load() { app::task_work_init(); motion_init(); - mothead_storage_init(); skin_param_data_init(); skin_param_data_load(); @@ -1238,7 +1237,6 @@ static void render_context_dispose(render_context* rctx) { input_state_free(); skin_param_data_free(); - mothead_storage_free(); motion_free(); app::task_work_free();