IAddOnContentManager.cs 6.5 KB

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