R/Ryujinx.HLE/HOS/Services/Mii/UtilityImpl.cs
Mary-nyan c6d05301aa
infra: Migrate to .NET 7 (#3795)
* Update readme to mention .NET 7

* infra: Migrate to .NET 7

.NET 7 is still in preview but this prepare for the release coming up
next month.

* Use Random.Shared in CreateRandom

* Move UInt128Utils.cs to Ryujinx.Common project

* Fix inverted parameters in System.UInt128 constructor

* Fix Visual Studio complains on  Ryujinx.Graphics.Vic

* time: Fix missing alignment enforcement in SystemClockContext

Fixes at least Smash

* time: Fix missing alignment enforcement in SteadyClockContext

Fix games (like recent version of Smash) using time shared memory

* Switch to .NET 7.0.100 release

* Enable Tiered PGO

* Ensure CreateId validity requirements are meet when doing random generation

Also enforce correct packing layout for other Mii structures.

This fix a Mario Kart 8 crashes related to the default Miis.
2022-11-09 20:22:43 +01:00

75 lines
1.8 KiB
C#

using Ryujinx.Common.Utilities;
using Ryujinx.Cpu;
using Ryujinx.HLE.HOS.Services.Mii.Types;
using Ryujinx.HLE.HOS.Services.Time;
using Ryujinx.HLE.HOS.Services.Time.Clock;
using System;
namespace Ryujinx.HLE.HOS.Services.Mii
{
class UtilityImpl
{
private uint _x;
private uint _y;
private uint _z;
private uint _w;
public UtilityImpl(ITickSource tickSource)
{
_x = 123456789;
_y = 362436069;
TimeSpanType time = TimeManager.Instance.TickBasedSteadyClock.GetCurrentRawTimePoint(tickSource);
_w = (uint)(time.NanoSeconds & uint.MaxValue);
_z = (uint)((time.NanoSeconds >> 32) & uint.MaxValue);
}
private uint GetRandom()
{
uint t = (_x ^ (_x << 11));
_x = _y;
_y = _z;
_z = _w;
_w = (_w ^ (_w >> 19)) ^ (t ^ (t >> 8));
return _w;
}
public int GetRandom(int end)
{
return (int)GetRandom((uint)end);
}
public uint GetRandom(uint end)
{
uint random = GetRandom();
return random - random / end * end;
}
public uint GetRandom(uint start, uint end)
{
uint random = GetRandom();
return random - random / (1 - start + end) * (1 - start + end) + start;
}
public int GetRandom(int start, int end)
{
return (int)GetRandom((uint)start, (uint)end);
}
public CreateId MakeCreateId()
{
UInt128 value = UInt128Utils.CreateRandom();
// Ensure the random ID generated is valid as a create id.
value &= ~new UInt128(0xC0, 0);
value |= new UInt128(0x80, 0);
return new CreateId(value);
}
}
}