TimeZoneContentManager.cs 6.7 KB

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