From deb723e6457794b4aa729a92ce670b59ec87c0f0 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Wed, 13 May 2020 05:38:22 +0200 Subject: [PATCH 1/3] fixed --- Client/mods/deathmatch/logic/CClientModel.h | 6 +++--- .../deathmatch/logic/CClientModelManager.cpp | 16 ++++++++++++++++ .../mods/deathmatch/logic/CClientModelManager.h | 4 ++++ .../logic/lua/CLuaFunctionDefs.Util.cpp | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Client/mods/deathmatch/logic/CClientModel.h b/Client/mods/deathmatch/logic/CClientModel.h index a39d36f954c..98f6b2932ca 100644 --- a/Client/mods/deathmatch/logic/CClientModel.h +++ b/Client/mods/deathmatch/logic/CClientModel.h @@ -28,12 +28,12 @@ class CClientModel CClientModel(CClientManager* pManager, int iModelID, eClientModelType eModelType); ~CClientModel(void); - int GetModelID(void) { return m_iModelID; }; - eClientModelType GetModelType(void) { return m_eModelType; }; + int GetModelID(void) const { return m_iModelID; }; + eClientModelType GetModelType(void) const { return m_eModelType; }; bool Allocate(void); bool Deallocate(void); void SetParentResource(CResource* pResource) { m_pParentResource = pResource; } - CResource* GetParentResource(void) { return m_pParentResource; } + CResource* GetParentResource(void) const { return m_pParentResource; } protected: CClientManager* m_pManager; diff --git a/Client/mods/deathmatch/logic/CClientModelManager.cpp b/Client/mods/deathmatch/logic/CClientModelManager.cpp index 745479acad1..4065b77a2c4 100644 --- a/Client/mods/deathmatch/logic/CClientModelManager.cpp +++ b/Client/mods/deathmatch/logic/CClientModelManager.cpp @@ -72,6 +72,22 @@ CClientModel* CClientModelManager::FindModelByID(int iModelID) return nullptr; } +std::vector CClientModelManager::GetModelsByType(const eClientModelType type, const unsigned int minModelID) +{ + std::vector found; + found.reserve(16); // just pre-allocate some space, because we know there might be a few models + + for (int i = minModelID; i < MAX_MODEL_ID; i++) + { + CClientModel* model = m_Models[i]; + if (model && model->GetModelType() == type) + { + found.push_back(model); + } + } + return found; +} + void CClientModelManager::DeallocateModelsAllocatedByResource(CResource* pResource) { for (ushort i = 0; i < MAX_MODEL_ID; i++) diff --git a/Client/mods/deathmatch/logic/CClientModelManager.h b/Client/mods/deathmatch/logic/CClientModelManager.h index 664caf875eb..c7734bbf4cd 100644 --- a/Client/mods/deathmatch/logic/CClientModelManager.h +++ b/Client/mods/deathmatch/logic/CClientModelManager.h @@ -13,6 +13,7 @@ class CClientModelManager; #pragma once #include +#include #include "CClientModel.h" #define MAX_MODEL_ID 20000 @@ -34,6 +35,9 @@ class CClientModelManager CClientModel* FindModelByID(int iModelID); + std::vector CClientModelManager::GetModelsByType(const eClientModelType type, const unsigned int minModelID = 0); + + void DeallocateModelsAllocatedByResource(CResource* pResource); private: diff --git a/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp b/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp index 1cef317c3d0..7216c47b508 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp +++ b/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp @@ -14,6 +14,8 @@ int CLuaFunctionDefs::GetValidPedModels(lua_State* luaVM) { int iIndex = 0; lua_newtable(luaVM); + + // gather GTASA default skins for (int i = 0; i <= 312; i++) { if (CClientPlayerManager::IsValidModel(i)) @@ -24,6 +26,18 @@ int CLuaFunctionDefs::GetValidPedModels(lua_State* luaVM) } } + // gather our, engineRequestModel skins + // start at 313, so we wont reinsert the same skin ids(sometimes 3 is allocated, and that would appear twice in the list if we would start at 0) + for (const CClientModel* model : m_pManager->GetModelManager()->GetModelsByType(eClientModelType::CCLIENTMODELPED, 313)) + { + lua_pushnumber(luaVM, ++iIndex); + lua_pushnumber(luaVM, model->GetModelID()); + lua_settable(luaVM, -3); + } + + // Another option could be to loop thru all MAX_MODELS skins, + // and check if they're valid, and if they're of the correct type, but i feel like thats pretty harsh, cosidering we can solve it like this + return 1; } From 2f09387539056861016f347ad526f7c7eb5ec41e Mon Sep 17 00:00:00 2001 From: Pirulax Date: Wed, 13 May 2020 18:23:29 +0200 Subject: [PATCH 2/3] made changes as suggested by sbx --- Client/mods/deathmatch/logic/CClientModelManager.cpp | 5 ++++- Client/mods/deathmatch/logic/CClientModelManager.h | 1 + Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp | 7 ++----- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Client/mods/deathmatch/logic/CClientModelManager.cpp b/Client/mods/deathmatch/logic/CClientModelManager.cpp index 4065b77a2c4..3361eda281b 100644 --- a/Client/mods/deathmatch/logic/CClientModelManager.cpp +++ b/Client/mods/deathmatch/logic/CClientModelManager.cpp @@ -30,6 +30,7 @@ void CClientModelManager::RemoveAll(void) { Remove(m_Models[i]); } + m_modelCount = 0; } void CClientModelManager::Add(CClientModel* pModel) @@ -37,6 +38,7 @@ void CClientModelManager::Add(CClientModel* pModel) if (m_Models[pModel->GetModelID()] == nullptr) { m_Models[pModel->GetModelID()] = pModel; + m_modelCount++; } } @@ -45,6 +47,7 @@ bool CClientModelManager::Remove(CClientModel* pModel) if (pModel && m_Models[pModel->GetModelID()] != nullptr) { m_Models[pModel->GetModelID()] = nullptr; + m_modelCount--; return true; } return false; @@ -75,7 +78,7 @@ CClientModel* CClientModelManager::FindModelByID(int iModelID) std::vector CClientModelManager::GetModelsByType(const eClientModelType type, const unsigned int minModelID) { std::vector found; - found.reserve(16); // just pre-allocate some space, because we know there might be a few models + found.reserve(m_modelCount); for (int i = minModelID; i < MAX_MODEL_ID; i++) { diff --git a/Client/mods/deathmatch/logic/CClientModelManager.h b/Client/mods/deathmatch/logic/CClientModelManager.h index c7734bbf4cd..4b8a958cd42 100644 --- a/Client/mods/deathmatch/logic/CClientModelManager.h +++ b/Client/mods/deathmatch/logic/CClientModelManager.h @@ -42,4 +42,5 @@ class CClientModelManager private: CClientModel* m_Models[MAX_MODEL_ID]; + unsigned int m_modelCount = 0; }; diff --git a/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp b/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp index 7216c47b508..f093ff4fa8c 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp +++ b/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp @@ -26,8 +26,8 @@ int CLuaFunctionDefs::GetValidPedModels(lua_State* luaVM) } } - // gather our, engineRequestModel skins - // start at 313, so we wont reinsert the same skin ids(sometimes 3 is allocated, and that would appear twice in the list if we would start at 0) + // gather our custom skin model IDs allocated with engineRequestModel + // (there might be some < 313 as well, and since we dont want duplicates, we start at 313, others are already included by the loop above) for (const CClientModel* model : m_pManager->GetModelManager()->GetModelsByType(eClientModelType::CCLIENTMODELPED, 313)) { lua_pushnumber(luaVM, ++iIndex); @@ -35,9 +35,6 @@ int CLuaFunctionDefs::GetValidPedModels(lua_State* luaVM) lua_settable(luaVM, -3); } - // Another option could be to loop thru all MAX_MODELS skins, - // and check if they're valid, and if they're of the correct type, but i feel like thats pretty harsh, cosidering we can solve it like this - return 1; } From ed35f81068e03595b2d0c17d11462d6a533dff9a Mon Sep 17 00:00:00 2001 From: Nikita Obrekht Date: Fri, 21 Aug 2020 19:52:18 +0200 Subject: [PATCH 3/3] Apply suggestions from code review --- Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp b/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp index f093ff4fa8c..873836847e4 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp +++ b/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp @@ -15,7 +15,7 @@ int CLuaFunctionDefs::GetValidPedModels(lua_State* luaVM) int iIndex = 0; lua_newtable(luaVM); - // gather GTASA default skins + // Gather GTASA default skins for (int i = 0; i <= 312; i++) { if (CClientPlayerManager::IsValidModel(i)) @@ -26,8 +26,8 @@ int CLuaFunctionDefs::GetValidPedModels(lua_State* luaVM) } } - // gather our custom skin model IDs allocated with engineRequestModel - // (there might be some < 313 as well, and since we dont want duplicates, we start at 313, others are already included by the loop above) + // Gather our custom skin model IDs allocated with engineRequestModel + // (there might be some < 313 as well, and since we don't want duplicates, we start at 313, others are already included by the loop above) for (const CClientModel* model : m_pManager->GetModelManager()->GetModelsByType(eClientModelType::CCLIENTMODELPED, 313)) { lua_pushnumber(luaVM, ++iIndex);