TimeZoneContentManager.cs 6.9 KB

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