Fix bad serials (#304)

- [X] Fixes bad serial calculations
- [X] Tightens HexStrings

Bumps release version
Updates documentation
This commit is contained in:
Kamron Batman 2020-11-08 12:09:26 -08:00 • committed by GitHub
parent b0c1076cb5
commit ae436cb55f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
46 changed files with 76 additions and 51 deletions

View file

@ -17,7 +17,7 @@ using System;
namespace Server
{
public class HexStringConverter
public static class HexStringConverter
{
public static readonly uint[] m_Lookup32Chars = CreateLookup32Chars();
@ -40,7 +40,11 @@ namespace Server
return result;
}
public static unsafe string GetString(ReadOnlySpan<byte> bytes)
public static string ToHexString(this byte[] bytes) => new ReadOnlySpan<byte>(bytes).ToHexString();
public static string ToHexString(this Span<byte> bytes) => ((ReadOnlySpan<byte>)bytes).ToHexString();
public static unsafe string ToHexString(this ReadOnlySpan<byte> bytes)
{
var result = new string((char)0, bytes.Length * 2);
fixed (char* resultP = result)
@ -55,6 +59,36 @@ namespace Server
return result;
}
public static string ToDelimitedHexString(this byte[] bytes) => ((ReadOnlySpan<byte>)bytes).ToDelimitedHexString();
public static unsafe string ToDelimitedHexString(this ReadOnlySpan<byte> bytes)
{
const uint delimiter = 0x20002C; // ", "
const char openBracket = '[';
const char closeBracket = ']';
var length = bytes.Length * 4; // len * 2 + (len - 1) * 2 + 2
var result = new string((char)0, length);
fixed (char* resultP = result)
{
resultP[0] = openBracket;
resultP[length - 1] = closeBracket;
var resultP2 = (uint*)(resultP + 1);
for (int a = 0, i = 0; a < bytes.Length; a++, i++)
{
if (a > 0)
{
resultP2[i++] = delimiter;
}
resultP2[i] = m_Lookup32Chars[bytes[a]];
}
}
return result;
}
public static unsafe void GetBytes(string str, Span<byte> bytes)
{
fixed (char* strP = str)

View file

@ -39,15 +39,14 @@ namespace Server
public static class World
{
private static readonly ManualResetEvent m_DiskWriteHandle = new ManualResetEvent(true);
private static Dictionary<Serial, IEntity> _pendingAdd;
private static Dictionary<Serial, IEntity> _pendingDelete;
private static ConcurrentQueue<Item> _decayQueue = new ConcurrentQueue<Item>();
private static readonly Dictionary<Serial, IEntity> _pendingAdd = new Dictionary<Serial, IEntity>();
private static readonly Dictionary<Serial, IEntity> _pendingDelete = new Dictionary<Serial, IEntity>();
private static readonly ConcurrentQueue<Item> _decayQueue = new ConcurrentQueue<Item>();
public const uint ItemOffset = 0x40000000;
public const uint MaxItemSerial = 0x7FFFFFFF;
private const uint _maxItems = int.MaxValue - ItemOffset;
private const uint _maxMobiles = ItemOffset;
public const uint MaxMobileSerial = ItemOffset - 1;
private const uint _maxItems = MaxItemSerial - ItemOffset + 1;
private static Serial _lastMobile = Serial.Zero;
private static Serial _lastItem = ItemOffset;
@ -59,19 +58,18 @@ namespace Server
{
uint last = _lastMobile;
for (int i = 0; i < _maxMobiles; i++)
for (int i = 0; i < MaxMobileSerial; i++)
{
last++;
if (last >= _lastMobile)
if (last > MaxMobileSerial)
{
last = 0;
last = 1;
}
if (FindMobile(last) == null)
{
_lastMobile = last;
return last;
return _lastMobile = last;
}
}
@ -89,15 +87,14 @@ namespace Server
{
last++;
if (last - ItemOffset >= _maxItems)
if (last > MaxItemSerial)
{
last = ItemOffset;
}
if (FindItem(last) == null)
{
_lastItem = last;
return last;
return _lastItem = last;
}
}
@ -117,7 +114,6 @@ namespace Server
}
}
internal static int _Saves;
internal static List<Type> ItemTypes { get; } = new List<Type>();
internal static List<Type> MobileTypes { get; } = new List<Type>();
@ -361,9 +357,6 @@ namespace Server
var watch = Stopwatch.StartNew();
_pendingAdd = new Dictionary<Serial, IEntity>();
_pendingDelete = new Dictionary<Serial, IEntity>();
List<EntityIndex<Item>> items;
List<EntityIndex<Mobile>> mobiles;
List<EntityIndex<BaseGuild>> guilds;