Ryujinx/Ryujinx.HLE/HOS/Services/Time/ITimeZoneService.cs

281 lines
9.9 KiB
C#
Raw Normal View History

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
{
2018-12-01 21:01:59 +01:00
private Dictionary<int, ServiceProcessRequest> _commands;
2018-12-01 21:01:59 +01:00
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
2018-12-01 21:01:59 +01:00
private TimeZoneInfo _timeZone = TimeZoneInfo.Local;
public ITimeZoneService()
{
2018-12-01 21:01:59 +01:00
_commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 0, GetDeviceLocationName },
{ 1, SetDeviceLocationName },
{ 2, GetTotalLocationNameCount },
{ 3, LoadLocationNameList },
{ 4, LoadTimeZoneRule },
{ 100, ToCalendarTime },
{ 101, ToCalendarTimeWithMyRule },
{ 201, ToPosixTime },
{ 202, ToPosixTimeWithMyRule }
};
}
2018-12-01 21:01:59 +01:00
public long GetDeviceLocationName(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
char[] tzName = _timeZone.Id.ToCharArray();
2018-12-01 21:01:59 +01:00
context.ResponseData.Write(tzName);
2018-12-01 21:01:59 +01:00
int padding = 0x24 - tzName.Length;
2018-12-01 21:01:59 +01:00
for (int index = 0; index < padding; index++)
{
2018-12-01 21:01:59 +01:00
context.ResponseData.Write((byte)0);
}
return 0;
}
2018-12-01 21:01:59 +01:00
public long SetDeviceLocationName(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
byte[] locationName = context.RequestData.ReadBytes(0x24);
2018-12-01 21:01:59 +01:00
string tzId = Encoding.ASCII.GetString(locationName).TrimEnd('\0');
2018-12-01 21:01:59 +01:00
long resultCode = 0;
try
{
2018-12-01 21:01:59 +01:00
_timeZone = TimeZoneInfo.FindSystemTimeZoneById(tzId);
}
catch (TimeZoneNotFoundException)
{
2018-12-01 21:01:59 +01:00
resultCode = MakeError(ErrorModule.Time, 0x3dd);
}
2018-12-01 21:01:59 +01:00
return resultCode;
}
2018-12-01 21:01:59 +01:00
public long GetTotalLocationNameCount(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
context.ResponseData.Write(TimeZoneInfo.GetSystemTimeZones().Count);
return 0;
}
2018-12-01 21:01:59 +01:00
public long LoadLocationNameList(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long bufferPosition = context.Response.SendBuff[0].Position;
long bufferSize = context.Response.SendBuff[0].Size;
2018-12-01 21:01:59 +01:00
int offset = 0;
foreach (TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
{
2018-12-01 21:01:59 +01:00
byte[] tzData = Encoding.ASCII.GetBytes(info.Id);
2018-12-01 21:01:59 +01:00
context.Memory.WriteBytes(bufferPosition + offset, tzData);
2018-12-01 21:01:59 +01:00
int padding = 0x24 - tzData.Length;
2018-12-01 21:01:59 +01:00
for (int index = 0; index < padding; index++)
{
2018-12-01 21:01:59 +01:00
context.ResponseData.Write((byte)0);
}
2018-12-01 21:01:59 +01:00
offset += 0x24;
}
return 0;
}
2018-12-01 21:01:59 +01:00
public long LoadTimeZoneRule(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long bufferPosition = context.Request.ReceiveBuff[0].Position;
long bufferSize = context.Request.ReceiveBuff[0].Size;
2018-12-01 21:01:59 +01:00
if (bufferSize != 0x4000)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
}
2018-12-01 21:01:59 +01:00
long resultCode = 0;
2018-12-01 21:01:59 +01:00
byte[] locationName = context.RequestData.ReadBytes(0x24);
2018-12-01 21:01:59 +01:00
string tzId = Encoding.ASCII.GetString(locationName).TrimEnd('\0');
// Check if the Time Zone exists, otherwise error out.
try
{
2018-12-01 21:01:59 +01:00
TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(tzId);
2018-12-01 21:01:59 +01:00
byte[] tzData = Encoding.ASCII.GetBytes(info.Id);
2018-07-22 21:38:29 +02:00
// 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.
2018-12-01 21:01:59 +01:00
context.Memory.WriteBytes(bufferPosition, tzData);
}
catch (TimeZoneNotFoundException)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {tzId} (len: {tzId.Length})");
2018-12-01 21:01:59 +01:00
resultCode = MakeError(ErrorModule.Time, 0x3dd);
}
2018-12-01 21:01:59 +01:00
return resultCode;
}
2018-12-01 21:01:59 +01:00
private long ToCalendarTimeWithTz(ServiceCtx context, long posixTime, TimeZoneInfo info)
{
2018-12-01 21:01:59 +01:00
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;
}
2018-12-01 21:01:59 +01:00
public long ToCalendarTime(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long posixTime = context.RequestData.ReadInt64();
long bufferPosition = context.Request.SendBuff[0].Position;
long bufferSize = context.Request.SendBuff[0].Size;
2018-12-01 21:01:59 +01:00
if (bufferSize != 0x4000)
{
2018-12-01 21:01:59 +01:00
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.
2018-12-01 21:01:59 +01:00
byte[] tzData = context.Memory.ReadBytes(bufferPosition, 0x24);
2018-12-01 21:01:59 +01:00
string tzId = Encoding.ASCII.GetString(tzData).TrimEnd('\0');
2018-12-01 21:01:59 +01:00
long resultCode = 0;
// Check if the Time Zone exists, otherwise error out.
try
{
2018-12-01 21:01:59 +01:00
TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(tzId);
2018-12-01 21:01:59 +01:00
resultCode = ToCalendarTimeWithTz(context, posixTime, info);
}
catch (TimeZoneNotFoundException)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {tzId} (len: {tzId.Length})");
2018-12-01 21:01:59 +01:00
resultCode = MakeError(ErrorModule.Time, 0x3dd);
}
2018-12-01 21:01:59 +01:00
return resultCode;
}
2018-12-01 21:01:59 +01:00
public long ToCalendarTimeWithMyRule(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long posixTime = context.RequestData.ReadInt64();
2018-12-01 21:01:59 +01:00
return ToCalendarTimeWithTz(context, posixTime, _timeZone);
}
2018-12-01 21:01:59 +01:00
public long ToPosixTime(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
long bufferPosition = context.Request.SendBuff[0].Position;
long bufferSize = context.Request.SendBuff[0].Size;
2018-12-01 21:01:59 +01:00
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();
2018-12-01 21:01:59 +01:00
DateTime calendarTime = new DateTime(year, month, day, hour, minute, second);
2018-12-01 21:01:59 +01:00
if (bufferSize != 0x4000)
{
2018-12-01 21:01:59 +01:00
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.
2018-12-01 21:01:59 +01:00
byte[] tzData = context.Memory.ReadBytes(bufferPosition, 0x24);
2018-12-01 21:01:59 +01:00
string tzId = Encoding.ASCII.GetString(tzData).TrimEnd('\0');
2018-12-01 21:01:59 +01:00
long resultCode = 0;
// Check if the Time Zone exists, otherwise error out.
try
{
2018-12-01 21:01:59 +01:00
TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(tzId);
2018-12-01 21:01:59 +01:00
return ToPosixTimeWithTz(context, calendarTime, info);
}
catch (TimeZoneNotFoundException)
{
2018-12-01 21:01:59 +01:00
Logger.PrintWarning(LogClass.ServiceTime, $"Timezone not found for string: {tzId} (len: {tzId.Length})");
2018-12-01 21:01:59 +01:00
resultCode = MakeError(ErrorModule.Time, 0x3dd);
}
2018-12-01 21:01:59 +01:00
return resultCode;
}
2018-12-01 21:01:59 +01:00
public long ToPosixTimeWithMyRule(ServiceCtx context)
{
2018-12-01 21:01:59 +01:00
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();
2018-12-01 21:01:59 +01:00
DateTime calendarTime = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Local);
2018-12-01 21:01:59 +01:00
return ToPosixTimeWithTz(context, calendarTime, _timeZone);
}
2018-12-01 21:01:59 +01:00
private long ToPosixTimeWithTz(ServiceCtx context, DateTime calendarTime, TimeZoneInfo info)
{
2018-12-01 21:01:59 +01:00
DateTime calenderTimeUtc = TimeZoneInfo.ConvertTimeToUtc(calendarTime, info);
2018-12-01 21:01:59 +01:00
long posixTime = ((DateTimeOffset)calenderTimeUtc).ToUnixTimeSeconds();
2018-12-01 21:01:59 +01:00
long position = context.Request.RecvListBuff[0].Position;
long size = context.Request.RecvListBuff[0].Size;
2018-12-01 21:01:59 +01:00
context.Memory.WriteInt64(position, posixTime);
2018-12-01 21:01:59 +01:00
context.ResponseData.Write(1);
return 0;
}
}
}