-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixes spawner timer deserialization, decimal deserialization, an…
…d 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
- Loading branch information
1 parent
70575e1
commit 1a7e7c7
Showing
7 changed files
with
126 additions
and
49 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
Projects/Server.Tests/Tests/Serialization/DecimalSerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
25 changes: 12 additions & 13 deletions
25
Projects/Server.Tests/Tests/Serialization/EnumConversionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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*)# | ||
} | ||
private static unsafe T ReadEnum<T>() where T : unmanaged, Enum | ||
{ | ||
var num = (long)TileFlag.Container; | ||
return *(T*)# | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters