aoc_u.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/core.h"
  9. #include "core/file_sys/common_funcs.h"
  10. #include "core/file_sys/content_archive.h"
  11. #include "core/file_sys/control_metadata.h"
  12. #include "core/file_sys/nca_metadata.h"
  13. #include "core/file_sys/patch_manager.h"
  14. #include "core/file_sys/registered_cache.h"
  15. #include "core/hle/ipc_helpers.h"
  16. #include "core/hle/kernel/k_event.h"
  17. #include "core/hle/kernel/k_readable_event.h"
  18. #include "core/hle/kernel/kernel.h"
  19. #include "core/hle/kernel/process.h"
  20. #include "core/hle/service/aoc/aoc_u.h"
  21. #include "core/loader/loader.h"
  22. #include "core/settings.h"
  23. namespace Service::AOC {
  24. static bool CheckAOCTitleIDMatchesBase(u64 title_id, u64 base) {
  25. return FileSys::GetBaseTitleID(title_id) == 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. class IPurchaseEventManager final : public ServiceFramework<IPurchaseEventManager> {
  45. public:
  46. explicit IPurchaseEventManager(Core::System& system_)
  47. : ServiceFramework{system_, "IPurchaseEventManager"} {
  48. // clang-format off
  49. static const FunctionInfo functions[] = {
  50. {0, &IPurchaseEventManager::SetDefaultDeliveryTarget, "SetDefaultDeliveryTarget"},
  51. {1, &IPurchaseEventManager::SetDeliveryTarget, "SetDeliveryTarget"},
  52. {2, &IPurchaseEventManager::GetPurchasedEventReadableHandle, "GetPurchasedEventReadableHandle"},
  53. {3, nullptr, "PopPurchasedProductInfo"},
  54. {4, nullptr, "PopPurchasedProductInfoWithUid"},
  55. };
  56. // clang-format on
  57. RegisterHandlers(functions);
  58. purchased_event =
  59. Kernel::KEvent::Create(system.Kernel(), "IPurchaseEventManager:PurchasedEvent");
  60. purchased_event->Initialize();
  61. }
  62. private:
  63. void SetDefaultDeliveryTarget(Kernel::HLERequestContext& ctx) {
  64. IPC::RequestParser rp{ctx};
  65. const auto unknown_1 = rp.Pop<u64>();
  66. [[maybe_unused]] const auto unknown_2 = ctx.ReadBuffer();
  67. LOG_WARNING(Service_AOC, "(STUBBED) called, unknown_1={}", unknown_1);
  68. IPC::ResponseBuilder rb{ctx, 2};
  69. rb.Push(RESULT_SUCCESS);
  70. }
  71. void SetDeliveryTarget(Kernel::HLERequestContext& ctx) {
  72. IPC::RequestParser rp{ctx};
  73. const auto unknown_1 = rp.Pop<u64>();
  74. [[maybe_unused]] const auto unknown_2 = ctx.ReadBuffer();
  75. LOG_WARNING(Service_AOC, "(STUBBED) called, unknown_1={}", unknown_1);
  76. IPC::ResponseBuilder rb{ctx, 2};
  77. rb.Push(RESULT_SUCCESS);
  78. }
  79. void GetPurchasedEventReadableHandle(Kernel::HLERequestContext& ctx) {
  80. LOG_WARNING(Service_AOC, "called");
  81. IPC::ResponseBuilder rb{ctx, 2, 1};
  82. rb.Push(RESULT_SUCCESS);
  83. rb.PushCopyObjects(purchased_event->GetReadableEvent());
  84. }
  85. std::shared_ptr<Kernel::KEvent> purchased_event;
  86. };
  87. AOC_U::AOC_U(Core::System& system_)
  88. : ServiceFramework{system_, "aoc:u"}, add_on_content{AccumulateAOCTitleIDs(system)} {
  89. // clang-format off
  90. static const FunctionInfo functions[] = {
  91. {0, nullptr, "CountAddOnContentByApplicationId"},
  92. {1, nullptr, "ListAddOnContentByApplicationId"},
  93. {2, &AOC_U::CountAddOnContent, "CountAddOnContent"},
  94. {3, &AOC_U::ListAddOnContent, "ListAddOnContent"},
  95. {4, nullptr, "GetAddOnContentBaseIdByApplicationId"},
  96. {5, &AOC_U::GetAddOnContentBaseId, "GetAddOnContentBaseId"},
  97. {6, nullptr, "PrepareAddOnContentByApplicationId"},
  98. {7, &AOC_U::PrepareAddOnContent, "PrepareAddOnContent"},
  99. {8, &AOC_U::GetAddOnContentListChangedEvent, "GetAddOnContentListChangedEvent"},
  100. {9, nullptr, "GetAddOnContentLostErrorCode"},
  101. {100, &AOC_U::CreateEcPurchasedEventManager, "CreateEcPurchasedEventManager"},
  102. {101, &AOC_U::CreatePermanentEcPurchasedEventManager, "CreatePermanentEcPurchasedEventManager"},
  103. };
  104. // clang-format on
  105. RegisterHandlers(functions);
  106. auto& kernel = system.Kernel();
  107. aoc_change_event = Kernel::KEvent::Create(kernel, "GetAddOnContentListChanged:Event");
  108. aoc_change_event->Initialize();
  109. }
  110. AOC_U::~AOC_U() = default;
  111. void AOC_U::CountAddOnContent(Kernel::HLERequestContext& ctx) {
  112. struct Parameters {
  113. u64 process_id;
  114. };
  115. static_assert(sizeof(Parameters) == 8);
  116. IPC::RequestParser rp{ctx};
  117. const auto params = rp.PopRaw<Parameters>();
  118. LOG_DEBUG(Service_AOC, "called. process_id={}", params.process_id);
  119. IPC::ResponseBuilder rb{ctx, 3};
  120. rb.Push(RESULT_SUCCESS);
  121. const auto current = system.CurrentProcess()->GetTitleID();
  122. const auto& disabled = Settings::values.disabled_addons[current];
  123. if (std::find(disabled.begin(), disabled.end(), "DLC") != disabled.end()) {
  124. rb.Push<u32>(0);
  125. return;
  126. }
  127. rb.Push<u32>(static_cast<u32>(
  128. std::count_if(add_on_content.begin(), add_on_content.end(),
  129. [current](u64 tid) { return CheckAOCTitleIDMatchesBase(tid, current); })));
  130. }
  131. void AOC_U::ListAddOnContent(Kernel::HLERequestContext& ctx) {
  132. struct Parameters {
  133. u32 offset;
  134. u32 count;
  135. u64 process_id;
  136. };
  137. static_assert(sizeof(Parameters) == 16);
  138. IPC::RequestParser rp{ctx};
  139. const auto [offset, count, process_id] = rp.PopRaw<Parameters>();
  140. LOG_DEBUG(Service_AOC, "called with offset={}, count={}, process_id={}", offset, count,
  141. process_id);
  142. const auto current = system.CurrentProcess()->GetTitleID();
  143. std::vector<u32> out;
  144. const auto& disabled = Settings::values.disabled_addons[current];
  145. if (std::find(disabled.begin(), disabled.end(), "DLC") == disabled.end()) {
  146. for (u64 content_id : add_on_content) {
  147. if (FileSys::GetBaseTitleID(content_id) != current) {
  148. continue;
  149. }
  150. out.push_back(static_cast<u32>(FileSys::GetAOCID(content_id)));
  151. }
  152. }
  153. if (out.size() < offset) {
  154. IPC::ResponseBuilder rb{ctx, 2};
  155. // TODO(DarkLordZach): Find the correct error code.
  156. rb.Push(RESULT_UNKNOWN);
  157. return;
  158. }
  159. const auto out_count = static_cast<u32>(std::min<size_t>(out.size() - offset, count));
  160. std::rotate(out.begin(), out.begin() + offset, out.end());
  161. out.resize(out_count);
  162. ctx.WriteBuffer(out);
  163. IPC::ResponseBuilder rb{ctx, 3};
  164. rb.Push(RESULT_SUCCESS);
  165. rb.Push(out_count);
  166. }
  167. void AOC_U::GetAddOnContentBaseId(Kernel::HLERequestContext& ctx) {
  168. struct Parameters {
  169. u64 process_id;
  170. };
  171. static_assert(sizeof(Parameters) == 8);
  172. IPC::RequestParser rp{ctx};
  173. const auto params = rp.PopRaw<Parameters>();
  174. LOG_DEBUG(Service_AOC, "called. process_id={}", params.process_id);
  175. IPC::ResponseBuilder rb{ctx, 4};
  176. rb.Push(RESULT_SUCCESS);
  177. const auto title_id = system.CurrentProcess()->GetTitleID();
  178. const FileSys::PatchManager pm{title_id, system.GetFileSystemController(),
  179. system.GetContentProvider()};
  180. const auto res = pm.GetControlMetadata();
  181. if (res.first == nullptr) {
  182. rb.Push(FileSys::GetAOCBaseTitleID(title_id));
  183. return;
  184. }
  185. rb.Push(res.first->GetDLCBaseTitleId());
  186. }
  187. void AOC_U::PrepareAddOnContent(Kernel::HLERequestContext& ctx) {
  188. struct Parameters {
  189. s32 addon_index;
  190. u64 process_id;
  191. };
  192. static_assert(sizeof(Parameters) == 16);
  193. IPC::RequestParser rp{ctx};
  194. const auto [addon_index, process_id] = rp.PopRaw<Parameters>();
  195. LOG_WARNING(Service_AOC, "(STUBBED) called with addon_index={}, process_id={}", addon_index,
  196. process_id);
  197. IPC::ResponseBuilder rb{ctx, 2};
  198. rb.Push(RESULT_SUCCESS);
  199. }
  200. void AOC_U::GetAddOnContentListChangedEvent(Kernel::HLERequestContext& ctx) {
  201. LOG_WARNING(Service_AOC, "(STUBBED) called");
  202. IPC::ResponseBuilder rb{ctx, 2, 1};
  203. rb.Push(RESULT_SUCCESS);
  204. rb.PushCopyObjects(aoc_change_event->GetReadableEvent());
  205. }
  206. void AOC_U::CreateEcPurchasedEventManager(Kernel::HLERequestContext& ctx) {
  207. LOG_WARNING(Service_AOC, "(STUBBED) called");
  208. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  209. rb.Push(RESULT_SUCCESS);
  210. rb.PushIpcInterface<IPurchaseEventManager>(system);
  211. }
  212. void AOC_U::CreatePermanentEcPurchasedEventManager(Kernel::HLERequestContext& ctx) {
  213. LOG_WARNING(Service_AOC, "(STUBBED) called");
  214. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  215. rb.Push(RESULT_SUCCESS);
  216. rb.PushIpcInterface<IPurchaseEventManager>(system);
  217. }
  218. void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system) {
  219. std::make_shared<AOC_U>(system)->InstallAsService(service_manager);
  220. }
  221. } // namespace Service::AOC