From 0f8cbc18de0c87aa28f7bdf18dc01f42179bdcd7 Mon Sep 17 00:00:00 2001 From: FileEX Date: Thu, 26 Sep 2024 18:36:03 +0200 Subject: [PATCH] Cleanup 1/2 --- Client/game_sa/C2DEffectSAInterface.h | 11 +- Client/game_sa/CModelInfoSA.cpp | 4 +- .../deathmatch/logic/CClient2DFXManager.cpp | 175 ++++-------------- .../deathmatch/logic/CClient2DFXManager.h | 2 +- .../logic/lua/CLuaFunctionParseHelpers.cpp | 3 +- .../logic/luadefs/CLuaEngineDefs.cpp | 4 +- .../deathmatch/logic/luadefs/CLuaEngineDefs.h | 4 +- Client/sdk/game/CModelInfo.h | 5 +- 8 files changed, 58 insertions(+), 150 deletions(-) diff --git a/Client/game_sa/C2DEffectSAInterface.h b/Client/game_sa/C2DEffectSAInterface.h index 90c1f8f1f5..624cb58757 100644 --- a/Client/game_sa/C2DEffectSAInterface.h +++ b/Client/game_sa/C2DEffectSAInterface.h @@ -288,16 +288,19 @@ static void PrepareTexturesForLightEffect(RwTexture*& coronaTex, RwTexture*& sha { using RwTextureDestroy = void(__cdecl*)(RwTexture*); - if (coronaTex) + if (coronaTex && coronaName) ((RwTextureDestroy)FUNC_RwTextureDestroy)(coronaTex); - if (shadowTex) + if (shadowTex && shadowName) ((RwTextureDestroy)FUNC_RwTextureDestroy)(shadowTex); } // Call RwReadTexture using RwReadTexture = RwTexture*(__cdecl*)(const char*, const char*); - coronaTex = ((RwReadTexture)FUNC_RwReadTexture)(coronaName, nullptr); - shadowTex = ((RwReadTexture)FUNC_RwReadTexture)(shadowName, nullptr); + if (coronaName) + coronaTex = ((RwReadTexture)FUNC_RwReadTexture)(coronaName, nullptr); + + if (shadowName) + shadowTex = ((RwReadTexture)FUNC_RwReadTexture)(shadowName, nullptr); // Call CTxdStore::PopCurrentTxd ((void(__cdecl*)())FUNC_PopCurrentTxd)(); diff --git a/Client/game_sa/CModelInfoSA.cpp b/Client/game_sa/CModelInfoSA.cpp index 5910e15d6e..6f5cd7ec78 100644 --- a/Client/game_sa/CModelInfoSA.cpp +++ b/Client/game_sa/CModelInfoSA.cpp @@ -2175,9 +2175,10 @@ bool CModelInfoSA::Reset2DFXEffects(bool removeCustomEffects) it = map.erase(it); } - // Clear both maps + // Clear maps map.clear(); ms_DefaultEffectsMap.clear(); + removedDefaultEffects.clear(); // Remove all custom effects if (removeCustomEffects) @@ -2352,7 +2353,6 @@ bool CModelInfoSA::RemoveAll2DFXEffects(bool includeDefault) return false; std::uint32_t numEffects = m_pInterface->ucNumOf2DEffects; - for (std::uint32_t i = 0; i < numEffects; i++) { C2DEffectSAInterface* effect = Get2DFXFromIndex(i); diff --git a/Client/mods/deathmatch/logic/CClient2DFXManager.cpp b/Client/mods/deathmatch/logic/CClient2DFXManager.cpp index 6c2079153e..5a8372d476 100644 --- a/Client/mods/deathmatch/logic/CClient2DFXManager.cpp +++ b/Client/mods/deathmatch/logic/CClient2DFXManager.cpp @@ -11,6 +11,7 @@ #include "CClient2DFXManager.h" #define mask(n) ((1 << (n)) - 1) +static constexpr std::size_t roadsignTextSize = 64; CClient2DFXManager::CClient2DFXManager(CClientManager* mainManager) : m_mainManager(mainManager) { @@ -68,134 +69,22 @@ bool CClient2DFXManager::Set2DFXProperties(C2DEffectSAInterface* effect, const e if (!effect) return false; - // Set properties - switch (effect->type) - { - case e2dEffectType::LIGHT: - { - t2dEffectLight& light = effect->effect.light; - - auto* drawDistance = MapFind(effectData, "drawDistance"); - light.coronaFarClip = std::get(*drawDistance); - - auto* lightRange = MapFind(effectData, "lightRange"); - light.pointLightRange = std::get(*lightRange); - - auto* coronaSize = MapFind(effectData, "coronaSize"); - light.coronaSize = std::get(*coronaSize); - - auto* shadowSize = MapFind(effectData, "shadowSize"); - light.shadowSize = std::get(*shadowSize); - - auto* shadowMultiplier = MapFind(effectData, "shadowMultiplier"); - light.shadowColorMultiplier = static_cast(std::get(*shadowMultiplier)); - - auto* showMode = MapFind(effectData, "showMode"); - e2dCoronaFlashType flashType; - StringToEnum(std::get(*showMode), flashType); - light.coronaFlashType = flashType; - - auto* coronaReflection = MapFind(effectData, "coronaReflection"); - light.coronaEnableReflection = std::get(*coronaReflection); - - auto* coronaFlareType = MapFind(effectData, "flareType"); - light.coronaFlareType = static_cast(std::get(*coronaFlareType)); - - auto* flags = MapFind(effectData, "flags"); - light.flags = static_cast(std::get(*flags)); - - auto* shadowZDistance = MapFind(effectData, "shadowDistance"); - light.shadowZDistance = static_cast(std::get(*shadowZDistance)); - - auto* offsetX = MapFind(effectData, "offsetX"); - light.offsetX = static_cast(std::get(*offsetX)); - - auto* offsetY = MapFind(effectData, "offsetY"); - light.offsetY = static_cast(std::get(*offsetY)); - - auto* offsetZ = MapFind(effectData, "offsetZ"); - light.offsetZ = static_cast(std::get(*offsetZ)); - - auto* color = MapFind(effectData, "color"); - unsigned long colorValue = static_cast(std::get(*color)); - light.color = RwColor{static_cast((colorValue >> 16) & mask(8)), static_cast((colorValue >> 8) & mask(8)), static_cast((colorValue >> 0) & mask(8)), static_cast((colorValue >> 24) & mask(8))}; + bool success = true; - auto* coronaTexture = MapFind(effectData, "coronaName"); - auto* shadowTexture = MapFind(effectData, "shadowName"); - PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, std::get(*coronaTexture).c_str(), std::get(*shadowTexture).c_str(), true); - break; - } - case e2dEffectType::PARTICLE: + for (const auto& [k, v] : effectData) + { + e2dEffectProperty property; + if (!StringToEnum(k, property)) { - auto* particleName = MapFind(effectData, "name"); - std::strncpy(effect->effect.particle.szName, std::get(*particleName).c_str(), sizeof(effect->effect.particle.szName) - 1); - - break; + success = false; + continue; } - case e2dEffectType::ROADSIGN: - { - t2dEffectRoadsign& roadsign = effect->effect.roadsign; - - auto* sizeX = MapFind(effectData, "sizeX"); - auto* sizeY = MapFind(effectData, "sizeY"); - roadsign.size = RwV2d{std::get(*sizeX), std::get(*sizeY)}; - auto* rotX = MapFind(effectData, "rotX"); - auto* rotY = MapFind(effectData, "rotY"); - auto* rotZ = MapFind(effectData, "rotZ"); - roadsign.rotation = RwV3d{std::get(*rotX), std::get(*rotY), std::get(*rotZ)}; - - auto* flags = MapFind(effectData, "flags"); - roadsign.flags = static_cast(std::get(*flags)); - - auto* text = MapFind(effectData, "text"); - auto* text2 = MapFind(effectData, "text2"); - auto* text3 = MapFind(effectData, "text3"); - auto* text4 = MapFind(effectData, "text4"); - - roadsign.text = static_cast(malloc(64)); - if (roadsign.text) - { - std::memcpy(roadsign.text, std::get(*text).c_str(), 4); - std::memcpy(roadsign.text + 4, std::get(*text2).c_str(), 4); - std::memcpy(roadsign.text + 8, std::get(*text3).c_str(), 4); - std::memcpy(roadsign.text + 12, std::get(*text4).c_str(), 4); - } - - roadsign.atomic = nullptr; - - break; - } - case e2dEffectType::ESCALATOR: - { - t2dEffectEscalator& escalator = effect->effect.escalator; - - auto* bottomX = MapFind(effectData, "bottomX"); - auto* bottomY = MapFind(effectData, "bottomY"); - auto* bottomZ = MapFind(effectData, "bottomZ"); - escalator.bottom = RwV3d{std::get(*bottomX), std::get(*bottomY), std::get(*bottomZ)}; - - auto* topX = MapFind(effectData, "topX"); - auto* topY = MapFind(effectData, "topY"); - auto* topZ = MapFind(effectData, "topZ"); - escalator.top = RwV3d{std::get(*topX), std::get(*topY), std::get(*topZ)}; - - auto* endX = MapFind(effectData, "endX"); - auto* endY = MapFind(effectData, "endY"); - auto* endZ = MapFind(effectData, "endZ"); - escalator.end = RwV3d{std::get(*endX), std::get(*endY), std::get(*endZ)}; - - auto* dir = MapFind(effectData, "direction"); - escalator.direction = static_cast(std::get(*dir)); - - break; - } - case e2dEffectType::SUN_GLARE: // This effect has no properties but works in MTA - default: - break; + if (!Set2DFXProperty(effect, property, v)) + success = false; } - return true; + return success; } effectDataMap CClient2DFXManager::Get2DFXProperties(C2DEffectSAInterface* effect) const @@ -276,7 +165,7 @@ effectDataMap CClient2DFXManager::Get2DFXProperties(C2DEffectSAInterface* effect return properties; } -bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2dEffectProperty& property, const std::variant& propertyValue) +bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2dEffectProperty& property, const std::variant& propertyValue) { if (!effect) return false; @@ -366,7 +255,7 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d } break; } - case e2dEffectProperty::CORONA_FLAGS: + case e2dEffectProperty::FLAGS: { if (std::holds_alternative(propertyValue)) { @@ -431,7 +320,7 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d e2dEffectTextureName coronaName; if (StringToEnum(std::get(propertyValue), coronaName)) { - PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, std::get(propertyValue).c_str(), light.shadowTex->name, true); + PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, std::get(propertyValue).c_str(), light.shadowTex ? light.shadowTex->name : nullptr, true); return true; } } @@ -445,7 +334,7 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d e2dEffectTextureName shadowName; if (StringToEnum(std::get(propertyValue), shadowName)) { - PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, light.coronaTex->name, std::get(propertyValue).c_str(), true); + PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, light.coronaTex ? light.coronaTex->name : nullptr, std::get(propertyValue).c_str(), true); return true; } } @@ -465,6 +354,8 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d if (std::holds_alternative(propertyValue)) { std::strncpy(effect->effect.particle.szName, std::get(propertyValue).c_str(), sizeof(effect->effect.particle.szName) - 1); + effect->effect.particle.szName[sizeof(effect->effect.particle.szName) - 1] = '\0'; + return true; } } @@ -527,7 +418,7 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d break; } - case e2dEffectProperty::ROADSIGN_FLAGS: + case e2dEffectProperty::FLAGS: { if (std::holds_alternative(propertyValue)) { @@ -542,9 +433,13 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d if (std::holds_alternative(propertyValue)) { if (!roadsign.text) - roadsign.text = static_cast(malloc(64)); + roadsign.text = static_cast(malloc(roadsignTextSize)); + + if (!roadsign.text) + break; std::memcpy(roadsign.text, std::get(propertyValue).c_str(), 4); + roadsign.text[roadsignTextSize - 1] = '\0'; return true; } @@ -555,9 +450,13 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d if (std::holds_alternative(propertyValue)) { if (!roadsign.text) - roadsign.text = static_cast(malloc(64)); + roadsign.text = static_cast(malloc(roadsignTextSize)); + + if (!roadsign.text) + break; std::memcpy(roadsign.text + 4, std::get(propertyValue).c_str(), 4); + roadsign.text[roadsignTextSize - 1] = '\0'; return true; } @@ -568,9 +467,13 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d if (std::holds_alternative(propertyValue)) { if (!roadsign.text) - roadsign.text = static_cast(malloc(64)); + roadsign.text = static_cast(malloc(roadsignTextSize)); + + if (!roadsign.text) + break; std::memcpy(roadsign.text + 8, std::get(propertyValue).c_str(), 4); + roadsign.text[roadsignTextSize - 1] = '\0'; return true; } @@ -581,9 +484,13 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d if (std::holds_alternative(propertyValue)) { if (!roadsign.text) - roadsign.text = static_cast(malloc(64)); + roadsign.text = static_cast(malloc(roadsignTextSize)); + + if (!roadsign.text) + break; std::memcpy(roadsign.text + 12, std::get(propertyValue).c_str(), 4); + roadsign.text[roadsignTextSize - 1] = '\0'; return true; } @@ -740,7 +647,7 @@ std::variant CClient2DFXManager::Get2DFXProperty(C2DEf return light.coronaEnableReflection; case e2dEffectProperty::FLARE_TYPE: return static_cast(light.coronaFlareType); - case e2dEffectProperty::CORONA_FLAGS: + case e2dEffectProperty::FLAGS: return static_cast(light.flags); case e2dEffectProperty::SHADOW_DISTANCE: return static_cast(light.shadowZDistance); @@ -786,7 +693,7 @@ std::variant CClient2DFXManager::Get2DFXProperty(C2DEf return roadsign.rotation.y; case e2dEffectProperty::ROT_Z: return roadsign.rotation.z; - case e2dEffectProperty::ROADSIGN_FLAGS: + case e2dEffectProperty::FLAGS: return static_cast(roadsign.flags); case e2dEffectProperty::TEXT: case e2dEffectProperty::TEXT_2: @@ -1018,7 +925,7 @@ const char* CClient2DFXManager::IsValidEffectData(const e2dEffectType& effectTyp return "Invalid \"endZ\" value"; auto* direction = MapFind(effectData, "direction"); - if (!direction || !std::holds_alternative(*direction) || (std::get(*direction) < 0 || std::get(*direction) > 1.0f)) + if (!direction || !std::holds_alternative(*direction) || (std::get(*direction) < 0.0f || std::get(*direction) > 1.0f)) return "Invalid \"direction\" value"; break; diff --git a/Client/mods/deathmatch/logic/CClient2DFXManager.h b/Client/mods/deathmatch/logic/CClient2DFXManager.h index 82e24a6566..5dd9220bcc 100644 --- a/Client/mods/deathmatch/logic/CClient2DFXManager.h +++ b/Client/mods/deathmatch/logic/CClient2DFXManager.h @@ -27,7 +27,7 @@ class CClient2DFXManager bool Set2DFXProperties(C2DEffectSAInterface* effect, const effectDataMap& effectData); effectDataMap Get2DFXProperties(C2DEffectSAInterface* effect) const; - bool Set2DFXProperty(C2DEffectSAInterface* effect, const e2dEffectProperty& property, const std::variant& propertyValue); + bool Set2DFXProperty(C2DEffectSAInterface* effect, const e2dEffectProperty& property, const std::variant& propertyValue); std::variant Get2DFXProperty(C2DEffectSAInterface* effect, const e2dEffectProperty& property); void Set2DFXPosition(C2DEffectSAInterface* effect, const CVector& position); diff --git a/Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp b/Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp index 83e014e3b8..0a3cf88e95 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp +++ b/Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp @@ -984,7 +984,6 @@ ADD_ENUM(e2dEffectProperty::SHADOW_MULT, "shadowMultiplier") ADD_ENUM(e2dEffectProperty::FLASH_TYPE, "showMode") ADD_ENUM(e2dEffectProperty::CORONA_REFLECTION, "coronaReflection") ADD_ENUM(e2dEffectProperty::FLARE_TYPE, "flareType") -ADD_ENUM(e2dEffectProperty::CORONA_FLAGS, "flags") ADD_ENUM(e2dEffectProperty::SHADOW_DISTANCE, "shadowDistance") ADD_ENUM(e2dEffectProperty::OFFSET_X, "offsetX") ADD_ENUM(e2dEffectProperty::OFFSET_Y, "offsetY") @@ -992,6 +991,7 @@ ADD_ENUM(e2dEffectProperty::OFFSET_Z, "offsetZ") ADD_ENUM(e2dEffectProperty::COLOR, "color") ADD_ENUM(e2dEffectProperty::CORONA_NAME, "coronaName") ADD_ENUM(e2dEffectProperty::SHADOW_NAME, "shadowName") +ADD_ENUM(e2dEffectProperty::FLAGS, "flags") ADD_ENUM(e2dEffectProperty::PRT_NAME, "name") @@ -1000,7 +1000,6 @@ ADD_ENUM(e2dEffectProperty::SIZE_Y, "sizeY") ADD_ENUM(e2dEffectProperty::ROT_X, "rotX") ADD_ENUM(e2dEffectProperty::ROT_Y, "rotY") ADD_ENUM(e2dEffectProperty::ROT_Z, "rotZ") -ADD_ENUM(e2dEffectProperty::ROADSIGN_FLAGS, "flags") ADD_ENUM(e2dEffectProperty::TEXT, "text1") ADD_ENUM(e2dEffectProperty::TEXT_2, "text2") ADD_ENUM(e2dEffectProperty::TEXT_3, "text3") diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp index b81178f433..69ebd9068f 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp @@ -2674,7 +2674,7 @@ bool CLuaEngineDefs::EngineSet2DFXProperties(CClient2DFX* effect, effectDataMap return m_p2DFXManager->Set2DFXProperties(effect->Get2DFX(), effectData); } -bool CLuaEngineDefs::EngineSetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property, std::variant propertyValue) +bool CLuaEngineDefs::EngineSetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property, std::variant propertyValue) { if (!CClient2DFXManager::IsValidModel(modelID)) throw std::invalid_argument("Invalid model ID"); @@ -2694,7 +2694,7 @@ bool CLuaEngineDefs::EngineSetModel2DFXProperty(std::uint32_t modelID, std::uint return m_p2DFXManager->Set2DFXProperty(effect, property, propertyValue); } -bool CLuaEngineDefs::EngineSet2DFXProperty(CClient2DFX* effect, e2dEffectProperty property, std::variant propertyValue) +bool CLuaEngineDefs::EngineSet2DFXProperty(CClient2DFX* effect, e2dEffectProperty property, std::variant propertyValue) { return m_p2DFXManager->Set2DFXProperty(effect->Get2DFX(), property, propertyValue); } diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h index e64d571d6c..b844f30355 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h @@ -102,8 +102,8 @@ class CLuaEngineDefs : public CLuaDefs // Set functions static bool EngineSetModel2DFXProperties(std::uint32_t modelID, std::uint32_t index, effectDataMap effectData); static bool EngineSet2DFXProperties(CClient2DFX* effect, effectDataMap effectData); - static bool EngineSetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property, std::variant propertyValue); - static bool EngineSet2DFXProperty(CClient2DFX* effect, e2dEffectProperty property, std::variant propertyValue); + static bool EngineSetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property, std::variant propertyValue); + static bool EngineSet2DFXProperty(CClient2DFX* effect, e2dEffectProperty property, std::variant propertyValue); static bool EngineSetModel2DFXPosition(std::uint32_t modelID, std::uint32_t index, CVector position); static bool EngineSet2DFXPosition(CClient2DFX* effect, CVector position); diff --git a/Client/sdk/game/CModelInfo.h b/Client/sdk/game/CModelInfo.h index 0e51570673..bb1036d498 100644 --- a/Client/sdk/game/CModelInfo.h +++ b/Client/sdk/game/CModelInfo.h @@ -122,7 +122,7 @@ enum class e2dCoronaFlashType : std::uint8_t enum class e2dEffectProperty { // light properties - FAR_CLIP_DISTANCE, + FAR_CLIP_DISTANCE = 0, LIGHT_RANGE, CORONA_SIZE, SHADOW_SIZE, @@ -130,7 +130,6 @@ enum class e2dEffectProperty FLASH_TYPE, CORONA_REFLECTION, FLARE_TYPE, - CORONA_FLAGS, SHADOW_DISTANCE, OFFSET_X, OFFSET_Y, @@ -138,6 +137,7 @@ enum class e2dEffectProperty COLOR, CORONA_NAME, SHADOW_NAME, + FLAGS, // for light & roadsign // particle properties PRT_NAME, @@ -148,7 +148,6 @@ enum class e2dEffectProperty ROT_X, ROT_Y, ROT_Z, - ROADSIGN_FLAGS, TEXT, TEXT_2, TEXT_3,