ContentManager.cs 12 KB

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