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