From 91754f3e0e679e75e9773d005c5f6c2d5a49e4ab Mon Sep 17 00:00:00 2001 From: Yimura Date: Thu, 25 Aug 2022 02:02:04 +0200 Subject: [PATCH] feat(util): Added model hash table helper (#423) --- BigBaseV2/src/pointers.cpp | 30 --------------- BigBaseV2/src/util/model_info.hpp | 64 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 BigBaseV2/src/util/model_info.hpp diff --git a/BigBaseV2/src/pointers.cpp b/BigBaseV2/src/pointers.cpp index 463c162c..c04b28e8 100644 --- a/BigBaseV2/src/pointers.cpp +++ b/BigBaseV2/src/pointers.cpp @@ -322,36 +322,6 @@ namespace big m_model_table = ptr.add(3).rip().as*>(); LOG(G3LOG_DEBUG) << "HashTable => [" << HEX_TO_UPPER(m_model_table) << "]"; - - // sample code to iterator models - /*for (int i = 0; i < m_model_table->m_size; ++i) - { - for (auto node = m_model_table->m_lookup_table[i]; node; node = node->m_next) - { - if (const auto table_idx = node->m_idx; table_idx < m_model_table->m_size) - { - if (const auto model = m_model_table->m_data[table_idx]; model && model->m_model_type == eModelType::Vehicle) - { - - } - } - } - }*/ - - // sample code to get a specific model - /*auto adder_hash = RAGE_JOAAT("adder"); - for (auto i = m_model_table->m_lookup_table[adder_hash % m_model_table->m_lookup_key]; i; i = i->m_next) - { - if (i->m_hash == adder_hash) - { - if (const auto model = m_model_table->m_data[i->m_idx]; model) - { - LOG(G3LOG_DEBUG) << "Found Model: " << HEX_TO_UPPER(model->m_model_hash) << " => type: " << (int)model->m_model_type; - - break; - } - } - }*/ }); // Get Label Text diff --git a/BigBaseV2/src/util/model_info.hpp b/BigBaseV2/src/util/model_info.hpp new file mode 100644 index 00000000..eca09377 --- /dev/null +++ b/BigBaseV2/src/util/model_info.hpp @@ -0,0 +1,64 @@ +#pragma once +#include "gta/joaat.hpp" +#include "pointers.hpp" + +namespace big +{ + // iterate all models + /* + for (int i = 0; i < m_model_table->m_size; ++i) + { + for (auto node = m_model_table->m_lookup_table[i]; node; node = node->m_next) + { + if (const auto table_idx = node->m_idx; table_idx < m_model_table->m_size) + { + if (const auto model = m_model_table->m_data[table_idx]; model && model->m_model_type == eModelType::Vehicle) + { + + } + } + } + }*/ + + class model_info + { + public: + static bool does_model_exist(const rage::joaat_t hash) + { + if (const auto model = model_info::get_model(hash); model) + return true; + return false; + } + + template + static T get_model(const rage::joaat_t hash) + { + const auto model_table = g_pointers->m_model_table; + for (auto i = model_table->m_lookup_table[hash % model_table->m_lookup_key]; i; i = i->m_next) + { + if (i->m_hash == hash) + { + if (const auto model = model_table->m_data[i->m_idx]; model) + { + return reinterpret_cast(model); + } + } + } + return nullptr; + } + + static CVehicleModelInfo* get_vehicle_model(const rage::joaat_t hash) + { + if (const auto model = model_info::get_model(hash); model && model->m_model_type == eModelType::Vehicle) + return model; + return nullptr; + } + + static bool is_model_of_type(const rage::joaat_t hash, const eModelType type) + { + if (const auto model = model_info::get_model(hash); model && model->m_model_type == type) + return true; + return false; + } + }; +} \ No newline at end of file