ILocationResolver.cs 7.3 KB

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