UtilityImpl.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Ryujinx.Common.Utilities;
  2. using Ryujinx.Cpu;
  3. using Ryujinx.HLE.HOS.Services.Mii.Types;
  4. using Ryujinx.HLE.HOS.Services.Time;
  5. using Ryujinx.HLE.HOS.Services.Time.Clock;
  6. using System;
  7. namespace Ryujinx.HLE.HOS.Services.Mii
  8. {
  9. class UtilityImpl
  10. {
  11. private uint _x;
  12. private uint _y;
  13. private uint _z;
  14. private uint _w;
  15. public UtilityImpl(ITickSource tickSource)
  16. {
  17. _x = 123456789;
  18. _y = 362436069;
  19. TimeSpanType time = TimeManager.Instance.TickBasedSteadyClock.GetCurrentRawTimePoint(tickSource);
  20. _w = (uint)(time.NanoSeconds & uint.MaxValue);
  21. _z = (uint)((time.NanoSeconds >> 32) & uint.MaxValue);
  22. }
  23. private uint GetRandom()
  24. {
  25. uint t = (_x ^ (_x << 11));
  26. _x = _y;
  27. _y = _z;
  28. _z = _w;
  29. _w = (_w ^ (_w >> 19)) ^ (t ^ (t >> 8));
  30. return _w;
  31. }
  32. public int GetRandom(int end)
  33. {
  34. return (int)GetRandom((uint)end);
  35. }
  36. public uint GetRandom(uint end)
  37. {
  38. uint random = GetRandom();
  39. return random - random / end * end;
  40. }
  41. public uint GetRandom(uint start, uint end)
  42. {
  43. uint random = GetRandom();
  44. return random - random / (1 - start + end) * (1 - start + end) + start;
  45. }
  46. public int GetRandom(int start, int end)
  47. {
  48. return (int)GetRandom((uint)start, (uint)end);
  49. }
  50. public CreateId MakeCreateId()
  51. {
  52. UInt128 value = UInt128Utils.CreateRandom();
  53. // Ensure the random ID generated is valid as a create id.
  54. value &= ~new UInt128(0xC0, 0);
  55. value |= new UInt128(0x80, 0);
  56. return new CreateId(value);
  57. }
  58. }
  59. }