ILocationResolver.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using LibHac.FsSystem.NcaUtils;
  2. using Ryujinx.HLE.FileSystem;
  3. using Ryujinx.HLE.FileSystem.Content;
  4. using System.Text;
  5. using static Ryujinx.HLE.Utilities.StringUtils;
  6. namespace Ryujinx.HLE.HOS.Services.Ncm.Lr.LocationResolverManager
  7. {
  8. class ILocationResolver : IpcService
  9. {
  10. private StorageId _storageId;
  11. public ILocationResolver(StorageId storageId)
  12. {
  13. _storageId = storageId;
  14. }
  15. [CommandHipc(0)]
  16. // ResolveProgramPath(u64 titleId)
  17. public ResultCode ResolveProgramPath(ServiceCtx context)
  18. {
  19. ulong titleId = context.RequestData.ReadUInt64();
  20. if (ResolvePath(context, titleId, NcaContentType.Program))
  21. {
  22. return ResultCode.Success;
  23. }
  24. else
  25. {
  26. return ResultCode.ProgramLocationEntryNotFound;
  27. }
  28. }
  29. [CommandHipc(1)]
  30. // RedirectProgramPath(u64 titleId)
  31. public ResultCode RedirectProgramPath(ServiceCtx context)
  32. {
  33. ulong titleId = context.RequestData.ReadUInt64();
  34. RedirectPath(context, titleId, 0, NcaContentType.Program);
  35. return ResultCode.Success;
  36. }
  37. [CommandHipc(2)]
  38. // ResolveApplicationControlPath(u64 titleId)
  39. public ResultCode ResolveApplicationControlPath(ServiceCtx context)
  40. {
  41. ulong titleId = context.RequestData.ReadUInt64();
  42. if (ResolvePath(context, titleId, NcaContentType.Control))
  43. {
  44. return ResultCode.Success;
  45. }
  46. else
  47. {
  48. return ResultCode.AccessDenied;
  49. }
  50. }
  51. [CommandHipc(3)]
  52. // ResolveApplicationHtmlDocumentPath(u64 titleId)
  53. public ResultCode ResolveApplicationHtmlDocumentPath(ServiceCtx context)
  54. {
  55. ulong titleId = context.RequestData.ReadUInt64();
  56. if (ResolvePath(context, titleId, NcaContentType.Manual))
  57. {
  58. return ResultCode.Success;
  59. }
  60. else
  61. {
  62. return ResultCode.AccessDenied;
  63. }
  64. }
  65. [CommandHipc(4)]
  66. // ResolveDataPath(u64 titleId)
  67. public ResultCode ResolveDataPath(ServiceCtx context)
  68. {
  69. ulong titleId = context.RequestData.ReadUInt64();
  70. if (ResolvePath(context, titleId, NcaContentType.Data) || ResolvePath(context, titleId, NcaContentType.PublicData))
  71. {
  72. return ResultCode.Success;
  73. }
  74. else
  75. {
  76. return ResultCode.AccessDenied;
  77. }
  78. }
  79. [CommandHipc(5)]
  80. // RedirectApplicationControlPath(u64 titleId)
  81. public ResultCode RedirectApplicationControlPath(ServiceCtx context)
  82. {
  83. ulong titleId = context.RequestData.ReadUInt64();
  84. RedirectPath(context, titleId, 1, NcaContentType.Control);
  85. return ResultCode.Success;
  86. }
  87. [CommandHipc(6)]
  88. // RedirectApplicationHtmlDocumentPath(u64 titleId)
  89. public ResultCode RedirectApplicationHtmlDocumentPath(ServiceCtx context)
  90. {
  91. ulong titleId = context.RequestData.ReadUInt64();
  92. RedirectPath(context, titleId, 1, NcaContentType.Manual);
  93. return ResultCode.Success;
  94. }
  95. [CommandHipc(7)]
  96. // ResolveApplicationLegalInformationPath(u64 titleId)
  97. public ResultCode ResolveApplicationLegalInformationPath(ServiceCtx context)
  98. {
  99. ulong titleId = context.RequestData.ReadUInt64();
  100. if (ResolvePath(context, titleId, NcaContentType.Manual))
  101. {
  102. return ResultCode.Success;
  103. }
  104. else
  105. {
  106. return ResultCode.AccessDenied;
  107. }
  108. }
  109. [CommandHipc(8)]
  110. // RedirectApplicationLegalInformationPath(u64 titleId)
  111. public ResultCode RedirectApplicationLegalInformationPath(ServiceCtx context)
  112. {
  113. ulong titleId = context.RequestData.ReadUInt64();
  114. RedirectPath(context, titleId, 1, NcaContentType.Manual);
  115. return ResultCode.Success;
  116. }
  117. [CommandHipc(9)]
  118. // Refresh()
  119. public ResultCode Refresh(ServiceCtx context)
  120. {
  121. context.Device.System.ContentManager.RefreshEntries(_storageId, 1);
  122. return ResultCode.Success;
  123. }
  124. [CommandHipc(10)]
  125. // SetProgramNcaPath2(u64 titleId)
  126. public ResultCode SetProgramNcaPath2(ServiceCtx context)
  127. {
  128. ulong titleId = context.RequestData.ReadUInt64();
  129. RedirectPath(context, titleId, 1, NcaContentType.Program);
  130. return ResultCode.Success;
  131. }
  132. [CommandHipc(11)]
  133. // ClearLocationResolver2()
  134. public ResultCode ClearLocationResolver2(ServiceCtx context)
  135. {
  136. context.Device.System.ContentManager.RefreshEntries(_storageId, 1);
  137. return ResultCode.Success;
  138. }
  139. [CommandHipc(12)]
  140. // DeleteProgramNcaPath(u64 titleId)
  141. public ResultCode DeleteProgramNcaPath(ServiceCtx context)
  142. {
  143. ulong titleId = context.RequestData.ReadUInt64();
  144. DeleteContentPath(context, titleId, NcaContentType.Program);
  145. return ResultCode.Success;
  146. }
  147. [CommandHipc(13)]
  148. // DeleteControlNcaPath(u64 titleId)
  149. public ResultCode DeleteControlNcaPath(ServiceCtx context)
  150. {
  151. ulong titleId = context.RequestData.ReadUInt64();
  152. DeleteContentPath(context, titleId, NcaContentType.Control);
  153. return ResultCode.Success;
  154. }
  155. [CommandHipc(14)]
  156. // DeleteDocHtmlNcaPath(u64 titleId)
  157. public ResultCode DeleteDocHtmlNcaPath(ServiceCtx context)
  158. {
  159. ulong titleId = context.RequestData.ReadUInt64();
  160. DeleteContentPath(context, titleId, NcaContentType.Manual);
  161. return ResultCode.Success;
  162. }
  163. [CommandHipc(15)]
  164. // DeleteInfoHtmlNcaPath(u64 titleId)
  165. public ResultCode DeleteInfoHtmlNcaPath(ServiceCtx context)
  166. {
  167. ulong titleId = context.RequestData.ReadUInt64();
  168. DeleteContentPath(context, titleId, NcaContentType.Manual);
  169. return ResultCode.Success;
  170. }
  171. private void RedirectPath(ServiceCtx context, ulong titleId, int flag, NcaContentType contentType)
  172. {
  173. string contentPath = ReadUtf8String(context);
  174. LocationEntry newLocation = new LocationEntry(contentPath, flag, titleId, contentType);
  175. context.Device.System.ContentManager.RedirectLocation(newLocation, _storageId);
  176. }
  177. private bool ResolvePath(ServiceCtx context, ulong titleId, NcaContentType contentType)
  178. {
  179. ContentManager contentManager = context.Device.System.ContentManager;
  180. string contentPath = contentManager.GetInstalledContentPath(titleId, _storageId, NcaContentType.Program);
  181. if (!string.IsNullOrWhiteSpace(contentPath))
  182. {
  183. ulong position = context.Request.RecvListBuff[0].Position;
  184. ulong size = context.Request.RecvListBuff[0].Size;
  185. byte[] contentPathBuffer = Encoding.UTF8.GetBytes(contentPath);
  186. context.Memory.Write(position, contentPathBuffer);
  187. }
  188. else
  189. {
  190. return false;
  191. }
  192. return true;
  193. }
  194. private void DeleteContentPath(ServiceCtx context, ulong titleId, NcaContentType contentType)
  195. {
  196. ContentManager contentManager = context.Device.System.ContentManager;
  197. string contentPath = contentManager.GetInstalledContentPath(titleId, _storageId, NcaContentType.Manual);
  198. contentManager.ClearEntry(titleId, NcaContentType.Manual, _storageId);
  199. }
  200. }
  201. }