-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
205 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "StatService.hpp" | ||
#include "App/Tweaks/TweakService.hpp" | ||
#include "Core/Facades/Container.hpp" | ||
|
||
namespace | ||
{ | ||
constexpr auto BaseStatPrefix = "BaseStats."; | ||
constexpr auto BaseStatPrefixLength = std::char_traits<char>::length(BaseStatPrefix); | ||
constexpr auto BaseStatCount = static_cast<uint32_t>(Red::game::data::StatType::Count); | ||
constexpr auto InvalidStat = static_cast<uint32_t>(Red::game::data::StatType::Invalid); | ||
|
||
bool s_statTypesModified = false; | ||
} | ||
|
||
void App::StatService::OnBootstrap() | ||
{ | ||
HookAfter<Raw::StatsDataSystem::InitializeRecords>(&OnInitializeStats); | ||
} | ||
|
||
void App::StatService::OnInitializeStats(void* aSystem) | ||
{ | ||
auto statRecords = Raw::StatsDataSystem::StatRecords::Get(aSystem); | ||
auto statTypeEnum = Red::GetDescriptor<Red::game::data::StatType>(); | ||
|
||
auto& tweakManager = Core::Resolve<TweakService>()->GetManager(); | ||
auto& tweakChangelog = Core::Resolve<TweakService>()->GetChangelog(); | ||
|
||
for (const auto& recordId : tweakChangelog.GetAffectedRecords()) | ||
{ | ||
const auto& recordName = tweakManager.GetName(recordId); | ||
if (recordName.starts_with(BaseStatPrefix)) | ||
{ | ||
auto enumName = tweakManager.GetFlat({recordId, ".enumName"}).As<Red::CString>().c_str(); | ||
if (!statTypeEnum->HasOption(enumName)) | ||
{ | ||
if (recordName.substr(BaseStatPrefixLength) != enumName) | ||
{ | ||
LogError("{}: Enum name must match the record name.", recordName); | ||
continue; | ||
} | ||
|
||
if (statRecords->size == BaseStatCount) | ||
{ | ||
// Add dummy entries for "Count" and "Invalid" | ||
statRecords->EmplaceBack(); | ||
statRecords->EmplaceBack(); | ||
} | ||
|
||
statTypeEnum->AddOption(statRecords->size, enumName); | ||
statRecords->PushBack(recordId); | ||
|
||
if (!s_statTypesModified) | ||
{ | ||
s_statTypesModified = true; | ||
Hook<Raw::StatsDataSystem::GetStatFlags>(&OnGetStatFlags); | ||
Hook<Raw::StatsDataSystem::GetStatRange>(&OnGetStatRange); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
uint32_t App::StatService::OnGetStatFlags(void* aSystem, uint32_t aStat) | ||
{ | ||
if (aStat != InvalidStat) | ||
{ | ||
auto& statParams = Raw::StatsDataSystem::StatParams::Ref(aSystem); | ||
auto& statLock = Raw::StatsDataSystem::StatLock::Ref(aSystem); | ||
|
||
std::shared_lock _(statLock); | ||
if (aStat < statParams.size) | ||
{ | ||
return statParams[aStat].flags; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
uint64_t* App::StatService::OnGetStatRange(void* aSystem, uint64_t* aRange, uint32_t aStat) | ||
{ | ||
if (aStat != InvalidStat) | ||
{ | ||
auto& statParams = Raw::StatsDataSystem::StatParams::Ref(aSystem); | ||
auto& statLock = Raw::StatsDataSystem::StatLock::Ref(aSystem); | ||
|
||
std::shared_lock _(statLock); | ||
if (aStat < statParams.size) | ||
{ | ||
*aRange = statParams[aStat].range; | ||
return aRange; | ||
} | ||
} | ||
|
||
*aRange = 0; | ||
return aRange; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include "Core/Foundation/Feature.hpp" | ||
#include "Core/Hooking/HookingAgent.hpp" | ||
#include "Core/Logging/LoggingAgent.hpp" | ||
#include "Red/StatsDataSystem.hpp" | ||
|
||
namespace App | ||
{ | ||
class StatService | ||
: public Core::Feature | ||
, public Core::HookingAgent | ||
, public Core::LoggingAgent | ||
{ | ||
protected: | ||
void OnBootstrap() override; | ||
|
||
static void OnInitializeStats(void* aSystem); | ||
static uint32_t OnGetStatFlags(void* aSystem, uint32_t aStat); | ||
static uint64_t* OnGetStatRange(void* aSystem, uint64_t* aRange, uint32_t aStat); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
|
||
#include "Red/Addresses.hpp" | ||
|
||
namespace Red | ||
{ | ||
struct StatParams | ||
{ | ||
#pragma pack(push, 1) | ||
union | ||
{ | ||
uint64_t range; | ||
struct | ||
{ | ||
float min; | ||
float max; | ||
}; | ||
}; | ||
#pragma pack(pop) | ||
uint32_t flags; | ||
}; | ||
} | ||
|
||
namespace Raw::StatsDataSystem | ||
{ | ||
using StatRecords = Core::OffsetPtr<0xD8, Red::DynArray<Red::TweakDBID>>; | ||
using StatParams = Core::OffsetPtr<0xE8, Red::DynArray<Red::StatParams>>; | ||
using StatLock = Core::OffsetPtr<0xFC, Red::SharedMutex>; | ||
|
||
constexpr auto InitializeRecords = Core::RawFunc< | ||
/* addr = */ Red::Addresses::StatsDataSystem_InitializeRecords, | ||
/* type = */ void (*)(void* aSystem)>(); | ||
|
||
constexpr auto InitializeParams = Core::RawFunc< | ||
/* addr = */ Red::Addresses::StatsDataSystem_InitializeParams, | ||
/* type = */ void (*)(void* aSystem)>(); | ||
|
||
constexpr auto GetStatFlags = Core::RawFunc< | ||
/* addr = */ Red::Addresses::StatsDataSystem_GetStatFlags, | ||
/* type = */ uint32_t (*)(void* aSystem, uint32_t aStat)>(); | ||
|
||
constexpr auto GetStatRange = Core::RawFunc< | ||
/* addr = */ Red::Addresses::StatsDataSystem_GetStatRange, | ||
/* type = */ uint64_t* (*)(void* aSystem, uint64_t*, uint32_t aStat)>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters