IAddOnContentManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace Ryujinx.HLE.HOS.Services.Ns.Aoc
  8. {
  9. [Service("aoc:u")]
  10. class IAddOnContentManager : IpcService
  11. {
  12. private readonly KEvent _addOnContentListChangedEvent;
  13. private int _addOnContentListChangedEventHandle;
  14. private ulong _addOnContentBaseId;
  15. public IAddOnContentManager(ServiceCtx context)
  16. {
  17. _addOnContentListChangedEvent = new KEvent(context.Device.System.KernelContext);
  18. }
  19. [CommandHipc(0)] // 1.0.0-6.2.0
  20. // CountAddOnContentByApplicationId(u64 title_id) -> u32
  21. public ResultCode CountAddOnContentByApplicationId(ServiceCtx context)
  22. {
  23. ulong titleId = context.RequestData.ReadUInt64();
  24. return CountAddOnContentImpl(context, titleId);
  25. }
  26. [CommandHipc(1)] // 1.0.0-6.2.0
  27. // ListAddOnContentByApplicationId(u64 title_id, u32 start_index, u32 buffer_size) -> (u32 count, buffer<u32>)
  28. public ResultCode ListAddOnContentByApplicationId(ServiceCtx context)
  29. {
  30. ulong titleId = context.RequestData.ReadUInt64();
  31. return ListAddContentImpl(context, titleId);
  32. }
  33. [CommandHipc(2)]
  34. // CountAddOnContent(pid) -> u32
  35. public ResultCode CountAddOnContent(ServiceCtx context)
  36. {
  37. long pid = context.Request.HandleDesc.PId;
  38. // NOTE: Service call arp:r GetApplicationLaunchProperty to get TitleId using the PId.
  39. return CountAddOnContentImpl(context, context.Device.Application.TitleId);
  40. }
  41. [CommandHipc(3)]
  42. // ListAddOnContent(u32 start_index, u32 buffer_size, pid) -> (u32 count, buffer<u32>)
  43. public ResultCode ListAddOnContent(ServiceCtx context)
  44. {
  45. long pid = context.Request.HandleDesc.PId;
  46. // NOTE: Service call arp:r GetApplicationLaunchProperty to get TitleId using the PId.
  47. return ListAddContentImpl(context, context.Device.Application.TitleId);
  48. }
  49. [CommandHipc(4)] // 1.0.0-6.2.0
  50. // GetAddOnContentBaseIdByApplicationId(u64 title_id) -> u64
  51. public ResultCode GetAddOnContentBaseIdByApplicationId(ServiceCtx context)
  52. {
  53. ulong titleId = context.RequestData.ReadUInt64();
  54. return GetAddOnContentBaseIdImpl(context, titleId);
  55. }
  56. [CommandHipc(5)]
  57. // GetAddOnContentBaseId(pid) -> u64
  58. public ResultCode GetAddOnContentBaseId(ServiceCtx context)
  59. {
  60. long pid = context.Request.HandleDesc.PId;
  61. // NOTE: Service call arp:r GetApplicationLaunchProperty to get TitleId using the PId.
  62. return GetAddOnContentBaseIdImpl(context, context.Device.Application.TitleId);
  63. }
  64. [CommandHipc(6)] // 1.0.0-6.2.0
  65. // PrepareAddOnContentByApplicationId(u64 title_id, u32 index)
  66. public ResultCode PrepareAddOnContentByApplicationId(ServiceCtx context)
  67. {
  68. ulong titleId = context.RequestData.ReadUInt64();
  69. return PrepareAddOnContentImpl(context, titleId);
  70. }
  71. [CommandHipc(7)]
  72. // PrepareAddOnContent(u32 index, pid)
  73. public ResultCode PrepareAddOnContent(ServiceCtx context)
  74. {
  75. long pid = context.Request.HandleDesc.PId;
  76. // NOTE: Service call arp:r GetApplicationLaunchProperty to get TitleId using the PId.
  77. return PrepareAddOnContentImpl(context, context.Device.Application.TitleId);
  78. }
  79. [CommandHipc(8)] // 4.0.0+
  80. // GetAddOnContentListChangedEvent() -> handle<copy>
  81. public ResultCode GetAddOnContentListChangedEvent(ServiceCtx context)
  82. {
  83. return GetAddOnContentListChangedEventImpl(context);
  84. }
  85. [CommandHipc(9)] // 10.0.0+
  86. // GetAddOnContentLostErrorCode() -> u64
  87. public ResultCode GetAddOnContentLostErrorCode(ServiceCtx context)
  88. {
  89. // NOTE: 0x7D0A4 -> 2164-1000
  90. context.ResponseData.Write(GetAddOnContentLostErrorCodeImpl(0x7D0A4));
  91. return ResultCode.Success;
  92. }
  93. [CommandHipc(10)] // 11.0.0+
  94. // GetAddOnContentListChangedEventWithProcessId(pid) -> handle<copy>
  95. public ResultCode GetAddOnContentListChangedEventWithProcessId(ServiceCtx context)
  96. {
  97. long pid = context.Request.HandleDesc.PId;
  98. // NOTE: Service call arp:r GetApplicationLaunchProperty to get TitleId using the PId.
  99. // TODO: Found where stored value is used.
  100. ResultCode resultCode = GetAddOnContentBaseIdFromTitleId(context, context.Device.Application.TitleId);
  101. if (resultCode != ResultCode.Success)
  102. {
  103. return resultCode;
  104. }
  105. return GetAddOnContentListChangedEventImpl(context);
  106. }
  107. [CommandHipc(100)] // 7.0.0+
  108. // CreateEcPurchasedEventManager() -> object<nn::ec::IPurchaseEventManager>
  109. public ResultCode CreateEcPurchasedEventManager(ServiceCtx context)
  110. {
  111. MakeObject(context, new IPurchaseEventManager(context.Device.System));
  112. return ResultCode.Success;
  113. }
  114. [CommandHipc(101)] // 9.0.0+
  115. // CreatePermanentEcPurchasedEventManager() -> object<nn::ec::IPurchaseEventManager>
  116. public ResultCode CreatePermanentEcPurchasedEventManager(ServiceCtx context)
  117. {
  118. // NOTE: Service call arp:r to get the TitleId, do some extra checks and pass it to returned interface.
  119. MakeObject(context, new IPurchaseEventManager(context.Device.System));
  120. return ResultCode.Success;
  121. }
  122. [CommandHipc(110)] // 12.0.0+
  123. // CreateContentsServiceManager() -> object<nn::ec::IContentsServiceManager>
  124. public ResultCode CreateContentsServiceManager(ServiceCtx context)
  125. {
  126. MakeObject(context, new IContentsServiceManager());
  127. return ResultCode.Success;
  128. }
  129. private ResultCode CountAddOnContentImpl(ServiceCtx context, ulong titleId)
  130. {
  131. // NOTE: Service call sys:set GetQuestFlag and store it internally.
  132. // If QuestFlag is true, counts some extra titles.
  133. ResultCode resultCode = GetAddOnContentBaseIdFromTitleId(context, titleId);
  134. if (resultCode != ResultCode.Success)
  135. {
  136. return resultCode;
  137. }
  138. // TODO: This should use _addOnContentBaseId;
  139. uint aocCount = (uint)context.Device.System.ContentManager.GetAocCount();
  140. context.ResponseData.Write(aocCount);
  141. return ResultCode.Success;
  142. }
  143. private ResultCode ListAddContentImpl(ServiceCtx context, ulong titleId)
  144. {
  145. // NOTE: Service call sys:set GetQuestFlag and store it internally.
  146. // If QuestFlag is true, counts some extra titles.
  147. uint startIndex = context.RequestData.ReadUInt32();
  148. uint indexNumber = context.RequestData.ReadUInt32();
  149. ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
  150. ulong bufferSize = context.Request.ReceiveBuff[0].Size;
  151. // TODO: This should use _addOnContentBaseId;
  152. uint aocTotalCount = (uint)context.Device.System.ContentManager.GetAocCount();
  153. if (indexNumber > bufferSize / sizeof(uint))
  154. {
  155. return ResultCode.InvalidBufferSize;
  156. }
  157. if (aocTotalCount <= startIndex)
  158. {
  159. context.ResponseData.Write(0);
  160. return ResultCode.Success;
  161. }
  162. IList<ulong> aocTitleIds = context.Device.System.ContentManager.GetAocTitleIds();
  163. GetAddOnContentBaseIdFromTitleId(context, titleId);
  164. uint indexCounter = 0;
  165. for (int i = 0; i < indexNumber; i++)
  166. {
  167. if (i + (int)startIndex < aocTitleIds.Count)
  168. {
  169. context.Memory.Write(bufferPosition + (ulong)i * sizeof(uint), (uint)(aocTitleIds[i + (int)startIndex] - _addOnContentBaseId));
  170. indexCounter++;
  171. }
  172. }
  173. context.ResponseData.Write(indexCounter);
  174. return ResultCode.Success;
  175. }
  176. private ResultCode GetAddOnContentBaseIdImpl(ServiceCtx context, ulong titleId)
  177. {
  178. ResultCode resultCode = GetAddOnContentBaseIdFromTitleId(context, titleId);
  179. context.ResponseData.Write(_addOnContentBaseId);
  180. return resultCode;
  181. }
  182. private ResultCode GetAddOnContentBaseIdFromTitleId(ServiceCtx context, ulong titleId)
  183. {
  184. // NOTE: Service calls arp:r GetApplicationControlProperty to get AddOnContentBaseId using TitleId,
  185. // If the call fails, it returns ResultCode.InvalidPid.
  186. _addOnContentBaseId = context.Device.Application.ControlData.Value.AddOnContentBaseId;
  187. if (_addOnContentBaseId == 0)
  188. {
  189. _addOnContentBaseId = titleId + 0x1000;
  190. }
  191. return ResultCode.Success;
  192. }
  193. private ResultCode PrepareAddOnContentImpl(ServiceCtx context, ulong titleId)
  194. {
  195. uint index = context.RequestData.ReadUInt32();
  196. ResultCode resultCode = GetAddOnContentBaseIdFromTitleId(context, context.Device.Application.TitleId);
  197. if (resultCode != ResultCode.Success)
  198. {
  199. return resultCode;
  200. }
  201. // TODO: Service calls ns:am RegisterContentsExternalKey?, GetOwnedApplicationContentMetaStatus? etc...
  202. // Ideally, this should probably initialize the AocData values for the specified index
  203. Logger.Stub?.PrintStub(LogClass.ServiceNs, new { index });
  204. return ResultCode.Success;
  205. }
  206. private ResultCode GetAddOnContentListChangedEventImpl(ServiceCtx context)
  207. {
  208. if (_addOnContentListChangedEventHandle == 0)
  209. {
  210. if (context.Process.HandleTable.GenerateHandle(_addOnContentListChangedEvent.ReadableEvent, out _addOnContentListChangedEventHandle) != KernelResult.Success)
  211. {
  212. throw new InvalidOperationException("Out of handles!");
  213. }
  214. }
  215. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_addOnContentListChangedEventHandle);
  216. return ResultCode.Success;
  217. }
  218. private static ulong GetAddOnContentLostErrorCodeImpl(int errorCode)
  219. {
  220. return ((ulong)errorCode & 0x1FF | ((((ulong)errorCode >> 9) & 0x1FFF) << 32)) + 2000;
  221. }
  222. }
  223. }