From db1481d2198e4cdb0a419c1c5c8f4b15e27ef687 Mon Sep 17 00:00:00 2001 From: Silent Date: Mon, 10 Apr 2017 13:18:23 +0200 Subject: [PATCH] Support arbitrary amount of numberplates --- SilentPatchSA/ModelInfoSA.cpp | 44 +++++++++++++++++++---------------- SilentPatchSA/ModelInfoSA.h | 23 ++++++++++++++---- SilentPatchSA/VehicleSA.cpp | 15 +++++------- 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/SilentPatchSA/ModelInfoSA.cpp b/SilentPatchSA/ModelInfoSA.cpp index 85400a2..f140d8a 100644 --- a/SilentPatchSA/ModelInfoSA.cpp +++ b/SilentPatchSA/ModelInfoSA.cpp @@ -88,20 +88,17 @@ void CVehicleModelInfo::SetCarCustomPlate() m_plateText[0] = '\0'; m_nPlateType = -1; - m_apPlateMaterials = new RpMaterial* [2*CCustomCarPlateMgr::NUM_MAX_PLATES]; - std::fill_n( m_apPlateMaterials, 2*CCustomCarPlateMgr::NUM_MAX_PLATES, nullptr ); + m_apPlateMaterials = new PlateMaterialsData; CCustomCarPlateMgr::SetupClump(reinterpret_cast(pRwObject), m_apPlateMaterials); } -void CCustomCarPlateMgr::PollPlates( RpClump* clump, RpMaterial** materials ) +void CCustomCarPlateMgr::PollPlates( RpClump* clump, PlateMaterialsData* materials ) { - RpMaterial** carplates = materials; - RpMaterial** carpbacks = materials+NUM_MAX_PLATES; + std::vector carplates; + std::vector carpbacks; - int numCarplates = 0, numCarpbacks = 0; - - RpClumpForAllAtomics( clump, [carplates, carpbacks, &numCarplates, &numCarpbacks] ( RpAtomic* atomic ) -> RpAtomic* { + RpClumpForAllAtomics( clump, [&] ( RpAtomic* atomic ) -> RpAtomic* { RpGeometryForAllMaterials( RpAtomicGetGeometry(atomic), [&] ( RpMaterial* material ) -> RpMaterial* { if ( RwTexture* texture = RpMaterialGetTexture(material) ) { @@ -109,19 +106,11 @@ void CCustomCarPlateMgr::PollPlates( RpClump* clump, RpMaterial** materials ) { if ( _stricmp( texName, "carplate" ) == 0 ) { - assert(numCarplates < NUM_MAX_PLATES); - if ( numCarplates < NUM_MAX_PLATES ) - { - carplates[numCarplates++] = material; - } + carplates.push_back( material ); } else if ( _stricmp( texName, "carpback" ) == 0 ) { - assert(numCarpbacks < NUM_MAX_PLATES); - if ( numCarpbacks < NUM_MAX_PLATES ) - { - carpbacks[numCarpbacks++] = material; - } + carpbacks.push_back( material ); } } } @@ -130,14 +119,29 @@ void CCustomCarPlateMgr::PollPlates( RpClump* clump, RpMaterial** materials ) } ); return atomic; } ); + + materials->m_numPlates = carplates.size(); + materials->m_numPlatebacks = carpbacks.size(); + + if ( materials->m_numPlates > 0 ) + { + materials->m_plates = new RpMaterial* [materials->m_numPlates]; + std::copy( carplates.begin(), carplates.end(), stdext::checked_array_iterator(materials->m_plates, materials->m_numPlates) ); + } + + if ( materials->m_numPlatebacks > 0 ) + { + materials->m_platebacks = new RpMaterial* [materials->m_numPlatebacks]; + std::copy( carpbacks.begin(), carpbacks.end(), stdext::checked_array_iterator(materials->m_platebacks, materials->m_numPlatebacks) ); + } } -void CCustomCarPlateMgr::SetupClump(RpClump* pClump, RpMaterial** pMatsArray) +void CCustomCarPlateMgr::SetupClump(RpClump* pClump, PlateMaterialsData* pMatsArray) { PollPlates( pClump, pMatsArray ); } -void CCustomCarPlateMgr::SetupClumpAfterVehicleUpgrade(RpClump* pClump, RpMaterial** pMatsArray, signed char nDesign) +void CCustomCarPlateMgr::SetupClumpAfterVehicleUpgrade(RpClump* pClump, PlateMaterialsData* pMatsArray, signed char nDesign) { UNREFERENCED_PARAMETER(nDesign); if ( pMatsArray != nullptr ) diff --git a/SilentPatchSA/ModelInfoSA.h b/SilentPatchSA/ModelInfoSA.h index 6643fa0..0d65779 100644 --- a/SilentPatchSA/ModelInfoSA.h +++ b/SilentPatchSA/ModelInfoSA.h @@ -222,12 +222,27 @@ public: virtual void SetClump(RpClump* pClump); }; +struct PlateMaterialsData // Added in SilentPatch +{ + RpMaterial** m_plates = nullptr; + RpMaterial** m_platebacks = nullptr; + + size_t m_numPlates = 0; + size_t m_numPlatebacks = 0; + + ~PlateMaterialsData() + { + delete m_plates; + delete m_platebacks; + } +}; + class NOVMT CVehicleModelInfo : public CClumpModelInfo { public: static const size_t PLATE_TEXT_LEN = 8; - RpMaterial** m_apPlateMaterials; // Changed in SilentPatchh + PlateMaterialsData* m_apPlateMaterials; // Changed in SilentPatch char m_plateText[PLATE_TEXT_LEN]; char field_30; signed char m_nPlateType; @@ -321,11 +336,11 @@ public: static signed char (*GetMapRegionPlateDesign)(); static void (*SetupMaterialPlatebackTexture)(RpMaterial* pMaterial, signed char nDesign); - static void SetupClump(RpClump* pClump, RpMaterial** pMatsArray); - static void SetupClumpAfterVehicleUpgrade(RpClump* pClump, RpMaterial** pMatsArray, signed char nDesign); + static void SetupClump(RpClump* pClump, PlateMaterialsData* pMatsArray); + static void SetupClumpAfterVehicleUpgrade(RpClump* pClump, PlateMaterialsData* pMatsArray, signed char nDesign); private: - static void PollPlates( RpClump* clump, RpMaterial** materials ); + static void PollPlates( RpClump* clump, PlateMaterialsData* materials ); }; static_assert(sizeof(CBaseModelInfo) == 0x20, "Wrong size: CBaseModelInfo"); diff --git a/SilentPatchSA/VehicleSA.cpp b/SilentPatchSA/VehicleSA.cpp index 8a68c3d..5d33431 100644 --- a/SilentPatchSA/VehicleSA.cpp +++ b/SilentPatchSA/VehicleSA.cpp @@ -124,17 +124,14 @@ bool CVehicle::CustomCarPlate_TextureCreate(CVehicleModelInfo* pModelInfo) void CVehicle::CustomCarPlate_BeforeRenderingStart(CVehicleModelInfo* pModelInfo) { - for ( ptrdiff_t i = 0; i < CCustomCarPlateMgr::NUM_MAX_PLATES; i++ ) + for ( size_t i = 0; i < pModelInfo->m_apPlateMaterials->m_numPlates; i++ ) { - if ( pModelInfo->m_apPlateMaterials[i] != nullptr ) - { - RpMaterialSetTexture(pModelInfo->m_apPlateMaterials[i], PlateTexture); - } + RpMaterialSetTexture(pModelInfo->m_apPlateMaterials->m_plates[i], PlateTexture); + } - if ( pModelInfo->m_apPlateMaterials[CCustomCarPlateMgr::NUM_MAX_PLATES+i] != nullptr ) - { - CCustomCarPlateMgr::SetupMaterialPlatebackTexture(pModelInfo->m_apPlateMaterials[CCustomCarPlateMgr::NUM_MAX_PLATES+i], PlateDesign); - } + for ( size_t i = 0; i < pModelInfo->m_apPlateMaterials->m_numPlatebacks; i++ ) + { + CCustomCarPlateMgr::SetupMaterialPlatebackTexture(pModelInfo->m_apPlateMaterials->m_platebacks[i], PlateDesign); } }