ITimeZoneService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using Ryujinx.HLE.Logging;
  2. using Ryujinx.HLE.OsHle.Ipc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. namespace Ryujinx.HLE.OsHle.Services.Time
  7. {
  8. class ITimeZoneService : IpcService
  9. {
  10. private Dictionary<int, ServiceProcessRequest> m_Commands;
  11. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  12. private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  13. private TimeZoneInfo TimeZone = TimeZoneInfo.Local;
  14. public ITimeZoneService()
  15. {
  16. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  17. {
  18. { 0, GetDeviceLocationName },
  19. { 1, SetDeviceLocationName },
  20. { 2, GetTotalLocationNameCount },
  21. { 3, LoadLocationNameList },
  22. { 4, LoadTimeZoneRule },
  23. { 100, ToCalendarTime },
  24. { 101, ToCalendarTimeWithMyRule }
  25. };
  26. }
  27. public long GetDeviceLocationName(ServiceCtx Context)
  28. {
  29. char[] TzName = TimeZone.Id.ToCharArray();
  30. Context.ResponseData.Write(TzName);
  31. int Padding = 0x24 - TzName.Length;
  32. for (int Index = 0; Index < Padding; Index++)
  33. {
  34. Context.ResponseData.Write((byte)0);
  35. }
  36. return 0;
  37. }
  38. public long SetDeviceLocationName(ServiceCtx Context)
  39. {
  40. byte[] LocationName = Context.RequestData.ReadBytes(0x24);
  41. string TzID = Encoding.ASCII.GetString(LocationName).TrimEnd('\0');
  42. long ResultCode = 0;
  43. try
  44. {
  45. TimeZone = TimeZoneInfo.FindSystemTimeZoneById(TzID);
  46. }
  47. catch (TimeZoneNotFoundException e)
  48. {
  49. ResultCode = 0x7BA74;
  50. }
  51. return ResultCode;
  52. }
  53. public long GetTotalLocationNameCount(ServiceCtx Context)
  54. {
  55. Context.ResponseData.Write(TimeZoneInfo.GetSystemTimeZones().Count);
  56. return 0;
  57. }
  58. public long LoadLocationNameList(ServiceCtx Context)
  59. {
  60. long BufferPosition = Context.Response.SendBuff[0].Position;
  61. long BufferSize = Context.Response.SendBuff[0].Size;
  62. int i = 0;
  63. foreach (TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
  64. {
  65. byte[] TzData = Encoding.ASCII.GetBytes(info.Id);
  66. Context.Memory.WriteBytes(BufferPosition + i, TzData);
  67. int Padding = 0x24 - TzData.Length;
  68. for (int Index = 0; Index < Padding; Index++)
  69. {
  70. Context.ResponseData.Write((byte)0);
  71. }
  72. i += 0x24;
  73. }
  74. return 0;
  75. }
  76. public long LoadTimeZoneRule(ServiceCtx Context)
  77. {
  78. long BufferPosition = Context.Request.ReceiveBuff[0].Position;
  79. long BufferSize = Context.Request.ReceiveBuff[0].Size;
  80. if (BufferSize != 0x4000)
  81. {
  82. Context.Ns.Log.PrintWarning(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{BufferSize:x} (expected 0x4000)");
  83. }
  84. long ResultCode = 0;
  85. byte[] LocationName = Context.RequestData.ReadBytes(0x24);
  86. string TzID = Encoding.ASCII.GetString(LocationName).TrimEnd('\0');
  87. // Check if the Time Zone exists, otherwise error out.
  88. try
  89. {
  90. TimeZoneInfo Info = TimeZoneInfo.FindSystemTimeZoneById(TzID);
  91. byte[] TzData = Encoding.ASCII.GetBytes(Info.Id);
  92. // FIXME: This is not in ANY cases accurate, but the games don't about the content of the buffer, they only pass it.
  93. // TODO: Reverse the TZif2 conversion in PCV to make this match with real hardware.
  94. Context.Memory.WriteBytes(BufferPosition, TzData);
  95. }
  96. catch (TimeZoneNotFoundException e)
  97. {
  98. Context.Ns.Log.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {TzID} (len: {TzID.Length})");
  99. ResultCode = 0x7BA74;
  100. }
  101. return ResultCode;
  102. }
  103. private long ToCalendarTimeWithTz(ServiceCtx Context, long PosixTime, TimeZoneInfo Info)
  104. {
  105. DateTime CurrentTime = Epoch.AddSeconds(PosixTime);
  106. CurrentTime = TimeZoneInfo.ConvertTimeFromUtc(CurrentTime, Info);
  107. Context.ResponseData.Write((ushort)CurrentTime.Year);
  108. Context.ResponseData.Write((byte)CurrentTime.Month);
  109. Context.ResponseData.Write((byte)CurrentTime.Day);
  110. Context.ResponseData.Write((byte)CurrentTime.Hour);
  111. Context.ResponseData.Write((byte)CurrentTime.Minute);
  112. Context.ResponseData.Write((byte)CurrentTime.Second);
  113. Context.ResponseData.Write((byte)0); //MilliSecond ?
  114. Context.ResponseData.Write((int)CurrentTime.DayOfWeek);
  115. Context.ResponseData.Write(CurrentTime.DayOfYear - 1);
  116. Context.ResponseData.Write(new byte[8]); //TODO: Find out the names used.
  117. Context.ResponseData.Write((byte)(CurrentTime.IsDaylightSavingTime() ? 1 : 0));
  118. Context.ResponseData.Write((int)Info.GetUtcOffset(CurrentTime).TotalSeconds);
  119. return 0;
  120. }
  121. public long ToCalendarTime(ServiceCtx Context)
  122. {
  123. long PosixTime = Context.RequestData.ReadInt64();
  124. long BufferPosition = Context.Request.SendBuff[0].Position;
  125. long BufferSize = Context.Request.SendBuff[0].Size;
  126. if (BufferSize != 0x4000)
  127. {
  128. Context.Ns.Log.PrintWarning(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{BufferSize:x} (expected 0x4000)");
  129. }
  130. // TODO: Reverse the TZif2 conversion in PCV to make this match with real hardware.
  131. byte[] TzData = Context.Memory.ReadBytes(BufferPosition, 0x24);
  132. string TzID = Encoding.ASCII.GetString(TzData).TrimEnd('\0');
  133. long ResultCode = 0;
  134. // Check if the Time Zone exists, otherwise error out.
  135. try
  136. {
  137. TimeZoneInfo Info = TimeZoneInfo.FindSystemTimeZoneById(TzID);
  138. ResultCode = ToCalendarTimeWithTz(Context, PosixTime, Info);
  139. }
  140. catch (TimeZoneNotFoundException e)
  141. {
  142. Context.Ns.Log.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {TzID} (len: {TzID.Length})");
  143. ResultCode = 0x7BA74;
  144. }
  145. return ResultCode;
  146. }
  147. public long ToCalendarTimeWithMyRule(ServiceCtx Context)
  148. {
  149. long PosixTime = Context.RequestData.ReadInt64();
  150. return ToCalendarTimeWithTz(Context, PosixTime, TimeZone);
  151. }
  152. }
  153. }