aoc_u.cpp 5.3 KB

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