TimeZoneManager.cs 9.6 KB

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