IApplicationFunctions.cs 6.8 KB

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