TimeZoneManager.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using LibHac.Fs.NcaUtils;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.HLE.FileSystem;
  4. using System;
  5. using System.Collections.ObjectModel;
  6. using LibHac.Fs;
  7. using System.IO;
  8. using System.Collections.Generic;
  9. using TimeZoneConverter.Posix;
  10. using TimeZoneConverter;
  11. using static Ryujinx.HLE.HOS.Services.Time.TimeZoneRule;
  12. using static Ryujinx.HLE.HOS.ErrorCode;
  13. namespace Ryujinx.HLE.HOS.Services.Time.TimeZone
  14. {
  15. public sealed class TimeZoneManager
  16. {
  17. private const long TimeZoneBinaryTitleId = 0x010000000000080E;
  18. private static TimeZoneManager instance;
  19. private static object instanceLock = new object();
  20. private Switch _device;
  21. private TimeZoneRule _myRules;
  22. private string _deviceLocationName;
  23. private string[] _locationNameCache;
  24. public static TimeZoneManager Instance
  25. {
  26. get
  27. {
  28. lock (instanceLock)
  29. {
  30. if (instance == null)
  31. {
  32. instance = new TimeZoneManager();
  33. }
  34. return instance;
  35. }
  36. }
  37. }
  38. TimeZoneManager()
  39. {
  40. // Empty rules (UTC)
  41. _myRules = new TimeZoneRule
  42. {
  43. Ats = new long[TzMaxTimes],
  44. Types = new byte[TzMaxTimes],
  45. Ttis = new TimeTypeInfo[TzMaxTypes],
  46. Chars = new char[TzCharsArraySize]
  47. };
  48. _deviceLocationName = "UTC";
  49. }
  50. internal void Initialize(Switch device)
  51. {
  52. _device = device;
  53. InitializeLocationNameCache();
  54. }
  55. private void InitializeLocationNameCache()
  56. {
  57. if (HasTimeZoneBinaryTitle())
  58. {
  59. using (IStorage ncaFileStream = new LocalStorage(_device.FileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open))
  60. {
  61. Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
  62. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  63. Stream binaryListStream = romfs.OpenFile("binaryList.txt", OpenMode.Read).AsStream();
  64. StreamReader reader = new StreamReader(binaryListStream);
  65. List<string> locationNameList = new List<string>();
  66. string locationName;
  67. while ((locationName = reader.ReadLine()) != null)
  68. {
  69. locationNameList.Add(locationName);
  70. }
  71. _locationNameCache = locationNameList.ToArray();
  72. }
  73. }
  74. else
  75. {
  76. ReadOnlyCollection<TimeZoneInfo> timeZoneInfos = TimeZoneInfo.GetSystemTimeZones();
  77. _locationNameCache = new string[timeZoneInfos.Count];
  78. int i = 0;
  79. foreach (TimeZoneInfo timeZoneInfo in timeZoneInfos)
  80. {
  81. bool needConversion = TZConvert.TryWindowsToIana(timeZoneInfo.Id, out string convertedName);
  82. if (needConversion)
  83. {
  84. _locationNameCache[i] = convertedName;
  85. }
  86. else
  87. {
  88. _locationNameCache[i] = timeZoneInfo.Id;
  89. }
  90. i++;
  91. }
  92. // As we aren't using the system archive, "UTC" might not exist on the host system.
  93. // Load from C# TimeZone APIs UTC id.
  94. string utcId = TimeZoneInfo.Utc.Id;
  95. bool utcNeedConversion = TZConvert.TryWindowsToIana(utcId, out string utcConvertedName);
  96. if (utcNeedConversion)
  97. {
  98. utcId = utcConvertedName;
  99. }
  100. _deviceLocationName = utcId;
  101. }
  102. }
  103. private bool IsLocationNameValid(string locationName)
  104. {
  105. foreach (string cachedLocationName in _locationNameCache)
  106. {
  107. if (cachedLocationName.Equals(locationName))
  108. {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. public string GetDeviceLocationName()
  115. {
  116. return _deviceLocationName;
  117. }
  118. public uint SetDeviceLocationName(string locationName)
  119. {
  120. uint resultCode = LoadTimeZoneRules(out TimeZoneRule rules, locationName);
  121. if (resultCode == 0)
  122. {
  123. _myRules = rules;
  124. _deviceLocationName = locationName;
  125. }
  126. return resultCode;
  127. }
  128. public uint LoadLocationNameList(uint index, out string[] outLocationNameArray, uint maxLength)
  129. {
  130. List<string> locationNameList = new List<string>();
  131. for (int i = 0; i < _locationNameCache.Length && i < maxLength; i++)
  132. {
  133. if (i < index)
  134. {
  135. continue;
  136. }
  137. string locationName = _locationNameCache[i];
  138. // If the location name is too long, error out.
  139. if (locationName.Length > 0x24)
  140. {
  141. outLocationNameArray = new string[0];
  142. return MakeError(ErrorModule.Time, TimeError.LocationNameTooLong);
  143. }
  144. locationNameList.Add(locationName);
  145. }
  146. outLocationNameArray = locationNameList.ToArray();
  147. return 0;
  148. }
  149. public uint GetTotalLocationNameCount()
  150. {
  151. return (uint)_locationNameCache.Length;
  152. }
  153. public string GetTimeZoneBinaryTitleContentPath()
  154. {
  155. return _device.System.ContentManager.GetInstalledContentPath(TimeZoneBinaryTitleId, StorageId.NandSystem, ContentType.Data);
  156. }
  157. public bool HasTimeZoneBinaryTitle()
  158. {
  159. return !string.IsNullOrEmpty(GetTimeZoneBinaryTitleContentPath());
  160. }
  161. internal uint LoadTimeZoneRules(out TimeZoneRule outRules, string locationName)
  162. {
  163. outRules = new TimeZoneRule
  164. {
  165. Ats = new long[TzMaxTimes],
  166. Types = new byte[TzMaxTimes],
  167. Ttis = new TimeTypeInfo[TzMaxTypes],
  168. Chars = new char[TzCharsArraySize]
  169. };
  170. if (!IsLocationNameValid(locationName))
  171. {
  172. return MakeError(ErrorModule.Time, TimeError.TimeZoneNotFound);
  173. }
  174. if (!HasTimeZoneBinaryTitle())
  175. {
  176. // If the user doesn't have the system archives, we generate a POSIX rule string and parse it to generate a incomplete TimeZoneRule
  177. // TODO: As for now not having system archives is fine, we should enforce the usage of system archives later.
  178. Logger.PrintWarning(LogClass.ServiceTime, "TimeZoneBinary system archive not found! Time conversions will not be accurate!");
  179. try
  180. {
  181. TimeZoneInfo info = TZConvert.GetTimeZoneInfo(locationName);
  182. string posixRule = PosixTimeZone.FromTimeZoneInfo(info);
  183. if (!TimeZone.ParsePosixName(posixRule, out outRules))
  184. {
  185. return MakeError(ErrorModule.Time, TimeError.TimeZoneConversionFailed);
  186. }
  187. return 0;
  188. }
  189. catch (TimeZoneNotFoundException)
  190. {
  191. Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {locationName})");
  192. return MakeError(ErrorModule.Time, TimeError.TimeZoneNotFound);
  193. }
  194. }
  195. else
  196. {
  197. using (IStorage ncaFileStream = new LocalStorage(_device.FileSystem.SwitchPathToSystemPath(GetTimeZoneBinaryTitleContentPath()), FileAccess.Read, FileMode.Open))
  198. {
  199. Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
  200. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  201. Stream tzIfStream = romfs.OpenFile($"zoneinfo/{locationName}", OpenMode.Read).AsStream();
  202. if (!TimeZone.LoadTimeZoneRules(out outRules, tzIfStream))
  203. {
  204. return MakeError(ErrorModule.Time, TimeError.TimeZoneConversionFailed);
  205. }
  206. }
  207. return 0;
  208. }
  209. }
  210. internal uint ToCalendarTimeWithMyRules(long time, out CalendarInfo calendar)
  211. {
  212. return ToCalendarTime(_myRules, time, out calendar);
  213. }
  214. internal static uint ToCalendarTime(TimeZoneRule rules, long time, out CalendarInfo calendar)
  215. {
  216. int error = TimeZone.ToCalendarTime(rules, time, out calendar);
  217. if (error != 0)
  218. {
  219. return MakeError(ErrorModule.Time, error);
  220. }
  221. return 0;
  222. }
  223. internal uint ToPosixTimeWithMyRules(CalendarTime calendarTime, out long posixTime)
  224. {
  225. return ToPosixTime(_myRules, calendarTime, out posixTime);
  226. }
  227. internal static uint ToPosixTime(TimeZoneRule rules, CalendarTime calendarTime, out long posixTime)
  228. {
  229. int error = TimeZone.ToPosixTime(rules, calendarTime, out posixTime);
  230. if (error != 0)
  231. {
  232. return MakeError(ErrorModule.Time, error);
  233. }
  234. return 0;
  235. }
  236. }
  237. }