fix: Cleans up core code (#1187)

**Only one functional change**
* Fixes a bug in LogFactory where `Warning` is being logged as `Information`

Non-functional changes:
* Updates/Fixes copyright headers
* Removes namespace scopes for core files.

View with [whitespace off](https://github.com/modernuo/ModernUO/pull/1187/files?w=1).
This commit is contained in:
Kamron Batman 2022-10-10 21:47:08 -07:00 committed by GitHub
parent 0138d40bda
commit f268d5d4e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
262 changed files with 28527 additions and 28646 deletions

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: BaseRandomSource.cs *
* *
@ -18,174 +18,173 @@ using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Server.Random
namespace Server.Random;
public abstract class BaseRandomSource : IRandomSource
{
public abstract class BaseRandomSource : IRandomSource
private const double INCR_DOUBLE = 1.0 / (1UL << 53);
private const float INCR_FLOAT = 1f / (1U << 24);
public abstract ulong NextULong();
public abstract void NextBytes(Span<byte> buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Next()
{
private const double INCR_DOUBLE = 1.0 / (1UL << 53);
private const float INCR_FLOAT = 1f / (1U << 24);
public abstract ulong NextULong();
public abstract void NextBytes(Span<byte> buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Next()
ulong rtn;
do
{
ulong rtn;
do
{
rtn = NextULong() >> 33;
} while (rtn == 0x7fff_ffffUL);
rtn = NextULong() >> 33;
} while (rtn == 0x7fff_ffffUL);
return (int)rtn;
return (int)rtn;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Next(int count)
{
Debug.Assert(count != 0, $"{nameof(count)} must not be 0");
if (count is -1 or 0 or 1)
{
return 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Next(int count)
{
Debug.Assert(count != 0, $"{nameof(count)} must not be 0");
var negative = count < 0;
if (count is -1 or 0 or 1)
var max = negative ? -count : count;
var bits = Log2((uint)max);
int x;
do
{
x = (int)(NextULong() >> (64 - bits));
} while (x >= max);
return negative ? -x : x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Next(int minValue, int count) => minValue + Next(count);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint Next(uint count)
{
Debug.Assert(count != 0, $"{nameof(count)} must not be 0");
if (count is 0 or 1)
{
return 0;
}
var bits = Log2(count);
uint x;
do
{
x = (uint)(NextULong() >> (64 - bits));
} while (x >= count);
return x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint Next(uint minValue, uint count) => minValue + Next(count);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Next(long count)
{
Debug.Assert(count != 0, $"{nameof(count)} must not be 0");
if (count is -1 or 0 or 1)
{
return 0;
}
var negative = count < 0;
var max = negative ? -count : count;
var bits = Log2((ulong)max);
long x;
do
{
x = (long)(NextULong() >> (64 - bits));
} while (x >= max);
return negative ? -x : x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Next(long minValue, long count) => minValue + Next(count);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double NextDouble() => (NextULong() >> 11) * INCR_DOUBLE;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int NextInt() => (int)(NextULong() >> 33);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint NextUInt() => (uint)NextULong();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool NextBool() => (NextULong() & 0x8000000000000000) != 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte NextByte() => (byte)(NextULong() >> 56);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float NextFloat() => (NextULong() >> 40) * INCR_FLOAT;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float NextFloatNonZero() => NextFloat() + INCR_FLOAT;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double NextDoubleNonZero() => NextDouble() + INCR_DOUBLE;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double NextDoubleHighRes()
{
var exponent = -64;
ulong significand;
int shift;
while ((significand = NextULong()) == 0)
{
exponent -= 64;
if (exponent < -1074)
{
return 0;
}
var negative = count < 0;
var max = negative ? -count : count;
var bits = Log2((uint)max);
int x;
do
{
x = (int)(NextULong() >> (64 - bits));
} while (x >= max);
return negative ? -x : x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Next(int minValue, int count) => minValue + Next(count);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint Next(uint count)
shift = BitOperations.LeadingZeroCount(significand);
if (shift != 0)
{
Debug.Assert(count != 0, $"{nameof(count)} must not be 0");
if (count is 0 or 1)
{
return 0;
}
var bits = Log2(count);
uint x;
do
{
x = (uint)(NextULong() >> (64 - bits));
} while (x >= count);
return x;
exponent -= shift;
significand <<= shift;
significand |= NextULong() >> (64 - shift);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint Next(uint minValue, uint count) => minValue + Next(count);
significand |= 1;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Next(long count)
{
Debug.Assert(count != 0, $"{nameof(count)} must not be 0");
return significand * Math.Pow(2, exponent);
}
if (count is -1 or 0 or 1)
{
return 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Log2(uint v)
{
var exp = BitOperations.Log2(v);
return v == 1 << exp ? exp : exp + 1;
}
var negative = count < 0;
var max = negative ? -count : count;
var bits = Log2((ulong)max);
long x;
do
{
x = (long)(NextULong() >> (64 - bits));
} while (x >= max);
return negative ? -x : x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Next(long minValue, long count) => minValue + Next(count);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double NextDouble() => (NextULong() >> 11) * INCR_DOUBLE;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int NextInt() => (int)(NextULong() >> 33);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint NextUInt() => (uint)NextULong();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool NextBool() => (NextULong() & 0x8000000000000000) != 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte NextByte() => (byte)(NextULong() >> 56);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float NextFloat() => (NextULong() >> 40) * INCR_FLOAT;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float NextFloatNonZero() => NextFloat() + INCR_FLOAT;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double NextDoubleNonZero() => NextDouble() + INCR_DOUBLE;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double NextDoubleHighRes()
{
var exponent = -64;
ulong significand;
int shift;
while ((significand = NextULong()) == 0)
{
exponent -= 64;
if (exponent < -1074)
{
return 0;
}
}
shift = BitOperations.LeadingZeroCount(significand);
if (shift != 0)
{
exponent -= shift;
significand <<= shift;
significand |= NextULong() >> (64 - shift);
}
significand |= 1;
return significand * Math.Pow(2, exponent);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Log2(uint v)
{
var exp = BitOperations.Log2(v);
return v == 1 << exp ? exp : exp + 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Log2(ulong v)
{
var exp = BitOperations.Log2(v);
return v == 1UL << exp ? exp : exp + 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Log2(ulong v)
{
var exp = BitOperations.Log2(v);
return v == 1UL << exp ? exp : exp + 1;
}
}

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: IRandomSource.cs *
* *
@ -15,27 +15,26 @@
using System;
namespace Server.Random
namespace Server.Random;
public interface IRandomSource
{
public interface IRandomSource
{
int Next();
int Next(int maxValue);
int Next(int minValue, int count);
uint Next(uint maxValue);
uint Next(uint minValue, uint count);
long Next(long maxValue);
long Next(long minValue, long count);
double NextDouble();
void NextBytes(Span<byte> buffer);
int NextInt();
uint NextUInt();
ulong NextULong();
bool NextBool();
byte NextByte();
float NextFloat();
float NextFloatNonZero();
double NextDoubleNonZero();
double NextDoubleHighRes();
}
int Next();
int Next(int maxValue);
int Next(int minValue, int count);
uint Next(uint maxValue);
uint Next(uint minValue, uint count);
long Next(long maxValue);
long Next(long minValue, long count);
double NextDouble();
void NextBytes(Span<byte> buffer);
int NextInt();
uint NextUInt();
ulong NextULong();
bool NextBool();
byte NextByte();
float NextFloat();
float NextFloatNonZero();
double NextDoubleNonZero();
double NextDoubleHighRes();
}

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: RandomSources.cs *
* *
@ -13,18 +13,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
namespace Server.Random
namespace Server.Random;
public static class RandomSources
{
public static class RandomSources
{
private static IRandomSource _source;
private static IRandomSource _secureSource;
private static IRandomSource _source;
private static IRandomSource _secureSource;
public static IRandomSource Source => _source ??= new Xoshiro256PlusPlus();
public static IRandomSource SecureSource => _secureSource ??= new SecureRandom();
public static IRandomSource Source => _source ??= new Xoshiro256PlusPlus();
public static IRandomSource SecureSource => _secureSource ??= new SecureRandom();
public static void SetRng(IRandomSource newSource) => _source = newSource;
public static void SetRng(IRandomSource newSource) => _source = newSource;
public static void SetSecureRng(IRandomSource newSource) => _secureSource = newSource;
}
public static void SetSecureRng(IRandomSource newSource) => _secureSource = newSource;
}

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SecureRandom.cs *
* *
@ -19,23 +19,22 @@ using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using Server.Random;
namespace Server
namespace Server;
public class SecureRandom : BaseRandomSource
{
public class SecureRandom : BaseRandomSource
private RandomNumberGenerator m_Random;
public RandomNumberGenerator Generator => m_Random ??= RandomNumberGenerator.Create();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override ulong NextULong()
{
private RandomNumberGenerator m_Random;
public RandomNumberGenerator Generator => m_Random ??= RandomNumberGenerator.Create();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override ulong NextULong()
{
Span<byte> buffer = stackalloc byte[sizeof(ulong)];
NextBytes(buffer);
return BinaryPrimitives.ReadUInt64BigEndian(buffer);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void NextBytes(Span<byte> buffer) => Generator.GetBytes(buffer);
Span<byte> buffer = stackalloc byte[sizeof(ulong)];
NextBytes(buffer);
return BinaryPrimitives.ReadUInt64BigEndian(buffer);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void NextBytes(Span<byte> buffer) => Generator.GetBytes(buffer);
}

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Xoshiro256PlusPlus.cs *
* *
@ -16,109 +16,88 @@
using System;
using System.Runtime.CompilerServices;
namespace Server.Random
namespace Server.Random;
public class Xoshiro256PlusPlus : BaseRandomSource
{
public class Xoshiro256PlusPlus : BaseRandomSource
private static readonly ulong[] JUMP =
{ 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c };
private static readonly ulong[] LONG_JUMP =
{ 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635 };
private ulong _s0, _s1, _s2, _s3;
public Xoshiro256PlusPlus() : this((ulong)Environment.TickCount64)
{
private static readonly ulong[] JUMP =
{ 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c };
}
private static readonly ulong[] LONG_JUMP =
{ 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635 };
public Xoshiro256PlusPlus(ulong seed)
{
var mix = new SplitMix64(seed);
var state = new ulong[4];
mix.FillArray(state);
_s0 = state[0];
_s1 = state[1];
_s2 = state[2];
_s3 = state[3];
}
private ulong _s0, _s1, _s2, _s3;
private Xoshiro256PlusPlus(ulong s0, ulong s1, ulong s2, ulong s3)
{
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
public Xoshiro256PlusPlus() : this((ulong)Environment.TickCount64)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override ulong NextULong()
{
var r1 = (_s1 << 2) + _s1;
var r2 = (r1 << 7) | (r1 >> 57);
var rslt = (r2 << 3) + r2;
var t = _s1 << 17;
_s2 ^= _s0;
_s3 ^= _s1;
_s1 ^= _s2;
_s0 ^= _s3;
_s2 ^= t;
_s3 = (_s3 << 45) | (_s3 >> 19);
return rslt;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override unsafe void NextBytes(Span<byte> b)
{
if (b.Length == 0)
{
return;
}
public Xoshiro256PlusPlus(ulong seed)
var s0 = _s0;
var s1 = _s1;
var s2 = _s2;
var s3 = _s3;
var i = 0;
fixed (byte* pBuffer = b)
{
var mix = new SplitMix64(seed);
var state = new ulong[4];
mix.FillArray(state);
_s0 = state[0];
_s1 = state[1];
_s2 = state[2];
_s3 = state[3];
}
var pULong = (ulong*)pBuffer;
private Xoshiro256PlusPlus(ulong s0, ulong s1, ulong s2, ulong s3)
{
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override ulong NextULong()
{
var r1 = (_s1 << 2) + _s1;
var r2 = (r1 << 7) | (r1 >> 57);
var rslt = (r2 << 3) + r2;
var t = _s1 << 17;
_s2 ^= _s0;
_s3 ^= _s1;
_s1 ^= _s2;
_s0 ^= _s3;
_s2 ^= t;
_s3 = (_s3 << 45) | (_s3 >> 19);
return rslt;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override unsafe void NextBytes(Span<byte> b)
{
if (b.Length == 0)
{
return;
}
var s0 = _s0;
var s1 = _s1;
var s2 = _s2;
var s3 = _s3;
var i = 0;
fixed (byte* pBuffer = b)
{
var pULong = (ulong*)pBuffer;
for (var bound = b.Length / sizeof(ulong); i < bound; i++)
{
var r1 = (s1 << 2) + s1;
var r2 = (r1 << 7) | (r1 >> 57);
pULong[i] = (r2 << 3) + r2;
var t = s1 << 17;
s2 ^= s0;
s3 ^= s1;
s1 ^= s2;
s0 ^= s3;
s2 ^= t;
s3 = (s3 << 45) | (s3 >> 19);
}
}
i *= 8;
if (i < b.Length)
for (var bound = b.Length / sizeof(ulong); i < bound; i++)
{
var r1 = (s1 << 2) + s1;
var r2 = (r1 << 7) | (r1 >> 57);
var rslt = (r2 << 3) + r2;
pULong[i] = (r2 << 3) + r2;
var t = s1 << 17;
s2 ^= s0;
s3 ^= s1;
s1 ^= s2;
@ -127,90 +106,110 @@ namespace Server.Random
s2 ^= t;
s3 = (s3 << 45) | (s3 >> 19);
while (i < b.Length)
{
b[i++] = (byte)rslt;
rslt >>= 8;
}
}
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
public void Jump() => Jump(JUMP);
i *= 8;
public void LongJump() => Jump(LONG_JUMP);
private void Jump(in ulong[] jumps)
if (i < b.Length)
{
ulong s0 = 0;
ulong s1 = 0;
ulong s2 = 0;
ulong s3 = 0;
var r1 = (s1 << 2) + s1;
var r2 = (r1 << 7) | (r1 >> 57);
var rslt = (r2 << 3) + r2;
for (var i = 0; i < jumps.Length; i++)
var t = s1 << 17;
s2 ^= s0;
s3 ^= s1;
s1 ^= s2;
s0 ^= s3;
s2 ^= t;
s3 = (s3 << 45) | (s3 >> 19);
while (i < b.Length)
{
for (var b = 0; b < 64; b++)
{
if ((jumps[i] & (1ul << b)) != 0)
{
s0 ^= _s0;
s1 ^= _s1;
s2 ^= _s2;
s3 ^= _s3;
}
NextULong();
}
b[i++] = (byte)rslt;
rslt >>= 8;
}
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
public Xoshiro256PlusPlus Split()
{
var rng = new Xoshiro256PlusPlus(_s0, _s1, _s2, _s3);
rng.Jump();
return rng;
}
public Xoshiro256PlusPlus LongSplit()
{
var rng = new Xoshiro256PlusPlus(_s0, _s1, _s2, _s3);
rng.LongJump();
return rng;
}
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
public class SplitMix64
public void Jump() => Jump(JUMP);
public void LongJump() => Jump(LONG_JUMP);
private void Jump(in ulong[] jumps)
{
private ulong x;
ulong s0 = 0;
ulong s1 = 0;
ulong s2 = 0;
ulong s3 = 0;
public SplitMix64(ulong seed) => x = seed;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong Next()
for (var i = 0; i < jumps.Length; i++)
{
var z = x += 0x9e3779b97f4a7c15;
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
for (var b = 0; b < 64; b++)
{
if ((jumps[i] & (1ul << b)) != 0)
{
s0 ^= _s0;
s1 ^= _s1;
s2 ^= _s2;
s3 ^= _s3;
}
NextULong();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FillArray(ulong[] arr)
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
public Xoshiro256PlusPlus Split()
{
var rng = new Xoshiro256PlusPlus(_s0, _s1, _s2, _s3);
rng.Jump();
return rng;
}
public Xoshiro256PlusPlus LongSplit()
{
var rng = new Xoshiro256PlusPlus(_s0, _s1, _s2, _s3);
rng.LongJump();
return rng;
}
}
public class SplitMix64
{
private ulong x;
public SplitMix64(ulong seed) => x = seed;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong Next()
{
var z = x += 0x9e3779b97f4a7c15;
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FillArray(ulong[] arr)
{
for (var i = 0; i < arr.Length; i++)
{
for (var i = 0; i < arr.Length; i++)
{
arr[i] = Next();
}
arr[i] = Next();
}
}
}