ILocationResolver.cs 8.3 KB

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