aoc_u.cpp 4.6 KB

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