Skip to content

Commit

Permalink
Cleanup 1/2
Browse files Browse the repository at this point in the history
  • Loading branch information
FileEX committed Sep 26, 2024
1 parent a26b3e6 commit 0f8cbc1
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 150 deletions.
11 changes: 7 additions & 4 deletions Client/game_sa/C2DEffectSAInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)();
Expand Down
4 changes: 2 additions & 2 deletions Client/game_sa/CModelInfoSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
175 changes: 41 additions & 134 deletions Client/mods/deathmatch/logic/CClient2DFXManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<float>(*drawDistance);

auto* lightRange = MapFind(effectData, "lightRange");
light.pointLightRange = std::get<float>(*lightRange);

auto* coronaSize = MapFind(effectData, "coronaSize");
light.coronaSize = std::get<float>(*coronaSize);

auto* shadowSize = MapFind(effectData, "shadowSize");
light.shadowSize = std::get<float>(*shadowSize);

auto* shadowMultiplier = MapFind(effectData, "shadowMultiplier");
light.shadowColorMultiplier = static_cast<std::uint8_t>(std::get<float>(*shadowMultiplier));

auto* showMode = MapFind(effectData, "showMode");
e2dCoronaFlashType flashType;
StringToEnum(std::get<std::string>(*showMode), flashType);
light.coronaFlashType = flashType;

auto* coronaReflection = MapFind(effectData, "coronaReflection");
light.coronaEnableReflection = std::get<bool>(*coronaReflection);

auto* coronaFlareType = MapFind(effectData, "flareType");
light.coronaFlareType = static_cast<std::uint8_t>(std::get<float>(*coronaFlareType));

auto* flags = MapFind(effectData, "flags");
light.flags = static_cast<std::uint16_t>(std::get<float>(*flags));

auto* shadowZDistance = MapFind(effectData, "shadowDistance");
light.shadowZDistance = static_cast<std::int8_t>(std::get<float>(*shadowZDistance));

auto* offsetX = MapFind(effectData, "offsetX");
light.offsetX = static_cast<std::int8_t>(std::get<float>(*offsetX));

auto* offsetY = MapFind(effectData, "offsetY");
light.offsetY = static_cast<std::int8_t>(std::get<float>(*offsetY));

auto* offsetZ = MapFind(effectData, "offsetZ");
light.offsetZ = static_cast<std::int8_t>(std::get<float>(*offsetZ));

auto* color = MapFind(effectData, "color");
unsigned long colorValue = static_cast<unsigned long>(std::get<float>(*color));
light.color = RwColor{static_cast<std::uint8_t>((colorValue >> 16) & mask(8)), static_cast<std::uint8_t>((colorValue >> 8) & mask(8)), static_cast<std::uint8_t>((colorValue >> 0) & mask(8)), static_cast<std::uint8_t>((colorValue >> 24) & mask(8))};
bool success = true;

auto* coronaTexture = MapFind(effectData, "coronaName");
auto* shadowTexture = MapFind(effectData, "shadowName");
PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, std::get<std::string>(*coronaTexture).c_str(), std::get<std::string>(*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<std::string>(*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<float>(*sizeX), std::get<float>(*sizeY)};

auto* rotX = MapFind(effectData, "rotX");
auto* rotY = MapFind(effectData, "rotY");
auto* rotZ = MapFind(effectData, "rotZ");
roadsign.rotation = RwV3d{std::get<float>(*rotX), std::get<float>(*rotY), std::get<float>(*rotZ)};

auto* flags = MapFind(effectData, "flags");
roadsign.flags = static_cast<std::uint8_t>(std::get<float>(*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<char*>(malloc(64));
if (roadsign.text)
{
std::memcpy(roadsign.text, std::get<std::string>(*text).c_str(), 4);
std::memcpy(roadsign.text + 4, std::get<std::string>(*text2).c_str(), 4);
std::memcpy(roadsign.text + 8, std::get<std::string>(*text3).c_str(), 4);
std::memcpy(roadsign.text + 12, std::get<std::string>(*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<float>(*bottomX), std::get<float>(*bottomY), std::get<float>(*bottomZ)};

auto* topX = MapFind(effectData, "topX");
auto* topY = MapFind(effectData, "topY");
auto* topZ = MapFind(effectData, "topZ");
escalator.top = RwV3d{std::get<float>(*topX), std::get<float>(*topY), std::get<float>(*topZ)};

auto* endX = MapFind(effectData, "endX");
auto* endY = MapFind(effectData, "endY");
auto* endZ = MapFind(effectData, "endZ");
escalator.end = RwV3d{std::get<float>(*endX), std::get<float>(*endY), std::get<float>(*endZ)};

auto* dir = MapFind(effectData, "direction");
escalator.direction = static_cast<std::uint8_t>(std::get<float>(*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
Expand Down Expand Up @@ -276,7 +165,7 @@ effectDataMap CClient2DFXManager::Get2DFXProperties(C2DEffectSAInterface* effect
return properties;
}

bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2dEffectProperty& property, const std::variant<float, bool, std::string>& propertyValue)
bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2dEffectProperty& property, const std::variant<bool, float, std::string>& propertyValue)
{
if (!effect)
return false;
Expand Down Expand Up @@ -366,7 +255,7 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d
}
break;
}
case e2dEffectProperty::CORONA_FLAGS:
case e2dEffectProperty::FLAGS:
{
if (std::holds_alternative<float>(propertyValue))
{
Expand Down Expand Up @@ -431,7 +320,7 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d
e2dEffectTextureName coronaName;
if (StringToEnum(std::get<std::string>(propertyValue), coronaName))
{
PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, std::get<std::string>(propertyValue).c_str(), light.shadowTex->name, true);
PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, std::get<std::string>(propertyValue).c_str(), light.shadowTex ? light.shadowTex->name : nullptr, true);
return true;
}
}
Expand All @@ -445,7 +334,7 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d
e2dEffectTextureName shadowName;
if (StringToEnum(std::get<std::string>(propertyValue), shadowName))
{
PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, light.coronaTex->name, std::get<std::string>(propertyValue).c_str(), true);
PrepareTexturesForLightEffect(light.coronaTex, light.shadowTex, light.coronaTex ? light.coronaTex->name : nullptr, std::get<std::string>(propertyValue).c_str(), true);
return true;
}
}
Expand All @@ -465,6 +354,8 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d
if (std::holds_alternative<std::string>(propertyValue))
{
std::strncpy(effect->effect.particle.szName, std::get<std::string>(propertyValue).c_str(), sizeof(effect->effect.particle.szName) - 1);
effect->effect.particle.szName[sizeof(effect->effect.particle.szName) - 1] = '\0';

return true;
}
}
Expand Down Expand Up @@ -527,7 +418,7 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d

break;
}
case e2dEffectProperty::ROADSIGN_FLAGS:
case e2dEffectProperty::FLAGS:
{
if (std::holds_alternative<float>(propertyValue))
{
Expand All @@ -542,9 +433,13 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d
if (std::holds_alternative<std::string>(propertyValue))
{
if (!roadsign.text)
roadsign.text = static_cast<char*>(malloc(64));
roadsign.text = static_cast<char*>(malloc(roadsignTextSize));

if (!roadsign.text)
break;

std::memcpy(roadsign.text, std::get<std::string>(propertyValue).c_str(), 4);
roadsign.text[roadsignTextSize - 1] = '\0';
return true;
}

Expand All @@ -555,9 +450,13 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d
if (std::holds_alternative<std::string>(propertyValue))
{
if (!roadsign.text)
roadsign.text = static_cast<char*>(malloc(64));
roadsign.text = static_cast<char*>(malloc(roadsignTextSize));

if (!roadsign.text)
break;

std::memcpy(roadsign.text + 4, std::get<std::string>(propertyValue).c_str(), 4);
roadsign.text[roadsignTextSize - 1] = '\0';
return true;
}

Expand All @@ -568,9 +467,13 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d
if (std::holds_alternative<std::string>(propertyValue))
{
if (!roadsign.text)
roadsign.text = static_cast<char*>(malloc(64));
roadsign.text = static_cast<char*>(malloc(roadsignTextSize));

if (!roadsign.text)
break;

std::memcpy(roadsign.text + 8, std::get<std::string>(propertyValue).c_str(), 4);
roadsign.text[roadsignTextSize - 1] = '\0';
return true;
}

Expand All @@ -581,9 +484,13 @@ bool CClient2DFXManager::Set2DFXProperty(C2DEffectSAInterface* effect, const e2d
if (std::holds_alternative<std::string>(propertyValue))
{
if (!roadsign.text)
roadsign.text = static_cast<char*>(malloc(64));
roadsign.text = static_cast<char*>(malloc(roadsignTextSize));

if (!roadsign.text)
break;

std::memcpy(roadsign.text + 12, std::get<std::string>(propertyValue).c_str(), 4);
roadsign.text[roadsignTextSize - 1] = '\0';
return true;
}

Expand Down Expand Up @@ -740,7 +647,7 @@ std::variant<float, bool, std::string> CClient2DFXManager::Get2DFXProperty(C2DEf
return light.coronaEnableReflection;
case e2dEffectProperty::FLARE_TYPE:
return static_cast<float>(light.coronaFlareType);
case e2dEffectProperty::CORONA_FLAGS:
case e2dEffectProperty::FLAGS:
return static_cast<float>(light.flags);
case e2dEffectProperty::SHADOW_DISTANCE:
return static_cast<float>(light.shadowZDistance);
Expand Down Expand Up @@ -786,7 +693,7 @@ std::variant<float, bool, std::string> 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<float>(roadsign.flags);
case e2dEffectProperty::TEXT:
case e2dEffectProperty::TEXT_2:
Expand Down Expand Up @@ -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<float>(*direction) || (std::get<float>(*direction) < 0 || std::get<float>(*direction) > 1.0f))
if (!direction || !std::holds_alternative<float>(*direction) || (std::get<float>(*direction) < 0.0f || std::get<float>(*direction) > 1.0f))
return "Invalid \"direction\" value";

break;
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/CClient2DFXManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float, bool, std::string>& propertyValue);
bool Set2DFXProperty(C2DEffectSAInterface* effect, const e2dEffectProperty& property, const std::variant<bool, float, std::string>& propertyValue);
std::variant<float, bool, std::string> Get2DFXProperty(C2DEffectSAInterface* effect, const e2dEffectProperty& property);

void Set2DFXPosition(C2DEffectSAInterface* effect, const CVector& position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,14 +984,14 @@ 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")
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")

Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float, bool, std::string> propertyValue)
bool CLuaEngineDefs::EngineSetModel2DFXProperty(std::uint32_t modelID, std::uint32_t index, e2dEffectProperty property, std::variant<bool, float, std::string> propertyValue)
{
if (!CClient2DFXManager::IsValidModel(modelID))
throw std::invalid_argument("Invalid model ID");
Expand All @@ -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<float, bool, std::string> propertyValue)
bool CLuaEngineDefs::EngineSet2DFXProperty(CClient2DFX* effect, e2dEffectProperty property, std::variant<bool, float, std::string> propertyValue)
{
return m_p2DFXManager->Set2DFXProperty(effect->Get2DFX(), property, propertyValue);
}
Expand Down
Loading

0 comments on commit 0f8cbc1

Please sign in to comment.