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