aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Time/ISystemClock.cs
blob: 1dfd7d74fdbeed3db4c1a49770bab1f55606b809 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using Ryujinx.HLE.HOS.Ipc;
using System;
using System.Collections.Generic;

namespace Ryujinx.HLE.HOS.Services.Time
{
    class ISystemClock : 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 SystemClockType _clockType;

        private DateTime _systemClockContextEpoch;

        private long _systemClockTimePoint;

        private byte[] _systemClockContextEnding;

        private long _timeOffset;

        public ISystemClock(SystemClockType clockType)
        {
            _commands = new Dictionary<int, ServiceProcessRequest>
            {
                { 0, GetCurrentTime        },
                { 1, SetCurrentTime        },
                { 2, GetSystemClockContext },
                { 3, SetSystemClockContext }
            };

            _clockType           = clockType;
            _systemClockContextEpoch  = System.Diagnostics.Process.GetCurrentProcess().StartTime;
            _systemClockContextEnding = new byte[0x10];
            _timeOffset               = 0;

            if (clockType == SystemClockType.User ||
                clockType == SystemClockType.Network)
            {
                _systemClockContextEpoch = _systemClockContextEpoch.ToUniversalTime();
            }

            _systemClockTimePoint = (long)(_systemClockContextEpoch - Epoch).TotalSeconds;
        }

        public long GetCurrentTime(ServiceCtx context)
        {
            DateTime currentTime = DateTime.Now;

            if (_clockType == SystemClockType.User ||
                _clockType == SystemClockType.Network)
            {
                currentTime = currentTime.ToUniversalTime();
            }

            context.ResponseData.Write((long)((currentTime - Epoch).TotalSeconds) + _timeOffset);

            return 0;
        }

        public long SetCurrentTime(ServiceCtx context)
        {
            DateTime currentTime = DateTime.Now;

            if (_clockType == SystemClockType.User ||
                _clockType == SystemClockType.Network)
            {
                currentTime = currentTime.ToUniversalTime();
            }

            _timeOffset = (context.RequestData.ReadInt64() - (long)(currentTime - Epoch).TotalSeconds);

            return 0;
        }

        public long GetSystemClockContext(ServiceCtx context)
        {
            context.ResponseData.Write((long)(_systemClockContextEpoch - Epoch).TotalSeconds);

            // The point in time, TODO: is there a link between epoch and this?
            context.ResponseData.Write(_systemClockTimePoint);

            // This seems to be some kind of identifier?
            for (int i = 0; i < 0x10; i++)
            {
                context.ResponseData.Write(_systemClockContextEnding[i]);
            }

            return 0;
        }

        public long SetSystemClockContext(ServiceCtx context)
        {
            long newSystemClockEpoch     = context.RequestData.ReadInt64();
            long newSystemClockTimePoint = context.RequestData.ReadInt64();

            _systemClockContextEpoch     = Epoch.Add(TimeSpan.FromSeconds(newSystemClockEpoch));
            _systemClockTimePoint        = newSystemClockTimePoint;
            _systemClockContextEnding    = context.RequestData.ReadBytes(0x10);

            return 0;
        }
    }
}