|
| 1 | +/***************************************************************************** |
| 2 | + * |
| 3 | + * PROJECT: Multi Theft Auto v1.0 |
| 4 | + * LICENSE: See LICENSE in the top level directory |
| 5 | + * FILE: game_sa/CColModelGuard.h |
| 6 | + * PURPOSE: Wrapper for collision model access with automatic reference counting |
| 7 | + * |
| 8 | + * Multi Theft Auto is available from https://www.multitheftauto.com/ |
| 9 | + * |
| 10 | + *****************************************************************************/ |
| 11 | + |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#include "CModelInfoSA.h" |
| 15 | + |
| 16 | + |
| 17 | +// Guard: Protects collision model from streaming GC |
| 18 | +class CColModelGuard |
| 19 | +{ |
| 20 | +public: |
| 21 | + |
| 22 | + // Increment ref count for pModelInfo's collision |
| 23 | + explicit CColModelGuard(CModelInfoSA* pModelInfo) : m_pModelInfo(pModelInfo), m_bValid(false), m_pColModel(nullptr), m_pColData(nullptr) |
| 24 | + { |
| 25 | + if (!m_pModelInfo) |
| 26 | + return; |
| 27 | + |
| 28 | + // Check and protect collision atomically |
| 29 | + if (m_pModelInfo->IsCollisionLoaded()) |
| 30 | + { |
| 31 | + m_pModelInfo->AddColRef(); |
| 32 | + bool bRefAdded = true; |
| 33 | + |
| 34 | + try |
| 35 | + { |
| 36 | + // Cache pointers while protected |
| 37 | + CBaseModelInfoSAInterface* pInterface = m_pModelInfo->GetInterface(); |
| 38 | + if (pInterface && pInterface->pColModel) |
| 39 | + { |
| 40 | + m_pColModel = pInterface->pColModel; |
| 41 | + m_pColData = m_pColModel->m_data; |
| 42 | + m_bValid = (m_pColData != nullptr); |
| 43 | + } |
| 44 | + |
| 45 | + // Release ref if validation failed |
| 46 | + if (!m_bValid) |
| 47 | + { |
| 48 | + m_pModelInfo->RemoveColRef(); |
| 49 | + bRefAdded = false; |
| 50 | + } |
| 51 | + } |
| 52 | + catch (...) |
| 53 | + { |
| 54 | + // Release reference on exception |
| 55 | + if (bRefAdded) |
| 56 | + m_pModelInfo->RemoveColRef(); |
| 57 | + throw; |
| 58 | + } |
| 59 | + |
| 60 | + // Prevent double-release in destructor |
| 61 | + if (!bRefAdded) |
| 62 | + m_pModelInfo = nullptr; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Decrement ref count on destruction |
| 67 | + ~CColModelGuard() noexcept |
| 68 | + { |
| 69 | + if (m_bValid && m_pModelInfo) |
| 70 | + { |
| 71 | + m_pModelInfo->RemoveColRef(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Prevent copying |
| 76 | + CColModelGuard(const CColModelGuard&) = delete; |
| 77 | + CColModelGuard& operator=(const CColModelGuard&) = delete; |
| 78 | + |
| 79 | + // Allow moving |
| 80 | + CColModelGuard(CColModelGuard&& other) noexcept |
| 81 | + : m_pModelInfo(other.m_pModelInfo), m_bValid(other.m_bValid), m_pColModel(other.m_pColModel), m_pColData(other.m_pColData) |
| 82 | + { |
| 83 | + // Transfer ownership |
| 84 | + other.m_pModelInfo = nullptr; |
| 85 | + other.m_bValid = false; |
| 86 | + other.m_pColModel = nullptr; |
| 87 | + other.m_pColData = nullptr; |
| 88 | + } |
| 89 | + |
| 90 | + CColModelGuard& operator=(CColModelGuard&& other) noexcept |
| 91 | + { |
| 92 | + if (this != &other) |
| 93 | + { |
| 94 | + // Release current, transfer from source |
| 95 | + if (m_bValid && m_pModelInfo) |
| 96 | + m_pModelInfo->RemoveColRef(); |
| 97 | + |
| 98 | + // Transfer ownership from source |
| 99 | + m_pModelInfo = other.m_pModelInfo; |
| 100 | + m_bValid = other.m_bValid; |
| 101 | + m_pColModel = other.m_pColModel; |
| 102 | + m_pColData = other.m_pColData; |
| 103 | + |
| 104 | + // Prevent double-release |
| 105 | + other.m_pModelInfo = nullptr; |
| 106 | + other.m_bValid = false; |
| 107 | + other.m_pColModel = nullptr; |
| 108 | + other.m_pColData = nullptr; |
| 109 | + } |
| 110 | + return *this; |
| 111 | + } |
| 112 | + |
| 113 | + // Check if collision is protected |
| 114 | + [[nodiscard]] bool IsValid() const noexcept { return m_bValid; } |
| 115 | + |
| 116 | + // Get collision data (check IsValid first) |
| 117 | + // Returns cached pointer or nullptr |
| 118 | + [[nodiscard]] CColDataSA* GetColData() const noexcept { return m_bValid ? m_pColData : nullptr; } |
| 119 | + |
| 120 | + // Get collision model (check IsValid first) |
| 121 | + // Returns cached pointer or nullptr |
| 122 | + [[nodiscard]] CColModelSAInterface* GetColModel() const noexcept { return m_bValid ? m_pColModel : nullptr; } |
| 123 | + |
| 124 | + explicit operator bool() const noexcept { return m_bValid; } |
| 125 | + |
| 126 | +private: |
| 127 | + CModelInfoSA* m_pModelInfo; |
| 128 | + bool m_bValid; |
| 129 | + CColModelSAInterface* m_pColModel; // Cached during construction |
| 130 | + CColDataSA* m_pColData; // Cached during construction |
| 131 | +}; |
0 commit comments