ISystemClock.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Ryujinx.HLE.OsHle.Ipc;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.HLE.OsHle.Services.Time
  5. {
  6. class ISystemClock : IpcService
  7. {
  8. private Dictionary<int, ServiceProcessRequest> m_Commands;
  9. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  10. private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  11. private SystemClockType ClockType;
  12. private DateTime SystemClockContextEpoch;
  13. private long SystemClockTimePoint;
  14. private byte[] SystemClockContextEnding;
  15. private long TimeOffset;
  16. public ISystemClock(SystemClockType ClockType)
  17. {
  18. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  19. {
  20. { 0, GetCurrentTime },
  21. { 1, SetCurrentTime },
  22. { 2, GetSystemClockContext },
  23. { 3, SetSystemClockContext }
  24. };
  25. this.ClockType = ClockType;
  26. SystemClockContextEpoch = System.Diagnostics.Process.GetCurrentProcess().StartTime;
  27. SystemClockContextEnding = new byte[0x10];
  28. TimeOffset = 0;
  29. if (ClockType == SystemClockType.User ||
  30. ClockType == SystemClockType.Network)
  31. {
  32. SystemClockContextEpoch = SystemClockContextEpoch.ToUniversalTime();
  33. }
  34. SystemClockTimePoint = (long)(SystemClockContextEpoch - Epoch).TotalSeconds;
  35. }
  36. public long GetCurrentTime(ServiceCtx Context)
  37. {
  38. DateTime CurrentTime = DateTime.Now;
  39. if (ClockType == SystemClockType.User ||
  40. ClockType == SystemClockType.Network)
  41. {
  42. CurrentTime = CurrentTime.ToUniversalTime();
  43. }
  44. Context.ResponseData.Write((long)((CurrentTime - Epoch).TotalSeconds) + TimeOffset);
  45. return 0;
  46. }
  47. public long SetCurrentTime(ServiceCtx Context)
  48. {
  49. DateTime CurrentTime = DateTime.Now;
  50. if (ClockType == SystemClockType.User ||
  51. ClockType == SystemClockType.Network)
  52. {
  53. CurrentTime = CurrentTime.ToUniversalTime();
  54. }
  55. TimeOffset = (Context.RequestData.ReadInt64() - (long)(CurrentTime - Epoch).TotalSeconds);
  56. return 0;
  57. }
  58. public long GetSystemClockContext(ServiceCtx Context)
  59. {
  60. Context.ResponseData.Write((long)(SystemClockContextEpoch - Epoch).TotalSeconds);
  61. // The point in time, TODO: is there a link between epoch and this?
  62. Context.ResponseData.Write(SystemClockTimePoint);
  63. // This seems to be some kind of identifier?
  64. for (int i = 0; i < 0x10; i++)
  65. {
  66. Context.ResponseData.Write(SystemClockContextEnding[i]);
  67. }
  68. return 0;
  69. }
  70. public long SetSystemClockContext(ServiceCtx Context)
  71. {
  72. long NewSystemClockEpoch = Context.RequestData.ReadInt64();
  73. long NewSystemClockTimePoint = Context.RequestData.ReadInt64();
  74. SystemClockContextEpoch = Epoch.Add(TimeSpan.FromSeconds(NewSystemClockEpoch));
  75. SystemClockTimePoint = NewSystemClockTimePoint;
  76. SystemClockContextEnding = Context.RequestData.ReadBytes(0x10);
  77. return 0;
  78. }
  79. }
  80. }