Skip to content

Commit

Permalink
refactor: move serialization into Mirage.Serialization (#700)
Browse files Browse the repository at this point in the history
Move reader and writer to a Mirage.Serialization namespace

BREAKING CHANGE: NetworkReader and NetworkWriter moved to Mirage.Serialization namespace
  • Loading branch information
paulpach committed Mar 15, 2021
1 parent 2022c4e commit 5dc037d
Show file tree
Hide file tree
Showing 72 changed files with 145 additions and 60 deletions.
1 change: 1 addition & 0 deletions Assets/Mirage/Components/NetworkAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEngine;
using UnityEngine.Serialization;
using Mirage.Logging;
using Mirage.Serialization;

namespace Mirage
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Components/NetworkTransformBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// interpolation. interpolation over time is never that good.
//
using UnityEngine;
using Mirage.Serialization;

namespace Mirage
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/ClientObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine;
using UnityEngine.Serialization;
using Mirage.Logging;
using Mirage.Serialization;

namespace Mirage
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/Collections/ISyncObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Mirage.Serialization;

namespace Mirage.Collections
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/Collections/SyncDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using Mirage.Serialization;

namespace Mirage.Collections
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/Collections/SyncList.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Mirage.Serialization;

namespace Mirage.Collections
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/Collections/SyncSet.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Mirage.Serialization;

namespace Mirage.Collections
{
Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirage/Runtime/GameObjectSyncvar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using Mirage.Serialization;
using UnityEngine;

namespace Mirage
{
Expand Down
4 changes: 3 additions & 1 deletion Assets/Mirage/Runtime/NetworkBehaviorSyncvar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Mirage
using Mirage.Serialization;

namespace Mirage
{

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Mirage.RemoteCalls;
using UnityEngine;
using Mirage.Logging;
using Mirage.Serialization;

namespace Mirage
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine.Serialization;
using UnityEngine.Events;
using Mirage.Logging;
using Mirage.Serialization;
#if UNITY_EDITOR
using UnityEditor;
#if UNITY_2018_3_OR_NEWER
Expand Down
4 changes: 3 additions & 1 deletion Assets/Mirage/Runtime/NetworkIdentitySyncvar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Mirage
using Mirage.Serialization;

namespace Mirage
{

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/NetworkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine;
using UnityEngine.Assertions;
using Mirage.Logging;
using Mirage.Serialization;

namespace Mirage
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine.Events;
using UnityEngine.Serialization;
using Mirage.Logging;
using Mirage.Serialization;

namespace Mirage
{
Expand Down
4 changes: 3 additions & 1 deletion Assets/Mirage/Runtime/NotifyPacket.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Mirage
using Mirage.Serialization;

namespace Mirage
{
// header for notify packet
public struct NotifyPacket
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/RemoteCallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Cysharp.Threading.Tasks;
using UnityEngine;
using Mirage.Logging;
using Mirage.Serialization;

namespace Mirage.RemoteCalls
{
Expand Down
8 changes: 8 additions & 0 deletions Assets/Mirage/Runtime/Serialization.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using UnityEngine;

namespace Mirage
namespace Mirage.Serialization
{
enum ComponentType : uint
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace Mirage
namespace Mirage.Serialization
{
// message packing all in one place, instead of constructing headers in all
// kinds of different places
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using UnityEngine;
using Mirage.Logging;

namespace Mirage
namespace Mirage.Serialization
{
/// <summary>
/// a class that holds readers for the different types
Expand All @@ -27,20 +27,6 @@ public static class Reader<T>
public static Func<NetworkReader, T> Read { internal get; set; }
}

/// <summary>
/// An object that implements this interface can find objects by their net id
/// This is used by readers when trying to deserialize gameobjects
/// </summary>
public interface IObjectLocator
{
/// <summary>
/// Finds a network identity by id
/// </summary>
/// <param name="netId">the id of the object to find</param>
/// <returns>The NetworkIdentity matching the netid or null if none is found</returns>
NetworkIdentity this[uint netId] { get; }
}

// Note: This class is intended to be extremely pedantic, and
// throw exceptions whenever stuff is going slightly wrong.
// The exceptions will be handled in NetworkServer/NetworkClient.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using UnityEngine;
using Mirage.Logging;

namespace Mirage
namespace Mirage.Serialization
{
/// <summary>
/// NetworkReader to be used with <see cref="NetworkReaderPool">NetworkReaderPool</see>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using UnityEngine;

namespace Mirage
namespace Mirage.Serialization
{
/// <summary>
/// a class that holds writers for the different types
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using UnityEngine;
using Mirage.Logging;

namespace Mirage
namespace Mirage.Serialization
{
/// <summary>
/// NetworkWriter to be used with <see cref="NetworkWriterPool">NetworkWriterPool</see>
Expand Down
47 changes: 47 additions & 0 deletions Assets/Mirage/Runtime/Serialization/TypeConversion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Runtime.InteropServices;

namespace Mirage.Serialization
{

/// <summary>
/// Converts between uint and float without allocations
/// </summary>
[StructLayout(LayoutKind.Explicit)]
internal struct UIntFloat
{
[FieldOffset(0)]
public float floatValue;

[FieldOffset(0)]
public uint intValue;
}

/// <summary>
/// Converts between ulong and double without allocations
/// </summary>
[StructLayout(LayoutKind.Explicit)]
internal struct UIntDouble
{
[FieldOffset(0)]
public double doubleValue;

[FieldOffset(0)]
public ulong longValue;
}

/// <summary>
/// Converts between ulong and decimal without allocations
/// </summary>
[StructLayout(LayoutKind.Explicit)]
internal struct UIntDecimal
{
[FieldOffset(0)]
public ulong longValue1;

[FieldOffset(8)]
public ulong longValue2;

[FieldOffset(0)]
public decimal decimalValue;
}
}
11 changes: 11 additions & 0 deletions Assets/Mirage/Runtime/Serialization/TypeConversion.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/ServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using UnityEngine;
using UnityEngine.Serialization;
using Mirage.Logging;
using Mirage.Serialization;

namespace Mirage
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/Transport/PipeConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Threading;
using Cysharp.Threading.Tasks;
using Mirage.Serialization;

namespace Mirage
{
Expand Down
45 changes: 9 additions & 36 deletions Assets/Mirage/Runtime/UNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,17 @@ public static class Version
}

/// <summary>
/// Converts between uint and float without allocations
/// An object that implements this interface can find objects by their net id
/// This is used by readers when trying to deserialize gameobjects
/// </summary>
[StructLayout(LayoutKind.Explicit)]
internal struct UIntFloat
public interface IObjectLocator
{
[FieldOffset(0)]
public float floatValue;

[FieldOffset(0)]
public uint intValue;
}

/// <summary>
/// Converts between ulong and double without allocations
/// </summary>
[StructLayout(LayoutKind.Explicit)]
internal struct UIntDouble
{
[FieldOffset(0)]
public double doubleValue;

[FieldOffset(0)]
public ulong longValue;
/// <summary>
/// Finds a network identity by id
/// </summary>
/// <param name="netId">the id of the object to find</param>
/// <returns>The NetworkIdentity matching the netid or null if none is found</returns>
NetworkIdentity this[uint netId] { get; }
}

/// <summary>
/// Converts between ulong and decimal without allocations
/// </summary>
[StructLayout(LayoutKind.Explicit)]
internal struct UIntDecimal
{
[FieldOffset(0)]
public ulong longValue1;

[FieldOffset(8)]
public ulong longValue2;

[FieldOffset(0)]
public decimal decimalValue;
}
}
1 change: 1 addition & 0 deletions Assets/Mirage/Weaver/Processors/ClientRpcProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Mirage.RemoteCalls;
using Mirage.Serialization;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace Mirage.Weaver
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Weaver/Processors/ReaderWriterProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Mirage.Serialization;
using Mono.Cecil;
using Mono.Cecil.Cil;
using UnityEditor;
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Weaver/Processors/ServerRpcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using Cysharp.Threading.Tasks;
using Mirage.RemoteCalls;
using Mirage.Serialization;
using Mono.Cecil;
using Mono.Cecil.Cil;
using MethodAttributes = Mono.Cecil.MethodAttributes;
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Weaver/Processors/SyncVarProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Mirage.Serialization;
using Mono.Cecil;
using Mono.Cecil.Cil;
using UnityEngine;
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Weaver/Readers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Mirage.Serialization;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Weaver/Writers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Mirage.Serialization;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Editor/Compression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NUnit.Framework;
using UnityEngine;
using RangeAttribute = NUnit.Framework.RangeAttribute;
using Mirage.Serialization;

namespace Mirage
{
Expand Down

0 comments on commit 5dc037d

Please sign in to comment.