From f45faf076e749daa8fc4ea96c63887f4ed7c4ace Mon Sep 17 00:00:00 2001 From: pongo1231 Date: Thu, 16 Jan 2025 00:01:21 +0000 Subject: [PATCH] ChaosMod/EffectThreads: Introduce OverrideEffectName & OverrideEffectNameFromId to CurrentEffect namespace --- ChaosMod/Components/EffectDispatcher.cpp | 72 ++++++++----------- ChaosMod/Components/EffectDispatcher.h | 3 - ChaosMod/Components/LuaScripts.cpp | 7 +- ChaosMod/Components/Voting.cpp | 2 - ChaosMod/Effects/EffectThreads.cpp | 32 +++++++-- ChaosMod/Effects/EffectThreads.h | 43 ++++++----- ChaosMod/Effects/db/Misc/MiscNothing.cpp | 5 +- ChaosMod/Effects/db/Misc/MiscUTurn.cpp | 5 +- .../Effects/db/Player/PlayerFakeDeath.cpp | 15 ++-- .../Effects/db/Player/PlayerTpController.cpp | 19 ++--- .../db/Player/PlayerWantedController.cpp | 4 +- ChaosMod/Main.cpp | 5 -- 12 files changed, 98 insertions(+), 114 deletions(-) diff --git a/ChaosMod/Components/EffectDispatcher.cpp b/ChaosMod/Components/EffectDispatcher.cpp index dead9aa48..da29d19d6 100644 --- a/ChaosMod/Components/EffectDispatcher.cpp +++ b/ChaosMod/Components/EffectDispatcher.cpp @@ -309,27 +309,49 @@ void EffectDispatcher::UpdateEffects(int deltaTime) if (activeEffect.HideEffectName && EffectThreads::HasThreadOnStartExecuted(activeEffect.ThreadId)) activeEffect.HideEffectName = false; - if (ComponentExists() && activeEffect.SoundId && !activeEffect.HasSetSoundOptions) + auto effectSharedData = EffectThreads::GetThreadSharedData(activeEffect.ThreadId); + if (effectSharedData) { - activeEffect.HasSetSoundOptions = true; + if (ComponentExists() && activeEffect.SoundId && !activeEffect.HasSetSoundOptions) + { + activeEffect.HasSetSoundOptions = true; + GetComponent()->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()->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() ? GetComponent()->EffectDurationModifier : 1.f); - } } else { @@ -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; diff --git a/ChaosMod/Components/EffectDispatcher.h b/ChaosMod/Components/EffectDispatcher.h index 84683904f..540134a83 100644 --- a/ChaosMod/Components/EffectDispatcher.h +++ b/ChaosMod/Components/EffectDispatcher.h @@ -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; }; diff --git a/ChaosMod/Components/LuaScripts.cpp b/ChaosMod/Components/LuaScripts.cpp index f015ec675..dbd083009 100644 --- a/ChaosMod/Components/LuaScripts.cpp +++ b/ChaosMod/Components/LuaScripts.cpp @@ -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" @@ -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()) - GetComponent()->OverrideEffectName(effectId, name); + CurrentEffect::OverrideEffectName(name); }; lua["OverrideEffectNameById"] = [effectId](const sol::this_state &lua, const std::string &overrideId) { - if (ComponentExists()) - GetComponent()->OverrideEffectNameId(effectId, overrideId); + CurrentEffect::OverrideEffectNameFromId(overrideId); }; EffectData effectData; diff --git a/ChaosMod/Components/Voting.cpp b/ChaosMod/Components/Voting.cpp index 9514d9a64..b49130ab9 100644 --- a/ChaosMod/Components/Voting.cpp +++ b/ChaosMod/Components/Voting.cpp @@ -228,9 +228,7 @@ void Voting::OnRun() { if ((m_OverlayMode == OverlayMode::OverlayIngame || m_OverlayMode == OverlayMode::OverlayOBS) && ComponentExists()) - { GetComponent()->EnableEffectTextExtraTopSpace = true; - } if (ComponentExists()) GetComponent()->ShowVotingSplash(); diff --git a/ChaosMod/Effects/EffectThreads.cpp b/ChaosMod/Effects/EffectThreads.cpp index c0dd52372..3a35819dc 100644 --- a/ChaosMod/Effects/EffectThreads.cpp +++ b/ChaosMod/Effects/EffectThreads.cpp @@ -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; } } \ No newline at end of file diff --git a/ChaosMod/Effects/EffectThreads.h b/ChaosMod/Effects/EffectThreads.h index df34d0111..b1b1bffed 100644 --- a/ChaosMod/Effects/EffectThreads.h +++ b/ChaosMod/Effects/EffectThreads.h @@ -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); @@ -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) @@ -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); } \ No newline at end of file diff --git a/ChaosMod/Effects/db/Misc/MiscNothing.cpp b/ChaosMod/Effects/db/Misc/MiscNothing.cpp index d8640dfbd..5f4487a6f 100644 --- a/ChaosMod/Effects/db/Misc/MiscNothing.cpp +++ b/ChaosMod/Effects/db/Misc/MiscNothing.cpp @@ -1,7 +1,5 @@ #include -#include "Components/EffectDispatcher.h" - static const std::array options = { "Nothing", "All Peds Are Peds", "Teleport To Current Location", @@ -28,8 +26,7 @@ static void OnStart() { const auto &effectOverride = options[g_Random.GetRandomInt(0, options.size() - 1)]; - if (ComponentExists()) - GetComponent()->OverrideEffectName("nothing", effectOverride); + CurrentEffect::OverrideEffectName(effectOverride); WAIT(25000); } diff --git a/ChaosMod/Effects/db/Misc/MiscUTurn.cpp b/ChaosMod/Effects/db/Misc/MiscUTurn.cpp index 74975ffe4..8597cac25 100644 --- a/ChaosMod/Effects/db/Misc/MiscUTurn.cpp +++ b/ChaosMod/Effects/db/Misc/MiscUTurn.cpp @@ -1,7 +1,5 @@ #include -#include - /* * Effect by juhana */ @@ -38,8 +36,7 @@ static void OnStartFake() { OnStart(); - if (ComponentExists()) - GetComponent()->OverrideEffectNameId("misc_fakeuturn", "misc_uturn"); + CurrentEffect::OverrideEffectNameFromId("misc_uturn"); WAIT(g_Random.GetRandomInt(6000, 9000)); diff --git a/ChaosMod/Effects/db/Player/PlayerFakeDeath.cpp b/ChaosMod/Effects/db/Player/PlayerFakeDeath.cpp index bd6a81fdb..e01253052 100644 --- a/ChaosMod/Effects/db/Player/PlayerFakeDeath.cpp +++ b/ChaosMod/Effects/db/Player/PlayerFakeDeath.cpp @@ -4,7 +4,6 @@ #include -#include "Components/EffectDispatcher.h" #include "Memory/Hooks/ScriptThreadRunHook.h" static const char *ms_TextPairs[] = { "Just kidding, keep playing", @@ -82,11 +81,8 @@ static void OnStart() { if (!IS_PED_IN_ANY_VEHICLE(playerPed, false)) { - if (ComponentExists()) - { - // Set the fake name accordingly - GetComponent()->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) { @@ -104,11 +100,8 @@ static void OnStart() } else if (IS_PED_IN_ANY_VEHICLE(playerPed, false)) { - if (ComponentExists()) - { - // Set the fake name accordingly - GetComponent()->OverrideEffectNameId("player_fakedeath", "playerveh_explode"); - } + // Set the fake name accordingly + CurrentEffect::OverrideEffectNameFromId("playerveh_explode"); Vehicle veh = GET_VEHICLE_PED_IS_IN(playerPed, false); diff --git a/ChaosMod/Effects/db/Player/PlayerTpController.cpp b/ChaosMod/Effects/db/Player/PlayerTpController.cpp index 208f69ff9..41bf705aa 100644 --- a/ChaosMod/Effects/db/Player/PlayerTpController.cpp +++ b/ChaosMod/Effects/db/Player/PlayerTpController.cpp @@ -1,7 +1,5 @@ #include -#include "Components/EffectDispatcher.h" - #include "Memory/Hooks/ScriptThreadRunHook.h" #include "Util/Player.h" @@ -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()) - GetComponent()->OverrideEffectNameId("tp_fake", overrideId); + CurrentEffect::OverrideEffectNameFromId(std::string(overrideId)); Player player = PLAYER_ID(); Ped playerPed = PLAYER_PED_ID(); @@ -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()) - GetComponent()->OverrideEffectNameId("tp_fakex2", overrideId); + CurrentEffect::OverrideEffectNameFromId(std::string(overrideId)); Player player = PLAYER_ID(); Ped playerPed = PLAYER_PED_ID(); @@ -415,8 +411,7 @@ static void OnStartFakeFakeTp() SET_PLAYER_WANTED_LEVEL(player, currentWanted, false); SET_PLAYER_WANTED_LEVEL_NOW(player, false); - if (ComponentExists()) - GetComponent()->OverrideEffectNameId("tp_fakex2", "tp_fake"); + CurrentEffect::OverrideEffectNameFromId("tp_fake"); WAIT(g_Random.GetRandomInt(3500, 6000)); diff --git a/ChaosMod/Effects/db/Player/PlayerWantedController.cpp b/ChaosMod/Effects/db/Player/PlayerWantedController.cpp index 30149c72a..090751660 100644 --- a/ChaosMod/Effects/db/Player/PlayerWantedController.cpp +++ b/ChaosMod/Effects/db/Player/PlayerWantedController.cpp @@ -1,6 +1,5 @@ #include -#include "Components/EffectDispatcher.h" #include "Memory/Hooks/ScriptThreadRunHook.h" static void OnStartFive() @@ -123,8 +122,7 @@ static void OnStartFake() Hooks::EnableScriptThreadBlock(); - if (ComponentExists()) - GetComponent()->OverrideEffectName("player_fakestars", selectedInfo.Name); + CurrentEffect::OverrideEffectName(selectedInfo.Name); Player player = PLAYER_ID(); int lastLevel = GET_PLAYER_WANTED_LEVEL(player); diff --git a/ChaosMod/Main.cpp b/ChaosMod/Main.cpp index 391879d25..97f0d823b 100644 --- a/ChaosMod/Main.cpp +++ b/ChaosMod/Main.cpp @@ -5,16 +5,13 @@ #include "Effects/EffectConfig.h" #include "Memory/Hooks/ScriptThreadRunHook.h" -#include "Memory/Hooks/ShaderHook.h" #include "Memory/Misc.h" -#include "Memory/Shader.h" #include "Components/DebugMenu.h" #include "Components/DebugSocket.h" #include "Components/EffectDispatchTimer.h" #include "Components/EffectDispatcher.h" #include "Components/EffectShortcuts.h" -#include "Components/EffectSound/EffectSoundManagers.h" #include "Components/Failsafe.h" #include "Components/HelpTextQueue.h" #include "Components/KeyStates.h" @@ -372,10 +369,8 @@ namespace Main else if (key == VK_OEM_PERIOD) { if (ms_Flags.PauseTimerShortcutEnabled && ComponentExists()) - { GetComponent()->SetTimerEnabled( !GetComponent()->IsTimerEnabled()); - } } else if (key == VK_OEM_COMMA) {