ContentManager.cs 12 KB

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