IAddOnContentManager.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. namespace Ryujinx.HLE.HOS.Services.Ns
  7. {
  8. [Service("aoc:u")]
  9. class IAddOnContentManager : IpcService
  10. {
  11. private readonly KEvent _addOnContentListChangedEvent;
  12. private int _addOnContentListChangedEventHandle;
  13. public IAddOnContentManager(ServiceCtx context)
  14. {
  15. _addOnContentListChangedEvent = new KEvent(context.Device.System.KernelContext);
  16. }
  17. [CommandHipc(2)]
  18. // CountAddOnContent(pid) -> u32
  19. public ResultCode CountAddOnContent(ServiceCtx context)
  20. {
  21. long pid = context.Process.Pid;
  22. // Official code checks ApplicationControlProperty.RuntimeAddOnContentInstall
  23. // if true calls ns:am ListAvailableAddOnContent again to get updated count
  24. byte runtimeAddOnContentInstall = context.Device.Application.ControlData.Value.RuntimeAddOnContentInstall;
  25. if (runtimeAddOnContentInstall != 0)
  26. {
  27. Logger.Warning?.Print(LogClass.ServiceNs, $"RuntimeAddOnContentInstall is true. Some DLC may be missing");
  28. }
  29. uint aocCount = CountAddOnContentImpl(context);
  30. context.ResponseData.Write(aocCount);
  31. Logger.Stub?.PrintStub(LogClass.ServiceNs, new { aocCount, runtimeAddOnContentInstall });
  32. return ResultCode.Success;
  33. }
  34. private static uint CountAddOnContentImpl(ServiceCtx context)
  35. {
  36. return (uint)context.Device.System.ContentManager.GetAocCount();
  37. }
  38. [CommandHipc(3)]
  39. // ListAddOnContent(u32, u32, pid) -> (u32, buffer<u32>)
  40. public ResultCode ListAddOnContent(ServiceCtx context)
  41. {
  42. uint startIndex = context.RequestData.ReadUInt32();
  43. uint bufferSize = context.RequestData.ReadUInt32();
  44. long pid = context.Process.Pid;
  45. var aocTitleIds = context.Device.System.ContentManager.GetAocTitleIds();
  46. uint aocCount = CountAddOnContentImpl(context);
  47. if (aocCount <= startIndex)
  48. {
  49. context.ResponseData.Write((uint)0);
  50. return ResultCode.Success;
  51. }
  52. aocCount = Math.Min(aocCount - startIndex, bufferSize);
  53. context.ResponseData.Write(aocCount);
  54. ulong bufAddr = (ulong)context.Request.ReceiveBuff[0].Position;
  55. ulong aocBaseId = GetAddOnContentBaseIdImpl(context);
  56. for (int i = 0; i < aocCount; ++i)
  57. {
  58. context.Memory.Write(bufAddr + (ulong)i * 4, (int)(aocTitleIds[i + (int)startIndex] - aocBaseId));
  59. }
  60. Logger.Stub?.PrintStub(LogClass.ServiceNs, new { bufferSize, startIndex, aocCount });
  61. return ResultCode.Success;
  62. }
  63. [CommandHipc(5)]
  64. // GetAddOnContentBaseId(pid) -> u64
  65. public ResultCode GetAddonContentBaseId(ServiceCtx context)
  66. {
  67. long pid = context.Process.Pid;
  68. // Official code calls arp:r GetApplicationControlProperty to get AddOnContentBaseId
  69. // If the call fails, calls arp:r GetApplicationLaunchProperty to get App TitleId
  70. ulong aocBaseId = GetAddOnContentBaseIdImpl(context);
  71. context.ResponseData.Write(aocBaseId);
  72. Logger.Stub?.PrintStub(LogClass.ServiceNs, $"aocBaseId={aocBaseId:X16}");
  73. // ResultCode will be error code of GetApplicationLaunchProperty if it fails
  74. return ResultCode.Success;
  75. }
  76. private static ulong GetAddOnContentBaseIdImpl(ServiceCtx context)
  77. {
  78. ulong aocBaseId = context.Device.Application.ControlData.Value.AddOnContentBaseId;
  79. if (aocBaseId == 0)
  80. {
  81. aocBaseId = context.Device.Application.TitleId + 0x1000;
  82. }
  83. return aocBaseId;
  84. }
  85. [CommandHipc(7)]
  86. // PrepareAddOnContent(u32, pid)
  87. public ResultCode PrepareAddOnContent(ServiceCtx context)
  88. {
  89. uint aocIndex = context.RequestData.ReadUInt32();
  90. long pid = context.Process.Pid;
  91. // Official Code calls a bunch of functions from arp:r for aocBaseId
  92. // and ns:am RegisterContentsExternalKey?, GetOwnedApplicationContentMetaStatus? etc...
  93. // Ideally, this should probably initialize the AocData values for the specified index
  94. Logger.Stub?.PrintStub(LogClass.ServiceNs, new { aocIndex });
  95. return ResultCode.Success;
  96. }
  97. [CommandHipc(8)]
  98. // GetAddOnContentListChangedEvent() -> handle<copy>
  99. public ResultCode GetAddOnContentListChangedEvent(ServiceCtx context)
  100. {
  101. // Official code seems to make an internal call to ns:am Cmd 84 GetDynamicCommitEvent()
  102. if (_addOnContentListChangedEventHandle == 0)
  103. {
  104. if (context.Process.HandleTable.GenerateHandle(_addOnContentListChangedEvent.ReadableEvent, out _addOnContentListChangedEventHandle) != KernelResult.Success)
  105. {
  106. throw new InvalidOperationException("Out of handles!");
  107. }
  108. }
  109. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_addOnContentListChangedEventHandle);
  110. Logger.Stub?.PrintStub(LogClass.ServiceNs);
  111. return ResultCode.Success;
  112. }
  113. [CommandHipc(9)] // [10.0.0+]
  114. // GetAddOnContentLostErrorCode() -> u64
  115. public ResultCode GetAddOnContentLostErrorCode(ServiceCtx context)
  116. {
  117. // Seems to calculate ((value & 0x1ff)) + 2000 on 0x7d0a4
  118. // which gives 0x874 (2000+164). 164 being Module ID of `EC (Shop)`
  119. context.ResponseData.Write(2164L);
  120. Logger.Stub?.PrintStub(LogClass.ServiceNs);
  121. return ResultCode.Success;
  122. }
  123. [CommandHipc(100)]
  124. // CreateEcPurchasedEventManager() -> object<nn::ec::IPurchaseEventManager>
  125. public ResultCode CreateEcPurchasedEventManager(ServiceCtx context)
  126. {
  127. MakeObject(context, new IPurchaseEventManager(context.Device.System));
  128. Logger.Stub?.PrintStub(LogClass.ServiceNs);
  129. return ResultCode.Success;
  130. }
  131. [CommandHipc(101)]
  132. // CreatePermanentEcPurchasedEventManager() -> object<nn::ec::IPurchaseEventManager>
  133. public ResultCode CreatePermanentEcPurchasedEventManager(ServiceCtx context)
  134. {
  135. // Very similar to CreateEcPurchasedEventManager but with some extra code
  136. MakeObject(context, new IPurchaseEventManager(context.Device.System));
  137. Logger.Stub?.PrintStub(LogClass.ServiceNs);
  138. return ResultCode.Success;
  139. }
  140. }
  141. }