IApplicationFunctions.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.Threading;
  12. using Ryujinx.HLE.HOS.Services.Am.AppletAE.Storage;
  13. using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService;
  14. using System;
  15. using static LibHac.Fs.ApplicationSaveDataManagement;
  16. using AccountUid = Ryujinx.HLE.HOS.Services.Account.Acc.UserId;
  17. namespace Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy
  18. {
  19. class IApplicationFunctions : IpcService
  20. {
  21. private KEvent _gpuErrorDetectedSystemEvent;
  22. public IApplicationFunctions(Horizon system)
  23. {
  24. _gpuErrorDetectedSystemEvent = new KEvent(system);
  25. }
  26. [Command(1)]
  27. // PopLaunchParameter(u32) -> object<nn::am::service::IStorage>
  28. public ResultCode PopLaunchParameter(ServiceCtx context)
  29. {
  30. // Only the first 0x18 bytes of the Data seems to be actually used.
  31. MakeObject(context, new AppletAE.IStorage(StorageHelper.MakeLaunchParams(context.Device.System.State.Account.LastOpenedUser)));
  32. return ResultCode.Success;
  33. }
  34. [Command(20)]
  35. // EnsureSaveData(nn::account::Uid) -> u64
  36. public ResultCode EnsureSaveData(ServiceCtx context)
  37. {
  38. Uid userId = context.RequestData.ReadStruct<AccountUid>().ToLibHacUid();
  39. TitleId titleId = new TitleId(context.Process.TitleId);
  40. BlitStruct<ApplicationControlProperty> controlHolder = context.Device.System.ControlData;
  41. ref ApplicationControlProperty control = ref controlHolder.Value;
  42. if (Util.IsEmpty(controlHolder.ByteSpan))
  43. {
  44. // If the current application doesn't have a loaded control property, create a dummy one
  45. // and set the savedata sizes so a user savedata will be created.
  46. control = ref new BlitStruct<ApplicationControlProperty>(1).Value;
  47. // The set sizes don't actually matter as long as they're non-zero because we use directory savedata.
  48. control.UserAccountSaveDataSize = 0x4000;
  49. control.UserAccountSaveDataJournalSize = 0x4000;
  50. Logger.PrintWarning(LogClass.ServiceAm,
  51. "No control file was found for this game. Using a dummy one instead. This may cause inaccuracies in some games.");
  52. }
  53. Result result = EnsureApplicationSaveData(context.Device.FileSystem.FsClient, out long requiredSize, titleId,
  54. ref context.Device.System.ControlData.Value, ref userId);
  55. context.ResponseData.Write(requiredSize);
  56. return (ResultCode)result.Value;
  57. }
  58. [Command(21)]
  59. // GetDesiredLanguage() -> nn::settings::LanguageCode
  60. public ResultCode GetDesiredLanguage(ServiceCtx context)
  61. {
  62. context.ResponseData.Write(context.Device.System.State.DesiredLanguageCode);
  63. return ResultCode.Success;
  64. }
  65. [Command(22)]
  66. // SetTerminateResult(u32)
  67. public ResultCode SetTerminateResult(ServiceCtx context)
  68. {
  69. int errorCode = context.RequestData.ReadInt32();
  70. string result = GetFormattedErrorCode(errorCode);
  71. Logger.PrintInfo(LogClass.ServiceAm, $"Result = 0x{errorCode:x8} ({result}).");
  72. return ResultCode.Success;
  73. }
  74. private string GetFormattedErrorCode(int errorCode)
  75. {
  76. int module = (errorCode >> 0) & 0x1ff;
  77. int description = (errorCode >> 9) & 0x1fff;
  78. return $"{(2000 + module):d4}-{description:d4}";
  79. }
  80. [Command(23)]
  81. // GetDisplayVersion() -> nn::oe::DisplayVersion
  82. public ResultCode GetDisplayVersion(ServiceCtx context)
  83. {
  84. // FIXME: Need to check correct version on a switch.
  85. context.ResponseData.Write(1L);
  86. context.ResponseData.Write(0L);
  87. return ResultCode.Success;
  88. }
  89. // GetSaveDataSize(u8, nn::account::Uid) -> (u64, u64)
  90. [Command(26)] // 3.0.0+
  91. public ResultCode GetSaveDataSize(ServiceCtx context)
  92. {
  93. SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadByte();
  94. context.RequestData.BaseStream.Seek(7, System.IO.SeekOrigin.Current);
  95. Uid userId = context.RequestData.ReadStruct<AccountUid>().ToLibHacUid();
  96. // TODO: We return a size of 2GB as we use a directory based save system. This should be enough for most of the games.
  97. context.ResponseData.Write(2000000000u);
  98. Logger.PrintStub(LogClass.ServiceAm, new { saveDataType, userId });
  99. return ResultCode.Success;
  100. }
  101. [Command(40)]
  102. // NotifyRunning() -> b8
  103. public ResultCode NotifyRunning(ServiceCtx context)
  104. {
  105. context.ResponseData.Write(1);
  106. return ResultCode.Success;
  107. }
  108. [Command(50)] // 2.0.0+
  109. // GetPseudoDeviceId() -> nn::util::Uuid
  110. public ResultCode GetPseudoDeviceId(ServiceCtx context)
  111. {
  112. context.ResponseData.Write(0L);
  113. context.ResponseData.Write(0L);
  114. Logger.PrintStub(LogClass.ServiceAm);
  115. return ResultCode.Success;
  116. }
  117. [Command(66)] // 3.0.0+
  118. // InitializeGamePlayRecording(u64, handle<copy>)
  119. public ResultCode InitializeGamePlayRecording(ServiceCtx context)
  120. {
  121. Logger.PrintStub(LogClass.ServiceAm);
  122. return ResultCode.Success;
  123. }
  124. [Command(67)] // 3.0.0+
  125. // SetGamePlayRecordingState(u32)
  126. public ResultCode SetGamePlayRecordingState(ServiceCtx context)
  127. {
  128. int state = context.RequestData.ReadInt32();
  129. Logger.PrintStub(LogClass.ServiceAm, new { state });
  130. return ResultCode.Success;
  131. }
  132. [Command(110)] // 5.0.0+
  133. // QueryApplicationPlayStatistics(buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  134. public ResultCode QueryApplicationPlayStatistics(ServiceCtx context)
  135. {
  136. // TODO: Call pdm:qry cmd 13 when IPC call between services will be implemented.
  137. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context);
  138. }
  139. [Command(111)] // 6.0.0+
  140. // QueryApplicationPlayStatisticsByUid(nn::account::Uid, buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count)
  141. public ResultCode QueryApplicationPlayStatisticsByUid(ServiceCtx context)
  142. {
  143. // TODO: Call pdm:qry cmd 16 when IPC call between services will be implemented.
  144. return (ResultCode)QueryPlayStatisticsManager.GetPlayStatistics(context, true);
  145. }
  146. [Command(130)] // 8.0.0+
  147. // GetGpuErrorDetectedSystemEvent() -> handle<copy>
  148. public ResultCode GetGpuErrorDetectedSystemEvent(ServiceCtx context)
  149. {
  150. if (context.Process.HandleTable.GenerateHandle(_gpuErrorDetectedSystemEvent.ReadableEvent, out int gpuErrorDetectedSystemEventHandle) != KernelResult.Success)
  151. {
  152. throw new InvalidOperationException("Out of handles!");
  153. }
  154. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(gpuErrorDetectedSystemEventHandle);
  155. // NOTE: This is used by "sdk" NSO during applet-application initialization.
  156. // A seperate thread is setup where event-waiting is handled.
  157. // When the Event is signaled, official sw will assert.
  158. return ResultCode.Success;
  159. }
  160. }
  161. }