From 270d07be2fb0c190e3b52686dbe7d72c0394f54d Mon Sep 17 00:00:00 2001
From: Liam <byteslice@airmail.cc>
Date: Sat, 17 Feb 2024 11:30:02 -0500
Subject: [PATCH] ns: rewrite IPlatformServiceManager

---
 src/core/CMakeLists.txt                       |   4 +-
 .../file_sys/system_archive/shared_font.cpp   |   2 +-
 .../am/frontend/applet_web_browser.cpp        |   2 +-
 src/core/hle/service/ns/ns.cpp                |   2 +-
 ...nager.cpp => platform_service_manager.cpp} | 130 +++++++-----------
 ...e_manager.h => platform_service_manager.h} |  33 ++++-
 6 files changed, 81 insertions(+), 92 deletions(-)
 rename src/core/hle/service/ns/{iplatform_service_manager.cpp => platform_service_manager.cpp} (70%)
 rename src/core/hle/service/ns/{iplatform_service_manager.h => platform_service_manager.h} (58%)

diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index c625550025..5eedaaf357 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -739,8 +739,6 @@ add_library(core STATIC
     hle/service/nim/nim.h
     hle/service/npns/npns.cpp
     hle/service/npns/npns.h
-    hle/service/ns/iplatform_service_manager.cpp
-    hle/service/ns/iplatform_service_manager.h
     hle/service/ns/language.cpp
     hle/service/ns/language.h
     hle/service/ns/ns_results.h
@@ -748,6 +746,8 @@ add_library(core STATIC
     hle/service/ns/ns.h
     hle/service/ns/pdm_qry.cpp
     hle/service/ns/pdm_qry.h
+    hle/service/ns/platform_service_manager.cpp
+    hle/service/ns/platform_service_manager.h
     hle/service/nvdrv/core/container.cpp
     hle/service/nvdrv/core/container.h
     hle/service/nvdrv/core/heap_mapper.cpp
diff --git a/src/core/file_sys/system_archive/shared_font.cpp b/src/core/file_sys/system_archive/shared_font.cpp
index deb52069db..9ea16aa59e 100644
--- a/src/core/file_sys/system_archive/shared_font.cpp
+++ b/src/core/file_sys/system_archive/shared_font.cpp
@@ -9,7 +9,7 @@
 #include "core/file_sys/system_archive/data/font_standard.h"
 #include "core/file_sys/system_archive/shared_font.h"
 #include "core/file_sys/vfs/vfs_vector.h"
-#include "core/hle/service/ns/iplatform_service_manager.h"
+#include "core/hle/service/ns/platform_service_manager.h"
 
 namespace FileSys::SystemArchive {
 
diff --git a/src/core/hle/service/am/frontend/applet_web_browser.cpp b/src/core/hle/service/am/frontend/applet_web_browser.cpp
index bb60260b4f..835c20c4e2 100644
--- a/src/core/hle/service/am/frontend/applet_web_browser.cpp
+++ b/src/core/hle/service/am/frontend/applet_web_browser.cpp
@@ -22,7 +22,7 @@
 #include "core/hle/service/am/frontend/applet_web_browser.h"
 #include "core/hle/service/am/service/storage.h"
 #include "core/hle/service/filesystem/filesystem.h"
-#include "core/hle/service/ns/iplatform_service_manager.h"
+#include "core/hle/service/ns/platform_service_manager.h"
 #include "core/loader/loader.h"
 
 namespace Service::AM::Frontend {
diff --git a/src/core/hle/service/ns/ns.cpp b/src/core/hle/service/ns/ns.cpp
index 2b95cdae3c..84961fc0c3 100644
--- a/src/core/hle/service/ns/ns.cpp
+++ b/src/core/hle/service/ns/ns.cpp
@@ -11,11 +11,11 @@
 #include "core/hle/service/filesystem/filesystem.h"
 #include "core/hle/service/glue/glue_manager.h"
 #include "core/hle/service/ipc_helpers.h"
-#include "core/hle/service/ns/iplatform_service_manager.h"
 #include "core/hle/service/ns/language.h"
 #include "core/hle/service/ns/ns.h"
 #include "core/hle/service/ns/ns_results.h"
 #include "core/hle/service/ns/pdm_qry.h"
+#include "core/hle/service/ns/platform_service_manager.h"
 #include "core/hle/service/server_manager.h"
 #include "core/hle/service/set/settings_server.h"
 
diff --git a/src/core/hle/service/ns/iplatform_service_manager.cpp b/src/core/hle/service/ns/platform_service_manager.cpp
similarity index 70%
rename from src/core/hle/service/ns/iplatform_service_manager.cpp
rename to src/core/hle/service/ns/platform_service_manager.cpp
index 46268be952..23cf05005c 100644
--- a/src/core/hle/service/ns/iplatform_service_manager.cpp
+++ b/src/core/hle/service/ns/platform_service_manager.cpp
@@ -18,9 +18,9 @@
 #include "core/hle/kernel/k_shared_memory.h"
 #include "core/hle/kernel/kernel.h"
 #include "core/hle/kernel/physical_memory.h"
+#include "core/hle/service/cmif_serialization.h"
 #include "core/hle/service/filesystem/filesystem.h"
-#include "core/hle/service/ipc_helpers.h"
-#include "core/hle/service/ns/iplatform_service_manager.h"
+#include "core/hle/service/ns/platform_service_manager.h"
 
 namespace Service::NS {
 
@@ -37,11 +37,6 @@ constexpr u32 EXPECTED_MAGIC{0x36f81a1e};  // What we expect the encrypted bfttf
 constexpr u64 SHARED_FONT_MEM_SIZE{0x1100000};
 constexpr FontRegion EMPTY_REGION{0, 0};
 
-enum class LoadState : u32 {
-    Loading = 0,
-    Done = 1,
-};
-
 static void DecryptSharedFont(const std::vector<u32>& input, Kernel::PhysicalMemory& output,
                               std::size_t& offset) {
     ASSERT_MSG(offset + (input.size() * sizeof(u32)) < SHARED_FONT_MEM_SIZE,
@@ -138,13 +133,13 @@ IPlatformServiceManager::IPlatformServiceManager(Core::System& system_, const ch
     : ServiceFramework{system_, service_name_}, impl{std::make_unique<Impl>()} {
     // clang-format off
     static const FunctionInfo functions[] = {
-        {0, &IPlatformServiceManager::RequestLoad, "RequestLoad"},
-        {1, &IPlatformServiceManager::GetLoadState, "GetLoadState"},
-        {2, &IPlatformServiceManager::GetSize, "GetSize"},
-        {3, &IPlatformServiceManager::GetSharedMemoryAddressOffset, "GetSharedMemoryAddressOffset"},
-        {4, &IPlatformServiceManager::GetSharedMemoryNativeHandle, "GetSharedMemoryNativeHandle"},
-        {5, &IPlatformServiceManager::GetSharedFontInOrderOfPriority, "GetSharedFontInOrderOfPriority"},
-        {6, &IPlatformServiceManager::GetSharedFontInOrderOfPriority, "GetSharedFontInOrderOfPriorityForSystem"},
+        {0, D<&IPlatformServiceManager::RequestLoad>, "RequestLoad"},
+        {1, D<&IPlatformServiceManager::GetLoadState>, "GetLoadState"},
+        {2, D<&IPlatformServiceManager::GetSize>, "GetSize"},
+        {3, D<&IPlatformServiceManager::GetSharedMemoryAddressOffset>, "GetSharedMemoryAddressOffset"},
+        {4, D<&IPlatformServiceManager::GetSharedMemoryNativeHandle>, "GetSharedMemoryNativeHandle"},
+        {5, D<&IPlatformServiceManager::GetSharedFontInOrderOfPriority>, "GetSharedFontInOrderOfPriority"},
+        {6, D<&IPlatformServiceManager::GetSharedFontInOrderOfPriority>, "GetSharedFontInOrderOfPriorityForSystem"},
         {100, nullptr, "RequestApplicationFunctionAuthorization"},
         {101, nullptr, "RequestApplicationFunctionAuthorizationByProcessId"},
         {102, nullptr, "RequestApplicationFunctionAuthorizationByApplicationId"},
@@ -208,47 +203,33 @@ IPlatformServiceManager::IPlatformServiceManager(Core::System& system_, const ch
 
 IPlatformServiceManager::~IPlatformServiceManager() = default;
 
-void IPlatformServiceManager::RequestLoad(HLERequestContext& ctx) {
-    IPC::RequestParser rp{ctx};
-    const u32 shared_font_type{rp.Pop<u32>()};
+Result IPlatformServiceManager::RequestLoad(SharedFontType type) {
     // Games don't call this so all fonts should be loaded
-    LOG_DEBUG(Service_NS, "called, shared_font_type={}", shared_font_type);
-
-    IPC::ResponseBuilder rb{ctx, 2};
-    rb.Push(ResultSuccess);
+    LOG_DEBUG(Service_NS, "called, shared_font_type={}", type);
+    R_SUCCEED();
 }
 
-void IPlatformServiceManager::GetLoadState(HLERequestContext& ctx) {
-    IPC::RequestParser rp{ctx};
-    const u32 font_id{rp.Pop<u32>()};
-    LOG_DEBUG(Service_NS, "called, font_id={}", font_id);
-
-    IPC::ResponseBuilder rb{ctx, 3};
-    rb.Push(ResultSuccess);
-    rb.Push<u32>(static_cast<u32>(LoadState::Done));
+Result IPlatformServiceManager::GetLoadState(Out<LoadState> out_load_state, SharedFontType type) {
+    LOG_DEBUG(Service_NS, "called, shared_font_type={}", type);
+    *out_load_state = LoadState::Loaded;
+    R_SUCCEED();
 }
 
-void IPlatformServiceManager::GetSize(HLERequestContext& ctx) {
-    IPC::RequestParser rp{ctx};
-    const u32 font_id{rp.Pop<u32>()};
-    LOG_DEBUG(Service_NS, "called, font_id={}", font_id);
-
-    IPC::ResponseBuilder rb{ctx, 3};
-    rb.Push(ResultSuccess);
-    rb.Push<u32>(impl->GetSharedFontRegion(font_id).size);
+Result IPlatformServiceManager::GetSize(Out<u32> out_size, SharedFontType type) {
+    LOG_DEBUG(Service_NS, "called, shared_font_type={}", type);
+    *out_size = impl->GetSharedFontRegion(static_cast<size_t>(type)).size;
+    R_SUCCEED();
 }
 
-void IPlatformServiceManager::GetSharedMemoryAddressOffset(HLERequestContext& ctx) {
-    IPC::RequestParser rp{ctx};
-    const u32 font_id{rp.Pop<u32>()};
-    LOG_DEBUG(Service_NS, "called, font_id={}", font_id);
-
-    IPC::ResponseBuilder rb{ctx, 3};
-    rb.Push(ResultSuccess);
-    rb.Push<u32>(impl->GetSharedFontRegion(font_id).offset);
+Result IPlatformServiceManager::GetSharedMemoryAddressOffset(Out<u32> out_shared_memory_offset,
+                                                             SharedFontType type) {
+    LOG_DEBUG(Service_NS, "called, shared_font_type={}", type);
+    *out_shared_memory_offset = impl->GetSharedFontRegion(static_cast<size_t>(type)).offset;
+    R_SUCCEED();
 }
 
-void IPlatformServiceManager::GetSharedMemoryNativeHandle(HLERequestContext& ctx) {
+Result IPlatformServiceManager::GetSharedMemoryNativeHandle(
+    OutCopyHandle<Kernel::KSharedMemory> out_shared_memory_native_handle) {
     // Map backing memory for the font data
     LOG_DEBUG(Service_NS, "called");
 
@@ -256,50 +237,37 @@ void IPlatformServiceManager::GetSharedMemoryNativeHandle(HLERequestContext& ctx
     std::memcpy(kernel.GetFontSharedMem().GetPointer(), impl->shared_font->data(),
                 impl->shared_font->size());
 
-    IPC::ResponseBuilder rb{ctx, 2, 1};
-    rb.Push(ResultSuccess);
-    rb.PushCopyObjects(&kernel.GetFontSharedMem());
+    // FIXME: this shouldn't belong to the kernel
+    *out_shared_memory_native_handle = &kernel.GetFontSharedMem();
+    R_SUCCEED();
 }
 
-void IPlatformServiceManager::GetSharedFontInOrderOfPriority(HLERequestContext& ctx) {
+Result IPlatformServiceManager::GetSharedFontInOrderOfPriority(
+    OutArray<u32, BufferAttr_HipcMapAlias> out_font_codes,
+    OutArray<u32, BufferAttr_HipcMapAlias> out_font_offsets,
+    OutArray<u32, BufferAttr_HipcMapAlias> out_font_sizes, Out<bool> out_fonts_are_loaded,
+    Out<u32> out_font_count, Set::LanguageCode language_code) {
+    LOG_DEBUG(Service_NS, "called, language_code={:#x}", language_code);
+
     // The maximum number of elements that can be returned is 6. Regardless of the available fonts
     // or buffer size.
-    constexpr std::size_t MaxElementCount = 6;
-    IPC::RequestParser rp{ctx};
-    const u64 language_code{rp.Pop<u64>()}; // TODO(ogniK): Find out what this is used for
-    const std::size_t font_codes_count =
-        std::min(MaxElementCount, ctx.GetWriteBufferNumElements<u32>(0));
-    const std::size_t font_offsets_count =
-        std::min(MaxElementCount, ctx.GetWriteBufferNumElements<u32>(1));
-    const std::size_t font_sizes_count =
-        std::min(MaxElementCount, ctx.GetWriteBufferNumElements<u32>(2));
-    LOG_DEBUG(Service_NS, "called, language_code={:X}", language_code);
-
-    IPC::ResponseBuilder rb{ctx, 4};
-    std::vector<u32> font_codes;
-    std::vector<u32> font_offsets;
-    std::vector<u32> font_sizes;
+    constexpr size_t MaxElementCount = 6;
 
     // TODO(ogniK): Have actual priority order
-    for (std::size_t i = 0; i < impl->shared_font_regions.size(); i++) {
-        font_codes.push_back(static_cast<u32>(i));
+    const auto max_size = std::min({MaxElementCount, out_font_codes.size(), out_font_offsets.size(),
+                                    out_font_sizes.size(), impl->shared_font_regions.size()});
+
+    for (size_t i = 0; i < max_size; i++) {
         auto region = impl->GetSharedFontRegion(i);
-        font_offsets.push_back(region.offset);
-        font_sizes.push_back(region.size);
+
+        out_font_codes[i] = static_cast<u32>(i);
+        out_font_offsets[i] = region.offset;
+        out_font_sizes[i] = region.size;
     }
 
-    // Resize buffers if game requests smaller size output
-    font_codes.resize(std::min(font_codes.size(), font_codes_count));
-    font_offsets.resize(std::min(font_offsets.size(), font_offsets_count));
-    font_sizes.resize(std::min(font_sizes.size(), font_sizes_count));
-
-    ctx.WriteBuffer(font_codes, 0);
-    ctx.WriteBuffer(font_offsets, 1);
-    ctx.WriteBuffer(font_sizes, 2);
-
-    rb.Push(ResultSuccess);
-    rb.Push<u8>(static_cast<u8>(LoadState::Done)); // Fonts Loaded
-    rb.Push<u32>(static_cast<u32>(font_codes.size()));
+    *out_fonts_are_loaded = true;
+    *out_font_count = static_cast<u32>(max_size);
+    R_SUCCEED();
 }
 
 } // namespace Service::NS
diff --git a/src/core/hle/service/ns/iplatform_service_manager.h b/src/core/hle/service/ns/platform_service_manager.h
similarity index 58%
rename from src/core/hle/service/ns/iplatform_service_manager.h
rename to src/core/hle/service/ns/platform_service_manager.h
index 03071e02b8..b82c385a6c 100644
--- a/src/core/hle/service/ns/iplatform_service_manager.h
+++ b/src/core/hle/service/ns/platform_service_manager.h
@@ -5,7 +5,9 @@
 
 #include <memory>
 #include <vector>
+#include "core/hle/service/cmif_types.h"
 #include "core/hle/service/service.h"
+#include "core/hle/service/set/settings_types.h"
 
 namespace Service {
 
@@ -23,6 +25,20 @@ enum class FontArchives : u64 {
     ChineseSimple = 0x0100000000000814,
 };
 
+enum class SharedFontType : u32 {
+    JapanUSEuropeStandard = 0,
+    ChineseSimplified = 1,
+    ExtendedChineseSimplified = 2,
+    ChineseTraditional = 3,
+    KoreanHangul = 4,
+    NintendoExtended = 5,
+};
+
+enum class LoadState : u32 {
+    Loading = 0,
+    Loaded = 1,
+};
+
 constexpr std::array<std::pair<FontArchives, const char*>, 7> SHARED_FONTS{
     std::make_pair(FontArchives::Standard, "nintendo_udsg-r_std_003.bfttf"),
     std::make_pair(FontArchives::ChineseSimple, "nintendo_udsg-r_org_zh-cn_003.bfttf"),
@@ -42,12 +58,17 @@ public:
     ~IPlatformServiceManager() override;
 
 private:
-    void RequestLoad(HLERequestContext& ctx);
-    void GetLoadState(HLERequestContext& ctx);
-    void GetSize(HLERequestContext& ctx);
-    void GetSharedMemoryAddressOffset(HLERequestContext& ctx);
-    void GetSharedMemoryNativeHandle(HLERequestContext& ctx);
-    void GetSharedFontInOrderOfPriority(HLERequestContext& ctx);
+    Result RequestLoad(SharedFontType type);
+    Result GetLoadState(Out<LoadState> out_load_state, SharedFontType type);
+    Result GetSize(Out<u32> out_size, SharedFontType type);
+    Result GetSharedMemoryAddressOffset(Out<u32> out_shared_memory_offset, SharedFontType type);
+    Result GetSharedMemoryNativeHandle(
+        OutCopyHandle<Kernel::KSharedMemory> out_shared_memory_native_handle);
+    Result GetSharedFontInOrderOfPriority(OutArray<u32, BufferAttr_HipcMapAlias> out_font_codes,
+                                          OutArray<u32, BufferAttr_HipcMapAlias> out_font_offsets,
+                                          OutArray<u32, BufferAttr_HipcMapAlias> out_font_sizes,
+                                          Out<bool> out_fonts_are_loaded, Out<u32> out_font_count,
+                                          Set::LanguageCode language_code);
 
     struct Impl;
     std::unique_ptr<Impl> impl;