TimeZoneContentManager.cs 7.1 KB

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