fix: Adds reset RNG command (#1201)
This commit is contained in:
parent
28040582bc
commit
24e2696510
2 changed files with 30 additions and 3 deletions
|
|
@ -14,6 +14,7 @@
|
|||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server.Random;
|
||||
|
|
@ -28,14 +29,20 @@ public class Xoshiro256PlusPlus : BaseRandomSource
|
|||
|
||||
private ulong _s0, _s1, _s2, _s3;
|
||||
|
||||
public Xoshiro256PlusPlus() : this((ulong)Environment.TickCount64)
|
||||
public Xoshiro256PlusPlus()
|
||||
{
|
||||
Span<byte> states = stackalloc byte[32];
|
||||
RandomSources.SecureSource.NextBytes(states);
|
||||
_s0 = BinaryPrimitives.ReadUInt64LittleEndian(states[..8]);
|
||||
_s1 = BinaryPrimitives.ReadUInt64LittleEndian(states[8..16]);
|
||||
_s2 = BinaryPrimitives.ReadUInt64LittleEndian(states[16..24]);
|
||||
_s3 = BinaryPrimitives.ReadUInt64LittleEndian(states[24..32]);
|
||||
}
|
||||
|
||||
public Xoshiro256PlusPlus(ulong seed)
|
||||
{
|
||||
var mix = new SplitMix64(seed);
|
||||
var state = new ulong[4];
|
||||
Span<ulong> state = stackalloc ulong[4];
|
||||
mix.FillArray(state);
|
||||
_s0 = state[0];
|
||||
_s1 = state[1];
|
||||
|
|
@ -205,7 +212,7 @@ public class SplitMix64
|
|||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void FillArray(ulong[] arr)
|
||||
public void FillArray(Span<ulong> arr)
|
||||
{
|
||||
for (var i = 0; i < arr.Length; i++)
|
||||
{
|
||||
|
|
|
|||
20
Projects/UOContent/Commands/ResetRng.cs
Normal file
20
Projects/UOContent/Commands/ResetRng.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using Server.Random;
|
||||
|
||||
namespace Server.Commands;
|
||||
|
||||
public class ResetRng
|
||||
{
|
||||
public static void Initialize()
|
||||
{
|
||||
CommandSystem.Register("RestRng", AccessLevel.Administrator, ResetRng_OnCommand);
|
||||
}
|
||||
|
||||
[Usage("RestRng")]
|
||||
[Description(
|
||||
"Resets the global RNG. Use with care! Do not reset often as this will affect the over-all distribution."
|
||||
)]
|
||||
private static void ResetRng_OnCommand(CommandEventArgs e)
|
||||
{
|
||||
RandomSources.SetRng(new Xoshiro256PlusPlus());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue