TimeZoneContentManager.cs 8.3 KB

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