TimeZoneContentManager.cs 7.1 KB

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