Merge pull request #205 from diamante0018/iw4/load-leaderboarddef

feat(iw4): load leaderboard definitions from raw
This commit is contained in:
Jan 2024-06-01 13:44:44 +02:00 committed by GitHub
commit 694548d6cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 151 additions and 22 deletions

View File

@ -35,11 +35,11 @@ namespace IW4
public:
std::string name;
int colId;
int propertyId;
bool hidden;
std::string statName;
std::optional<int> propertyId;
std::optional<bool> hidden;
std::optional<std::string> statName;
LbColType type;
int precision;
std::optional<int> precision;
LbAggType aggregationFunction;
};

View File

@ -1,10 +1,13 @@
#include "AssetLoaderLeaderboard.h"
#include "Game/IW4/IW4.h"
#include "Game/IW4/Leaderboard/JsonLeaderboardDefLoader.h"
#include "ObjLoading.h"
#include "Pool/GlobalAssetPool.h"
#include <cstring>
#include <format>
#include <iostream>
using namespace IW4;
@ -15,3 +18,26 @@ void* AssetLoaderLeaderboard::CreateEmptyAsset(const std::string& assetName, Mem
leaderboard->name = memory->Dup(assetName.c_str());
return leaderboard;
}
bool AssetLoaderLeaderboard::CanLoadFromRaw() const
{
return true;
}
bool AssetLoaderLeaderboard::LoadFromRaw(
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
{
const auto file = searchPath->Open(std::format("leaderboards/{}.json", assetName));
if (!file.IsOpen())
return false;
auto* leaderboardDef = memory->Alloc<LeaderboardDef>();
leaderboardDef->name = memory->Dup(assetName.c_str());
if (LoadLeaderboardAsJson(*file.m_stream, *leaderboardDef, memory))
manager->AddAsset<AssetLeaderboard>(assetName, leaderboardDef);
else
std::cerr << std::format("Failed to load leaderboard \"{}\"\n", assetName);
return true;
}

View File

@ -10,5 +10,8 @@ namespace IW4
{
public:
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
_NODISCARD bool CanLoadFromRaw() const override;
bool
LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override;
};
} // namespace IW4

View File

@ -0,0 +1,105 @@
#include "JsonLeaderboardDefLoader.h"
#include "Game/IW4/CommonIW4.h"
#include "Game/IW4/Leaderboard/JsonLeaderboardDef.h"
#include <format>
#include <iostream>
#include <nlohmann/json.hpp>
using namespace nlohmann;
using namespace IW4;
namespace
{
class JsonLoader
{
public:
JsonLoader(std::istream& stream, MemoryManager& memory)
: m_stream(stream),
m_memory(memory)
{
}
bool Load(LeaderboardDef& leaderboardDef) const
{
const auto jRoot = json::parse(m_stream);
std::string type;
unsigned version;
jRoot.at("_type").get_to(type);
jRoot.at("_version").get_to(version);
if (type != "leaderboard" || version != 1u)
{
std::cerr << std::format("Tried to load leaderboard \"{}\" but did not find expected type leaderboard of version 1\n", leaderboardDef.name);
return false;
}
const auto jLeaderboard = jRoot.get<JsonLeaderboardDef>();
return CreateLeaderboardFromJson(jLeaderboard, leaderboardDef);
}
private:
bool CreateColumnDefFromJson(const JsonColumnDef& jColumn, LbColumnDef& lbColumnDef, LeaderboardDef& leaderboardDef) const
{
lbColumnDef.name = m_memory.Dup(jColumn.name.c_str());
lbColumnDef.id = jColumn.colId;
lbColumnDef.propertyId = jColumn.propertyId.value_or(0);
lbColumnDef.hidden = jColumn.hidden.value_or(false);
if (jColumn.statName)
lbColumnDef.statName = m_memory.Dup(jColumn.statName->c_str());
else
lbColumnDef.statName = nullptr;
lbColumnDef.type = jColumn.type;
lbColumnDef.precision = jColumn.precision.value_or(0);
lbColumnDef.agg = jColumn.aggregationFunction;
return true;
}
bool CreateLeaderboardFromJson(const JsonLeaderboardDef& jLeaderboardDef, LeaderboardDef& leaderboardDef) const
{
leaderboardDef.id = jLeaderboardDef.id;
leaderboardDef.xpColId = jLeaderboardDef.xpColId.value_or(-1);
leaderboardDef.prestigeColId = jLeaderboardDef.prestigeColId.value_or(-1);
if (!jLeaderboardDef.columns.empty())
{
leaderboardDef.columnCount = static_cast<int>(jLeaderboardDef.columns.size());
leaderboardDef.columns = m_memory.Alloc<LbColumnDef>(leaderboardDef.columnCount);
for (auto i = 0; i < leaderboardDef.columnCount; i++)
{
if (!CreateColumnDefFromJson(jLeaderboardDef.columns[i], leaderboardDef.columns[i], leaderboardDef))
return false;
}
}
else
{
leaderboardDef.columnCount = 0;
leaderboardDef.columns = nullptr;
}
return true;
}
std::istream& m_stream;
MemoryManager& m_memory;
};
} // namespace
namespace IW4
{
bool LoadLeaderboardAsJson(std::istream& stream, LeaderboardDef& leaderboard, MemoryManager* memory)
{
const JsonLoader loader(stream, *memory);
return loader.Load(leaderboard);
}
} // namespace IW4

View File

@ -0,0 +1,9 @@
#pragma once
#include "Game/IW4/IW4.h"
#include "Utils/MemoryManager.h"
namespace IW4
{
bool LoadLeaderboardAsJson(std::istream& stream, LeaderboardDef& leaderboard, MemoryManager* memory);
} // namespace IW4

View File

@ -7,12 +7,6 @@
using namespace IW4;
std::string AssetDumperLeaderboardDef::GetFileNameForAsset(const std::string& assetName)
{
return std::format("leaderboards/{}.json", assetName);
}
bool AssetDumperLeaderboardDef::ShouldDump(XAssetInfo<LeaderboardDef>* asset)
{
return true;
@ -20,7 +14,8 @@ bool AssetDumperLeaderboardDef::ShouldDump(XAssetInfo<LeaderboardDef>* asset)
void AssetDumperLeaderboardDef::DumpAsset(AssetDumpingContext& context, XAssetInfo<LeaderboardDef>* asset)
{
const auto assetFile = context.OpenAssetFile(GetFileNameForAsset(asset->m_name));
const auto assetName = asset->m_name;
const auto assetFile = context.OpenAssetFile(std::format("leaderboards/{}.json", assetName));
if (!assetFile)
return;

View File

@ -7,8 +7,6 @@ namespace IW4
{
class AssetDumperLeaderboardDef final : public AbstractAssetDumper<LeaderboardDef>
{
static std::string GetFileNameForAsset(const std::string& assetName);
protected:
_NODISCARD bool ShouldDump(XAssetInfo<LeaderboardDef>* asset) override;
void DumpAsset(AssetDumpingContext& context, XAssetInfo<LeaderboardDef>* asset) override;

View File

@ -7,12 +7,6 @@
using namespace IW5;
std::string AssetDumperLeaderboardDef::GetFileNameForAsset(const std::string& assetName)
{
return std::format("leaderboards/{}.json", assetName);
}
bool AssetDumperLeaderboardDef::ShouldDump(XAssetInfo<LeaderboardDef>* asset)
{
return true;
@ -20,7 +14,8 @@ bool AssetDumperLeaderboardDef::ShouldDump(XAssetInfo<LeaderboardDef>* asset)
void AssetDumperLeaderboardDef::DumpAsset(AssetDumpingContext& context, XAssetInfo<LeaderboardDef>* asset)
{
const auto assetFile = context.OpenAssetFile(GetFileNameForAsset(asset->m_name));
const auto assetName = asset->m_name;
const auto assetFile = context.OpenAssetFile(std::format("leaderboards/{}.json", assetName));
if (!assetFile)
return;

View File

@ -7,8 +7,6 @@ namespace IW5
{
class AssetDumperLeaderboardDef final : public AbstractAssetDumper<LeaderboardDef>
{
static std::string GetFileNameForAsset(const std::string& assetName);
protected:
_NODISCARD bool ShouldDump(XAssetInfo<LeaderboardDef>* asset) override;
void DumpAsset(AssetDumpingContext& context, XAssetInfo<LeaderboardDef>* asset) override;