From d28ad95d5ce5199f838957d9122913e07b7dd80b Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 29 Jun 2014 22:21:18 +0200 Subject: [PATCH] A fix for numplates with tuningparts --- SilentPatch/ModelInfoSA.cpp | 94 +++++++++++++++++++++++++++++++++---- SilentPatch/ModelInfoSA.h | 5 +- SilentPatch/Vehicle.cpp | 1 + SilentPatch/dllmain.cpp | 1 + 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/SilentPatch/ModelInfoSA.cpp b/SilentPatch/ModelInfoSA.cpp index 149d8b3..6e6911b 100644 --- a/SilentPatch/ModelInfoSA.cpp +++ b/SilentPatch/ModelInfoSA.cpp @@ -42,7 +42,7 @@ void CVehicleModelInfo::SetCarCustomPlate() for ( int i = 0; i < 2*NUM_MAX_PLATES; i++ ) m_apPlateMaterials[i] = nullptr; -CCustomCarPlateMgr::SetupClump(reinterpret_cast(pRwObject), m_apPlateMaterials); + CCustomCarPlateMgr::SetupClump(reinterpret_cast(pRwObject), m_apPlateMaterials); } RpAtomic* CVehicleModelInfo::GetEditableMaterialListCB(RpAtomic* pAtomic, void* pData) @@ -66,32 +66,58 @@ RpMaterial* CVehicleModelInfo::GetEditableMaterialListCB(RpMaterial* pMaterial, return pMaterial; } +// TODO: FIX IT static RpMaterial* PollPlateData(RpMaterial* pMaterial, void* pData) { if ( RwTexture* pTexture = RpMaterialGetTexture(pMaterial) ) { - if ( RwTextureGetName(pTexture) ) + const char* pTexName = RwTextureGetName(pTexture); + if ( pTexName ) { - if ( !_strnicmp(RwTextureGetName(pTexture), "carplate", 8) ) + if ( !_strnicmp(pTexName, "carplate", 8) ) { auto& pCallbackData = *static_cast*>(pData); assert(std::get<2>(pCallbackData) < NUM_MAX_PLATES); - if ( std::get<2>(pCallbackData)++ < NUM_MAX_PLATES ) - *(std::get<0>(pCallbackData)++) = pMaterial; + if ( std::get<2>(pCallbackData) < NUM_MAX_PLATES ) + { + // Append data + for ( auto ptr = std::get<0>(pCallbackData); *ptr; ptr++ ) + { + if ( *ptr == pMaterial ) + return pMaterial; + } + + while ( std::get<0>(pCallbackData)[std::get<2>(pCallbackData)] ) + std::get<2>(pCallbackData)++; + + std::get<0>(pCallbackData)[std::get<2>(pCallbackData)++] = pMaterial; + } } - else if ( !_strnicmp(RwTextureGetName(pTexture), "carpback", 8) ) + else if ( !_strnicmp(pTexName, "carpback", 8) ) { auto& pCallbackData = *static_cast*>(pData); assert(std::get<3>(pCallbackData) < NUM_MAX_PLATES); - if ( std::get<3>(pCallbackData)++ < NUM_MAX_PLATES ) - *(std::get<1>(pCallbackData)++) = pMaterial; + if ( std::get<3>(pCallbackData) < NUM_MAX_PLATES ) + { + // Append data + for ( auto ptr = std::get<0>(pCallbackData); *ptr; ptr++ ) + { + if ( *ptr == pMaterial ) + return pMaterial; + } + + while ( std::get<1>(pCallbackData)[std::get<3>(pCallbackData)] ) + std::get<3>(pCallbackData)++; + + std::get<1>(pCallbackData)[std::get<3>(pCallbackData)] = pMaterial; + + } } } } - return pMaterial; } @@ -101,10 +127,60 @@ static RpAtomic* PollPlateData(RpAtomic* pAtomic, void* pData) return pAtomic; } +static RpMaterial* SetPlateData(RpMaterial* pMaterial, void* pData) +{ + if ( RwTexture* pTexture = RpMaterialGetTexture(pMaterial) ) + { + if ( RwTextureGetName(pTexture) ) + { + if ( !_strnicmp(RwTextureGetName(pTexture), "carplate", 8) ) + { + auto& pCallbackData = *static_cast*>(pData); + + RpMaterialSetTexture(pMaterial, pCallbackData.first); + } + else if ( !_strnicmp(RwTextureGetName(pTexture), "carpback", 8) ) + { + auto& pCallbackData = *static_cast*>(pData); + + CCustomCarPlateMgr::SetupMaterialPlatebackTexture(pMaterial, pCallbackData.second); + } + } + } + + + return pMaterial; +} + +static RpAtomic* SetPlateData(RpAtomic* pAtomic, void* pData) +{ + RpGeometryForAllMaterials(RpAtomicGetGeometry(pAtomic), SetPlateData, pData); + return pAtomic; +} + void CCustomCarPlateMgr::SetupClump(RpClump* pClump, RpMaterial** pMatsArray) { // Split pMatsArray std::tuple CallbackData = std::make_tuple(pMatsArray, pMatsArray+NUM_MAX_PLATES, 0, 0); RpClumpForAllAtomics(pClump, PollPlateData, &CallbackData); +} + +void CCustomCarPlateMgr::SetupClumpAfterVehicleUpgrade(RpClump* pClump, RpMaterial** pMatsArray, signed char nDesign) +{ + UNREFERENCED_PARAMETER(nDesign); + if ( pMatsArray ) + { + // Split pMatsArray + std::tuple CallbackData = std::make_tuple(pMatsArray, pMatsArray+NUM_MAX_PLATES, 0, 0); + + RpClumpForAllAtomics(pClump, PollPlateData, &CallbackData); + } +} + +void CCustomCarPlateMgr::SetupPlates(RpClump* pClump, RwTexture* pTexture, signed char nDesign) +{ + std::pair CallbackData = std::make_pair(pTexture, nDesign); + + RpClumpForAllAtomics(pClump, SetPlateData, &CallbackData); } \ No newline at end of file diff --git a/SilentPatch/ModelInfoSA.h b/SilentPatch/ModelInfoSA.h index da03f5d..3ae962e 100644 --- a/SilentPatch/ModelInfoSA.h +++ b/SilentPatch/ModelInfoSA.h @@ -281,7 +281,7 @@ public: static RpMaterial* GetEditableMaterialListCB(RpMaterial* pMaterial, void* pData); }; -#define NUM_MAX_PLATES 6 +#define NUM_MAX_PLATES 12 class CCustomCarPlateMgr { @@ -298,6 +298,9 @@ public: 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 SetupPlates(RpClump* pClump, RwTexture* pTexture, signed char nDesign); }; static_assert(sizeof(CBaseModelInfo) == 0x20, "Wrong size: CBaseModelInfo"); diff --git a/SilentPatch/Vehicle.cpp b/SilentPatch/Vehicle.cpp index 316e975..6010831 100644 --- a/SilentPatch/Vehicle.cpp +++ b/SilentPatch/Vehicle.cpp @@ -48,6 +48,7 @@ bool CVehicle::CustomCarPlate_TextureCreate(CVehicleModelInfo* pModelInfo) void CVehicle::CustomCarPlate_BeforeRenderingStart(CVehicleModelInfo* pModelInfo) { + //CCustomCarPlateMgr::SetupPlates(reinterpret_cast(pModelInfo->pRwObject), PlateTexture, PlateDesign); for ( int i = 0; i < NUM_MAX_PLATES; i++ ) { if ( pModelInfo->m_apPlateMaterials[i] ) diff --git a/SilentPatch/dllmain.cpp b/SilentPatch/dllmain.cpp index 3037af1..6d5f5d2 100644 --- a/SilentPatch/dllmain.cpp +++ b/SilentPatch/dllmain.cpp @@ -2392,6 +2392,7 @@ __forceinline void Patch_SA_10() InjectMethodVP(0x4C9660, CVehicleModelInfo::SetCarCustomPlate, PATCH_NOTHING); InjectMethodVP(0x6D6A58, CVehicle::CustomCarPlate_TextureCreate, PATCH_NOTHING); InjectMethodVP(0x6D651C, CVehicle::CustomCarPlate_BeforeRenderingStart, PATCH_NOTHING); + InjectHook(0x6FDFE0, CCustomCarPlateMgr::SetupClumpAfterVehicleUpgrade, PATCH_JUMP); //InjectMethodVP(0x6D0E53, CVehicle::CustomCarPlate_AfterRenderingStop, PATCH_NOTHING); Nop(0x6D6517, 2);