ContentManager.cs 11 KB

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