aoc_u.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <numeric>
  6. #include <vector>
  7. #include "common/logging/log.h"
  8. #include "core/file_sys/content_archive.h"
  9. #include "core/file_sys/control_metadata.h"
  10. #include "core/file_sys/nca_metadata.h"
  11. #include "core/file_sys/partition_filesystem.h"
  12. #include "core/file_sys/patch_manager.h"
  13. #include "core/file_sys/registered_cache.h"
  14. #include "core/hle/ipc_helpers.h"
  15. #include "core/hle/kernel/kernel.h"
  16. #include "core/hle/kernel/process.h"
  17. #include "core/hle/kernel/readable_event.h"
  18. #include "core/hle/kernel/writable_event.h"
  19. #include "core/hle/service/aoc/aoc_u.h"
  20. #include "core/hle/service/filesystem/filesystem.h"
  21. #include "core/loader/loader.h"
  22. namespace Service::AOC {
  23. constexpr u64 DLC_BASE_TITLE_ID_MASK = 0xFFFFFFFFFFFFE000;
  24. constexpr u64 DLC_BASE_TO_AOC_ID = 0x1000;
  25. static bool CheckAOCTitleIDMatchesBase(u64 title_id, u64 base) {
  26. return (title_id & DLC_BASE_TITLE_ID_MASK) == base;
  27. }
  28. static std::vector<u64> AccumulateAOCTitleIDs() {
  29. std::vector<u64> add_on_content;
  30. const auto rcu = FileSystem::GetUnionContents();
  31. const auto list =
  32. rcu->ListEntriesFilter(FileSys::TitleType::AOC, FileSys::ContentRecordType::Data);
  33. std::transform(list.begin(), list.end(), std::back_inserter(add_on_content),
  34. [](const FileSys::RegisteredCacheEntry& rce) { return rce.title_id; });
  35. add_on_content.erase(
  36. std::remove_if(
  37. add_on_content.begin(), add_on_content.end(),
  38. [&rcu](u64 tid) {
  39. return rcu->GetEntry(tid, FileSys::ContentRecordType::Data)->GetStatus() !=
  40. Loader::ResultStatus::Success;
  41. }),
  42. add_on_content.end());
  43. return add_on_content;
  44. }
  45. AOC_U::AOC_U() : ServiceFramework("aoc:u"), add_on_content(AccumulateAOCTitleIDs()) {
  46. static const FunctionInfo functions[] = {
  47. {0, nullptr, "CountAddOnContentByApplicationId"},
  48. {1, nullptr, "ListAddOnContentByApplicationId"},
  49. {2, &AOC_U::CountAddOnContent, "CountAddOnContent"},
  50. {3, &AOC_U::ListAddOnContent, "ListAddOnContent"},
  51. {4, nullptr, "GetAddOnContentBaseIdByApplicationId"},
  52. {5, &AOC_U::GetAddOnContentBaseId, "GetAddOnContentBaseId"},
  53. {6, nullptr, "PrepareAddOnContentByApplicationId"},
  54. {7, &AOC_U::PrepareAddOnContent, "PrepareAddOnContent"},
  55. {8, &AOC_U::GetAddOnContentListChangedEvent, "GetAddOnContentListChangedEvent"},
  56. };
  57. RegisterHandlers(functions);
  58. auto& kernel = Core::System::GetInstance().Kernel();
  59. aoc_change_event = Kernel::WritableEvent::CreateEventPair(kernel, Kernel::ResetType::Sticky,
  60. "GetAddOnContentListChanged:Event");
  61. }
  62. AOC_U::~AOC_U() = default;
  63. void AOC_U::CountAddOnContent(Kernel::HLERequestContext& ctx) {
  64. LOG_DEBUG(Service_AOC, "called");
  65. IPC::ResponseBuilder rb{ctx, 3};
  66. rb.Push(RESULT_SUCCESS);
  67. const auto current = Core::System::GetInstance().CurrentProcess()->GetTitleID();
  68. rb.Push<u32>(static_cast<u32>(
  69. std::count_if(add_on_content.begin(), add_on_content.end(),
  70. [current](u64 tid) { return CheckAOCTitleIDMatchesBase(tid, current); })));
  71. }
  72. void AOC_U::ListAddOnContent(Kernel::HLERequestContext& ctx) {
  73. IPC::RequestParser rp{ctx};
  74. const auto offset = rp.PopRaw<u32>();
  75. auto count = rp.PopRaw<u32>();
  76. LOG_DEBUG(Service_AOC, "called with offset={}, count={}", offset, count);
  77. const auto current = Core::System::GetInstance().CurrentProcess()->GetTitleID();
  78. std::vector<u32> out;
  79. for (size_t i = 0; i < add_on_content.size(); ++i) {
  80. if ((add_on_content[i] & DLC_BASE_TITLE_ID_MASK) == current)
  81. out.push_back(static_cast<u32>(add_on_content[i] & 0x7FF));
  82. }
  83. if (out.size() < offset) {
  84. IPC::ResponseBuilder rb{ctx, 2};
  85. // TODO(DarkLordZach): Find the correct error code.
  86. rb.Push(ResultCode(-1));
  87. return;
  88. }
  89. count = static_cast<u32>(std::min<size_t>(out.size() - offset, count));
  90. std::rotate(out.begin(), out.begin() + offset, out.end());
  91. out.resize(count);
  92. ctx.WriteBuffer(out);
  93. IPC::ResponseBuilder rb{ctx, 3};
  94. rb.Push(RESULT_SUCCESS);
  95. rb.Push(count);
  96. }
  97. void AOC_U::GetAddOnContentBaseId(Kernel::HLERequestContext& ctx) {
  98. LOG_DEBUG(Service_AOC, "called");
  99. IPC::ResponseBuilder rb{ctx, 4};
  100. rb.Push(RESULT_SUCCESS);
  101. const auto title_id = Core::System::GetInstance().CurrentProcess()->GetTitleID();
  102. FileSys::PatchManager pm{title_id};
  103. const auto res = pm.GetControlMetadata();
  104. if (res.first == nullptr) {
  105. rb.Push(title_id + DLC_BASE_TO_AOC_ID);
  106. return;
  107. }
  108. rb.Push(res.first->GetDLCBaseTitleId());
  109. }
  110. void AOC_U::PrepareAddOnContent(Kernel::HLERequestContext& ctx) {
  111. IPC::RequestParser rp{ctx};
  112. const auto aoc_id = rp.PopRaw<u32>();
  113. LOG_WARNING(Service_AOC, "(STUBBED) called with aoc_id={:08X}", aoc_id);
  114. IPC::ResponseBuilder rb{ctx, 2};
  115. rb.Push(RESULT_SUCCESS);
  116. }
  117. void AOC_U::GetAddOnContentListChangedEvent(Kernel::HLERequestContext& ctx) {
  118. LOG_WARNING(Service_AOC, "(STUBBED) called");
  119. IPC::ResponseBuilder rb{ctx, 2, 1};
  120. rb.Push(RESULT_SUCCESS);
  121. rb.PushCopyObjects(aoc_change_event.readable);
  122. }
  123. void InstallInterfaces(SM::ServiceManager& service_manager) {
  124. std::make_shared<AOC_U>()->InstallAsService(service_manager);
  125. }
  126. } // namespace Service::AOC