Skip to content

Commit

Permalink
Register stats from scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
psiberx committed May 31, 2024
1 parent 59f08e8 commit 28e9d32
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions scripts/TweakDBBatch.reds
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public native class TweakDBBatch {
public native func CreateRecord(id: TweakDBID, type: CName) -> Bool
public native func CloneRecord(id: TweakDBID, base: TweakDBID) -> Bool
public native func UpdateRecord(id: TweakDBID) -> Bool
public native func RegisterEnum(id: TweakDBID)
public native func RegisterName(name: CName) -> Bool
public native func Commit()
Expand Down
1 change: 1 addition & 0 deletions scripts/TweakDBManager.reds
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public abstract native class TweakDBManager {
public final static native func CreateRecord(id: TweakDBID, type: CName) -> Bool
public final static native func CloneRecord(id: TweakDBID, base: TweakDBID) -> Bool
public final static native func UpdateRecord(id: TweakDBID) -> Bool
public final static native func RegisterEnum(id: TweakDBID)
public final static native func RegisterName(name: CName) -> Bool
public final static native func StartBatch() -> ref<TweakDBBatch>
Expand Down
19 changes: 12 additions & 7 deletions src/App/Stats/StatService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@ void App::StatService::OnBootstrap()

void App::StatService::OnInitializeStats(void* aSystem)
{
auto statRecords = Raw::StatsDataSystem::StatRecords::Ptr(aSystem);
RegisterStats(aSystem, Core::Resolve<TweakService>()->GetChangelog().GetAffectedRecords());
RegisterStats(aSystem, Core::Resolve<TweakService>()->GetManager().GetEnums());
}

void App::StatService::RegisterStats(void* aStatSystem, const Core::Set<Red::TweakDBID>& aRecordIDs)
{
auto statRecords = Raw::StatsDataSystem::StatRecords::Ptr(aStatSystem);
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())
for (const auto& recordID : aRecordIDs)
{
const auto& recordName = tweakManager.GetName(recordId);
const auto& recordName = tweakManager.GetName(recordID);

if (!recordName.starts_with(BaseStatPrefix))
continue;

auto enumNameProp = tweakManager.GetFlat({recordId, ".enumName"});
auto enumNameProp = tweakManager.GetFlat({recordID, ".enumName"});

if (!enumNameProp)
continue;
Expand All @@ -58,7 +63,7 @@ void App::StatService::OnInitializeStats(void* aSystem)
const auto enumValue = statRecords->size;

statTypeEnum->AddOption(enumValue, enumName);
statRecords->PushBack(recordId);
statRecords->PushBack(recordID);

if (!s_statTypesModified)
{
Expand All @@ -69,7 +74,7 @@ void App::StatService::OnInitializeStats(void* aSystem)
}

{
const auto record = tweakManager.GetRecord(recordId);
const auto record = tweakManager.GetRecord(recordID);
Raw::StatRecord::EnumValue::Ref(record) = enumValue;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/App/Stats/StatService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ class StatService
static uint64_t* OnGetStatRange(void* aSystem, uint64_t* aRange, uint32_t aStat);
static uint32_t OnGetStatFlags(void* aSystem, uint32_t aStat);
static bool OnCheckStatFlag(void* aSystem, uint32_t aStat, uint32_t aFlag);

static void RegisterStats(void* aStatSystem, const Core::Set<Red::TweakDBID>& aRecordIDs);
};
}
11 changes: 11 additions & 0 deletions src/App/Tweaks/Executable/Scriptable/ScriptBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ bool App::ScriptBatch::UpdateRecord(Red::TweakDBID aRecordID) const
return false;
}

bool App::ScriptBatch::RegisterEnum(Red::TweakDBID aRecordID) const
{
if (m_batch && aRecordID)
{
m_manager->RegisterEnum(m_batch, aRecordID);
return true;
}

return false;
}

bool App::ScriptBatch::RegisterName(Red::CName aName) const
{
if (m_batch && aName)
Expand Down
2 changes: 2 additions & 0 deletions src/App/Tweaks/Executable/Scriptable/ScriptBatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct ScriptBatch : Red::IScriptable
bool CreateRecord(Red::TweakDBID aRecordID, Red::CName aTypeName) const;
bool CloneRecord(Red::TweakDBID aRecordID, Red::TweakDBID aSourceID) const;
bool UpdateRecord(Red::TweakDBID aRecordID) const;
bool RegisterEnum(Red::TweakDBID aRecordID) const;
bool RegisterName(Red::CName aName) const;
void Commit() const;

Expand All @@ -30,6 +31,7 @@ RTTI_DEFINE_CLASS(App::ScriptBatch, "TweakDBBatch", {
RTTI_METHOD(CreateRecord);
RTTI_METHOD(CloneRecord);
RTTI_METHOD(UpdateRecord);
RTTI_METHOD(RegisterEnum);
RTTI_METHOD(RegisterName);
RTTI_METHOD(Commit);
});
8 changes: 8 additions & 0 deletions src/App/Tweaks/Executable/Scriptable/ScriptManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ void App::ScriptManager::RegisterName(Red::IScriptable*, Red::CStackFrame* aFram
}
}

void App::ScriptManager::RegisterEnum(Red::TweakDBID aRecordID)
{
if (s_manager)
{
s_manager->RegisterEnum(aRecordID);
}
}

Red::Handle<App::ScriptBatch> App::ScriptManager::StartBatch()
{
return Red::MakeHandle<ScriptBatch>(s_manager);
Expand Down
1 change: 1 addition & 0 deletions src/App/Tweaks/Executable/Scriptable/ScriptManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ScriptManager : public Red::IScriptable
public:
static void SetManager(Core::SharedPtr<Red::TweakDBManager> aManager);

static void RegisterEnum(Red::TweakDBID aRecordID);
static Red::Handle<ScriptBatch> StartBatch();

private:
Expand Down
15 changes: 15 additions & 0 deletions src/Red/TweakDB/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ bool Red::TweakDBManager::UpdateRecord(Red::TweakDBID aRecordId)
return m_tweakDb->UpdateRecord(record);
}

void Red::TweakDBManager::RegisterEnum(Red::TweakDBID aRecordId)
{
m_knownEnums.insert(aRecordId);
}

void Red::TweakDBManager::RegisterName(const std::string& aName, const Red::CClass* aType)
{
RegisterName(aName.data(), aName);
Expand Down Expand Up @@ -355,6 +360,11 @@ bool Red::TweakDBManager::UpdateRecord(const Red::TweakDBManager::BatchPtr& aBat
return true;
}

void Red::TweakDBManager::RegisterEnum(const Red::TweakDBManager::BatchPtr& aBatch, Red::TweakDBID aRecordId)
{
RegisterEnum(aRecordId);
}

void Red::TweakDBManager::RegisterName(const Red::TweakDBManager::BatchPtr& aBatch, Red::TweakDBID aId,
const std::string& aName)
{
Expand Down Expand Up @@ -663,3 +673,8 @@ const std::string& Red::TweakDBManager::GetName(Red::TweakDBID aId)

return it->second;
}

const Core::Set<Red::TweakDBID>& Red::TweakDBManager::GetEnums()
{
return m_knownEnums;
}
4 changes: 4 additions & 0 deletions src/Red/TweakDB/Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ class TweakDBManager
bool CloneRecord(Red::TweakDBID aRecordId, Red::TweakDBID aSourceId);
bool InheritProps(Red::TweakDBID aRecordId, Red::TweakDBID aSourceId);
bool UpdateRecord(Red::TweakDBID aRecordId);
void RegisterEnum(Red::TweakDBID aRecordId);
void RegisterName(const std::string& aName, const Red::CClass* aType = nullptr);
void RegisterName(Red::TweakDBID aId, const std::string& aName, const Red::CClass* aType = nullptr);
const Core::Set<Red::TweakDBID>& GetEnums();
const std::string& GetName(Red::TweakDBID aId);

BatchPtr StartBatch();
Expand All @@ -55,6 +57,7 @@ class TweakDBManager
bool CloneRecord(const BatchPtr& aBatch, Red::TweakDBID aRecordId, Red::TweakDBID aSourceId);
bool InheritProps(const BatchPtr& aBatch, Red::TweakDBID aRecordId, Red::TweakDBID aSourceId);
bool UpdateRecord(const BatchPtr& aBatch, Red::TweakDBID aRecordId);
void RegisterEnum(const BatchPtr& aBatch, Red::TweakDBID aRecordId);
void RegisterName(const BatchPtr& aBatch, Red::TweakDBID aId, const std::string& aName);
void CommitBatch(const BatchPtr& aBatch);

Expand Down Expand Up @@ -87,5 +90,6 @@ class TweakDBManager
Core::SharedPtr<Red::TweakDBBuffer> m_buffer;
Core::SharedPtr<Red::TweakDBReflection> m_reflection;
Core::Map<Red::TweakDBID, std::string> m_knownNames;
Core::Set<Red::TweakDBID> m_knownEnums;
};
}

0 comments on commit 28e9d32

Please sign in to comment.