Skip to content

Commit

Permalink
ChaosMod/EffectThreads: Introduce OverrideEffectName & OverrideEffect…
Browse files Browse the repository at this point in the history
…NameFromId to CurrentEffect namespace
  • Loading branch information
pongo1231 committed Jan 16, 2025
1 parent 0b2527c commit f45faf0
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 114 deletions.
72 changes: 31 additions & 41 deletions ChaosMod/Components/EffectDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,27 +309,49 @@ void EffectDispatcher::UpdateEffects(int deltaTime)
if (activeEffect.HideEffectName && EffectThreads::HasThreadOnStartExecuted(activeEffect.ThreadId))
activeEffect.HideEffectName = false;

if (ComponentExists<EffectSoundManager>() && activeEffect.SoundId && !activeEffect.HasSetSoundOptions)
auto effectSharedData = EffectThreads::GetThreadSharedData(activeEffect.ThreadId);
if (effectSharedData)
{
activeEffect.HasSetSoundOptions = true;
if (ComponentExists<EffectSoundManager>() && activeEffect.SoundId && !activeEffect.HasSetSoundOptions)
{
activeEffect.HasSetSoundOptions = true;
GetComponent<EffectSoundManager>()->SetSoundOptions(activeEffect.SoundId,
effectSharedData->EffectSoundPlayOptions);
}

if (!effectSharedData->OverrideEffectName.empty())
{
activeEffect.FakeName = effectSharedData->OverrideEffectName;
effectSharedData->OverrideEffectId.clear();
}

auto effectSoundPlayOptions = EffectThreads::GetThreadEffectSoundPlayOptions(activeEffect.ThreadId);
if (effectSoundPlayOptions)
GetComponent<EffectSoundManager>()->SetSoundOptions(activeEffect.SoundId, *effectSoundPlayOptions);
if (!effectSharedData->OverrideEffectId.empty())
{
if (g_EnabledEffects.contains(effectSharedData->OverrideEffectId))
{
auto &fakeEffect = g_EnabledEffects.at(effectSharedData->OverrideEffectId);
activeEffect.FakeName = fakeEffect.HasCustomName() ? fakeEffect.CustomName : fakeEffect.Name;
}
else
{
auto result = g_EffectsMap.find(effectSharedData->OverrideEffectId);
if (result != g_EffectsMap.end())
activeEffect.FakeName = result->second.Name;
}

effectSharedData->OverrideEffectId.clear();
}
}

if (activeEffect.MaxTime > 0.f)
{
if (isEffectPaused)
{
activeEffect.Timer -= adjustedDeltaTime;
}

else
{
activeEffect.Timer -=
adjustedDeltaTime
/ (ComponentExists<MetaModifiers>() ? GetComponent<MetaModifiers>()->EffectDurationModifier : 1.f);
}
}
else
{
Expand Down Expand Up @@ -632,38 +654,6 @@ void EffectDispatcher::RegisterPermanentEffects()
}
}

// (kolyaventuri): Forces the name of the provided effect to change, using any given string
void EffectDispatcher::OverrideEffectName(std::string_view effectId, const std::string &overrideName)
{
for (auto &effect : SharedState.ActiveEffects)
if (effect.Identifier.GetEffectId() == effectId)
effect.FakeName = overrideName;
}

// (kolyaventuri): Forces the name of the provided effect to change, using the defined name of another effect
void EffectDispatcher::OverrideEffectNameId(std::string_view effectId, std::string_view fakeEffectId)
{
for (auto &effect : SharedState.ActiveEffects)
{
if (effect.Identifier.GetEffectId() == effectId)
{
auto effectIdentifier = EffectIdentifier(std::string(fakeEffectId));

if (g_EnabledEffects.contains(effectIdentifier))
{
auto &fakeEffect = g_EnabledEffects.at(effectIdentifier);
effect.FakeName = fakeEffect.HasCustomName() ? fakeEffect.CustomName : fakeEffect.Name;
}
else
{
auto result = g_EffectsMap.find(fakeEffectId);
if (result != g_EffectsMap.end())
effect.FakeName = result->second.Name;
}
}
}
}

bool EffectDispatcher::IsClearingEffects() const
{
return m_ClearEffectsState != ClearEffectsState::None;
Expand Down
3 changes: 0 additions & 3 deletions ChaosMod/Components/EffectDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,5 @@ class EffectDispatcher : public Component

void Reset(ClearEffectsFlags clearEffectFlags = ClearEffectsFlag_None);

void OverrideEffectName(std::string_view effectId, const std::string &overrideName);
void OverrideEffectNameId(std::string_view effectId, std::string_view fakeEffectId);

bool IsClearingEffects() const;
};
7 changes: 2 additions & 5 deletions ChaosMod/Components/LuaScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "Util/EntityIterator.h"
#include "Util/File.h"
#include "Util/HelpText.h"
#include "Util/Peds.h"
#include "Util/Player.h"
#include "Util/PoolSpawner.h"
#include "Util/Script.h"
Expand Down Expand Up @@ -804,13 +803,11 @@ LuaScripts::ParseScriptRaw(std::string scriptName, std::string_view script, Pars

lua["OverrideEffectName"] = [effectId](const sol::this_state &lua, const std::string &name)
{
if (ComponentExists<EffectDispatcher>())
GetComponent<EffectDispatcher>()->OverrideEffectName(effectId, name);
CurrentEffect::OverrideEffectName(name);
};
lua["OverrideEffectNameById"] = [effectId](const sol::this_state &lua, const std::string &overrideId)
{
if (ComponentExists<EffectDispatcher>())
GetComponent<EffectDispatcher>()->OverrideEffectNameId(effectId, overrideId);
CurrentEffect::OverrideEffectNameFromId(overrideId);
};

EffectData effectData;
Expand Down
2 changes: 0 additions & 2 deletions ChaosMod/Components/Voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ void Voting::OnRun()
{
if ((m_OverlayMode == OverlayMode::OverlayIngame || m_OverlayMode == OverlayMode::OverlayOBS)
&& ComponentExists<EffectDispatcher>())
{
GetComponent<EffectDispatcher>()->EnableEffectTextExtraTopSpace = true;
}

if (ComponentExists<SplashTexts>())
GetComponent<SplashTexts>()->ShowVotingSplash();
Expand Down
32 changes: 25 additions & 7 deletions ChaosMod/Effects/EffectThreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,41 @@ namespace EffectThreads
return m_Threads.contains(GetCurrentFiber());
}

EffectSoundPlayOptions *GetThreadEffectSoundPlayOptions(LPVOID threadId)
EffectThreadSharedData *GetThreadSharedData(LPVOID threadId)
{
if (!DoesThreadExist(threadId))
return nullptr;

return &m_Threads.at(threadId)->ThreadData.EffectSoundPlayOptions;
return &m_Threads.at(threadId)->ThreadData.SharedData;
}
}

namespace CurrentEffect
{
static EffectThreadSharedData *GetCurrentThreadSharedData()
{
auto threadId = GetCurrentFiber();
return EffectThreads::GetThreadSharedData(threadId);
}

void SetEffectSoundPlayOptions(const EffectSoundPlayOptions &soundPlayOptions)
{
auto threadId = GetCurrentFiber();
auto targetSoundPlayOptions = EffectThreads::GetThreadEffectSoundPlayOptions(threadId);
if (!targetSoundPlayOptions)
return;
*targetSoundPlayOptions = soundPlayOptions;
auto sharedData = GetCurrentThreadSharedData();
if (sharedData)
sharedData->EffectSoundPlayOptions = soundPlayOptions;
}

void OverrideEffectName(const std::string &effectName)
{
auto sharedData = GetCurrentThreadSharedData();
if (sharedData)
sharedData->OverrideEffectName = effectName;
}

void OverrideEffectNameFromId(const std::string &effectId)
{
auto sharedData = GetCurrentThreadSharedData();
if (sharedData)
sharedData->OverrideEffectId = effectId;
}
}
43 changes: 26 additions & 17 deletions ChaosMod/Effects/EffectThreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ using DWORD64 = unsigned long long;

using LPVOID = void *;

struct EffectThreadSharedData
{
EffectSoundPlayOptions EffectSoundPlayOptions;
std::string OverrideEffectName;
std::string OverrideEffectId;
};

struct EffectThreadData
{
RegisteredEffect *Effect = nullptr;
bool HasOnStartExecuted = false;
bool IsRunning = false;
bool HasStopped = false;

void *CallerFiber = nullptr;

EffectThreadSharedData SharedData;

EffectThreadData(RegisteredEffect *effect, bool isRunning) : Effect(effect), IsRunning(isRunning)
{
}
};

namespace EffectThreads
{
LPVOID CreateThread(RegisteredEffect *effect, bool isTimed);
Expand All @@ -33,23 +56,7 @@ namespace EffectThreads

bool IsThreadAnEffectThread();

EffectSoundPlayOptions *GetThreadEffectSoundPlayOptions(LPVOID threadId);
};

struct EffectThreadData
{
RegisteredEffect *Effect = nullptr;
bool HasOnStartExecuted = false;
bool IsRunning = false;
bool HasStopped = false;

void *CallerFiber = nullptr;

EffectSoundPlayOptions EffectSoundPlayOptions;

EffectThreadData(RegisteredEffect *effect, bool isRunning) : Effect(effect), IsRunning(isRunning)
{
}
EffectThreadSharedData *GetThreadSharedData(LPVOID threadId);
};

inline void EffectThreadFunc(LPVOID data)
Expand Down Expand Up @@ -137,4 +144,6 @@ class EffectThread
namespace CurrentEffect
{
void SetEffectSoundPlayOptions(const EffectSoundPlayOptions &soundPlayOptions);
void OverrideEffectName(const std::string &effectName);
void OverrideEffectNameFromId(const std::string &effectId);
}
5 changes: 1 addition & 4 deletions ChaosMod/Effects/db/Misc/MiscNothing.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <stdafx.h>

#include "Components/EffectDispatcher.h"

static const std::array options = { "Nothing",
"All Peds Are Peds",
"Teleport To Current Location",
Expand All @@ -28,8 +26,7 @@ static void OnStart()
{
const auto &effectOverride = options[g_Random.GetRandomInt(0, options.size() - 1)];

if (ComponentExists<EffectDispatcher>())
GetComponent<EffectDispatcher>()->OverrideEffectName("nothing", effectOverride);
CurrentEffect::OverrideEffectName(effectOverride);

WAIT(25000);
}
Expand Down
5 changes: 1 addition & 4 deletions ChaosMod/Effects/db/Misc/MiscUTurn.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <stdafx.h>

#include <Components/EffectDispatcher.h>

/*
* Effect by juhana
*/
Expand Down Expand Up @@ -38,8 +36,7 @@ static void OnStartFake()
{
OnStart();

if (ComponentExists<EffectDispatcher>())
GetComponent<EffectDispatcher>()->OverrideEffectNameId("misc_fakeuturn", "misc_uturn");
CurrentEffect::OverrideEffectNameFromId("misc_uturn");

WAIT(g_Random.GetRandomInt(6000, 9000));

Expand Down
15 changes: 4 additions & 11 deletions ChaosMod/Effects/db/Player/PlayerFakeDeath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <stdafx.h>

#include "Components/EffectDispatcher.h"
#include "Memory/Hooks/ScriptThreadRunHook.h"

static const char *ms_TextPairs[] = { "Just kidding, keep playing",
Expand Down Expand Up @@ -82,11 +81,8 @@ static void OnStart()
{
if (!IS_PED_IN_ANY_VEHICLE(playerPed, false))
{
if (ComponentExists<EffectDispatcher>())
{
// Set the fake name accordingly
GetComponent<EffectDispatcher>()->OverrideEffectNameId("player_fakedeath", "player_suicide");
}
// Set the fake name accordingly
CurrentEffect::OverrideEffectNameFromId("player_suicide");

if (IS_PED_ON_FOOT(playerPed) && GET_PED_PARACHUTE_STATE(playerPed) == -1)
{
Expand All @@ -104,11 +100,8 @@ static void OnStart()
}
else if (IS_PED_IN_ANY_VEHICLE(playerPed, false))
{
if (ComponentExists<EffectDispatcher>())
{
// Set the fake name accordingly
GetComponent<EffectDispatcher>()->OverrideEffectNameId("player_fakedeath", "playerveh_explode");
}
// Set the fake name accordingly
CurrentEffect::OverrideEffectNameFromId("playerveh_explode");

Vehicle veh = GET_VEHICLE_PED_IS_IN(playerPed, false);

Expand Down
19 changes: 7 additions & 12 deletions ChaosMod/Effects/db/Player/PlayerTpController.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <stdafx.h>

#include "Components/EffectDispatcher.h"

#include "Memory/Hooks/ScriptThreadRunHook.h"

#include "Util/Player.h"
Expand Down Expand Up @@ -302,11 +300,10 @@ static int GetFakeWantedLevel(std::string_view effect)

static void OnStartFakeTp()
{
FakeTeleportInfo selectedLocationInfo = tpLocations.at(g_Random.GetRandomInt(0, tpLocations.size() - 1));
auto overrideId = selectedLocationInfo.type;
auto selectedLocationInfo = tpLocations.at(g_Random.GetRandomInt(0, tpLocations.size() - 1));
auto overrideId = selectedLocationInfo.type;

if (ComponentExists<EffectDispatcher>())
GetComponent<EffectDispatcher>()->OverrideEffectNameId("tp_fake", overrideId);
CurrentEffect::OverrideEffectNameFromId(std::string(overrideId));

Player player = PLAYER_ID();
Ped playerPed = PLAYER_PED_ID();
Expand Down Expand Up @@ -366,11 +363,10 @@ REGISTER_EFFECT(OnStartFakeTp, nullptr, nullptr, EffectInfo

static void OnStartFakeFakeTp()
{
FakeTeleportInfo selectedLocationInfo = tpLocations.at(g_Random.GetRandomInt(0, tpLocations.size() - 1));
auto overrideId = selectedLocationInfo.type;
auto selectedLocationInfo = tpLocations.at(g_Random.GetRandomInt(0, tpLocations.size() - 1));
auto overrideId = selectedLocationInfo.type;

if (ComponentExists<EffectDispatcher>())
GetComponent<EffectDispatcher>()->OverrideEffectNameId("tp_fakex2", overrideId);
CurrentEffect::OverrideEffectNameFromId(std::string(overrideId));

Player player = PLAYER_ID();
Ped playerPed = PLAYER_PED_ID();
Expand Down Expand Up @@ -415,8 +411,7 @@ static void OnStartFakeFakeTp()
SET_PLAYER_WANTED_LEVEL(player, currentWanted, false);
SET_PLAYER_WANTED_LEVEL_NOW(player, false);

if (ComponentExists<EffectDispatcher>())
GetComponent<EffectDispatcher>()->OverrideEffectNameId("tp_fakex2", "tp_fake");
CurrentEffect::OverrideEffectNameFromId("tp_fake");

WAIT(g_Random.GetRandomInt(3500, 6000));

Expand Down
4 changes: 1 addition & 3 deletions ChaosMod/Effects/db/Player/PlayerWantedController.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <stdafx.h>

#include "Components/EffectDispatcher.h"
#include "Memory/Hooks/ScriptThreadRunHook.h"

static void OnStartFive()
Expand Down Expand Up @@ -123,8 +122,7 @@ static void OnStartFake()

Hooks::EnableScriptThreadBlock();

if (ComponentExists<EffectDispatcher>())
GetComponent<EffectDispatcher>()->OverrideEffectName("player_fakestars", selectedInfo.Name);
CurrentEffect::OverrideEffectName(selectedInfo.Name);

Player player = PLAYER_ID();
int lastLevel = GET_PLAYER_WANTED_LEVEL(player);
Expand Down
Loading

0 comments on commit f45faf0

Please sign in to comment.