fix(core): Fixes several serialization issues (#355)

- [X] Fixes an issue where a buffer smaller than 8 bytes would not double with enough space in some cases.
- [X] Fixes an issue with dupe copying the savebuffer reference (ugh).
- [X] Streamlines the IGenericWriter API to use better generics.
- [X] Streamlines the IGenericReader API to use better generics.
- [X] Forces `tidying` of a List/HashSet to be done externally since Writers/Readers should not have side effects.
- [X] Fixes an issue where Tidying a list didn't TrimExcess, causing memory leaks.
- [X] Reverted the meaning of `World.Running` to specifically refer to any world state post world loading.
  - NOTE: Do not use this if you want to block on world saves. Instead use checks against `WorldState.Saving` states.
- [X] Fixes an issue with serializing negative DateTime deltas.
- [X] Fixes a potential issue with serializing non-UTC DateTime.

Bumps release version
This commit is contained in:
Kamron Batman 2020-12-23 07:11:41 -08:00 committed by GitHub
parent 0888ab9b86
commit 4ddb3de026
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
162 changed files with 652 additions and 1045 deletions

View file

@ -43,41 +43,21 @@ namespace Server
}
var length = ReadEncodedInt();
var s = length == 0 ? "" : _encoding.GetString(_buffer.AsSpan(Position, length));
if (length <= 0)
{
return "";
}
var s = _encoding.GetString(_buffer.AsSpan(Position, length));
Position += length;
return s;
}
public DateTime ReadDateTime() => new(ReadLong());
public DateTimeOffset ReadDateTimeOffset() => new(ReadLong(), ReadTimeSpan());
public DateTime ReadDateTime() => new(ReadLong(), DateTimeKind.Utc);
public TimeSpan ReadTimeSpan() => new(ReadLong());
public DateTime ReadDeltaTime()
{
var ticks = ReadLong();
var now = DateTime.UtcNow.Ticks;
if (ticks > 0 && ticks + now < 0)
{
return DateTime.MaxValue;
}
if (ticks < 0 && ticks + now < 0)
{
return DateTime.MinValue;
}
try
{
return new DateTime(now + ticks);
}
catch
{
return ticks > 0 ? DateTime.MaxValue : DateTime.MinValue;
}
}
public DateTime ReadDeltaTime() => new(ReadLong() + DateTime.UtcNow.Ticks, DateTimeKind.Utc);
public decimal ReadDecimal() => new(new[] { ReadInt(), ReadInt(), ReadInt(), ReadInt() });
@ -177,27 +157,20 @@ namespace Server
public Map ReadMap() => Map.Maps[ReadByte()];
public IEntity ReadEntity()
public T ReadEntity<T>() where T : class, ISerializable
{
Serial serial = ReadUInt();
return World.FindEntity(serial) ?? new Entity(serial, new Point3D(0, 0, 0), Map.Internal);
// Special case for now:
if (typeof(T).IsAssignableTo(typeof(BaseGuild)))
{
return World.FindGuild(serial) as T;
}
return World.FindEntity(serial) as T;
}
public Item ReadItem() => World.FindItem(ReadUInt());
public Mobile ReadMobile() => World.FindMobile(ReadUInt());
public BaseGuild ReadGuild() => World.FindGuild(ReadUInt());
public T ReadItem<T>() where T : Item => ReadItem() as T;
public T ReadMobile<T>() where T : Mobile => ReadMobile() as T;
public T ReadGuild<T>() where T : BaseGuild => ReadGuild() as T;
public List<Item> ReadStrongItemList() => ReadStrongItemList<Item>();
public List<T> ReadStrongItemList<T>() where T : Item
public List<T> ReadEntityList<T>() where T : class, ISerializable
{
var count = ReadInt();
@ -205,108 +178,28 @@ namespace Server
for (var i = 0; i < count; ++i)
{
if (ReadItem() is T item)
var entity = ReadEntity<T>();
if (entity != null)
{
list.Add(item);
list.Add(entity);
}
}
return list;
}
public HashSet<Item> ReadItemSet() => ReadItemSet<Item>();
public HashSet<T> ReadItemSet<T>() where T : Item
public HashSet<T> ReadEntitySet<T>() where T : class, ISerializable
{
var count = ReadInt();
var set = new HashSet<T>();
var set = new HashSet<T>(count);
for (var i = 0; i < count; ++i)
{
if (ReadItem() is T item)
var entity = ReadEntity<T>();
if (entity != null)
{
set.Add(item);
}
}
return set;
}
public List<Mobile> ReadStrongMobileList() => ReadStrongMobileList<Mobile>();
public List<T> ReadStrongMobileList<T>() where T : Mobile
{
var count = ReadInt();
var list = new List<T>(count);
for (var i = 0; i < count; ++i)
{
if (ReadMobile() is T m)
{
list.Add(m);
}
}
return list;
}
public HashSet<Mobile> ReadMobileSet() => ReadMobileSet<Mobile>();
public HashSet<T> ReadMobileSet<T>() where T : Mobile
{
var count = ReadInt();
var set = new HashSet<T>();
for (var i = 0; i < count; ++i)
{
if (ReadMobile() is T item)
{
set.Add(item);
}
}
return set;
}
public List<BaseGuild> ReadStrongGuildList() => ReadStrongGuildList<BaseGuild>();
public List<T> ReadStrongGuildList<T>() where T : BaseGuild
{
var count = ReadInt();
var list = new List<T>(count);
if (count > 0)
{
for (var i = 0; i < count; ++i)
{
if (ReadGuild() is T g)
{
list.Add(g);
}
}
}
return list;
}
public HashSet<BaseGuild> ReadGuildSet() => ReadGuildSet<BaseGuild>();
public HashSet<T> ReadGuildSet<T>() where T : BaseGuild
{
var count = ReadInt();
var set = new HashSet<T>();
if (count > 0)
{
for (var i = 0; i < count; ++i)
{
if (ReadGuild() is T item)
{
set.Add(item);
}
set.Add(entity);
}
}
@ -332,10 +225,9 @@ namespace Server
{
return origin switch
{
SeekOrigin.Begin => Position = offset,
SeekOrigin.Current => Position += offset,
SeekOrigin.End => Position = _buffer.Length - offset,
_ => Position
_ => Position = offset // Begin
};
}
}

View file

@ -19,7 +19,6 @@ using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using Server.Guilds;
namespace Server
{
@ -45,6 +44,13 @@ namespace Server
_buffer = GC.AllocateUninitializedArray<byte>(BufferSize);
}
public BufferWriter(int count, bool prefixStr)
{
m_PrefixStrings = prefixStr;
m_Encoding = Utility.UTF8;
_buffer = GC.AllocateUninitializedArray<byte>(count);
}
public virtual long Position => Index;
protected virtual int BufferSize => 256;
@ -69,21 +75,22 @@ namespace Server
public virtual void Flush()
{
Resize(_buffer.Length * 2);
// Need to avoid buffer.Length = 2, buffer * 2 is 4, but we need 8 or 16bytes, causing an exception.
// The least we need is 16bytes + Index, but we use BufferSize since it should always be big enough for a single
// non-dynamic field.
Resize(Math.Max(BufferSize, _buffer.Length * 2));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void FlushIfNeeded(int amount)
private bool FlushIfNeeded(int amount)
{
if (Index + amount > _buffer.Length)
{
Flush();
return true;
}
}
public void Reset()
{
Index = 0;
return false;
}
public void Write(ReadOnlySpan<byte> bytes)
@ -94,6 +101,7 @@ namespace Server
while (remaining > 0)
{
FlushIfNeeded(remaining);
var count = Math.Min((int)(_buffer.Length - Index), remaining);
bytes.Slice(idx, count).CopyTo(_buffer.AsSpan((int)Index, count));
@ -107,10 +115,9 @@ namespace Server
{
return origin switch
{
SeekOrigin.Begin => Index = offset,
SeekOrigin.Current => Index += offset,
SeekOrigin.End => Index = BufferSize - offset,
_ => Index
SeekOrigin.End => Index = _buffer.Length - offset,
_ => Index = offset // Begin
};
}
@ -133,11 +140,11 @@ namespace Server
{
if (value == null)
{
Write((byte)0);
Write(false);
}
else
{
Write((byte)1);
Write(true);
InternalWriteString(value);
}
}
@ -149,32 +156,27 @@ namespace Server
public void Write(DateTime value)
{
Write(value.Ticks);
}
var ticks = (value.Kind switch
{
DateTimeKind.Local => value.ToUniversalTime(),
DateTimeKind.Unspecified => value.ToLocalTime().ToUniversalTime(),
_ => value
}).Ticks;
public void Write(DateTimeOffset value)
{
Write(value.Ticks);
Write(value.Offset.Ticks);
Write(ticks);
}
public void WriteDeltaTime(DateTime value)
{
var ticks = value.Ticks;
var now = DateTime.UtcNow.Ticks;
TimeSpan d;
try
var ticks = (value.Kind switch
{
d = new TimeSpan(ticks - now);
}
catch
{
d = TimeSpan.MaxValue;
}
DateTimeKind.Local => value.ToUniversalTime(),
DateTimeKind.Unspecified => value.ToLocalTime().ToUniversalTime(),
_ => value
}).Ticks;
Write(d);
// Technically supports negative deltas for times in the past
Write(ticks - DateTime.UtcNow.Ticks);
}
public void Write(IPAddress value)
@ -204,70 +206,64 @@ namespace Server
{
FlushIfNeeded(8);
_buffer[Index] = (byte)value;
_buffer[Index + 1] = (byte)(value >> 8);
_buffer[Index + 2] = (byte)(value >> 16);
_buffer[Index + 3] = (byte)(value >> 24);
_buffer[Index + 4] = (byte)(value >> 32);
_buffer[Index + 5] = (byte)(value >> 40);
_buffer[Index + 6] = (byte)(value >> 48);
_buffer[Index + 7] = (byte)(value >> 56);
Index += 8;
_buffer[Index++] = (byte)value;
_buffer[Index++] = (byte)(value >> 8);
_buffer[Index++] = (byte)(value >> 16);
_buffer[Index++] = (byte)(value >> 24);
_buffer[Index++] = (byte)(value >> 32);
_buffer[Index++] = (byte)(value >> 40);
_buffer[Index++] = (byte)(value >> 48);
_buffer[Index++] = (byte)(value >> 56);
}
public void Write(ulong value)
{
FlushIfNeeded(8);
_buffer[Index] = (byte)value;
_buffer[Index + 1] = (byte)(value >> 8);
_buffer[Index + 2] = (byte)(value >> 16);
_buffer[Index + 3] = (byte)(value >> 24);
_buffer[Index + 4] = (byte)(value >> 32);
_buffer[Index + 5] = (byte)(value >> 40);
_buffer[Index + 6] = (byte)(value >> 48);
_buffer[Index + 7] = (byte)(value >> 56);
Index += 8;
_buffer[Index++] = (byte)value;
_buffer[Index++] = (byte)(value >> 8);
_buffer[Index++] = (byte)(value >> 16);
_buffer[Index++] = (byte)(value >> 24);
_buffer[Index++] = (byte)(value >> 32);
_buffer[Index++] = (byte)(value >> 40);
_buffer[Index++] = (byte)(value >> 48);
_buffer[Index++] = (byte)(value >> 56);
}
public void Write(int value)
{
FlushIfNeeded(4);
_buffer[Index] = (byte)value;
_buffer[Index + 1] = (byte)(value >> 8);
_buffer[Index + 2] = (byte)(value >> 16);
_buffer[Index + 3] = (byte)(value >> 24);
Index += 4;
_buffer[Index++] = (byte)value;
_buffer[Index++] = (byte)(value >> 8);
_buffer[Index++] = (byte)(value >> 16);
_buffer[Index++] = (byte)(value >> 24);
}
public void Write(uint value)
{
FlushIfNeeded(4);
_buffer[Index] = (byte)value;
_buffer[Index + 1] = (byte)(value >> 8);
_buffer[Index + 2] = (byte)(value >> 16);
_buffer[Index + 3] = (byte)(value >> 24);
Index += 4;
_buffer[Index++] = (byte)value;
_buffer[Index++] = (byte)(value >> 8);
_buffer[Index++] = (byte)(value >> 16);
_buffer[Index++] = (byte)(value >> 24);
}
public void Write(short value)
{
FlushIfNeeded(2);
_buffer[Index] = (byte)value;
_buffer[Index + 1] = (byte)(value >> 8);
Index += 2;
_buffer[Index++] = (byte)value;
_buffer[Index++] = (byte)(value >> 8);
}
public void Write(ushort value)
{
FlushIfNeeded(2);
_buffer[Index] = (byte)value;
_buffer[Index + 1] = (byte)(value >> 8);
Index += 2;
_buffer[Index++] = (byte)value;
_buffer[Index++] = (byte)(value >> 8);
}
public unsafe void Write(double value)
@ -301,16 +297,18 @@ namespace Server
_buffer[Index++] = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(sbyte value)
{
FlushIfNeeded(1);
_buffer[Index++] = (byte)value;
}
public void Write(bool value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Write(bool value)
{
FlushIfNeeded(1);
_buffer[Index++] = value ? 1 : 0;
_buffer[Index++] = *(byte*)&value; // up to 30% faster to dereference the raw value on the stack
}
public void Write(Point3D value)
@ -348,318 +346,32 @@ namespace Server
Write((byte)(value?.RaceIndex ?? 0xFF));
}
public void WriteEntity(IEntity value)
public void Write(ISerializable value)
{
Write(value?.Deleted != false ? Serial.MinusOne : value.Serial);
}
public void Write(Item value)
public void Write<T>(ICollection<T> coll) where T : class, ISerializable
{
Write(value?.Deleted != false ? Serial.MinusOne : value.Serial);
}
public void Write(Mobile value)
{
Write(value?.Deleted != false ? Serial.MinusOne : value.Serial);
}
public void Write(BaseGuild value)
{
Write(value?.Serial ?? 0);
}
public void WriteItem<T>(T value) where T : Item
{
Write(value);
}
public void WriteMobile<T>(T value) where T : Mobile
{
Write(value);
}
public void WriteGuild<T>(T value) where T : BaseGuild
{
Write(value);
}
public void Write(List<Item> list)
{
WriteItemList(list);
}
public void Write(List<Item> list, bool tidy)
{
WriteItemList(list, tidy);
}
public void WriteItemList<T>(List<T> list) where T : Item
{
WriteItemList(list, false);
}
public void WriteItemList<T>(List<T> list, bool tidy) where T : Item
{
if (tidy)
Write(coll.Count);
foreach (var entry in coll)
{
for (var i = 0; i < list.Count;)
{
if (list[i].Deleted)
{
list.RemoveAt(i);
}
else
{
++i;
}
}
}
Write(list.Count);
for (var i = 0; i < list.Count; ++i)
{
Write(list[i]);
Write(entry);
}
}
public void Write(HashSet<Item> set)
public void Write<T>(ICollection<T> coll, Action<IGenericWriter, T> action) where T : class, ISerializable
{
Write(set, false);
}
public void Write(HashSet<Item> set, bool tidy)
{
if (tidy)
if (coll == null)
{
set.RemoveWhere(item => item.Deleted);
Write(0);
return;
}
Write(set.Count);
foreach (var item in set)
Write(coll.Count);
foreach (var entry in coll)
{
Write(item);
}
}
public void WriteItemSet<T>(HashSet<T> set) where T : Item
{
WriteItemSet(set, false);
}
public void WriteItemSet<T>(HashSet<T> set, bool tidy) where T : Item
{
if (tidy)
{
set.RemoveWhere(item => item.Deleted);
}
Write(set.Count);
foreach (var item in set)
{
Write(item);
}
}
public void Write(List<Mobile> list)
{
Write(list, false);
}
public void Write(List<Mobile> list, bool tidy)
{
if (tidy)
{
for (var i = 0; i < list.Count;)
{
if (list[i].Deleted)
{
list.RemoveAt(i);
}
else
{
++i;
}
}
}
Write(list.Count);
for (var i = 0; i < list.Count; ++i)
{
Write(list[i]);
}
}
public void WriteMobileList<T>(List<T> list) where T : Mobile
{
WriteMobileList(list, false);
}
public void WriteMobileList<T>(List<T> list, bool tidy) where T : Mobile
{
if (tidy)
{
for (var i = 0; i < list.Count;)
{
if (list[i].Deleted)
{
list.RemoveAt(i);
}
else
{
++i;
}
}
}
Write(list.Count);
for (var i = 0; i < list.Count; ++i)
{
Write(list[i]);
}
}
public void Write(HashSet<Mobile> set)
{
Write(set, false);
}
public void Write(HashSet<Mobile> set, bool tidy)
{
if (tidy)
{
set.RemoveWhere(mobile => mobile.Deleted);
}
Write(set.Count);
foreach (var mob in set)
{
Write(mob);
}
}
public void WriteMobileSet<T>(HashSet<T> set) where T : Mobile
{
WriteMobileSet(set, false);
}
public void WriteMobileSet<T>(HashSet<T> set, bool tidy) where T : Mobile
{
if (tidy)
{
set.RemoveWhere(mob => mob.Deleted);
}
Write(set.Count);
foreach (var mob in set)
{
Write(mob);
}
}
public void Write(List<BaseGuild> list)
{
Write(list, false);
}
public void Write(List<BaseGuild> list, bool tidy)
{
if (tidy)
{
for (var i = 0; i < list.Count;)
{
if (list[i].Disbanded)
{
list.RemoveAt(i);
}
else
{
++i;
}
}
}
Write(list.Count);
for (var i = 0; i < list.Count; ++i)
{
Write(list[i]);
}
}
public void WriteGuildList<T>(List<T> list) where T : BaseGuild
{
WriteGuildList(list, false);
}
public void WriteGuildList<T>(List<T> list, bool tidy) where T : BaseGuild
{
if (tidy)
{
for (var i = 0; i < list.Count;)
{
if (list[i].Disbanded)
{
list.RemoveAt(i);
}
else
{
++i;
}
}
}
Write(list.Count);
for (var i = 0; i < list.Count; ++i)
{
Write(list[i]);
}
}
public void Write(HashSet<BaseGuild> set)
{
Write(set, false);
}
public void Write(HashSet<BaseGuild> set, bool tidy)
{
if (tidy)
{
set.RemoveWhere(guild => guild.Disbanded);
}
Write(set.Count);
foreach (var guild in set)
{
Write(guild);
}
}
public void WriteGuildSet<T>(HashSet<T> set) where T : BaseGuild
{
WriteGuildSet(set, false);
}
public void WriteGuildSet<T>(HashSet<T> set, bool tidy) where T : BaseGuild
{
if (tidy)
{
set.RemoveWhere(guild => guild.Disbanded);
}
Write(set.Count);
foreach (var guild in set)
{
Write(guild);
action(this, entry);
}
}
@ -668,6 +380,7 @@ namespace Server
var remaining = m_Encoding.GetByteCount(value);
WriteEncodedInt(remaining);
if (remaining == 0)
{
return;

View file

@ -16,7 +16,6 @@
using System;
using System.Collections.Generic;
using System.Net;
using Server.Guilds;
namespace Server
{
@ -24,7 +23,6 @@ namespace Server
{
string ReadString();
DateTime ReadDateTime();
DateTimeOffset ReadDateTimeOffset();
TimeSpan ReadTimeSpan();
DateTime ReadDeltaTime();
decimal ReadDecimal();
@ -46,25 +44,9 @@ namespace Server
Rectangle2D ReadRect2D();
Rectangle3D ReadRect3D();
Map ReadMap();
IEntity ReadEntity();
Item ReadItem();
Mobile ReadMobile();
BaseGuild ReadGuild();
T ReadItem<T>() where T : Item;
T ReadMobile<T>() where T : Mobile;
T ReadGuild<T>() where T : BaseGuild;
List<Item> ReadStrongItemList();
List<T> ReadStrongItemList<T>() where T : Item;
List<Mobile> ReadStrongMobileList();
List<T> ReadStrongMobileList<T>() where T : Mobile;
List<BaseGuild> ReadStrongGuildList();
List<T> ReadStrongGuildList<T>() where T : BaseGuild;
HashSet<Item> ReadItemSet();
HashSet<T> ReadItemSet<T>() where T : Item;
HashSet<Mobile> ReadMobileSet();
HashSet<T> ReadMobileSet<T>() where T : Mobile;
HashSet<BaseGuild> ReadGuildSet();
HashSet<T> ReadGuildSet<T>() where T : BaseGuild;
T ReadEntity<T>() where T : class, ISerializable;
List<T> ReadEntityList<T>() where T : class, ISerializable;
HashSet<T> ReadEntitySet<T>() where T : class, ISerializable;
Race ReadRace();
int Read(Span<byte> buffer);
}

View file

@ -17,7 +17,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Server.Guilds;
namespace Server
{
@ -27,7 +26,6 @@ namespace Server
void Close();
void Write(string value);
void Write(DateTime value);
void Write(DateTimeOffset value);
void Write(TimeSpan value);
void Write(decimal value);
void Write(long value);
@ -49,38 +47,9 @@ namespace Server
void Write(Rectangle2D value);
void Write(Rectangle3D value);
void Write(Map value);
void WriteEntity(IEntity value);
void Write(Item value);
void Write(Mobile value);
void Write(BaseGuild value);
void WriteItem<T>(T value) where T : Item;
void WriteMobile<T>(T value) where T : Mobile;
void WriteGuild<T>(T value) where T : BaseGuild;
void Write(ISerializable value);
void Write(Race value);
void Write(List<Item> list);
void Write(List<Item> list, bool tidy);
void WriteItemList<T>(List<T> list) where T : Item;
void WriteItemList<T>(List<T> list, bool tidy) where T : Item;
void Write(HashSet<Item> list);
void Write(HashSet<Item> list, bool tidy);
void WriteItemSet<T>(HashSet<T> set) where T : Item;
void WriteItemSet<T>(HashSet<T> set, bool tidy) where T : Item;
void Write(List<Mobile> list);
void Write(List<Mobile> list, bool tidy);
void WriteMobileList<T>(List<T> list) where T : Mobile;
void WriteMobileList<T>(List<T> list, bool tidy) where T : Mobile;
void Write(HashSet<Mobile> list);
void Write(HashSet<Mobile> list, bool tidy);
void WriteMobileSet<T>(HashSet<T> set) where T : Mobile;
void WriteMobileSet<T>(HashSet<T> set, bool tidy) where T : Mobile;
void Write(List<BaseGuild> list);
void Write(List<BaseGuild> list, bool tidy);
void WriteGuildList<T>(List<T> list) where T : BaseGuild;
void WriteGuildList<T>(List<T> list, bool tidy) where T : BaseGuild;
void Write(HashSet<BaseGuild> list);
void Write(HashSet<BaseGuild> list, bool tidy);
void WriteGuildSet<T>(HashSet<T> set) where T : BaseGuild;
void WriteGuildSet<T>(HashSet<T> set, bool tidy) where T : BaseGuild;
void Write<T>(ICollection<T> list) where T : class, ISerializable;
void Write(ReadOnlySpan<byte> bytes);
long Seek(long offset, SeekOrigin origin);

View file

@ -13,16 +13,30 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.IO;
namespace Server
{
public interface ISerializable
{
BufferWriter SaveBuffer { get; set; }
BufferWriter SaveBuffer { get; protected internal set; }
int TypeRef { get; }
Serial Serial { get; }
void Serialize();
void Deserialize(IGenericReader reader);
void Serialize(IGenericWriter writer);
void Delete();
bool Deleted { get; }
public void InitializeSaveBuffer(byte[] buffer)
{
SaveBuffer = new BufferWriter(buffer, true);
}
public void Serialize()
{
SaveBuffer ??= new BufferWriter(true);
SaveBuffer.Seek(0, SeekOrigin.Begin);
Serialize(SaveBuffer);
}
}
}