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> m_Commands;
  12. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_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. m_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. }