Update NetSerializer, move to NuGet with it. (#311)

NetSerializer has been updated and moved to the latest version. This
means that the static nonsense it previously had is fixed and we can
properly use ISS14Serializer.
This commit is contained in:
Pieter-Jan Briers
2017-08-08 12:47:18 +02:00
committed by GitHub
parent a43da12f9b
commit d1b29a6f94
13 changed files with 120 additions and 74 deletions
@@ -1,20 +1,20 @@
using Lidgren.Network;
using NetSerializer;
using SS14.Client.Interfaces.Network;
using SS14.Shared;
using SS14.Shared.GameObjects;
using SS14.Shared.IoC;
using SS14.Shared.Interfaces.Configuration;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Interfaces.Network;
using SS14.Shared.Interfaces.Serialization;
using SS14.Shared.IoC;
using System;
using System.Collections.Generic;
using System.IO;
using SS14.Shared.Interfaces.Network;
namespace SS14.Client.GameObjects
{
public class ClientEntityNetworkManager : IEntityNetworkManager
{
[Dependency]
private readonly ISS14Serializer serializer;
[Dependency]
private readonly IClientNetManager _networkManager;
@@ -40,10 +40,12 @@ namespace SS14.Client.GameObjects
NetOutgoingMessage newMsg = CreateEntityMessage();
newMsg.Write((byte)EntityMessage.SystemMessage);
var stream = new MemoryStream();
Serializer.Serialize(stream, message);
newMsg.Write((int)stream.Length);
newMsg.Write(stream.ToArray());
using (var stream = new MemoryStream())
{
serializer.Serialize(stream, message);
newMsg.Write((int)stream.Length);
newMsg.Write(stream.ToArray());
}
//Send the message
_networkManager.ClientSendMessage(newMsg, method);
+4 -6
View File
@@ -90,6 +90,9 @@
<HintPath>$(SolutionDir)packages\Microsoft.Diagnostics.Runtime.0.8.31-beta\lib\net40\Microsoft.Diagnostics.Runtime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NetSerializer, Version=4.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\NetSerializer.4.1.0\lib\net45\NetSerializer.dll</HintPath>
</Reference>
<Reference Include="OpenTK, Version=3.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\OpenTK.3.0.0-pre\lib\net20\OpenTK.dll</HintPath>
</Reference>
@@ -140,11 +143,6 @@
<Name>System.ServiceModel</Name>
<Private>False</Private>
</Reference>
<Reference Include="NetSerializer">
<Name>NetSerializer</Name>
<HintPath>..\Third-Party\NetSerializer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp">
<Name>Microsoft.CSharp</Name>
<Private>False</Private>
@@ -382,4 +380,4 @@
<PropertyGroup>
<DefineConstants Condition="Exists('Microsoft.VisualStudio.DebuggerVisualizers.dll') ">$(DefineConstants);VS_DEBUGGERVISUALIZERS_EXISTS</DefineConstants>
</PropertyGroup>
</Project>
</Project>
+1
View File
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Diagnostics.Runtime" version="0.8.31-beta" targetFramework="net451" />
<package id="NetSerializer" version="4.1.0" targetFramework="net451" />
<package id="OpenTK" version="3.0.0-pre" targetFramework="net451" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net451" />
<package id="YamlDotNet" version="4.2.1" targetFramework="net451" />
@@ -1,22 +1,21 @@
using Lidgren.Network;
using NetSerializer;
using SS14.Shared.Interfaces.Configuration;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared;
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Interfaces.Network;
using SS14.Shared.Interfaces.Serialization;
using SS14.Shared.IoC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using SS14.Shared.Interfaces.Network;
using SS14.Shared.Network;
namespace SS14.Server.GameObjects
{
public class ServerEntityNetworkManager : IEntityNetworkManager
{
[Dependency]
private readonly ISS14Serializer serializer;
[Dependency]
private readonly IServerNetManager _mNetManager;
@@ -216,10 +215,12 @@ namespace SS14.Server.GameObjects
NetOutgoingMessage newMsg = CreateEntityMessage();
newMsg.Write((byte)EntityMessage.SystemMessage);
var stream = new MemoryStream();
Serializer.Serialize(stream, message);
newMsg.Write((int)stream.Length);
newMsg.Write(stream.ToArray());
using (var stream = new MemoryStream())
{
serializer.Serialize(stream, message);
newMsg.Write((int)stream.Length);
newMsg.Write(stream.ToArray());
}
//Send the message
if (targetConnection != null)
+4 -6
View File
@@ -81,6 +81,9 @@
<HintPath>$(SolutionDir)packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NetSerializer, Version=4.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\NetSerializer.4.1.0\lib\net45\NetSerializer.dll</HintPath>
</Reference>
<Reference Include="OpenTK, Version=3.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\OpenTK.3.0.0-pre\lib\net20\OpenTK.dll</HintPath>
</Reference>
@@ -119,11 +122,6 @@
<Reference Include="CommandLine">
<HintPath>$(SolutionDir)packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
</Reference>
<Reference Include="NetSerializer">
<Name>NetSerializer</Name>
<HintPath>..\Third-Party\NetSerializer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ServiceModel">
<Name>System.ServiceModel</Name>
<Private>False</Private>
@@ -270,4 +268,4 @@
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>
+1
View File
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommandLineParser" version="1.9.71" targetFramework="net451" />
<package id="NetSerializer" version="4.1.0" targetFramework="net451" />
<package id="OpenTK" version="3.0.0-pre" targetFramework="net451" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net451" />
<package id="YamlDotNet" version="4.2.1" targetFramework="net451" />
+12 -5
View File
@@ -1,8 +1,8 @@
using Lidgren.Network;
using NetSerializer;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Interfaces.GameObjects.System;
using SS14.Shared.Interfaces.Reflection;
using SS14.Shared.Interfaces.Serialization;
using SS14.Shared.IoC;
using System;
using System.Collections.Generic;
@@ -13,6 +13,8 @@ namespace SS14.Shared.GameObjects
{
public class EntitySystemManager : IEntitySystemManager
{
[Dependency]
private readonly ISS14Serializer serializer;
[Dependency]
private readonly IReflectionManager ReflectionManager;
/// <summary>
@@ -113,12 +115,17 @@ namespace SS14.Shared.GameObjects
public void HandleSystemMessage(EntitySystemData sysMsg)
{
int messageLength = sysMsg.message.ReadInt32();
EntitySystemMessage deserialized;
object deserialized = Serializer.Deserialize(new MemoryStream(sysMsg.message.ReadBytes(messageLength)));
using (var stream = new MemoryStream(sysMsg.message.ReadBytes(messageLength)))
{
deserialized = serializer.Deserialize<EntitySystemMessage>(stream);
}
if (deserialized is EntitySystemMessage)
foreach (KeyValuePair<Type, IEntitySystem> current in SystemMessageTypes.Where(x => x.Key == deserialized.GetType()))
current.Value.HandleNetMessage((EntitySystemMessage)deserialized);
foreach (var current in SystemMessageTypes.Where(x => x.Key == deserialized.GetType()))
{
current.Value.HandleNetMessage((EntitySystemMessage)deserialized);
}
}
public void Update(float frameTime)
+4 -2
View File
@@ -1,7 +1,8 @@
using BsDiffLib;
using Lidgren.Network;
using NetSerializer;
using SS14.Shared.GameStates;
using SS14.Shared.Interfaces.Serialization;
using SS14.Shared.IoC;
using SS14.Shared.Serialization;
using System;
using System.IO;
@@ -46,7 +47,8 @@ namespace SS14.Shared
BinaryPatchUtility.Apply(new MemoryStream(fromBuffer), () => new MemoryStream(deltaBytes.ToArray()),
toStream);
toStream.Seek(0, SeekOrigin.Begin);
return (GameState) Serializer.Deserialize(toStream);
var serializer = IoCManager.Resolve<ISS14Serializer>();
return serializer.Deserialize<GameState>(toStream);
}
public static GameStateDelta ReadDelta(NetIncomingMessage message)
+22 -10
View File
@@ -1,6 +1,7 @@
using Lidgren.Network;
using NetSerializer;
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.Serialization;
using SS14.Shared.IoC;
using SS14.Shared.Serialization;
using System;
using System.Collections.Generic;
@@ -83,7 +84,8 @@ namespace SS14.Shared.GameStates
if (_serialized)
return;
_serializedData = new MemoryStream();
new SS14Serializer().Serialize(_serializedData, this);
var serializer = IoCManager.Resolve<ISS14Serializer>();
serializer.Serialize(_serializedData, this);
_serialized = true;
}
@@ -122,7 +124,11 @@ namespace SS14.Shared.GameStates
/// <returns></returns>
public static GameState Deserialize(byte[] data)
{
return (GameState) Serializer.Deserialize(new MemoryStream(data));
var serializer = IoCManager.Resolve<ISS14Serializer>();
using (var stream = new MemoryStream(data))
{
return serializer.Deserialize<GameState>(new MemoryStream(data));
}
}
/// <summary>
@@ -131,7 +137,7 @@ namespace SS14.Shared.GameStates
/// <param name="message">NetOutgoingMessage to write to</param>
public int WriteStateMessage(NetOutgoingMessage message)
{
message.Write((byte) NetMessages.FullState);
message.Write((byte)NetMessages.FullState);
byte[] stateData = Compress(GetSerializedDataBuffer());
message.Write(Sequence);
message.Write(stateData.Length);
@@ -150,7 +156,10 @@ namespace SS14.Shared.GameStates
int length = message.ReadInt32();
byte[] stateData = Decompress(message.ReadBytes(length));
using (var stateStream = new MemoryStream(stateData))
return (GameState) Serializer.Deserialize(stateStream);
{
var serializer = IoCManager.Resolve<ISS14Serializer>();
return serializer.Deserialize<GameState>(stateStream);
}
}
/// <summary>
@@ -160,12 +169,14 @@ namespace SS14.Shared.GameStates
/// <returns></returns>
private static byte[] Compress(byte[] stateData)
{
var compressedDataStream = new MemoryStream();
using (var gzip = new GZipStream(compressedDataStream, CompressionMode.Compress, true))
using (var compressedDataStream = new MemoryStream())
{
gzip.Write(stateData, 0, stateData.Length);
using (var gzip = new GZipStream(compressedDataStream, CompressionMode.Compress, true))
{
gzip.Write(stateData, 0, stateData.Length);
}
return compressedDataStream.ToArray();
}
return compressedDataStream.ToArray();
}
/// <summary>
@@ -177,7 +188,8 @@ namespace SS14.Shared.GameStates
{
// Create a GZIP stream with decompression mode.
// ... Then create a buffer and write into while reading from the GZIP stream.
using (var stream = new GZipStream(new MemoryStream(compressedStateData), CompressionMode.Decompress))
using (var compressedStream = new MemoryStream(compressedStateData))
using (var stream = new GZipStream(compressedStream, CompressionMode.Decompress))
{
const int size = 2048;
var buffer = new byte[size];
@@ -1,7 +1,12 @@
using System.IO;
namespace SS14.Shared.Interfaces.Serialization
{
public interface ISS14Serializer
{
void Initialize();
void Serialize(Stream stream, object toSerialize);
T Deserialize<T>(Stream stream);
object Deserialize(Stream stream);
}
}
+3 -4
View File
@@ -66,9 +66,8 @@
<Reference Include="Mono.Cecil, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="NetSerializer">
<Name>NetSerializer</Name>
<HintPath>..\Third-Party\NetSerializer.dll</HintPath>
<Reference Include="NetSerializer, Version=4.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\NetSerializer.4.1.0\lib\net45\NetSerializer.dll</HintPath>
</Reference>
<Reference Include="Nett, Version=0.7.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\Nett.0.7.0\lib\Net40\Nett.dll</HintPath>
@@ -320,4 +319,4 @@
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>
+41 -22
View File
@@ -1,7 +1,9 @@
using NetSerializer;
using NetSerializer;
using SFML.Graphics;
using SFML.System;
using SS14.Shared.Interfaces.Reflection;
using SS14.Shared.Interfaces.Serialization;
using SS14.Shared.IoC;
using System;
using System.Collections.Generic;
using System.IO;
@@ -12,26 +14,40 @@ namespace SS14.Shared.Serialization
{
public class SS14Serializer : ISS14Serializer
{
[Dependency]
private readonly IReflectionManager reflectionManager;
private Serializer Serializer;
public void Initialize()
{
Serializer.Initialize(
(from asm in AppDomain.CurrentDomain.GetAssemblies()
from type in asm.GetTypes()
where typeof(INetSerializableType).IsAssignableFrom(type)
select type
), new[] { new SfmlTypeSerializer() });
var types = reflectionManager.GetAllChildren<INetSerializableType>();
var settings = new Settings()
{
CustomTypeSerializers = new ITypeSerializer[] { new SfmlTypeSerializer() }
};
Serializer = new Serializer(types, settings);
}
public void Serialize(Stream stream, object obj)
public void Serialize(Stream stream, object toSerialize)
{
Serializer.Serialize(stream, obj);
Serializer.Serialize(stream, toSerialize);
}
public T Deserialize<T>(Stream stream)
{
return (T)Deserialize(stream);
}
public object Deserialize(Stream stream)
{
return Serializer.Deserialize(stream);
}
}
// Do all the dang work ourselves because they can't be bothered to put [Serializable] on their structs.
public class SfmlTypeSerializer : IStaticTypeSerializer
public class SfmlTypeSerializer : IStaticTypeSerializer, ITypeSerializer
{
private static Type[] handledTypes =
private static HashSet<Type> handledTypes = new HashSet<Type>
{
typeof(Vector2f),
typeof(Vector2i),
@@ -43,13 +59,16 @@ namespace SS14.Shared.Serialization
};
public bool Handles(Type type) => handledTypes.Contains(type);
public IEnumerable<Type> GetSubtypes(Type type) => Enumerable.Empty<Type>();
public void GetStaticMethods(Type type, out MethodInfo writer, out MethodInfo reader)
public MethodInfo GetStaticWriter(Type type)
{
writer = typeof(SfmlTypeSerializer).GetMethod("Write", new Type[] { typeof(Stream), type });
reader = typeof(SfmlTypeSerializer).GetMethod("Read", new Type[] { typeof(Stream), type.MakeByRefType() });
return typeof(SfmlTypeSerializer).GetMethod("Write", new Type[] { typeof(Stream), type });
}
public MethodInfo GetStaticReader(Type type)
{
return typeof(SfmlTypeSerializer).GetMethod("Read", new Type[] { typeof(Stream), type.MakeByRefType() });
}
#region Vector2f
@@ -71,7 +90,7 @@ namespace SS14.Shared.Serialization
value = new Vector2f(x, y);
}
#endregion
#endregion Vector2f
#region Vector2i
@@ -92,7 +111,7 @@ namespace SS14.Shared.Serialization
value = new Vector2i(x, y);
}
#endregion
#endregion Vector2i
#region Vector2u
@@ -113,7 +132,7 @@ namespace SS14.Shared.Serialization
value = new Vector2u(x, y);
}
#endregion
#endregion Vector2u
#region Vector3f
@@ -137,7 +156,7 @@ namespace SS14.Shared.Serialization
value = new Vector3f(x, y, z);
}
#endregion
#endregion Vector3f
#region IntRect
@@ -164,7 +183,7 @@ namespace SS14.Shared.Serialization
value = new IntRect(left, top, width, height);
}
#endregion
#endregion IntRect
#region FloatRect
@@ -191,7 +210,7 @@ namespace SS14.Shared.Serialization
value = new FloatRect(left, top, width, height);
}
#endregion
#endregion FloatRect
#region Color
@@ -212,6 +231,6 @@ namespace SS14.Shared.Serialization
value = new Color(r, g, b, a);
}
#endregion
#endregion Color
}
}
+1
View File
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Mono.Cecil" version="0.9.6.4" targetFramework="net451" />
<package id="NetSerializer" version="4.1.0" targetFramework="net451" />
<package id="Nett" version="0.7.0" targetFramework="net451" />
<package id="OpenTK" version="3.0.0-pre" targetFramework="net451" />
<package id="SharpZipLib" version="0.86.0" targetFramework="net451" />