2018-08-16 23:47:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2018-02-10 00:14:55 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Time
|
2018-02-10 00:14:55 +00:00
|
|
|
{
|
2018-03-19 18:58:46 +00:00
|
|
|
class ISystemClock : IpcService
|
2018-02-10 00:14:55 +00:00
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
private Dictionary<int, ServiceProcessRequest> _commands;
|
2018-02-10 00:14:55 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
|
2018-02-10 00:14:55 +00:00
|
|
|
|
2018-04-11 00:16:27 +00:00
|
|
|
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
2018-02-10 00:14:55 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
private SystemClockType _clockType;
|
2018-02-14 05:43:21 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
private DateTime _systemClockContextEpoch;
|
2018-07-02 00:03:05 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
private long _systemClockTimePoint;
|
2018-07-02 00:03:05 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
private byte[] _systemClockContextEnding;
|
2018-07-02 00:03:05 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
private long _timeOffset;
|
2018-07-02 00:03:05 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
public ISystemClock(SystemClockType clockType)
|
2018-02-10 00:14:55 +00:00
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
_commands = new Dictionary<int, ServiceProcessRequest>
|
2018-02-10 00:14:55 +00:00
|
|
|
{
|
2018-07-02 00:03:05 +00:00
|
|
|
{ 0, GetCurrentTime },
|
|
|
|
{ 1, SetCurrentTime },
|
|
|
|
{ 2, GetSystemClockContext },
|
|
|
|
{ 3, SetSystemClockContext }
|
2018-02-10 00:14:55 +00:00
|
|
|
};
|
2018-02-14 05:43:21 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
_clockType = clockType;
|
|
|
|
_systemClockContextEpoch = System.Diagnostics.Process.GetCurrentProcess().StartTime;
|
|
|
|
_systemClockContextEnding = new byte[0x10];
|
|
|
|
_timeOffset = 0;
|
2018-07-02 00:03:05 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
if (clockType == SystemClockType.User ||
|
|
|
|
clockType == SystemClockType.Network)
|
2018-07-02 00:03:05 +00:00
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
_systemClockContextEpoch = _systemClockContextEpoch.ToUniversalTime();
|
2018-07-02 00:03:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
_systemClockTimePoint = (long)(_systemClockContextEpoch - Epoch).TotalSeconds;
|
2018-02-10 00:14:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
public long GetCurrentTime(ServiceCtx context)
|
2018-02-10 00:14:55 +00:00
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
DateTime currentTime = DateTime.Now;
|
2018-02-14 05:43:21 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
if (_clockType == SystemClockType.User ||
|
|
|
|
_clockType == SystemClockType.Network)
|
2018-02-14 05:43:21 +00:00
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
currentTime = currentTime.ToUniversalTime();
|
2018-02-14 05:43:21 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
context.ResponseData.Write((long)((currentTime - Epoch).TotalSeconds) + _timeOffset);
|
2018-07-02 00:03:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
public long SetCurrentTime(ServiceCtx context)
|
2018-07-02 00:03:05 +00:00
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
DateTime currentTime = DateTime.Now;
|
2018-07-02 00:03:05 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
if (_clockType == SystemClockType.User ||
|
|
|
|
_clockType == SystemClockType.Network)
|
2018-07-02 00:03:05 +00:00
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
currentTime = currentTime.ToUniversalTime();
|
2018-07-02 00:03:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
_timeOffset = (context.RequestData.ReadInt64() - (long)(currentTime - Epoch).TotalSeconds);
|
2018-07-02 00:03:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
public long GetSystemClockContext(ServiceCtx context)
|
2018-07-02 00:03:05 +00:00
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
context.ResponseData.Write((long)(_systemClockContextEpoch - Epoch).TotalSeconds);
|
2018-07-02 00:03:05 +00:00
|
|
|
|
|
|
|
// The point in time, TODO: is there a link between epoch and this?
|
2018-12-04 20:23:37 +00:00
|
|
|
context.ResponseData.Write(_systemClockTimePoint);
|
2018-07-02 00:03:05 +00:00
|
|
|
|
|
|
|
// This seems to be some kind of identifier?
|
|
|
|
for (int i = 0; i < 0x10; i++)
|
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
context.ResponseData.Write(_systemClockContextEnding[i]);
|
2018-07-02 00:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
public long SetSystemClockContext(ServiceCtx context)
|
2018-07-02 00:03:05 +00:00
|
|
|
{
|
2018-12-04 20:23:37 +00:00
|
|
|
long newSystemClockEpoch = context.RequestData.ReadInt64();
|
|
|
|
long newSystemClockTimePoint = context.RequestData.ReadInt64();
|
2018-07-02 00:03:05 +00:00
|
|
|
|
2018-12-04 20:23:37 +00:00
|
|
|
_systemClockContextEpoch = Epoch.Add(TimeSpan.FromSeconds(newSystemClockEpoch));
|
|
|
|
_systemClockTimePoint = newSystemClockTimePoint;
|
|
|
|
_systemClockContextEnding = context.RequestData.ReadBytes(0x10);
|
2018-02-10 00:14:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|