IApplicationFunctions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. int errorCode = context.RequestData.ReadInt32();
  71. string result = GetFormattedErrorCode(errorCode);
  72. Logger.PrintInfo(LogClass.ServiceAm, $"Result = 0x{errorCode:x8} ({result}).");
  73. return ResultCode.Success;
  74. }
  75. private string GetFormattedErrorCode(int errorCode)
  76. {
  77. int module = (errorCode >> 0) & 0x1ff;
  78. int description = (errorCode >> 9) & 0x1fff;
  79. return $"{(2000 + module):d4}-{description:d4}";
  80. }
  81. [Command(23)]
  82. // GetDisplayVersion() -> nn::oe::DisplayVersion
  83. public ResultCode GetDisplayVersion(ServiceCtx context)
  84. {
  85. // FIXME: Need to check correct version on a switch.
  86. context.ResponseData.Write(1L);
  87. context.ResponseData.Write(0L);
  88. return ResultCode.Success;
  89. }
  90. // GetSaveDataSize(u8, nn::account::Uid) -> (u64, u64)
  91. [Command(26)] // 3.0.0+
  92. public ResultCode GetSaveDataSize(ServiceCtx context)
  93. {
  94. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadByte();
  95. context.RequestData.BaseStream.Seek(7, System.IO.SeekOrigin.Current);
  96. Uid userId = context.RequestData.ReadStruct<AccountUid>().ToLibHacUid();
  97. // TODO: We return a size of 2GB as we use a directory based save system. This should be enough for most of the games.
  98. context.ResponseData.Write(2000000000u);
  99. Logger.PrintStub(LogClass.ServiceAm, new { saveDataType, userId });
  100. return ResultCode.Success;
  101. }
  102. [Command(40)]
  103. // NotifyRunning() -> b8
  104. public ResultCode NotifyRunning(ServiceCtx context)
  105. {
  106. context.ResponseData.Write(1);
  107. return ResultCode.Success;
  108. }
  109. [Command(50)] // 2.0.0+
  110. // GetPseudoDeviceId() -> nn::util::Uuid
  111. public ResultCode GetPseudoDeviceId(ServiceCtx context)
  112. {
  113. context.ResponseData.Write(0L);
  114. context.ResponseData.Write(0L);
  115. Logger.PrintStub(LogClass.ServiceAm);
  116. return ResultCode.Success;
  117. }
  118. [Command(66)] // 3.0.0+
  119. // InitializeGamePlayRecording(u64, handle<copy>)
  120. public ResultCode InitializeGamePlayRecording(ServiceCtx context)
  121. {
  122. Logger.PrintStub(LogClass.ServiceAm);
  123. return ResultCode.Success;
  124. }
  125. [Command(67)] // 3.0.0+
  126. // SetGamePlayRecordingState(u32)
  127. public ResultCode SetGamePlayRecordingState(ServiceCtx context)
  128. {
  129. int state = context.RequestData.ReadInt32();
  130. Logger.PrintStub(LogClass.ServiceAm, new { state });
  131. return ResultCode.Success;
  132. }
  133. [Command(100)] // 5.0.0+
  134. // InitializeApplicationCopyrightFrameBuffer(s32 width, s32 height, handle<copy, transfer_memory> transfer_memory, u64 transfer_memory_size)
  135. public ResultCode InitializeApplicationCopyrightFrameBuffer(ServiceCtx context)
  136. {
  137. int width = context.RequestData.ReadInt32();
  138. int height = context.RequestData.ReadInt32();
  139. ulong transferMemorySize = context.RequestData.ReadUInt64();
  140. int transferMemoryHandle = context.Request.HandleDesc.ToCopy[0];
  141. ulong transferMemoryAddress = context.Process.HandleTable.GetObject<KTransferMemory>(transferMemoryHandle).Address;
  142. ResultCode resultCode = ResultCode.InvalidParameters;
  143. if (((transferMemorySize & 0x3FFFF) == 0) && width <= 1280 && height <= 720)
  144. {
  145. resultCode = InitializeApplicationCopyrightFrameBufferImpl(transferMemoryAddress, transferMemorySize, width, height);
  146. }
  147. /*
  148. if (transferMemoryHandle)
  149. {
  150. svcCloseHandle(transferMemoryHandle);
  151. }
  152. */
  153. return resultCode;
  154. }
  155. private ResultCode InitializeApplicationCopyrightFrameBufferImpl(ulong transferMemoryAddress, ulong transferMemorySize, int width, int height)
  156. {
  157. ResultCode resultCode = ResultCode.ObjectInvalid;
  158. if ((transferMemorySize & 0x3FFFF) != 0)
  159. {
  160. return ResultCode.InvalidParameters;
  161. }
  162. // if (_copyrightBuffer == null)
  163. {
  164. // TODO: Initialize buffer and object.
  165. Logger.PrintStub(LogClass.ServiceAm, new { transferMemoryAddress, transferMemorySize, width, height });
  166. resultCode = ResultCode.Success;
  167. }
  168. return resultCode;
  169. }
  170. [Command(101)] // 5.0.0+
  171. // SetApplicationCopyrightImage(buffer<bytes, 0x45> frame_buffer, s32 x, s32 y, s32 width, s32 height, s32 window_origin_mode)
  172. public ResultCode SetApplicationCopyrightImage(ServiceCtx context)
  173. {
  174. long frameBufferPos = context.Request.SendBuff[0].Position;
  175. long frameBufferSize = context.Request.SendBuff[0].Size;
  176. int x = context.RequestData.ReadInt32();
  177. int y = context.RequestData.ReadInt32();
  178. int width = context.RequestData.ReadInt32();
  179. int height = context.RequestData.ReadInt32();
  180. uint windowOriginMode = context.RequestData.ReadUInt32();
  181. ResultCode resultCode = ResultCode.InvalidParameters;
  182. if (((y | x) >= 0) && width >= 1 && height >= 1)
  183. {
  184. ResultCode result = SetApplicationCopyrightImageImpl(x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode);
  185. if (resultCode != ResultCode.Success)
  186. {
  187. resultCode = result;
  188. }
  189. else
  190. {
  191. resultCode = ResultCode.Success;
  192. }
  193. }
  194. Logger.PrintStub(LogClass.ServiceAm, new { frameBufferPos, frameBufferSize, x, y, width, height, windowOriginMode });
  195. return resultCode;
  196. }
  197. private ResultCode SetApplicationCopyrightImageImpl(int x, int y, int width, int height, long frameBufferPos, long frameBufferSize, uint windowOriginMode)
  198. {
  199. /*
  200. if (_copyrightBuffer == null)
  201. {
  202. return ResultCode.NullCopyrightObject;
  203. }
  204. */
  205. Logger.PrintStub(LogClass.ServiceAm, new { x, y, width, height, frameBufferPos, frameBufferSize, windowOriginMode });
  206. return ResultCode.Success;
  207. }
  208. [Command(102)] // 5.0.0+
  209. // SetApplicationCopyrightVisibility(bool visible)
  210. public ResultCode SetApplicationCopyrightVisibility(ServiceCtx context)
  211. {
  212. bool visible = context.RequestData.ReadBoolean();
  213. Logger.PrintStub(LogClass.ServiceAm, new { visible });
  214. // NOTE: It sets an internal field and return ResultCode.Success in all case.
  215. return ResultCode.Success;
  216. }
  217. [Command(110)] // 5.0.0+
  218. // QueryApplicationPlayStatistics(buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  219. public ResultCode QueryApplicationPlayStatistics(ServiceCtx context)
  220. {
  221. // TODO: Call pdm:qry cmd 13 when IPC call between services will be implemented.
  222. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context);
  223. }
  224. [Command(111)] // 6.0.0+
  225. // QueryApplicationPlayStatisticsByUid(nn::account::Uid, buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  226. public ResultCode QueryApplicationPlayStatisticsByUid(ServiceCtx context)
  227. {
  228. // TODO: Call pdm:qry cmd 16 when IPC call between services will be implemented.
  229. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context, true);
  230. }
  231. [Command(130)] // 8.0.0+
  232. // GetGpuErrorDetectedSystemEvent() -> handle<copy>
  233. public ResultCode GetGpuErrorDetectedSystemEvent(ServiceCtx context)
  234. {
  235. if (context.Process.HandleTable.GenerateHandle(_gpuErrorDetectedSystemEvent.ReadableEvent, out int gpuErrorDetectedSystemEventHandle) != KernelResult.Success)
  236. {
  237. throw new InvalidOperationException("Out of handles!");
  238. }
  239. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(gpuErrorDetectedSystemEventHandle);
  240. // NOTE: This is used by "sdk" NSO during applet-application initialization.
  241. // A seperate thread is setup where event-waiting is handled.
  242. // When the Event is signaled, official sw will assert.
  243. return ResultCode.Success;
  244. }
  245. }
  246. }