| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- using Ryujinx.Common.Logging;
- using Ryujinx.HLE.HOS.Ipc;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using static Ryujinx.HLE.HOS.ErrorCode;
- namespace Ryujinx.HLE.HOS.Services.Time
- {
- class ITimeZoneService : IpcService
- {
- private Dictionary<int, ServiceProcessRequest> _commands;
- public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
- private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- private TimeZoneInfo _timeZone = TimeZoneInfo.Local;
- public ITimeZoneService()
- {
- _commands = new Dictionary<int, ServiceProcessRequest>
- {
- { 0, GetDeviceLocationName },
- { 1, SetDeviceLocationName },
- { 2, GetTotalLocationNameCount },
- { 3, LoadLocationNameList },
- { 4, LoadTimeZoneRule },
- { 100, ToCalendarTime },
- { 101, ToCalendarTimeWithMyRule },
- { 201, ToPosixTime },
- { 202, ToPosixTimeWithMyRule }
- };
- }
- public long GetDeviceLocationName(ServiceCtx context)
- {
- char[] tzName = _timeZone.Id.ToCharArray();
- context.ResponseData.Write(tzName);
- int padding = 0x24 - tzName.Length;
- for (int index = 0; index < padding; index++)
- {
- context.ResponseData.Write((byte)0);
- }
- return 0;
- }
- public long SetDeviceLocationName(ServiceCtx context)
- {
- byte[] locationName = context.RequestData.ReadBytes(0x24);
- string tzId = Encoding.ASCII.GetString(locationName).TrimEnd('\0');
- long resultCode = 0;
- try
- {
- _timeZone = TimeZoneInfo.FindSystemTimeZoneById(tzId);
- }
- catch (TimeZoneNotFoundException)
- {
- resultCode = MakeError(ErrorModule.Time, 0x3dd);
- }
- return resultCode;
- }
- public long GetTotalLocationNameCount(ServiceCtx context)
- {
- context.ResponseData.Write(TimeZoneInfo.GetSystemTimeZones().Count);
- return 0;
- }
- public long LoadLocationNameList(ServiceCtx context)
- {
- long bufferPosition = context.Response.SendBuff[0].Position;
- long bufferSize = context.Response.SendBuff[0].Size;
- int offset = 0;
- foreach (TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
- {
- byte[] tzData = Encoding.ASCII.GetBytes(info.Id);
- context.Memory.WriteBytes(bufferPosition + offset, tzData);
- int padding = 0x24 - tzData.Length;
- for (int index = 0; index < padding; index++)
- {
- context.ResponseData.Write((byte)0);
- }
- offset += 0x24;
- }
- return 0;
- }
- public long LoadTimeZoneRule(ServiceCtx context)
- {
- long bufferPosition = context.Request.ReceiveBuff[0].Position;
- long bufferSize = context.Request.ReceiveBuff[0].Size;
- if (bufferSize != 0x4000)
- {
- Logger.PrintWarning(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
- }
- long resultCode = 0;
- byte[] locationName = context.RequestData.ReadBytes(0x24);
- string tzId = Encoding.ASCII.GetString(locationName).TrimEnd('\0');
- // Check if the Time Zone exists, otherwise error out.
- try
- {
- TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(tzId);
- byte[] tzData = Encoding.ASCII.GetBytes(info.Id);
- // FIXME: This is not in ANY cases accurate, but the games don't care about the content of the buffer, they only pass it.
- // TODO: Reverse the TZif2 conversion in PCV to make this match with real hardware.
- context.Memory.WriteBytes(bufferPosition, tzData);
- }
- catch (TimeZoneNotFoundException)
- {
- Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {tzId} (len: {tzId.Length})");
- resultCode = MakeError(ErrorModule.Time, 0x3dd);
- }
- return resultCode;
- }
- private long ToCalendarTimeWithTz(ServiceCtx context, long posixTime, TimeZoneInfo info)
- {
- DateTime currentTime = Epoch.AddSeconds(posixTime);
- currentTime = TimeZoneInfo.ConvertTimeFromUtc(currentTime, info);
- context.ResponseData.Write((ushort)currentTime.Year);
- context.ResponseData.Write((byte)currentTime.Month);
- context.ResponseData.Write((byte)currentTime.Day);
- context.ResponseData.Write((byte)currentTime.Hour);
- context.ResponseData.Write((byte)currentTime.Minute);
- context.ResponseData.Write((byte)currentTime.Second);
- context.ResponseData.Write((byte)0); //MilliSecond ?
- context.ResponseData.Write((int)currentTime.DayOfWeek);
- context.ResponseData.Write(currentTime.DayOfYear - 1);
- context.ResponseData.Write(new byte[8]); //TODO: Find out the names used.
- context.ResponseData.Write((byte)(currentTime.IsDaylightSavingTime() ? 1 : 0));
- context.ResponseData.Write((int)info.GetUtcOffset(currentTime).TotalSeconds);
- return 0;
- }
- public long ToCalendarTime(ServiceCtx context)
- {
- long posixTime = context.RequestData.ReadInt64();
- long bufferPosition = context.Request.SendBuff[0].Position;
- long bufferSize = context.Request.SendBuff[0].Size;
- if (bufferSize != 0x4000)
- {
- Logger.PrintWarning(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
- }
- // TODO: Reverse the TZif2 conversion in PCV to make this match with real hardware.
- byte[] tzData = context.Memory.ReadBytes(bufferPosition, 0x24);
- string tzId = Encoding.ASCII.GetString(tzData).TrimEnd('\0');
- long resultCode = 0;
- // Check if the Time Zone exists, otherwise error out.
- try
- {
- TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(tzId);
- resultCode = ToCalendarTimeWithTz(context, posixTime, info);
- }
- catch (TimeZoneNotFoundException)
- {
- Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {tzId} (len: {tzId.Length})");
- resultCode = MakeError(ErrorModule.Time, 0x3dd);
- }
- return resultCode;
- }
- public long ToCalendarTimeWithMyRule(ServiceCtx context)
- {
- long posixTime = context.RequestData.ReadInt64();
- return ToCalendarTimeWithTz(context, posixTime, _timeZone);
- }
- public long ToPosixTime(ServiceCtx context)
- {
- long bufferPosition = context.Request.SendBuff[0].Position;
- long bufferSize = context.Request.SendBuff[0].Size;
- ushort year = context.RequestData.ReadUInt16();
- byte month = context.RequestData.ReadByte();
- byte day = context.RequestData.ReadByte();
- byte hour = context.RequestData.ReadByte();
- byte minute = context.RequestData.ReadByte();
- byte second = context.RequestData.ReadByte();
- DateTime calendarTime = new DateTime(year, month, day, hour, minute, second);
- if (bufferSize != 0x4000)
- {
- Logger.PrintWarning(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
- }
- // TODO: Reverse the TZif2 conversion in PCV to make this match with real hardware.
- byte[] tzData = context.Memory.ReadBytes(bufferPosition, 0x24);
- string tzId = Encoding.ASCII.GetString(tzData).TrimEnd('\0');
- long resultCode = 0;
- // Check if the Time Zone exists, otherwise error out.
- try
- {
- TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(tzId);
- return ToPosixTimeWithTz(context, calendarTime, info);
- }
- catch (TimeZoneNotFoundException)
- {
- Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {tzId} (len: {tzId.Length})");
- resultCode = MakeError(ErrorModule.Time, 0x3dd);
- }
- return resultCode;
- }
- public long ToPosixTimeWithMyRule(ServiceCtx context)
- {
- ushort year = context.RequestData.ReadUInt16();
- byte month = context.RequestData.ReadByte();
- byte day = context.RequestData.ReadByte();
- byte hour = context.RequestData.ReadByte();
- byte minute = context.RequestData.ReadByte();
- byte second = context.RequestData.ReadByte();
- DateTime calendarTime = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Local);
- return ToPosixTimeWithTz(context, calendarTime, _timeZone);
- }
- private long ToPosixTimeWithTz(ServiceCtx context, DateTime calendarTime, TimeZoneInfo info)
- {
- DateTime calenderTimeUtc = TimeZoneInfo.ConvertTimeToUtc(calendarTime, info);
- long posixTime = ((DateTimeOffset)calenderTimeUtc).ToUnixTimeSeconds();
- long position = context.Request.RecvListBuff[0].Position;
- long size = context.Request.RecvListBuff[0].Size;
- context.Memory.WriteInt64(position, posixTime);
- context.ResponseData.Write(1);
- return 0;
- }
- }
- }
|