ITimeZoneService.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using static Ryujinx.HLE.HOS.ErrorCode;
  7. namespace Ryujinx.HLE.HOS.Services.Time
  8. {
  9. class ITimeZoneService : IpcService
  10. {
  11. private Dictionary<int, ServiceProcessRequest> _commands;
  12. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  13. private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  14. private TimeZoneInfo _timeZone = TimeZoneInfo.Local;
  15. public ITimeZoneService()
  16. {
  17. _commands = new Dictionary<int, ServiceProcessRequest>
  18. {
  19. { 0, GetDeviceLocationName },
  20. { 1, SetDeviceLocationName },
  21. { 2, GetTotalLocationNameCount },
  22. { 3, LoadLocationNameList },
  23. { 4, LoadTimeZoneRule },
  24. { 100, ToCalendarTime },
  25. { 101, ToCalendarTimeWithMyRule },
  26. { 201, ToPosixTime },
  27. { 202, ToPosixTimeWithMyRule }
  28. };
  29. }
  30. public long GetDeviceLocationName(ServiceCtx context)
  31. {
  32. char[] tzName = _timeZone.Id.ToCharArray();
  33. context.ResponseData.Write(tzName);
  34. int padding = 0x24 - tzName.Length;
  35. for (int index = 0; index < padding; index++)
  36. {
  37. context.ResponseData.Write((byte)0);
  38. }
  39. return 0;
  40. }
  41. public long SetDeviceLocationName(ServiceCtx context)
  42. {
  43. byte[] locationName = context.RequestData.ReadBytes(0x24);
  44. string tzId = Encoding.ASCII.GetString(locationName).TrimEnd('\0');
  45. long resultCode = 0;
  46. try
  47. {
  48. _timeZone = TimeZoneInfo.FindSystemTimeZoneById(tzId);
  49. }
  50. catch (TimeZoneNotFoundException)
  51. {
  52. resultCode = MakeError(ErrorModule.Time, 0x3dd);
  53. }
  54. return resultCode;
  55. }
  56. public long GetTotalLocationNameCount(ServiceCtx context)
  57. {
  58. context.ResponseData.Write(TimeZoneInfo.GetSystemTimeZones().Count);
  59. return 0;
  60. }
  61. public long LoadLocationNameList(ServiceCtx context)
  62. {
  63. long bufferPosition = context.Response.SendBuff[0].Position;
  64. long bufferSize = context.Response.SendBuff[0].Size;
  65. int offset = 0;
  66. foreach (TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
  67. {
  68. byte[] tzData = Encoding.ASCII.GetBytes(info.Id);
  69. context.Memory.WriteBytes(bufferPosition + offset, tzData);
  70. int padding = 0x24 - tzData.Length;
  71. for (int index = 0; index < padding; index++)
  72. {
  73. context.ResponseData.Write((byte)0);
  74. }
  75. offset += 0x24;
  76. }
  77. return 0;
  78. }
  79. public long LoadTimeZoneRule(ServiceCtx context)
  80. {
  81. long bufferPosition = context.Request.ReceiveBuff[0].Position;
  82. long bufferSize = context.Request.ReceiveBuff[0].Size;
  83. if (bufferSize != 0x4000)
  84. {
  85. Logger.PrintWarning(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
  86. }
  87. long resultCode = 0;
  88. byte[] locationName = context.RequestData.ReadBytes(0x24);
  89. string tzId = Encoding.ASCII.GetString(locationName).TrimEnd('\0');
  90. // Check if the Time Zone exists, otherwise error out.
  91. try
  92. {
  93. TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(tzId);
  94. byte[] tzData = Encoding.ASCII.GetBytes(info.Id);
  95. // FIXME: This is not in ANY cases accurate, but the games don't care about the content of the buffer, they only pass it.
  96. // TODO: Reverse the TZif2 conversion in PCV to make this match with real hardware.
  97. context.Memory.WriteBytes(bufferPosition, tzData);
  98. }
  99. catch (TimeZoneNotFoundException)
  100. {
  101. Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {tzId} (len: {tzId.Length})");
  102. resultCode = MakeError(ErrorModule.Time, 0x3dd);
  103. }
  104. return resultCode;
  105. }
  106. private long ToCalendarTimeWithTz(ServiceCtx context, long posixTime, TimeZoneInfo info)
  107. {
  108. DateTime currentTime = Epoch.AddSeconds(posixTime);
  109. currentTime = TimeZoneInfo.ConvertTimeFromUtc(currentTime, info);
  110. context.ResponseData.Write((ushort)currentTime.Year);
  111. context.ResponseData.Write((byte)currentTime.Month);
  112. context.ResponseData.Write((byte)currentTime.Day);
  113. context.ResponseData.Write((byte)currentTime.Hour);
  114. context.ResponseData.Write((byte)currentTime.Minute);
  115. context.ResponseData.Write((byte)currentTime.Second);
  116. context.ResponseData.Write((byte)0); //MilliSecond ?
  117. context.ResponseData.Write((int)currentTime.DayOfWeek);
  118. context.ResponseData.Write(currentTime.DayOfYear - 1);
  119. context.ResponseData.Write(new byte[8]); //TODO: Find out the names used.
  120. context.ResponseData.Write((byte)(currentTime.IsDaylightSavingTime() ? 1 : 0));
  121. context.ResponseData.Write((int)info.GetUtcOffset(currentTime).TotalSeconds);
  122. return 0;
  123. }
  124. public long ToCalendarTime(ServiceCtx context)
  125. {
  126. long posixTime = context.RequestData.ReadInt64();
  127. long bufferPosition = context.Request.SendBuff[0].Position;
  128. long bufferSize = context.Request.SendBuff[0].Size;
  129. if (bufferSize != 0x4000)
  130. {
  131. Logger.PrintWarning(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
  132. }
  133. // TODO: Reverse the TZif2 conversion in PCV to make this match with real hardware.
  134. byte[] tzData = context.Memory.ReadBytes(bufferPosition, 0x24);
  135. string tzId = Encoding.ASCII.GetString(tzData).TrimEnd('\0');
  136. long resultCode = 0;
  137. // Check if the Time Zone exists, otherwise error out.
  138. try
  139. {
  140. TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(tzId);
  141. resultCode = ToCalendarTimeWithTz(context, posixTime, info);
  142. }
  143. catch (TimeZoneNotFoundException)
  144. {
  145. Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {tzId} (len: {tzId.Length})");
  146. resultCode = MakeError(ErrorModule.Time, 0x3dd);
  147. }
  148. return resultCode;
  149. }
  150. public long ToCalendarTimeWithMyRule(ServiceCtx context)
  151. {
  152. long posixTime = context.RequestData.ReadInt64();
  153. return ToCalendarTimeWithTz(context, posixTime, _timeZone);
  154. }
  155. public long ToPosixTime(ServiceCtx context)
  156. {
  157. long bufferPosition = context.Request.SendBuff[0].Position;
  158. long bufferSize = context.Request.SendBuff[0].Size;
  159. ushort year = context.RequestData.ReadUInt16();
  160. byte month = context.RequestData.ReadByte();
  161. byte day = context.RequestData.ReadByte();
  162. byte hour = context.RequestData.ReadByte();
  163. byte minute = context.RequestData.ReadByte();
  164. byte second = context.RequestData.ReadByte();
  165. DateTime calendarTime = new DateTime(year, month, day, hour, minute, second);
  166. if (bufferSize != 0x4000)
  167. {
  168. Logger.PrintWarning(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
  169. }
  170. // TODO: Reverse the TZif2 conversion in PCV to make this match with real hardware.
  171. byte[] tzData = context.Memory.ReadBytes(bufferPosition, 0x24);
  172. string tzId = Encoding.ASCII.GetString(tzData).TrimEnd('\0');
  173. long resultCode = 0;
  174. // Check if the Time Zone exists, otherwise error out.
  175. try
  176. {
  177. TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(tzId);
  178. return ToPosixTimeWithTz(context, calendarTime, info);
  179. }
  180. catch (TimeZoneNotFoundException)
  181. {
  182. Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {tzId} (len: {tzId.Length})");
  183. resultCode = MakeError(ErrorModule.Time, 0x3dd);
  184. }
  185. return resultCode;
  186. }
  187. public long ToPosixTimeWithMyRule(ServiceCtx context)
  188. {
  189. ushort year = context.RequestData.ReadUInt16();
  190. byte month = context.RequestData.ReadByte();
  191. byte day = context.RequestData.ReadByte();
  192. byte hour = context.RequestData.ReadByte();
  193. byte minute = context.RequestData.ReadByte();
  194. byte second = context.RequestData.ReadByte();
  195. DateTime calendarTime = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Local);
  196. return ToPosixTimeWithTz(context, calendarTime, _timeZone);
  197. }
  198. private long ToPosixTimeWithTz(ServiceCtx context, DateTime calendarTime, TimeZoneInfo info)
  199. {
  200. DateTime calenderTimeUtc = TimeZoneInfo.ConvertTimeToUtc(calendarTime, info);
  201. long posixTime = ((DateTimeOffset)calenderTimeUtc).ToUnixTimeSeconds();
  202. long position = context.Request.RecvListBuff[0].Position;
  203. long size = context.Request.RecvListBuff[0].Size;
  204. context.Memory.WriteInt64(position, posixTime);
  205. context.ResponseData.Write(1);
  206. return 0;
  207. }
  208. }
  209. }