IAddOnContentManager.cs 12 KB

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