IApplicationFunctions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using LibHac;
  2. using LibHac.Account;
  3. using LibHac.Common;
  4. using LibHac.Fs;
  5. using LibHac.Ncm;
  6. using LibHac.Ns;
  7. using Ryujinx.Common;
  8. using Ryujinx.Common.Logging;
  9. using Ryujinx.HLE.HOS.Ipc;
  10. using Ryujinx.HLE.HOS.Kernel.Common;
  11. using Ryujinx.HLE.HOS.Kernel.Memory;
  12. using Ryujinx.HLE.HOS.Kernel.Threading;
  13. using Ryujinx.HLE.HOS.Services.Am.AppletAE.Storage;
  14. using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService;
  15. using System;
  16. using static LibHac.Fs.ApplicationSaveDataManagement;
  17. using AccountUid = Ryujinx.HLE.HOS.Services.Account.Acc.UserId;
  18. namespace Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy
  19. {
  20. class IApplicationFunctions : IpcService
  21. {
  22. private KEvent _gpuErrorDetectedSystemEvent;
  23. public IApplicationFunctions(Horizon system)
  24. {
  25. _gpuErrorDetectedSystemEvent = new KEvent(system);
  26. }
  27. [Command(1)]
  28. // PopLaunchParameter(u32) -> object<nn::am::service::IStorage>
  29. public ResultCode PopLaunchParameter(ServiceCtx context)
  30. {
  31. // Only the first 0x18 bytes of the Data seems to be actually used.
  32. MakeObject(context, new AppletAE.IStorage(StorageHelper.MakeLaunchParams(context.Device.System.State.Account.LastOpenedUser)));
  33. return ResultCode.Success;
  34. }
  35. [Command(20)]
  36. // EnsureSaveData(nn::account::Uid) -> u64
  37. public ResultCode EnsureSaveData(ServiceCtx context)
  38. {
  39. Uid userId = context.RequestData.ReadStruct<AccountUid>().ToLibHacUid();
  40. TitleId titleId = new TitleId(context.Process.TitleId);
  41. BlitStruct<ApplicationControlProperty> controlHolder = context.Device.System.ControlData;
  42. ref ApplicationControlProperty control = ref controlHolder.Value;
  43. if (Util.IsEmpty(controlHolder.ByteSpan))
  44. {
  45. // If the current application doesn't have a loaded control property, create a dummy one
  46. // and set the savedata sizes so a user savedata will be created.
  47. control = ref new BlitStruct<ApplicationControlProperty>(1).Value;
  48. // The set sizes don't actually matter as long as they're non-zero because we use directory savedata.
  49. control.UserAccountSaveDataSize = 0x4000;
  50. control.UserAccountSaveDataJournalSize = 0x4000;
  51. Logger.PrintWarning(LogClass.ServiceAm,
  52. "No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games.");
  53. }
  54. Result result = EnsureApplicationSaveData(context.Device.FileSystem.FsClient, out long requiredSize, titleId,
  55. ref control, ref userId);
  56. context.ResponseData.Write(requiredSize);
  57. return (ResultCode)result.Value;
  58. }
  59. [Command(21)]
  60. // GetDesiredLanguage() -> nn::settings::LanguageCode
  61. public ResultCode GetDesiredLanguage(ServiceCtx context)
  62. {
  63. context.ResponseData.Write(context.Device.System.State.DesiredLanguageCode);
  64. return ResultCode.Success;
  65. }
  66. [Command(22)]
  67. // SetTerminateResult(u32)
  68. public ResultCode SetTerminateResult(ServiceCtx context)
  69. {
  70. Result result = new Result(context.RequestData.ReadUInt32());
  71. Logger.PrintInfo(LogClass.ServiceAm, $"Result = 0x{result.Value:x8} ({result.ToStringWithName()}).");
  72. return ResultCode.Success;
  73. }
  74. [Command(23)]
  75. // GetDisplayVersion() -> nn::oe::DisplayVersion
  76. public ResultCode GetDisplayVersion(ServiceCtx context)
  77. {
  78. // FIXME: Need to check correct version on a switch.
  79. context.ResponseData.Write(1L);
  80. context.ResponseData.Write(0L);
  81. return ResultCode.Success;
  82. }
  83. // GetSaveDataSize(u8, nn::account::Uid) -> (u64, u64)
  84. [Command(26)] // 3.0.0+
  85. public ResultCode GetSaveDataSize(ServiceCtx context)
  86. {
  87. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadByte();
  88. context.RequestData.BaseStream.Seek(7, System.IO.SeekOrigin.Current);
  89. Uid userId = context.RequestData.ReadStruct<AccountUid>().ToLibHacUid();
  90. // TODO: We return a size of 2GB as we use a directory based save system. This should be enough for most of the games.
  91. context.ResponseData.Write(2000000000u);
  92. Logger.PrintStub(LogClass.ServiceAm, new { saveDataType, userId });
  93. return ResultCode.Success;
  94. }
  95. [Command(40)]
  96. // NotifyRunning() -> b8
  97. public ResultCode NotifyRunning(ServiceCtx context)
  98. {
  99. context.ResponseData.Write(1);
  100. return ResultCode.Success;
  101. }
  102. [Command(50)] // 2.0.0+
  103. // GetPseudoDeviceId() -> nn::util::Uuid
  104. public ResultCode GetPseudoDeviceId(ServiceCtx context)
  105. {
  106. context.ResponseData.Write(0L);
  107. context.ResponseData.Write(0L);
  108. Logger.PrintStub(LogClass.ServiceAm);
  109. return ResultCode.Success;
  110. }
  111. [Command(66)] // 3.0.0+
  112. // InitializeGamePlayRecording(u64, handle<copy>)
  113. public ResultCode InitializeGamePlayRecording(ServiceCtx context)
  114. {
  115. Logger.PrintStub(LogClass.ServiceAm);
  116. return ResultCode.Success;
  117. }
  118. [Command(67)] // 3.0.0+
  119. // SetGamePlayRecordingState(u32)
  120. public ResultCode SetGamePlayRecordingState(ServiceCtx context)
  121. {
  122. int state = context.RequestData.ReadInt32();
  123. Logger.PrintStub(LogClass.ServiceAm, new { state });
  124. return ResultCode.Success;
  125. }
  126. [Command(100)] // 5.0.0+
  127. // InitializeApplicationCopyrightFrameBuffer(s32 width, s32 height, handle<copy, transfer_memory> transfer_memory, u64 transfer_memory_size)
  128. public ResultCode InitializeApplicationCopyrightFrameBuffer(ServiceCtx context)
  129. {
  130. int width = context.RequestData.ReadInt32();
  131. int height = context.RequestData.ReadInt32();
  132. ulong transferMemorySize = context.RequestData.ReadUInt64();
  133. int transferMemoryHandle = context.Request.HandleDesc.ToCopy[0];
  134. ulong transferMemoryAddress = context.Process.HandleTable.GetObject<KTransferMemory>(transferMemoryHandle).Address;
  135. ResultCode resultCode = ResultCode.InvalidParameters;
  136. if (((transferMemorySize & 0x3FFFF) == 0) && width <= 1280 && height <= 720)
  137. {
  138. resultCode = InitializeApplicationCopyrightFrameBufferImpl(transferMemoryAddress, transferMemorySize, width, height);
  139. }
  140. /*
  141. if (transferMemoryHandle)
  142. {
  143. svcCloseHandle(transferMemoryHandle);
  144. }
  145. */
  146. return resultCode;
  147. }
  148. private ResultCode InitializeApplicationCopyrightFrameBufferImpl(ulong transferMemoryAddress, ulong transferMemorySize, int width, int height)
  149. {
  150. ResultCode resultCode = ResultCode.ObjectInvalid;
  151. if ((transferMemorySize & 0x3FFFF) != 0)
  152. {
  153. return ResultCode.InvalidParameters;
  154. }
  155. // if (_copyrightBuffer == null)
  156. {
  157. // TODO: Initialize buffer and object.
  158. Logger.PrintStub(LogClass.ServiceAm, new { transferMemoryAddress, transferMemorySize, width, height });
  159. resultCode = ResultCode.Success;
  160. }
  161. return resultCode;
  162. }
  163. [Command(101)] // 5.0.0+
  164. // SetApplicationCopyrightImage(buffer<bytes, 0x45> frame_buffer, s32 x, s32 y, s32 width, s32 height, s32 window_origin_mode)
  165. public ResultCode SetApplicationCopyrightImage(ServiceCtx context)
  166. {
  167. long frameBufferPos = context.Request.SendBuff[0].Position;
  168. long frameBufferSize = context.Request.SendBuff[0].Size;
  169. int x = context.RequestData.ReadInt32();
  170. int y = context.RequestData.ReadInt32();
  171. int width = context.RequestData.ReadInt32();
  172. int height = context.RequestData.ReadInt32();
  173. uint windowOriginMode = context.RequestData.ReadUInt32();
  174. ResultCode resultCode = ResultCode.InvalidParameters;
  175. if (((y | x) >= 0) && width >= 1 && height >= 1)
  176. {
  177. ResultCode result = SetApplicationCopyrightImageImpl(x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode);
  178. if (resultCode != ResultCode.Success)
  179. {
  180. resultCode = result;
  181. }
  182. else
  183. {
  184. resultCode = ResultCode.Success;
  185. }
  186. }
  187. Logger.PrintStub(LogClass.ServiceAm, new { frameBufferPos, frameBufferSize, x, y, width, height, windowOriginMode });
  188. return resultCode;
  189. }
  190. private ResultCode SetApplicationCopyrightImageImpl(int x, int y, int width, int height, long frameBufferPos, long frameBufferSize, uint windowOriginMode)
  191. {
  192. /*
  193. if (_copyrightBuffer == null)
  194. {
  195. return ResultCode.NullCopyrightObject;
  196. }
  197. */
  198. Logger.PrintStub(LogClass.ServiceAm, new { x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode });
  199. return ResultCode.Success;
  200. }
  201. [Command(102)] // 5.0.0+
  202. // SetApplicationCopyrightVisibility(bool visible)
  203. public ResultCode SetApplicationCopyrightVisibility(ServiceCtx context)
  204. {
  205. bool visible = context.RequestData.ReadBoolean();
  206. Logger.PrintStub(LogClass.ServiceAm, new { visible });
  207. // NOTE: It sets an internal field and return ResultCode.Success in all case.
  208. return ResultCode.Success;
  209. }
  210. [Command(110)] // 5.0.0+
  211. // QueryApplicationPlayStatistics(buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  212. public ResultCode QueryApplicationPlayStatistics(ServiceCtx context)
  213. {
  214. // TODO: Call pdm:qry cmd 13 when IPC call between services will be implemented.
  215. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context);
  216. }
  217. [Command(111)] // 6.0.0+
  218. // QueryApplicationPlayStatisticsByUid(nn::account::Uid, buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  219. public ResultCode QueryApplicationPlayStatisticsByUid(ServiceCtx context)
  220. {
  221. // TODO: Call pdm:qry cmd 16 when IPC call between services will be implemented.
  222. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context, true);
  223. }
  224. [Command(130)] // 8.0.0+
  225. // GetGpuErrorDetectedSystemEvent() -> handle<copy>
  226. public ResultCode GetGpuErrorDetectedSystemEvent(ServiceCtx context)
  227. {
  228. if (context.Process.HandleTable.GenerateHandle(_gpuErrorDetectedSystemEvent.ReadableEvent, out int gpuErrorDetectedSystemEventHandle) != KernelResult.Success)
  229. {
  230. throw new InvalidOperationException("Out of handles!");
  231. }
  232. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(gpuErrorDetectedSystemEventHandle);
  233. // NOTE: This is used by "sdk" NSO during applet-application initialization.
  234. // A seperate thread is setup where event-waiting is handled.
  235. // When the Event is signaled, official sw will assert.
  236. return ResultCode.Success;
  237. }
  238. }
  239. }