aoc_u.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/patch_manager.h"
  12. #include "core/file_sys/registered_cache.h"
  13. #include "core/hle/ipc_helpers.h"
  14. #include "core/hle/kernel/kernel.h"
  15. #include "core/hle/kernel/process.h"
  16. #include "core/hle/kernel/readable_event.h"
  17. #include "core/hle/kernel/writable_event.h"
  18. #include "core/hle/service/aoc/aoc_u.h"
  19. #include "core/loader/loader.h"
  20. #include "core/settings.h"
  21. namespace Service::AOC {
  22. constexpr u64 DLC_BASE_TITLE_ID_MASK = 0xFFFFFFFFFFFFE000;
  23. constexpr u64 DLC_BASE_TO_AOC_ID = 0x1000;
  24. static bool CheckAOCTitleIDMatchesBase(u64 title_id, u64 base) {
  25. return (title_id & DLC_BASE_TITLE_ID_MASK) == base;
  26. }
  27. static std::vector<u64> AccumulateAOCTitleIDs(Core::System& system) {
  28. std::vector<u64> add_on_content;
  29. const auto& rcu = system.GetContentProvider();
  30. const auto list =
  31. rcu.ListEntriesFilter(FileSys::TitleType::AOC, FileSys::ContentRecordType::Data);
  32. std::transform(list.begin(), list.end(), std::back_inserter(add_on_content),
  33. [](const FileSys::ContentProviderEntry& rce) { return rce.title_id; });
  34. add_on_content.erase(
  35. std::remove_if(
  36. add_on_content.begin(), add_on_content.end(),
  37. [&rcu](u64 tid) {
  38. return rcu.GetEntry(tid, FileSys::ContentRecordType::Data)->GetStatus() !=
  39. Loader::ResultStatus::Success;
  40. }),
  41. add_on_content.end());
  42. return add_on_content;
  43. }
  44. AOC_U::AOC_U(Core::System& system)
  45. : ServiceFramework("aoc:u"), add_on_content(AccumulateAOCTitleIDs(system)), system(system) {
  46. // clang-format off
  47. static const FunctionInfo functions[] = {
  48. {0, nullptr, "CountAddOnContentByApplicationId"},
  49. {1, nullptr, "ListAddOnContentByApplicationId"},
  50. {2, &AOC_U::CountAddOnContent, "CountAddOnContent"},
  51. {3, &AOC_U::ListAddOnContent, "ListAddOnContent"},
  52. {4, nullptr, "GetAddOnContentBaseIdByApplicationId"},
  53. {5, &AOC_U::GetAddOnContentBaseId, "GetAddOnContentBaseId"},
  54. {6, nullptr, "PrepareAddOnContentByApplicationId"},
  55. {7, &AOC_U::PrepareAddOnContent, "PrepareAddOnContent"},
  56. {8, &AOC_U::GetAddOnContentListChangedEvent, "GetAddOnContentListChangedEvent"},
  57. {100, nullptr, "CreateEcPurchasedEventManager"},
  58. {101, nullptr, "CreatePermanentEcPurchasedEventManager"},
  59. };
  60. // clang-format on
  61. RegisterHandlers(functions);
  62. auto& kernel = system.Kernel();
  63. aoc_change_event =
  64. Kernel::WritableEvent::CreateEventPair(kernel, "GetAddOnContentListChanged:Event");
  65. }
  66. AOC_U::~AOC_U() = default;
  67. void AOC_U::CountAddOnContent(Kernel::HLERequestContext& ctx) {
  68. struct Parameters {
  69. u64 process_id;
  70. };
  71. static_assert(sizeof(Parameters) == 8);
  72. IPC::RequestParser rp{ctx};
  73. const auto params = rp.PopRaw<Parameters>();
  74. LOG_DEBUG(Service_AOC, "called. process_id={}", params.process_id);
  75. IPC::ResponseBuilder rb{ctx, 3};
  76. rb.Push(RESULT_SUCCESS);
  77. const auto current = system.CurrentProcess()->GetTitleID();
  78. const auto& disabled = Settings::values.disabled_addons[current];
  79. if (std::find(disabled.begin(), disabled.end(), "DLC") != disabled.end()) {
  80. rb.Push<u32>(0);
  81. return;
  82. }
  83. rb.Push<u32>(static_cast<u32>(
  84. std::count_if(add_on_content.begin(), add_on_content.end(),
  85. [current](u64 tid) { return CheckAOCTitleIDMatchesBase(tid, current); })));
  86. }
  87. void AOC_U::ListAddOnContent(Kernel::HLERequestContext& ctx) {
  88. struct Parameters {
  89. u32 offset;
  90. u32 count;
  91. u64 process_id;
  92. };
  93. static_assert(sizeof(Parameters) == 16);
  94. IPC::RequestParser rp{ctx};
  95. const auto [offset, count, process_id] = rp.PopRaw<Parameters>();
  96. LOG_DEBUG(Service_AOC, "called with offset={}, count={}, process_id={}", offset, count,
  97. process_id);
  98. const auto current = system.CurrentProcess()->GetTitleID();
  99. std::vector<u32> out;
  100. const auto& disabled = Settings::values.disabled_addons[current];
  101. if (std::find(disabled.begin(), disabled.end(), "DLC") == disabled.end()) {
  102. for (u64 content_id : add_on_content) {
  103. if ((content_id & DLC_BASE_TITLE_ID_MASK) != current) {
  104. continue;
  105. }
  106. out.push_back(static_cast<u32>(content_id & 0x7FF));
  107. }
  108. }
  109. if (out.size() < offset) {
  110. IPC::ResponseBuilder rb{ctx, 2};
  111. // TODO(DarkLordZach): Find the correct error code.
  112. rb.Push(RESULT_UNKNOWN);
  113. return;
  114. }
  115. const auto out_count = static_cast<u32>(std::min<size_t>(out.size() - offset, count));
  116. std::rotate(out.begin(), out.begin() + offset, out.end());
  117. out.resize(out_count);
  118. ctx.WriteBuffer(out);
  119. IPC::ResponseBuilder rb{ctx, 3};
  120. rb.Push(RESULT_SUCCESS);
  121. rb.Push(out_count);
  122. }
  123. void AOC_U::GetAddOnContentBaseId(Kernel::HLERequestContext& ctx) {
  124. struct Parameters {
  125. u64 process_id;
  126. };
  127. static_assert(sizeof(Parameters) == 8);
  128. IPC::RequestParser rp{ctx};
  129. const auto params = rp.PopRaw<Parameters>();
  130. LOG_DEBUG(Service_AOC, "called. process_id={}", params.process_id);
  131. IPC::ResponseBuilder rb{ctx, 4};
  132. rb.Push(RESULT_SUCCESS);
  133. const auto title_id = system.CurrentProcess()->GetTitleID();
  134. FileSys::PatchManager pm{title_id};
  135. const auto res = pm.GetControlMetadata();
  136. if (res.first == nullptr) {
  137. rb.Push(title_id + DLC_BASE_TO_AOC_ID);
  138. return;
  139. }
  140. rb.Push(res.first->GetDLCBaseTitleId());
  141. }
  142. void AOC_U::PrepareAddOnContent(Kernel::HLERequestContext& ctx) {
  143. struct Parameters {
  144. s32 addon_index;
  145. u64 process_id;
  146. };
  147. static_assert(sizeof(Parameters) == 16);
  148. IPC::RequestParser rp{ctx};
  149. const auto [addon_index, process_id] = rp.PopRaw<Parameters>();
  150. LOG_WARNING(Service_AOC, "(STUBBED) called with addon_index={}, process_id={}", addon_index,
  151. process_id);
  152. IPC::ResponseBuilder rb{ctx, 2};
  153. rb.Push(RESULT_SUCCESS);
  154. }
  155. void AOC_U::GetAddOnContentListChangedEvent(Kernel::HLERequestContext& ctx) {
  156. LOG_WARNING(Service_AOC, "(STUBBED) called");
  157. IPC::ResponseBuilder rb{ctx, 2, 1};
  158. rb.Push(RESULT_SUCCESS);
  159. rb.PushCopyObjects(aoc_change_event.readable);
  160. }
  161. void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system) {
  162. std::make_shared<AOC_U>(system)->InstallAsService(service_manager);
  163. }
  164. } // namespace Service::AOC