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
26 changes: 26 additions & 0 deletions core/libs/prx/libSceNpTrophy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,29 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceNpTrophy>")
endif()
endif()

if(BUILD_TESTING)
add_test_executable(guest_np_trophy_system_debug_tests ${LIBS_INCLUDE_DIR}/tests/GuestNpTrophySystemDebug.cpp)
target_include_directories(guest_np_trophy_system_debug_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_np_trophy_system_debug_tests PRIVATE libSceNpTrophy)
configure_windows_unwind(guest_np_trophy_system_debug_tests)
add_test(NAME guest_np_trophy_system_debug COMMAND guest_np_trophy_system_debug_tests)
set_tests_properties(guest_np_trophy_system_debug PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_np_trophy_system_debug PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceNpTrophy>")
endif()
endif()

if(BUILD_TESTING)
add_test_executable(guest_np_trophy_helpers_tests ${LIBS_INCLUDE_DIR}/tests/GuestNpTrophyHelpers.cpp)
target_include_directories(guest_np_trophy_helpers_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_np_trophy_helpers_tests PRIVATE libSceNpTrophy ${CMAKE_DL_LIBS})
configure_windows_unwind(guest_np_trophy_helpers_tests)
add_test(NAME guest_np_trophy_helpers COMMAND guest_np_trophy_helpers_tests)
set_tests_properties(guest_np_trophy_helpers PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_np_trophy_helpers PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceNpTrophy>")
endif()
endif()
49 changes: 24 additions & 25 deletions core/libs/prx/libSceNpTrophy/Export.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdexcept>
#include "prx/libSceNpTrophy/include/Api.hpp"
#include <array>
#include <bit>
#include <cstdint>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -417,14 +418,13 @@ int APS5_VABI sceNpTrophyConfigGetTrophyTitleDetails(void) {
return 0;
}

int APS5_VABI sceNpTrophyConfigHasGroupFeature(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceNpTrophyConfigHasGroupFeature(const std::uint8_t* config) {
if (!config) return 0;
return config[0x90] & 1;
}

int APS5_VABI sceNpTrophyGroupArrayGetNum(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceNpTrophyGroupArrayGetNum(const std::uint32_t* groupArray) {
return std::popcount(groupArray[0]);
}

int APS5_VABI sceNpTrophyIntAbortHandle(void) {
Expand Down Expand Up @@ -482,19 +482,23 @@ int APS5_VABI sceNpTrophyIntNetSyncTitles(void) {
return 0;
}

int APS5_VABI sceNpTrophyNumInfoGetTotal(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceNpTrophyNumInfoGetTotal(const std::uint32_t* numInfo) {
return static_cast<int>(numInfo[0] + numInfo[1] + numInfo[2] + numInfo[3]);
}

int APS5_VABI sceNpTrophySetInfoGetTrophyFlagArray(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceNpTrophySetInfoGetTrophyFlagArray(const std::uint8_t* setInfo, std::uint8_t* flagArray) {
int count = 0;
for (std::size_t index = 0; index < 16; ++index) {
flagArray[index] = setInfo[index] | setInfo[16 + index] | setInfo[32 + index] | setInfo[48 + index];
count += std::popcount(flagArray[index]);
}
return count;
}

int APS5_VABI sceNpTrophySetInfoGetTrophyNum(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
int APS5_VABI sceNpTrophySetInfoGetTrophyNum(const std::uint8_t* setInfo) {
int count = 0;
for (std::size_t index = 0; index < 64; ++index) count += std::popcount(setInfo[index]);
return count;
}

int APS5_VABI sceNpTrophyShowTrophyList(void) {
Expand Down Expand Up @@ -558,8 +562,7 @@ int APS5_VABI sceNpTrophySystemCreateHandle(void) {
}

int APS5_VABI sceNpTrophySystemDbgCtl(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
return static_cast<int>(0x80551692);
}

int APS5_VABI sceNpTrophySystemDebugLockTrophy(void) {
Expand Down Expand Up @@ -588,13 +591,11 @@ int APS5_VABI sceNpTrophySystemDestroyTrophyConfig(void) {
}

int APS5_VABI sceNpTrophySystemGetDbgParam(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
return static_cast<int>(0x80551692);
}

int APS5_VABI sceNpTrophySystemGetDbgParamInt(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
return static_cast<int>(0x80551692);
}

int APS5_VABI sceNpTrophySystemGetGroupIcon(void) {
Expand Down Expand Up @@ -718,13 +719,11 @@ int APS5_VABI sceNpTrophySystemRemoveUserData(void) {
}

int APS5_VABI sceNpTrophySystemSetDbgParam(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
return static_cast<int>(0x80551692);
}

int APS5_VABI sceNpTrophySystemSetDbgParamInt(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
return static_cast<int>(0x80551692);
}

int APS5_VABI sceNpTrophySystemUnregisterTitleSyncedCallback(void) {
Expand Down
10 changes: 5 additions & 5 deletions core/libs/prx/libSceNpTrophy/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ int APS5_VABI sceNpTrophyConfigGetTrophySetInfo();
int APS5_VABI sceNpTrophyConfigGetTrophySetInfoInGroup();
int APS5_VABI sceNpTrophyConfigGetTrophySetVersion();
int APS5_VABI sceNpTrophyConfigGetTrophyTitleDetails();
int APS5_VABI sceNpTrophyConfigHasGroupFeature();
int APS5_VABI sceNpTrophyGroupArrayGetNum();
int APS5_VABI sceNpTrophyConfigHasGroupFeature(const std::uint8_t* config);
int APS5_VABI sceNpTrophyGroupArrayGetNum(const std::uint32_t* groupArray);
int APS5_VABI sceNpTrophyIntAbortHandle();
int APS5_VABI sceNpTrophyIntCheckNetSyncTitles();
int APS5_VABI sceNpTrophyIntCreateHandle();
Expand All @@ -112,9 +112,9 @@ int APS5_VABI sceNpTrophyIntGetRunningTitles();
int APS5_VABI sceNpTrophyIntGetTrpIconByUri();
int APS5_VABI sceNpTrophyIntNetSyncTitle();
int APS5_VABI sceNpTrophyIntNetSyncTitles();
int APS5_VABI sceNpTrophyNumInfoGetTotal();
int APS5_VABI sceNpTrophySetInfoGetTrophyFlagArray();
int APS5_VABI sceNpTrophySetInfoGetTrophyNum();
int APS5_VABI sceNpTrophyNumInfoGetTotal(const std::uint32_t* numInfo);
int APS5_VABI sceNpTrophySetInfoGetTrophyFlagArray(const std::uint8_t* setInfo, std::uint8_t* flagArray);
int APS5_VABI sceNpTrophySetInfoGetTrophyNum(const std::uint8_t* setInfo);
int APS5_VABI sceNpTrophyShowTrophyList();
int APS5_VABI sceNpTrophySystemAbortHandle();
int APS5_VABI sceNpTrophySystemBuildGroupIconUri();
Expand Down
90 changes: 90 additions & 0 deletions core/libs/tests/GuestNpTrophyHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "prx/libSceNpTrophy/include/Api.hpp"

#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif

namespace {

using WordsFn = int (APS5_VABI*)(const std::uint32_t*);
using BytesFn = int (APS5_VABI*)(const std::uint8_t*);
using FlagArrayFn = int (APS5_VABI*)(const std::uint8_t*, std::uint8_t*);

template <typename Function>
Function Resolve(const char* name) {
#ifdef _WIN32
void* symbol = reinterpret_cast<void*>(GetProcAddress(LoadLibraryA("libSceNpTrophy.prx"), name));
#else
void* symbol = dlsym(RTLD_DEFAULT, name);
#endif
if (!symbol) {
std::fprintf(stderr, "%s is not exported by libSceNpTrophy\n", name);
std::abort();
}
return reinterpret_cast<Function>(symbol);
}

void Require(bool value, const char* what) {
if (value) return;
std::fprintf(stderr, "NpTrophy helpers: %s\n", what);
std::abort();
}

}

int main() {
const auto numInfoGetTotal = Resolve<WordsFn>("sceNpTrophyNumInfoGetTotal");
const auto groupArrayGetNum = Resolve<WordsFn>("sceNpTrophyGroupArrayGetNum");
const auto setInfoGetTrophyNum = Resolve<BytesFn>("sceNpTrophySetInfoGetTrophyNum");
const auto setInfoGetTrophyFlagArray = Resolve<FlagArrayFn>("sceNpTrophySetInfoGetTrophyFlagArray");
const auto configHasGroupFeature = Resolve<BytesFn>("sceNpTrophyConfigHasGroupFeature");

const std::uint32_t counts[4] = {1, 20, 300, 4000};
Require(numInfoGetTotal(counts) == 4321, "the four counts were not summed");
const std::uint32_t wrapping[4] = {0xFFFFFFFFu, 2, 0, 0};
Require(numInfoGetTotal(wrapping) == 1, "the sum did not wrap at 32 bits");

const std::uint32_t groups[4] = {0x12345678u, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu};
Require(groupArrayGetNum(groups) == 13, "the group count read more than the first word");
const std::uint32_t allGroups[4] = {0xFFFFFFFFu, 0, 0, 0};
Require(groupArrayGetNum(allGroups) == 32, "32 groups were not counted");

std::uint8_t setInfo[128];
std::memset(setInfo, 0x0F, 64);
std::memset(setInfo + 64, 0xFF, 64);
Require(setInfoGetTrophyNum(setInfo) == 256, "the trophy count read past the four flag arrays");

std::memset(setInfo, 0, sizeof(setInfo));
std::memset(setInfo + 32, 0xFF, 16);
std::uint8_t flags[32];
std::memset(flags, 0xA5, sizeof(flags));
Require(setInfoGetTrophyFlagArray(setInfo, flags) == 128, "a full flag array did not return 128 set bits");
for (std::size_t index = 0; index < 16; ++index) Require(flags[index] == 0xFF, "one full flag array did not fill the output");
for (std::size_t index = 16; index < 32; ++index) Require(flags[index] == 0xA5, "bytes past the 16-byte output were written");
std::memset(setInfo, 0, sizeof(setInfo));
setInfo[0] = 0x01;
setInfo[16] = 0x02;
setInfo[32] = 0x04;
setInfo[48 + 15] = 0x80;
Require(setInfoGetTrophyFlagArray(setInfo, flags) == 4 && flags[0] == 0x07 && flags[15] == 0x80 && flags[1] == 0, "the four flag arrays were not combined with OR, or the set bits were not counted");
std::memset(setInfo, 0, sizeof(setInfo));
Require(setInfoGetTrophyFlagArray(setInfo, flags) == 0 && flags[0] == 0, "an empty set did not return 0");
setInfo[0] = 0x01;
Require(setInfoGetTrophyFlagArray(setInfo, flags) == 1, "a single trophy did not return 1");

std::uint8_t config[0x100] = {};
Require(configHasGroupFeature(nullptr) == 0, "a null config did not answer 0");
config[0x90] = 0x03;
Require(configHasGroupFeature(config) == 1, "bit 0 of the feature byte was not reported");
config[0x90] = 0x02;
Require(configHasGroupFeature(config) == 0, "a bit other than bit 0 was reported");

std::puts("NpTrophy helper tests passed");
return 0;
}
29 changes: 29 additions & 0 deletions core/libs/tests/GuestNpTrophySystemDebug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "prx/libSceNpTrophy/include/Api.hpp"

#include <cstdio>
#include <cstdlib>

int main() {
constexpr int DebugResult = static_cast<int>(0x80551692u);
struct Call {
const char* name;
int (APS5_VABI* function)();
};
const Call calls[] = {
{"sceNpTrophySystemDbgCtl", sceNpTrophySystemDbgCtl},
{"sceNpTrophySystemGetDbgParam", sceNpTrophySystemGetDbgParam},
{"sceNpTrophySystemGetDbgParamInt", sceNpTrophySystemGetDbgParamInt},
{"sceNpTrophySystemSetDbgParam", sceNpTrophySystemSetDbgParam},
{"sceNpTrophySystemSetDbgParamInt", sceNpTrophySystemSetDbgParamInt},
};
for (const Call& call : calls) {
for (int attempt = 0; attempt < 2; ++attempt) {
if (call.function() != DebugResult) {
std::fprintf(stderr, "%s did not return 0x80551692\n", call.name);
return 1;
}
}
}
std::puts("NpTrophy system debug tests passed");
return 0;
}
Loading
Loading