ContentManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using LibHac;
  2. using LibHac.IO;
  3. using Ryujinx.HLE.Utilities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. namespace Ryujinx.HLE.FileSystem.Content
  9. {
  10. internal class ContentManager
  11. {
  12. private Dictionary<StorageId, LinkedList<LocationEntry>> _locationEntries;
  13. private Dictionary<string, long> _sharedFontTitleDictionary;
  14. private SortedDictionary<(ulong, ContentType), string> _contentDictionary;
  15. private Switch _device;
  16. public ContentManager(Switch device)
  17. {
  18. _contentDictionary = new SortedDictionary<(ulong, ContentType), string>();
  19. _locationEntries = new Dictionary<StorageId, LinkedList<LocationEntry>>();
  20. _sharedFontTitleDictionary = new Dictionary<string, long>
  21. {
  22. { "FontStandard", 0x0100000000000811 },
  23. { "FontChineseSimplified", 0x0100000000000814 },
  24. { "FontExtendedChineseSimplified", 0x0100000000000814 },
  25. { "FontKorean", 0x0100000000000812 },
  26. { "FontChineseTraditional", 0x0100000000000813 },
  27. { "FontNintendoExtended", 0x0100000000000810 }
  28. };
  29. _device = device;
  30. }
  31. public void LoadEntries()
  32. {
  33. _contentDictionary = new SortedDictionary<(ulong, ContentType), string>();
  34. foreach (StorageId storageId in Enum.GetValues(typeof(StorageId)))
  35. {
  36. string contentDirectory = null;
  37. string contentPathString = null;
  38. string registeredDirectory = null;
  39. try
  40. {
  41. contentPathString = LocationHelper.GetContentRoot(storageId);
  42. contentDirectory = LocationHelper.GetRealPath(_device.FileSystem, contentPathString);
  43. registeredDirectory = Path.Combine(contentDirectory, "registered");
  44. }
  45. catch (NotSupportedException)
  46. {
  47. continue;
  48. }
  49. Directory.CreateDirectory(registeredDirectory);
  50. LinkedList<LocationEntry> locationList = new LinkedList<LocationEntry>();
  51. void AddEntry(LocationEntry entry)
  52. {
  53. locationList.AddLast(entry);
  54. }
  55. foreach (string directoryPath in Directory.EnumerateDirectories(registeredDirectory))
  56. {
  57. if (Directory.GetFiles(directoryPath).Length > 0)
  58. {
  59. string ncaName = new DirectoryInfo(directoryPath).Name.Replace(".nca", string.Empty);
  60. using (FileStream ncaFile = new FileStream(Directory.GetFiles(directoryPath)[0], FileMode.Open, FileAccess.Read))
  61. {
  62. Nca nca = new Nca(_device.System.KeySet, ncaFile.AsStorage(), false);
  63. string switchPath = Path.Combine(contentPathString + ":",
  64. ncaFile.Name.Replace(contentDirectory, string.Empty).TrimStart('\\'));
  65. // Change path format to switch's
  66. switchPath = switchPath.Replace('\\', '/');
  67. LocationEntry entry = new LocationEntry(switchPath,
  68. 0,
  69. (long)nca.Header.TitleId,
  70. nca.Header.ContentType);
  71. AddEntry(entry);
  72. _contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName);
  73. }
  74. }
  75. }
  76. foreach (string filePath in Directory.EnumerateFiles(contentDirectory))
  77. {
  78. if (Path.GetExtension(filePath) == ".nca")
  79. {
  80. string ncaName = Path.GetFileNameWithoutExtension(filePath);
  81. using (FileStream ncaFile = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  82. {
  83. Nca nca = new Nca(_device.System.KeySet, ncaFile.AsStorage(), false);
  84. string switchPath = Path.Combine(contentPathString + ":",
  85. filePath.Replace(contentDirectory, string.Empty).TrimStart('\\'));
  86. // Change path format to switch's
  87. switchPath = switchPath.Replace('\\', '/');
  88. LocationEntry entry = new LocationEntry(switchPath,
  89. 0,
  90. (long)nca.Header.TitleId,
  91. nca.Header.ContentType);
  92. AddEntry(entry);
  93. _contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName);
  94. }
  95. }
  96. }
  97. if(_locationEntries.ContainsKey(storageId) && _locationEntries[storageId]?.Count == 0)
  98. {
  99. _locationEntries.Remove(storageId);
  100. }
  101. if (!_locationEntries.ContainsKey(storageId))
  102. {
  103. _locationEntries.Add(storageId, locationList);
  104. }
  105. }
  106. }
  107. public void ClearEntry(long titleId, ContentType contentType, StorageId storageId)
  108. {
  109. RemoveLocationEntry(titleId, contentType, storageId);
  110. }
  111. public void RefreshEntries(StorageId storageId, int flag)
  112. {
  113. LinkedList<LocationEntry> locationList = _locationEntries[storageId];
  114. LinkedListNode<LocationEntry> locationEntry = locationList.First;
  115. while (locationEntry != null)
  116. {
  117. LinkedListNode<LocationEntry> nextLocationEntry = locationEntry.Next;
  118. if (locationEntry.Value.Flag == flag)
  119. {
  120. locationList.Remove(locationEntry.Value);
  121. }
  122. locationEntry = nextLocationEntry;
  123. }
  124. }
  125. public bool HasNca(string ncaId, StorageId storageId)
  126. {
  127. if (_contentDictionary.ContainsValue(ncaId))
  128. {
  129. var content = _contentDictionary.FirstOrDefault(x => x.Value == ncaId);
  130. long titleId = (long)content.Key.Item1;
  131. ContentType contentType = content.Key.Item2;
  132. StorageId storage = GetInstalledStorage(titleId, contentType, storageId);
  133. return storage == storageId;
  134. }
  135. return false;
  136. }
  137. public UInt128 GetInstalledNcaId(long titleId, ContentType contentType)
  138. {
  139. if (_contentDictionary.ContainsKey(((ulong)titleId,contentType)))
  140. {
  141. return new UInt128(_contentDictionary[((ulong)titleId,contentType)]);
  142. }
  143. return new UInt128();
  144. }
  145. public StorageId GetInstalledStorage(long titleId, ContentType contentType, StorageId storageId)
  146. {
  147. LocationEntry locationEntry = GetLocation(titleId, contentType, storageId);
  148. return locationEntry.ContentPath != null ?
  149. LocationHelper.GetStorageId(locationEntry.ContentPath) : StorageId.None;
  150. }
  151. public string GetInstalledContentPath(long titleId, StorageId storageId, ContentType contentType)
  152. {
  153. LocationEntry locationEntry = GetLocation(titleId, contentType, storageId);
  154. if (VerifyContentType(locationEntry, contentType))
  155. {
  156. return locationEntry.ContentPath;
  157. }
  158. return string.Empty;
  159. }
  160. public void RedirectLocation(LocationEntry newEntry, StorageId storageId)
  161. {
  162. LocationEntry locationEntry = GetLocation(newEntry.TitleId, newEntry.ContentType, storageId);
  163. if (locationEntry.ContentPath != null)
  164. {
  165. RemoveLocationEntry(newEntry.TitleId, newEntry.ContentType, storageId);
  166. }
  167. AddLocationEntry(newEntry, storageId);
  168. }
  169. private bool VerifyContentType(LocationEntry locationEntry, ContentType contentType)
  170. {
  171. if (locationEntry.ContentPath == null)
  172. {
  173. return false;
  174. }
  175. StorageId storageId = LocationHelper.GetStorageId(locationEntry.ContentPath);
  176. string installedPath = _device.FileSystem.SwitchPathToSystemPath(locationEntry.ContentPath);
  177. if (!string.IsNullOrWhiteSpace(installedPath))
  178. {
  179. if (File.Exists(installedPath))
  180. {
  181. using (FileStream file = new FileStream(installedPath, FileMode.Open, FileAccess.Read))
  182. {
  183. Nca nca = new Nca(_device.System.KeySet, file.AsStorage(), false);
  184. bool contentCheck = nca.Header.ContentType == contentType;
  185. return contentCheck;
  186. }
  187. }
  188. }
  189. return false;
  190. }
  191. private void AddLocationEntry(LocationEntry entry, StorageId storageId)
  192. {
  193. LinkedList<LocationEntry> locationList = null;
  194. if (_locationEntries.ContainsKey(storageId))
  195. {
  196. locationList = _locationEntries[storageId];
  197. }
  198. if (locationList != null)
  199. {
  200. if (locationList.Contains(entry))
  201. {
  202. locationList.Remove(entry);
  203. }
  204. locationList.AddLast(entry);
  205. }
  206. }
  207. private void RemoveLocationEntry(long titleId, ContentType contentType, StorageId storageId)
  208. {
  209. LinkedList<LocationEntry> locationList = null;
  210. if (_locationEntries.ContainsKey(storageId))
  211. {
  212. locationList = _locationEntries[storageId];
  213. }
  214. if (locationList != null)
  215. {
  216. LocationEntry entry =
  217. locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType);
  218. if (entry.ContentPath != null)
  219. {
  220. locationList.Remove(entry);
  221. }
  222. }
  223. }
  224. public bool TryGetFontTitle(string fontName, out long titleId)
  225. {
  226. return _sharedFontTitleDictionary.TryGetValue(fontName, out titleId);
  227. }
  228. private LocationEntry GetLocation(long titleId, ContentType contentType, StorageId storageId)
  229. {
  230. LinkedList<LocationEntry> locationList = _locationEntries[storageId];
  231. return locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType);
  232. }
  233. }
  234. }