TimeZoneContentManager.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using LibHac;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.FsSystem;
  5. using LibHac.FsSystem.NcaUtils;
  6. using Ryujinx.Common.Logging;
  7. using Ryujinx.Configuration;
  8. using Ryujinx.HLE.Exceptions;
  9. using Ryujinx.HLE.FileSystem;
  10. using Ryujinx.HLE.FileSystem.Content;
  11. using Ryujinx.HLE.HOS.Services.Time.Clock;
  12. using Ryujinx.HLE.Utilities;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using static Ryujinx.HLE.HOS.Services.Time.TimeZone.TimeZoneRule;
  16. namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
  17. {
  18. public class TimeZoneContentManager
  19. {
  20. private const long TimeZoneBinaryTitleId = 0x010000000000080E;
  21. private VirtualFileSystem _virtualFileSystem;
  22. private IntegrityCheckLevel _fsIntegrityCheckLevel;
  23. private ContentManager _contentManager;
  24. public string[] LocationNameCache { get; private set; }
  25. internal TimeZoneManager Manager { get; private set; }
  26. public TimeZoneContentManager()
  27. {
  28. Manager = new TimeZoneManager();
  29. }
  30. public void InitializeInstance(VirtualFileSystem virtualFileSystem, ContentManager contentManager, IntegrityCheckLevel fsIntegrityCheckLevel)
  31. {
  32. _virtualFileSystem = virtualFileSystem;
  33. _contentManager = contentManager;
  34. _fsIntegrityCheckLevel = fsIntegrityCheckLevel;
  35. InitializeLocationNameCache();
  36. }
  37. public string SanityCheckDeviceLocationName()
  38. {
  39. string locationName = ConfigurationState.Instance.System.TimeZone;
  40. if (IsLocationNameValid(locationName))
  41. {
  42. return locationName;
  43. }
  44. Logger.PrintWarning(LogClass.ServiceTime, $"Invalid device TimeZone {locationName}, switching back to UTC");
  45. ConfigurationState.Instance.System.TimeZone.Value = "UTC";
  46. return "UTC";
  47. }
  48. internal void Initialize(TimeManager timeManager, Switch device)
  49. {
  50. InitializeInstance(device.FileSystem, device.System.ContentManager, device.System.FsIntegrityCheckLevel);
  51. SteadyClockTimePoint timeZoneUpdatedTimePoint = timeManager.StandardSteadyClock.GetCurrentTimePoint(null);
  52. string deviceLocationName = SanityCheckDeviceLocationName();
  53. ResultCode result = GetTimeZoneBinary(deviceLocationName, out Stream timeZoneBinaryStream, out LocalStorage ncaFile);
  54. if (result == ResultCode.Success)
  55. {
  56. // TODO: Read TimeZoneVersion from sysarchive.
  57. timeManager.SetupTimeZoneManager(deviceLocationName, timeZoneUpdatedTimePoint, (uint)LocationNameCache.Length, new UInt128(), timeZoneBinaryStream);
  58. ncaFile.Dispose();
  59. }
  60. else
  61. {
  62. // In the case the user don't have the timezone system archive, we just mark the manager as initialized.
  63. Manager.MarkInitialized();
  64. }
  65. }
  66. private void InitializeLocationNameCache()
  67. {
  68. if (HasTimeZoneBinaryTitle())
  69. {
  70. using (IStorage ncaFileStream = new LocalStorage(_virtualFileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open))
  71. {
  72. Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFileStream);
  73. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _fsIntegrityCheckLevel);
  74. romfs.OpenFile(out IFile binaryListFile, "/binaryList.txt".ToU8Span(), OpenMode.Read).ThrowIfFailure();
  75. StreamReader reader = new StreamReader(binaryListFile.AsStream());
  76. List<string> locationNameList = new List<string>();
  77. string locationName;
  78. while ((locationName = reader.ReadLine()) != null)
  79. {
  80. locationNameList.Add(locationName);
  81. }
  82. LocationNameCache = locationNameList.ToArray();
  83. }
  84. }
  85. else
  86. {
  87. LocationNameCache = new string[] { "UTC" };
  88. Logger.PrintWarning(LogClass.ServiceTime, "TimeZoneBinary system title not found! TimeZone conversions will not work, provide the system archive to fix this warning. (See https://github.com/Ryujinx/Ryujinx#requirements for more informations)");
  89. }
  90. }
  91. private bool IsLocationNameValid(string locationName)
  92. {
  93. foreach (string cachedLocationName in LocationNameCache)
  94. {
  95. if (cachedLocationName.Equals(locationName))
  96. {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. public ResultCode SetDeviceLocationName(string locationName)
  103. {
  104. ResultCode result = GetTimeZoneBinary(locationName, out Stream timeZoneBinaryStream, out LocalStorage ncaFile);
  105. if (result == ResultCode.Success)
  106. {
  107. result = Manager.SetDeviceLocationNameWithTimeZoneRule(locationName, timeZoneBinaryStream);
  108. ncaFile.Dispose();
  109. }
  110. return result;
  111. }
  112. public ResultCode LoadLocationNameList(uint index, out string[] outLocationNameArray, uint maxLength)
  113. {
  114. List<string> locationNameList = new List<string>();
  115. for (int i = 0; i < LocationNameCache.Length && i < maxLength; i++)
  116. {
  117. if (i < index)
  118. {
  119. continue;
  120. }
  121. string locationName = LocationNameCache[i];
  122. // If the location name is too long, error out.
  123. if (locationName.Length > 0x24)
  124. {
  125. outLocationNameArray = new string[0];
  126. return ResultCode.LocationNameTooLong;
  127. }
  128. locationNameList.Add(locationName);
  129. }
  130. outLocationNameArray = locationNameList.ToArray();
  131. return ResultCode.Success;
  132. }
  133. public string GetTimeZoneBinaryTitleContentPath()
  134. {
  135. return _contentManager.GetInstalledContentPath(TimeZoneBinaryTitleId, StorageId.NandSystem, NcaContentType.Data);
  136. }
  137. public bool HasTimeZoneBinaryTitle()
  138. {
  139. return !string.IsNullOrEmpty(GetTimeZoneBinaryTitleContentPath());
  140. }
  141. internal ResultCode GetTimeZoneBinary(string locationName, out Stream timeZoneBinaryStream, out LocalStorage ncaFile)
  142. {
  143. timeZoneBinaryStream = null;
  144. ncaFile = null;
  145. if (!IsLocationNameValid(locationName))
  146. {
  147. return ResultCode.TimeZoneNotFound;
  148. }
  149. ncaFile = new LocalStorage(_virtualFileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open);
  150. Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile);
  151. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _fsIntegrityCheckLevel);
  152. Result result = romfs.OpenFile(out IFile timeZoneBinaryFile, $"/zoneinfo/{locationName}".ToU8Span(), OpenMode.Read);
  153. timeZoneBinaryStream = timeZoneBinaryFile.AsStream();
  154. return (ResultCode)result.Value;
  155. }
  156. internal ResultCode LoadTimeZoneRule(out TimeZoneRule outRules, string locationName)
  157. {
  158. outRules = new TimeZoneRule
  159. {
  160. Ats = new long[TzMaxTimes],
  161. Types = new byte[TzMaxTimes],
  162. Ttis = new TimeTypeInfo[TzMaxTypes],
  163. Chars = new char[TzCharsArraySize]
  164. };
  165. if (!HasTimeZoneBinaryTitle())
  166. {
  167. throw new InvalidSystemResourceException($"TimeZoneBinary system title not found! Please provide it. (See https://github.com/Ryujinx/Ryujinx#requirements for more informations)");
  168. }
  169. ResultCode result = GetTimeZoneBinary(locationName, out Stream timeZoneBinaryStream, out LocalStorage ncaFile);
  170. if (result == ResultCode.Success)
  171. {
  172. result = Manager.ParseTimeZoneRuleBinary(out outRules, timeZoneBinaryStream);
  173. ncaFile.Dispose();
  174. }
  175. return result;
  176. }
  177. }
  178. }