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> _commands;
  14. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  15. private StorageId _storageId;
  16. public ILocationResolver(StorageId storageId)
  17. {
  18. _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. _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. }