Skip to content

Commit

Permalink
Merge branch 'master' into jsonify-configs
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 authored Feb 14, 2025
2 parents 3ce3328 + 36fe2b0 commit a53931c
Show file tree
Hide file tree
Showing 35 changed files with 345 additions and 328 deletions.
7 changes: 6 additions & 1 deletion ChaosMod/Components/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ class Component

Component &operator=(const Component &) = delete;

virtual void OnModPauseCleanup()
enum PauseCleanupFlags
{
// Passed if called from another thread
PauseCleanupFlags_UnsafeCleanup = (1 << 0)
};
virtual void OnModPauseCleanup(PauseCleanupFlags cleanupFlags = {})
{
}

Expand Down
14 changes: 9 additions & 5 deletions ChaosMod/Components/CrossingChallenge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,18 +561,22 @@ void CrossingChallenge::OnRun()
}
}

void CrossingChallenge::OnModPauseCleanup()
void CrossingChallenge::OnModPauseCleanup(PauseCleanupFlags cleanupFlags)
{
if (m_StartedState != 2)
SET_ENTITY_INVINCIBLE(PLAYER_PED_ID(), false);

m_StartedState = 0;
m_ButtonsScaleformHandle = 0;
m_ButtonsScaleformLoading = false;
if (DOES_BLIP_EXIST(m_StartBlip))
REMOVE_BLIP(&m_StartBlip);
if (DOES_BLIP_EXIST(m_EndBlip))
REMOVE_BLIP(&m_EndBlip);

if (!(cleanupFlags & PauseCleanupFlags_UnsafeCleanup))
{
if (DOES_BLIP_EXIST(m_StartBlip))
REMOVE_BLIP(&m_StartBlip);
if (DOES_BLIP_EXIST(m_EndBlip))
REMOVE_BLIP(&m_EndBlip);
}

if (m_HelpMessageTick != -1)
{
Expand Down
2 changes: 1 addition & 1 deletion ChaosMod/Components/CrossingChallenge.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CrossingChallenge : public Component
CrossingChallenge();

virtual void OnRun() override;
virtual void OnModPauseCleanup() override;
virtual void OnModPauseCleanup(PauseCleanupFlags cleanupFlags = {}) override;
virtual void OnKeyInput(DWORD key, bool repeated, bool isUpNow, bool isCtrlPressed, bool isShiftPressed,
bool isAltPressed) override;
inline void IncrementEffects()
Expand Down
1 change: 0 additions & 1 deletion ChaosMod/Components/DebugMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class DebugMenu : public Component
DebugMenu();

virtual void OnRun() override;

virtual void OnKeyInput(DWORD key, bool repeated, bool isUpNow, bool isCtrlPressed, bool isShiftPressed,
bool isAltPressed) override;

Expand Down
36 changes: 18 additions & 18 deletions ChaosMod/Components/DebugSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,24 @@ DebugSocket::DebugSocket()
}
}

void DebugSocket::OnModPauseCleanup(PauseCleanupFlags cleanupFlags)
{
Close();
}

void DebugSocket::OnRun()
{
if (!m_DelegateQueue.empty())
{
std::lock_guard lock(m_DelegateQueueMutex);
while (!m_DelegateQueue.empty())
{
m_DelegateQueue.front()();
m_DelegateQueue.pop();
}
}
}

void DebugSocket::Close()
{
m_Server->stop();
Expand All @@ -288,22 +306,4 @@ void DebugSocket::ScriptLog(std::string_view scriptName, std::string_view text)
client->send(json.dump());
}

void DebugSocket::OnModPauseCleanup()
{
Close();
}

void DebugSocket::OnRun()
{
if (!m_DelegateQueue.empty())
{
std::lock_guard lock(m_DelegateQueueMutex);
while (!m_DelegateQueue.empty())
{
m_DelegateQueue.front()();
m_DelegateQueue.pop();
}
}
}

#endif
6 changes: 3 additions & 3 deletions ChaosMod/Components/DebugSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ class DebugSocket : public Component
public:
DebugSocket();

virtual void OnModPauseCleanup(PauseCleanupFlags cleanupFlags = {}) override;
virtual void OnRun() override;

private:
void Connect();

public:
void Close();

void ScriptLog(std::string_view scriptName, std::string_view text);

virtual void OnModPauseCleanup() override;
virtual void OnRun() override;
};

#endif
76 changes: 38 additions & 38 deletions ChaosMod/Components/EffectDispatchTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,44 @@ EffectDispatchTimer::EffectDispatchTimer(const std::array<BYTE, 3> &timerColor)
g_OptionsManager.GetConfigValue({ "DistanceType" }, OPTION_DEFAULT_DISTANCE_TYPE));
}

void EffectDispatchTimer::OnRun()
{
auto curTime = GetTickCount64();

if (m_EnableTimer && m_DrawTimerBar
&& (!ComponentExists<MetaModifiers>() || !GetComponent<MetaModifiers>()->HideChaosUI)
&& (!ComponentExists<MetaModifiers>() || !GetComponent<MetaModifiers>()->DisableChaos))
{
float percentage = m_FakeTimerPercentage != 0.f ? m_FakeTimerPercentage : m_TimerPercentage;

// Timer bar at the top
DRAW_RECT(.5f, .01f, 1.f, .021f, 0, 0, 0, 127, false);

if (ComponentExists<MetaModifiers>() && GetComponent<MetaModifiers>()->FlipChaosUI)
DRAW_RECT(1.f - percentage * .5f, .01f, percentage, .018f, m_TimerColor[0], m_TimerColor[1],
m_TimerColor[2], 255, false);
else
DRAW_RECT(percentage * .5f, .01f, percentage, .018f, m_TimerColor[0], m_TimerColor[1], m_TimerColor[2], 255,
false);
}

int deltaTime = curTime - m_Timer;

// The game was paused
if (deltaTime > 1000)
deltaTime = 0;

if (!m_PauseTimer)
{
if (m_DistanceChaosState.EnableDistanceBasedEffectDispatch)
UpdateTravelledDistance();
else
UpdateTimer(deltaTime);
}

m_Timer = curTime;
}

void EffectDispatchTimer::UpdateTimer(int deltaTime)
{
if (!m_EnableTimer || (ComponentExists<MetaModifiers>() && GetComponent<MetaModifiers>()->DisableChaos))
Expand Down Expand Up @@ -177,42 +215,4 @@ void EffectDispatchTimer::SetTimerPaused(bool pause)
bool EffectDispatchTimer::IsUsingDistanceBasedDispatch() const
{
return m_DistanceChaosState.EnableDistanceBasedEffectDispatch;
}

void EffectDispatchTimer::OnRun()
{
auto curTime = GetTickCount64();

if (m_EnableTimer && m_DrawTimerBar
&& (!ComponentExists<MetaModifiers>() || !GetComponent<MetaModifiers>()->HideChaosUI)
&& (!ComponentExists<MetaModifiers>() || !GetComponent<MetaModifiers>()->DisableChaos))
{
float percentage = m_FakeTimerPercentage != 0.f ? m_FakeTimerPercentage : m_TimerPercentage;

// Timer bar at the top
DRAW_RECT(.5f, .01f, 1.f, .021f, 0, 0, 0, 127, false);

if (ComponentExists<MetaModifiers>() && GetComponent<MetaModifiers>()->FlipChaosUI)
DRAW_RECT(1.f - percentage * .5f, .01f, percentage, .018f, m_TimerColor[0], m_TimerColor[1],
m_TimerColor[2], 255, false);
else
DRAW_RECT(percentage * .5f, .01f, percentage, .018f, m_TimerColor[0], m_TimerColor[1], m_TimerColor[2], 255,
false);
}

int deltaTime = curTime - m_Timer;

// The game was paused
if (deltaTime > 1000)
deltaTime = 0;

if (!m_PauseTimer)
{
if (m_DistanceChaosState.EnableDistanceBasedEffectDispatch)
UpdateTravelledDistance();
else
UpdateTimer(deltaTime);
}

m_Timer = curTime;
}
4 changes: 2 additions & 2 deletions ChaosMod/Components/EffectDispatchTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class EffectDispatchTimer : public Component
public:
EffectDispatchTimer(const std::array<BYTE, 3> &timerColor);

virtual void OnRun() override;

private:
void UpdateTimer(int deltaTime);
void UpdateTravelledDistance();
Expand All @@ -53,6 +55,4 @@ class EffectDispatchTimer : public Component
void SetTimerPaused(bool pause);

bool IsUsingDistanceBasedDispatch() const;

virtual void OnRun() override;
};
8 changes: 4 additions & 4 deletions ChaosMod/Components/EffectDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ static void _DispatchEffect(EffectDispatcher *effectDispatcher, const EffectDisp
// Increase weight for all effects first
for (auto &[effectId, enabledEffectData] : g_EnabledEffects)
if (!enabledEffectData.IsMeta())
enabledEffectData.Weight += enabledEffectData.WeightMult;
enabledEffectData.Weight += enabledEffectData.WeightMult * enabledEffectData.WeightMult;

// Reset weight of this effect (or every effect in group) to reduce chance of same effect (group) happening multiple
// times in a row
if (effectData.GroupType.empty())
effectData.Weight = effectData.WeightMult;
effectData.Weight = effectData.WeightMult * effectData.WeightMult;
else
{
for (auto &[effectId, enabledEffectData] : g_EnabledEffects)
if (enabledEffectData.GroupType == effectData.GroupType)
effectData.Weight = effectData.WeightMult;
enabledEffectData.Weight = enabledEffectData.WeightMult * enabledEffectData.WeightMult;
}

auto playEffectDispatchSound = [&](EffectDispatcher::ActiveEffect &activeEffect)
Expand Down Expand Up @@ -263,7 +263,7 @@ EffectDispatcher::EffectDispatcher(const std::array<BYTE, 3> &textColor, const s
g_EffectDispatcherThread = CreateFiber(0, _OnRunEffects, this);
}

void EffectDispatcher::OnModPauseCleanup()
void EffectDispatcher::OnModPauseCleanup(PauseCleanupFlags cleanupFlags)
{
ClearEffects();
}
Expand Down
6 changes: 3 additions & 3 deletions ChaosMod/Components/EffectDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ class EffectDispatcher : public Component

EffectDispatcher(const std::array<std::uint8_t, 3> &textColor, const std::array<std::uint8_t, 3> &effectTimerColor);

virtual void OnModPauseCleanup(PauseCleanupFlags cleanupFlags = {}) override;
virtual void OnRun() override;

private:
float GetEffectTopSpace();

void RegisterPermanentEffects();

public:
virtual void OnModPauseCleanup() override;
virtual void OnRun() override;

void DrawEffectTexts();

void DispatchEffect(const EffectIdentifier &effectId,
Expand Down
1 change: 0 additions & 1 deletion ChaosMod/Components/EffectShortcuts.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class EffectShortcuts : public Component
EffectShortcuts();

virtual void OnRun() override;

virtual void OnKeyInput(DWORD key, bool repeated, bool isUpNow, bool isCtrlPressed, bool isShiftPressed,
bool isAltPressed) override;
};
10 changes: 6 additions & 4 deletions ChaosMod/Components/EffectSound/EffectSound3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ EffectSound3D::EffectSound3D()
{
Sleep(100);

if (IS_PAUSE_MENU_ACTIVE())
if (GetTickCount64() > m_ThreadPingTimestamp + 100)
{
std::lock_guard lock(m_SoundsMutex);
for (auto &[soundId, sound] : m_Sounds)
Expand All @@ -40,7 +40,7 @@ void EffectSound3D::FreeSounds()
}
}

void EffectSound3D::OnModPauseCleanup()
void EffectSound3D::OnModPauseCleanup(PauseCleanupFlags cleanupFlags)
{
m_IsStopping = true;
m_PauseSoundsThread.join();
Expand All @@ -55,9 +55,11 @@ void EffectSound3D::OnRun()
if (m_Sounds.empty())
return;

auto playerPed = PLAYER_PED_ID();
m_ThreadPingTimestamp = GetTickCount64();

auto adjCamCoords = GET_GAMEPLAY_CAM_COORD();
auto playerPed = PLAYER_PED_ID();

auto adjCamCoords = GET_GAMEPLAY_CAM_COORD();
ma_engine_listener_set_position(&m_maEngine, 0, adjCamCoords.x, adjCamCoords.y, adjCamCoords.z);

float camHeading = GET_GAMEPLAY_CAM_RELATIVE_HEADING();
Expand Down
4 changes: 3 additions & 1 deletion ChaosMod/Components/EffectSound/EffectSound3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <miniaudio.h>
#include <scripthookv/inc/types.h>

#include <cstdint>
#include <map>
#include <mutex>
#include <string>
Expand All @@ -23,6 +24,7 @@ class EffectSound3D : public EffectSoundManager
std::mutex m_SoundsMutex;
bool m_IsStopping = false;
std::thread m_PauseSoundsThread;
uint64_t m_ThreadPingTimestamp;

public:
EffectSound3D();
Expand All @@ -31,7 +33,7 @@ class EffectSound3D : public EffectSoundManager
void FreeSounds();

public:
virtual void OnModPauseCleanup() override;
virtual void OnModPauseCleanup(PauseCleanupFlags cleanupFlags = {}) override;
virtual void OnRun() override;
virtual DWORD64 HandleSound(const std::string &soundFile) override;
virtual void SetSoundOptions(DWORD64 soundId, const EffectSoundPlayOptions &soundPlayOptions) override;
Expand Down
18 changes: 9 additions & 9 deletions ChaosMod/Components/HelpTextQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@

#include "Memory/Hooks/GetLabelTextHook.h"

void HelpTextQueue::DisplayLabel(std::string_view label, std::uint8_t durationSecs)
{
if (durationSecs == 0)
return;

m_HelpTextQueue.emplace(label, durationSecs / 1000.f);
}

void HelpTextQueue::OnModPauseCleanup()
void HelpTextQueue::OnModPauseCleanup(PauseCleanupFlags cleanupFlags)
{
Hooks::ClearCustomLabels();
}
Expand All @@ -29,4 +21,12 @@ void HelpTextQueue::OnRun()

if ((helpText.TimerSecs -= GET_FRAME_TIME()) < 0.f)
m_HelpTextQueue.pop();
}

void HelpTextQueue::DisplayLabel(std::string_view label, std::uint8_t durationSecs)
{
if (durationSecs == 0)
return;

m_HelpTextQueue.emplace(label, durationSecs / 1000.f);
}
6 changes: 3 additions & 3 deletions ChaosMod/Components/HelpTextQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class HelpTextQueue : public Component
std::queue<HelpText> m_HelpTextQueue;

public:
void DisplayLabel(std::string_view label, std::uint8_t durationSecs);

virtual void OnModPauseCleanup() override;
virtual void OnModPauseCleanup(PauseCleanupFlags cleanupFlags = {}) override;
virtual void OnRun() override;

void DisplayLabel(std::string_view label, std::uint8_t durationSecs);
};
Loading

0 comments on commit a53931c

Please sign in to comment.