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
31 changes: 17 additions & 14 deletions core/libs/prx/libSceNpCppWebApi/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,19 @@ static bool VectorGrow(VectorBody* body, const std::size_t elementSize) {
return true;
}

static std::uint64_t HostVectorSize(const void* self, const char* function) {
const auto* body = static_cast<const VectorBody*>(static_cast<const IntrusivePtr*>(self)->object);
if (body == nullptr) return 0;
if (body->magic != VectorBodyMagic) NotImplemented_nid_no_patch(function);
return body->size;
}

static IntrusivePtr* HostVectorElement(void* self, const std::uint64_t index, const char* function) {
auto* body = static_cast<VectorBody*>(static_cast<IntrusivePtr*>(self)->object);
if (body == nullptr || body->magic != VectorBodyMagic || body->elementKind != 1) NotImplemented_nid_no_patch(function);
return static_cast<IntrusivePtr*>(body->elements) + index;
}

static int VectorPushBackIntrusivePtr(IntrusivePtr* self, const IntrusivePtr* element) {
VectorBody* body = VectorBodyFor(self, 1, __func__);
if (body == nullptr) return -1;
Expand Down Expand Up @@ -1248,10 +1261,7 @@ bool APS5_VABI _ZNK3sce2Np9CppWebApi14SessionManager2V154GetUsersAccountIdPlayer
}

void* APS5_VABI _ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_14SessionManager2V113PlayerSessionEEEEixEm(void* self, std::uint64_t index) {
(void)self;
(void)index;
NotImplemented_nid_no_patch(__func__);
return nullptr;
return HostVectorElement(self, index, __func__);
}

int APS5_VABI _ZN3sce2Np9CppWebApi14SessionManager2V117PlayerSessionsApi29ParameterToLeavePlayerSession10initializeEPNS1_6Common10LibContextEPKcSA_() {
Expand Down Expand Up @@ -1673,16 +1683,11 @@ int APS5_VABI _ZN3sce2Np9CppWebApi6Common15TransactionBaseINS2_12IntrusivePtrINS
}

void* APS5_VABI _ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEEEixEm(void* self, std::uint64_t index) {
(void)self;
(void)index;
NotImplemented_nid_no_patch(__func__);
return nullptr;
return HostVectorElement(self, index, __func__);
}

std::size_t APS5_VABI _ZNK3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEEE4sizeEv(const void* self) {
(void)self;
NotImplemented_nid_no_patch(__func__);
return 0;
return HostVectorSize(self, __func__);
}

void* APS5_VABI _ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_13InGameCatalog2V57ProductEEEE5beginEv(void* self) {
Expand All @@ -1698,9 +1703,7 @@ void* APS5_VABI _ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_13InGa
}

std::size_t APS5_VABI _ZNK3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_13InGameCatalog2V53SkuEEEE4sizeEv(const void* self) {
(void)self;
NotImplemented_nid_no_patch(__func__);
return 0;
return HostVectorSize(self, __func__);
}

void* APS5_VABI _ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_17TitleCloudStorage2V110DataStatusEEEE5beginEv(void* self) {
Expand Down
52 changes: 52 additions & 0 deletions core/libs/tests/GuestNpCppWebApiOffline.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "prx/libSceNpCppWebApi/include/Api.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>

using SelfCall = int (APS5_VABI*)(void*);
using Size = std::size_t (APS5_VABI*)(const void*);
using Element = void* (APS5_VABI*)(void*, std::uint64_t);

static void Require(bool value, const char* what) {
if (value) return;
Expand All @@ -23,8 +27,56 @@ static void TestIteratorDestructors() {
for (const auto byte : iterator) Require(byte == 0x5a, "an iterator destructor wrote to the iterator");
}

static const Size sizes[] = {
_ZNK3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_13InGameCatalog2V53SkuEEEE4sizeEv,
_ZNK3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEEE4sizeEv,
};

static const Element elements[] = {
_ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_13InGameCatalog2V59ContainerEEEEixEm,
_ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_14SessionManager2V113PlayerSessionEEEEixEm,
};

template <typename TCall>
static bool Throws(TCall call) {
try {
call();
} catch (const std::runtime_error&) {
return true;
}
return false;
}

static void TestVectorSizeAndElements() {
std::uint64_t context = 1;
IntrusivePtr vector{nullptr, nullptr, &context};
for (const auto size : sizes) Require(size(&vector) == 0, "a vector without a body has elements");
for (const auto element : elements) Require(Throws([&] { element(&vector, 0); }), "indexing a vector without a body did not throw");

alignas(8) std::int32_t first[4] = {1};
alignas(8) std::int32_t second[4] = {1};
const IntrusivePtr pushed[] = {{first, nullptr, &context}, {second, nullptr, &context}};
_ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_14SessionManager2V112JoinableUserEEEEC1EPNS2_10LibContextE(&vector, &context);
for (const auto& element : pushed)
Require(_ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_14SessionManager2V112JoinableUserEEEE8pushBackERKS8_(&vector, &element) == 0, "pushBack failed");
for (const auto size : sizes) Require(size(&vector) == 2, "a vector reported the wrong size");
for (const auto element : elements) {
Require(static_cast<IntrusivePtr*>(element(&vector, 0))->object == first, "operator[] returned the wrong first element");
Require(static_cast<IntrusivePtr*>(element(&vector, 1))->object == second, "operator[] returned the wrong second element");
Require(static_cast<IntrusivePtr*>(element(&vector, 1)) == static_cast<IntrusivePtr*>(element(&vector, 0)) + 1, "operator[] elements are not contiguous");
}
_ZN3sce2Np9CppWebApi6Common6VectorINS2_12IntrusivePtrINS1_14SessionManager2V112JoinableUserEEEED1Ev(&vector);
Require(first[0] == 1 && second[0] == 1, "destroying the vector did not release its references");

alignas(8) unsigned char foreignBody[0x50] = {};
IntrusivePtr foreign{foreignBody, nullptr, &context};
for (const auto size : sizes) Require(Throws([&] { size(&foreign); }), "sizing a foreign body did not throw");
for (const auto element : elements) Require(Throws([&] { element(&foreign, 0); }), "indexing a foreign body did not throw");
}

int main() {
TestIteratorDestructors();
TestVectorSizeAndElements();
std::puts("NpCppWebApi offline tests passed");
return 0;
}
Loading