-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
GenerateSerializationForGenericParameterAttribute
and `Genera…
…teSerializationForTypeAttribute` This enables users to control the generation of serialization code through codegen, making it easier to create their own network variable subtypes, and also making it possible for them to easily generate serialization for specific types they know they need to use. NetworkVariable and NetworkList have been changed to use these attributes so they are no longer special cases in our codegen. This PR also exposes methods in `NetworkVariableSerialization<T>` to further support this type of serialization need. resolves #2686
- Loading branch information
1 parent
f59f5e6
commit 404d032
Showing
9 changed files
with
391 additions
and
38 deletions.
There are no files selected for viewing
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
82 changes: 82 additions & 0 deletions
82
...etcode.gameobjects/Runtime/Messaging/GenerateSerializationForGenericParameterAttribute.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,82 @@ | ||
using System; | ||
|
||
namespace Unity.Netcode | ||
{ | ||
/// <summary> | ||
/// Marks a generic parameter in this class as a type that should be serialized through | ||
/// <see cref="NetworkVariableSerialization{T}"/>. This enables the use of the following methods to support | ||
/// serialization within a Network Variable type: | ||
/// <br/> | ||
/// <br/> | ||
/// <see cref="NetworkVariableSerialization{T}"/>.<see cref="NetworkVariableSerialization{T}.Read"/> | ||
/// <br/> | ||
/// <see cref="NetworkVariableSerialization{T}"/>.<see cref="NetworkVariableSerialization{T}.Write"/> | ||
/// <br/> | ||
/// <see cref="NetworkVariableSerialization{T}"/>.<see cref="NetworkVariableSerialization{T}.AreEqual"/> | ||
/// <br/> | ||
/// <see cref="NetworkVariableSerialization{T}"/>.<see cref="NetworkVariableSerialization{T}.Duplicate"/> | ||
/// <br/> | ||
/// <br/> | ||
/// The parameter is indicated by index (and is 0-indexed); for example: | ||
/// <br/> | ||
/// <code> | ||
/// [SerializesGenericParameter(1)] | ||
/// public class MyClass<TTypeOne, TTypeTwo> | ||
/// { | ||
/// } | ||
/// </code> | ||
/// <br/> | ||
/// This tells the code generation for <see cref="NetworkVariableSerialization{T}"/> to generate | ||
/// serialized code for <b>TTypeTwo</b> (generic parameter 1). | ||
/// <br/> | ||
/// <br/> | ||
/// Note that this is primarily intended to support subtypes of <see cref="NetworkVariableBase"/>, | ||
/// and as such, the type resolution is done by examining fields of <see cref="NetworkBehaviour"/> | ||
/// subclasses. If your type is not used in a <see cref="NetworkBehaviour"/>, the codegen will | ||
/// not find the types, even with this attribute. | ||
/// <br/> | ||
/// <br/> | ||
/// This attribute is properly inherited by subclasses. For example: | ||
/// <br/> | ||
/// <code> | ||
/// [SerializesGenericParameter(0)] | ||
/// public class MyClass<T> | ||
/// { | ||
/// } | ||
/// <br/> | ||
/// public class MySubclass1 : MyClass<Foo> | ||
/// { | ||
/// } | ||
/// <br/> | ||
/// public class MySubclass2<T> : MyClass<T> | ||
/// { | ||
/// } | ||
/// <br/> | ||
/// [SerializesGenericParameter(1)] | ||
/// public class MySubclass3<TTypeOne, TTypeTwo> : MyClass<TTypeOne> | ||
/// { | ||
/// } | ||
/// <br/> | ||
/// public class MyBehaviour : NetworkBehaviour | ||
/// { | ||
/// public MySubclass1 TheValue; | ||
/// public MySubclass2<Bar> TheValue; | ||
/// public MySubclass3<Baz, Qux> TheValue; | ||
/// } | ||
/// </code> | ||
/// <br/> | ||
/// The above code will trigger generation of serialization code for <b>Foo</b> (passed directly to the | ||
/// base class), <b>Bar</b> (passed indirectly to the base class), <b>Baz</b> (passed indirectly to the base class), | ||
/// and <b>Qux</b> (marked as serializable in the subclass). | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)] | ||
public class GenerateSerializationForGenericParameterAttribute : Attribute | ||
{ | ||
internal int ParameterIndex; | ||
|
||
public GenerateSerializationForGenericParameterAttribute(int parameterIndex) | ||
{ | ||
ParameterIndex = parameterIndex; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...e.gameobjects/Runtime/Messaging/GenerateSerializationForGenericParameterAttribute.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
com.unity.netcode.gameobjects/Runtime/Messaging/GenerateSerializationForTypeAttribute.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,26 @@ | ||
using System; | ||
|
||
namespace Unity.Netcode | ||
{ | ||
/// <summary> | ||
/// Specifies a specific type that needs serialization to be generated by codegen. | ||
/// This is only needed in special circumstances where manual serialization is being done. | ||
/// If you are making a generic network variable-style class, use <see cref="GenerateSerializationForGenericParameterAttribute"/>. | ||
/// <br /> | ||
/// <br /> | ||
/// This attribute can be attached to any class or method anywhere in the codebase and | ||
/// will trigger codegen to generate serialization code for the provided type. It only needs | ||
/// to be included once type per codebase, but including it multiple times for the same type | ||
/// is safe. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method, AllowMultiple = true)] | ||
public class GenerateSerializationForTypeAttribute : Attribute | ||
{ | ||
internal Type Type; | ||
|
||
public GenerateSerializationForTypeAttribute(Type type) | ||
{ | ||
Type = type; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...unity.netcode.gameobjects/Runtime/Messaging/GenerateSerializationForTypeAttribute.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.