Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions core/libs/prx/libSceNpTrophy2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ add_library(libSceNpTrophy2 SHARED EXCLUDE_FROM_ALL
${CMAKE_CURRENT_SOURCE_DIR}/src/GameInfo.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/TrophyInfo.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/GroupInfo.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/TrophyPackage.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/TitleTrophies.cpp
)

target_include_directories(libSceNpTrophy2 PRIVATE
Expand All @@ -23,3 +25,16 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceNpTrophy2>")
endif()
endif()

add_test_executable(np_trophy2_package_tests EXCLUDE_FROM_ALL tests/TrophyPackage.cpp)
target_include_directories(np_trophy2_package_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(np_trophy2_package_tests PRIVATE libSceNpTrophy2 libc)
configure_windows_unwind(np_trophy2_package_tests)
if(BUILD_TESTING)
set_target_properties(np_trophy2_package_tests PROPERTIES EXCLUDE_FROM_ALL FALSE)
add_test(NAME np_trophy2_package COMMAND np_trophy2_package_tests)
if(WIN32)
set_tests_properties(np_trophy2_package PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceNpTrophy2>")
endif()
endif()
3 changes: 2 additions & 1 deletion core/libs/prx/libSceNpTrophy2/include/NpTrophy2Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ static constexpr std::uint32_t NP_TROPHY2_PROGRESS_PERCENTAGE = 0;
static constexpr std::int32_t NP_TROPHY2_TROPHY_GRADE_BRONZE = 4;

static constexpr std::int32_t NP_TROPHY2_PROGRESS_TYPE_NONE = 0;
static constexpr std::uint32_t NP_TROPHY2_PROGRESS_VALUE_NONE = 0;
static constexpr std::int32_t NP_TROPHY2_PROGRESS_TYPE_VALUE = 1;
static constexpr std::uint64_t NP_TROPHY2_PROGRESS_VALUE_NONE = 0;

static constexpr std::size_t NP_TROPHY2_ICON_SIZE_NONE = 0;

Expand Down
14 changes: 13 additions & 1 deletion core/libs/prx/libSceNpTrophy2/include/NpTrophy2Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
#include <cstddef>
#include <cstdint>

struct NpTrophy2Progress { std::uint32_t value; };
struct NpTrophy2Progress {
std::int32_t type;
std::uint8_t reserved[4];
std::uint64_t value;
};

struct NpTrophy2GameDetails {
std::uint32_t num_groups;
Expand Down Expand Up @@ -67,4 +71,12 @@ struct NpTrophy2Data {
std::uint64_t timestamp_tick;
};

static_assert(sizeof(NpTrophy2Progress) == 16);
static_assert(sizeof(NpTrophy2GameDetails) == 152);
static_assert(sizeof(NpTrophy2GameData) == 24);
static_assert(sizeof(NpTrophy2GroupDetails) == 152);
static_assert(sizeof(NpTrophy2GroupData) == 32);
static_assert(sizeof(NpTrophy2Details) == 1312);
static_assert(sizeof(NpTrophy2Data) == 32);

#endif
54 changes: 54 additions & 0 deletions core/libs/prx/libSceNpTrophy2/include/TitleTrophies.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef CORE_LIBS_PRX_LIBSCENPTROPHY2_INCLUDE_TITLETROPHIES_HPP
#define CORE_LIBS_PRX_LIBSCENPTROPHY2_INCLUDE_TITLETROPHIES_HPP

#include <array>
#include <cstddef>
#include <cstdint>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "prx/libSceNpTrophy2/include/TrophyPackage.hpp"

namespace NpTrophy2 {

inline constexpr std::uint64_t MaxUcpSize = std::uint64_t{256} << 20u;

struct TitleTrophies {
std::optional<TrophyPackage> package;
};

std::string PackagePath(std::uint32_t serviceLabel);
TitleTrophies LoadTitleTrophies(std::uint32_t serviceLabel, std::string_view language);
const TitleTrophies& Title(std::uint32_t serviceLabel);
const TrophyPackage* Package(std::uint32_t serviceLabel);
void CopyText(char* destination, std::size_t size, std::string_view text);

struct ContextOwner {
std::int32_t userId = 0;
std::uint32_t serviceLabel = 0;
};

int CreateContext(std::int32_t userId, std::uint32_t serviceLabel);
void DestroyContext(int context);
std::optional<ContextOwner> FindContext(int context);

std::optional<std::uint64_t> ProgressTarget(const TrophyDefinition& trophy);
std::uint64_t TrophyProgress(const TrophyDefinition& trophy, std::optional<std::uint64_t> stat, bool unlocked);

struct TrophyCounts {
std::uint32_t trophies = 0;
std::uint32_t unlocked = 0;
std::array<std::uint32_t, GradeBronze + 1> grades{};
std::array<std::uint32_t, GradeBronze + 1> unlockedGrades{};

std::uint32_t Percentage() const;
};

TrophyCounts CountTrophies(const TrophyPackage& package, std::optional<std::int32_t> group);

}

#endif
96 changes: 96 additions & 0 deletions core/libs/prx/libSceNpTrophy2/include/TrophyPackage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#ifndef CORE_LIBS_PRX_LIBSCENPTROPHY2_INCLUDE_TROPHYPACKAGE_HPP
#define CORE_LIBS_PRX_LIBSCENPTROPHY2_INCLUDE_TROPHYPACKAGE_HPP

#include <cstddef>
#include <cstdint>
#include <functional>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace NpTrophy2 {

inline constexpr std::uint32_t UcpMagic = 0xB228C60Au;
inline constexpr std::uint32_t UcpVersion = 1;
inline constexpr std::uint64_t UcpHeaderSize = 0x40;
inline constexpr std::uint64_t UcpTocHeaderSize = 0x20;
inline constexpr std::uint64_t UcpEntrySize = 0x40;
inline constexpr std::size_t UcpNameSize = 0x20;
inline constexpr std::uint64_t UcpMaxEntries = 4096;

using UcpFiles = std::map<std::string, std::string_view, std::less<>>;

UcpFiles ReadUcp(std::string_view data);

class Json {
public:
enum class Kind { Null, Bool, Number, String, Array, Object };

Kind kind = Kind::Null;
bool boolean = false;
std::string text;
std::vector<Json> items;
std::vector<std::pair<std::string, Json>> members;

const Json* Find(std::string_view key) const;
bool IsTrue() const;
std::optional<std::uint64_t> Unsigned() const;
std::optional<std::int32_t> Id() const;
};

Json ParseJson(std::string_view text);

inline constexpr std::int32_t GradePlatinum = 1;
inline constexpr std::int32_t GradeGold = 2;
inline constexpr std::int32_t GradeSilver = 3;
inline constexpr std::int32_t GradeBronze = 4;
inline constexpr std::int32_t BaseGroup = -1;
inline constexpr std::size_t MaxTrophies = 1000;
inline constexpr std::size_t MaxGroups = 50;

enum class Comparator { None, GreaterEqual, Greater, LessEqual, Less };

struct UnlockCondition {
std::uint64_t statId = 0;
std::uint64_t target = 0;
Comparator comparator = Comparator::None;

bool Reached(std::uint64_t value) const;
};

struct TrophyDefinition {
std::int32_t id = 0;
std::int32_t grade = GradeBronze;
std::int32_t groupId = BaseGroup;
bool hidden = false;
bool hasReward = false;
bool progressive = false;
std::optional<std::int32_t> platinumId;
std::optional<std::uint64_t> statId;
std::optional<std::uint64_t> target;
Comparator comparator = Comparator::None;
std::string name;
std::string detail;
std::string reward;

std::optional<UnlockCondition> Condition() const;
};

struct TrophyPackage {
std::string language;
std::string title;
std::map<std::int32_t, TrophyDefinition> trophies;
std::map<std::int32_t, std::string> groups;
std::vector<std::string> skipped;
};

TrophyPackage ParseTrophyPackage(std::string_view ucp, std::string_view language);

std::string_view LanguageTag(int consoleLanguage);

}

#endif
50 changes: 46 additions & 4 deletions core/libs/prx/libSceNpTrophy2/src/Context.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,68 @@
#include <cstddef>
#include <cstdint>
#include <map>
#include <mutex>
#include <stdexcept>
#include <string>

#include "prx/libSceNpTrophy2/include/NpTrophy2.hpp"
#include "prx/libSceNpTrophy2/include/TitleTrophies.hpp"
#include "prx/libc/include/General.hpp"

namespace NpTrophy2 {

namespace {

struct Contexts {
std::mutex mutex;
std::map<int, ContextOwner> owners;
int next = NP_TROPHY2_CONTEXT_DEFAULT;
};

Contexts& contexts() {
static Contexts table;
return table;
}

}

int CreateContext(std::int32_t userId, std::uint32_t serviceLabel) {
auto& table = contexts();
std::lock_guard lock(table.mutex);
const int context = table.next++;
table.owners[context] = {userId, serviceLabel};
return context;
}

void DestroyContext(int context) {
auto& table = contexts();
std::lock_guard lock(table.mutex);
table.owners.erase(context);
}

std::optional<ContextOwner> FindContext(int context) {
auto& table = contexts();
std::lock_guard lock(table.mutex);
const auto found = table.owners.find(context);
if (found == table.owners.end()) return std::nullopt;
return found->second;
}

}

extern "C" {

int APS5_VABI sceNpTrophy2CreateContext(int* context, int user_id, uint32_t service_label, uint64_t options) {
if (context == nullptr) {
APS5_INVALID_ARG_EX;
}
(void)user_id;
(void)service_label;
(void)options;
*context = NP_TROPHY2_CONTEXT_DEFAULT;
*context = NpTrophy2::CreateContext(user_id, service_label);
return SCE_NP_TROPHY2_OK;
}

int APS5_VABI sceNpTrophy2DestroyContext(int context) {
(void)context;
NpTrophy2::DestroyContext(context);
return SCE_NP_TROPHY2_OK;
}

Expand Down
54 changes: 40 additions & 14 deletions core/libs/prx/libSceNpTrophy2/src/GameInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <cstddef>
#include <cstring>
#include <optional>
#include <stdexcept>
#include <string>

#include "prx/libSceNpTrophy2/include/NpTrophy2.hpp"
#include "prx/libSceNpTrophy2/include/TitleTrophies.hpp"
#include "prx/libc/include/General.hpp"

extern "C" {
Expand All @@ -12,26 +14,50 @@ int APS5_VABI sceNpTrophy2GetGameInfo(int context, int handle, NpTrophy2GameDeta
if (details == nullptr && data == nullptr) {
APS5_INVALID_ARG_EX;
}
(void)context;
(void)handle;
const auto owner = NpTrophy2::FindContext(context);
const auto* package = owner ? NpTrophy2::Package(owner->serviceLabel) : nullptr;
if (package == nullptr) {
if (details != nullptr) {
std::memset(details, 0, sizeof(*details));
details->num_groups = NP_TROPHY2_NUM_GROUPS;
details->num_trophies = NP_TROPHY2_NUM_TROPHIES;
details->num_platinum = NP_TROPHY2_NUM_PLATINUM;
details->num_gold = NP_TROPHY2_NUM_GOLD;
details->num_silver = NP_TROPHY2_NUM_SILVER;
details->num_bronze = NP_TROPHY2_NUM_BRONZE;
std::strncpy(details->title, NP_TROPHY2_GAME_TITLE, sizeof(details->title) - 1);
}
if (data != nullptr) {
std::memset(data, 0, sizeof(*data));
data->unlocked_trophies = NP_TROPHY2_UNLOCKED_TROPHIES;
data->unlocked_platinum = NP_TROPHY2_UNLOCKED_PLATINUM;
data->unlocked_gold = NP_TROPHY2_UNLOCKED_GOLD;
data->unlocked_silver = NP_TROPHY2_UNLOCKED_SILVER;
data->unlocked_bronze = NP_TROPHY2_UNLOCKED_BRONZE;
data->progress_percentage = NP_TROPHY2_PROGRESS_PERCENTAGE;
}
return SCE_NP_TROPHY2_OK;
}
const auto counts = NpTrophy2::CountTrophies(*package, std::nullopt);
if (details != nullptr) {
std::memset(details, 0, sizeof(*details));
details->num_groups = NP_TROPHY2_NUM_GROUPS;
details->num_trophies = NP_TROPHY2_NUM_TROPHIES;
details->num_platinum = NP_TROPHY2_NUM_PLATINUM;
details->num_gold = NP_TROPHY2_NUM_GOLD;
details->num_silver = NP_TROPHY2_NUM_SILVER;
details->num_bronze = NP_TROPHY2_NUM_BRONZE;
std::strncpy(details->title, NP_TROPHY2_GAME_TITLE, sizeof(details->title) - 1);
details->num_groups = static_cast<std::uint32_t>(package->groups.size());
details->num_trophies = counts.trophies;
details->num_platinum = counts.grades[NpTrophy2::GradePlatinum];
details->num_gold = counts.grades[NpTrophy2::GradeGold];
details->num_silver = counts.grades[NpTrophy2::GradeSilver];
details->num_bronze = counts.grades[NpTrophy2::GradeBronze];
NpTrophy2::CopyText(details->title, sizeof(details->title), package->title);
}
if (data != nullptr) {
std::memset(data, 0, sizeof(*data));
data->unlocked_trophies = NP_TROPHY2_UNLOCKED_TROPHIES;
data->unlocked_platinum = NP_TROPHY2_UNLOCKED_PLATINUM;
data->unlocked_gold = NP_TROPHY2_UNLOCKED_GOLD;
data->unlocked_silver = NP_TROPHY2_UNLOCKED_SILVER;
data->unlocked_bronze = NP_TROPHY2_UNLOCKED_BRONZE;
data->progress_percentage = NP_TROPHY2_PROGRESS_PERCENTAGE;
data->unlocked_trophies = counts.unlocked;
data->unlocked_platinum = counts.unlockedGrades[NpTrophy2::GradePlatinum];
data->unlocked_gold = counts.unlockedGrades[NpTrophy2::GradeGold];
data->unlocked_silver = counts.unlockedGrades[NpTrophy2::GradeSilver];
data->unlocked_bronze = counts.unlockedGrades[NpTrophy2::GradeBronze];
data->progress_percentage = counts.Percentage();
}
return SCE_NP_TROPHY2_OK;
}
Expand Down
Loading
Loading