fix: Fixes spawner timer deserialization, decimal deserialization, and adds potion keg reverse lookup (#1711)

### Summary
* Fixes spawner timer deserialization
* Adds a check for a null timer and allows the timer to get recreated
* Adds PotionKeg reverse lookup
* Heavily optimizes decimal serialize/deserialize
This commit is contained in:
Kamron Batman 2024-03-28 13:34:12 -07:00 • committed by GitHub
parent 70575e1517
commit 1a7e7c7c70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 126 additions and 49 deletions

View file

@ -0,0 +1,46 @@
using System;
using System.Buffers.Binary;
using System.Runtime.InteropServices;
using Xunit;
namespace Server.Tests;
public class DecimalSerializationTests
{
private static void Write(byte[] buffer, decimal value)
{
Span<int> bytes = stackalloc int[sizeof(decimal) / 4];
decimal.GetBits(value, bytes);
MemoryMarshal.Cast<int, byte>(bytes).CopyTo(buffer.AsSpan());
}
private static int ReadInt(ReadOnlySpan<byte> buffer) => BinaryPrimitives.ReadInt32LittleEndian(buffer);
private static decimal ReadDecimal(ReadOnlySpan<byte> buffer) => new(stackalloc int[4] { ReadInt(buffer), ReadInt(buffer[4..]), ReadInt(buffer[8..]), ReadInt(buffer[12..]) });
public static TheoryData<decimal> Data =>
new()
{
123.46m,
0.0256m,
10000m,
2m,
0.0001m,
0.0000000000000000000000000001m
};
[Theory]
[MemberData(nameof(Data))]
public void TestSerializeDecimal(decimal value)
{
// Arrange
byte[] buffer = new byte[sizeof(decimal)];
// Act
Write(buffer, value);
// Assert
Assert.Equal(value, ReadDecimal(buffer));
}
}

View file

@ -1,22 +1,21 @@
using System;
using Xunit;
namespace Server.Tests
namespace Server.Tests;
public class EnumConversionTests
{
public class EnumConversionTests
[Fact]
public void TestToEnum()
{
[Fact]
public void TestToEnum()
{
var e = ReadEnum<TileFlag>();
var e = ReadEnum<TileFlag>();
Assert.Equal(TileFlag.Container, e);
}
Assert.Equal(TileFlag.Container, e);
}
private unsafe T ReadEnum<T>() where T : unmanaged, Enum
{
var num = (long)TileFlag.Container;
return *(T*)&num;
}
private static unsafe T ReadEnum<T>() where T : unmanaged, Enum
{
var num = (long)TileFlag.Container;
return *(T*)&num;
}
}