IAddOnContentManager.cs 10 KB

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