feat: Added GenerateSerializationForGenericParameterAttribute and GenerateSerializationForTypeAttribute#2694
Merged
ShadauxCat merged 8 commits intoOct 2, 2023
Conversation
…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
| if (UserNetworkVariableSerialization<T>.ReadValue == null || UserNetworkVariableSerialization<T>.WriteValue == null || UserNetworkVariableSerialization<T>.DuplicateValue == null) | ||
| { | ||
| throw new ArgumentException($"Type {typeof(T).FullName} is not supported by {typeof(NetworkVariable<>).Name}. If this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)}, {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.DuplicateValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}."); | ||
| throw new ArgumentException($"Serialization has not been generated for type {typeof(T).FullName}. This can be addressed by adding a [{nameof(GenerateSerializationForGenericParameterAttribute)}] to your generic class that serializes this value (if you are using one), adding [{nameof(GenerateSerializationForTypeAttribute)}(typeof({typeof(T).FullName})] to the class or method that is attempting to serialize it, or creating a field on a {nameof(NetworkBehaviour)} of type {nameof(NetworkVariable<T>)}. If this error continues to appear after doing one of those things and this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list to enable automatic serialization generation. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)}, {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.DuplicateValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}."); |
Member
There was a problem hiding this comment.
(Not required suggestion)
Not a blocker, but it might be worth while making this exception a const that is used in this and the next two argument exceptions...up to you but it does seem like it might be less problematic in the very....very very...off chance that the message needs to be adjusted.
| /// <typeparam name="T"></typeparam> | ||
| internal class FallbackSerializer<T> : INetworkVariableSerializer<T> | ||
| { | ||
| private void ThrowArgumentError() |
…on_attributes' into feat/user_serialization_generation_attributes
NoelStephensUnity
added a commit
that referenced
this pull request
Oct 5, 2023
…GenerateSerializationForTypeAttribute` (#2694) 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 --------- Co-authored-by: Noel Stephens <noel.stephens@unity3d.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Changelog
GenerateSerializationForGenericParameterAttribute, which can be applied to user-created Network Variable types to ensure the codegen generates serialization for the generic types they wrap.GenerateSerializationForTypeAttribute, which can be applied to any class or method to ensure the codegen generates serialization for the specific provided type.NetworkVariableSerialization<T>.Read,NetworkVariableSerialization<T>.Write,NetworkVariableSerialization<T>.AreEqual, andNetworkVariableSerialization<T>.Duplicateto further support the creation of user-created network variables by allowing users to access the generated serialization methods and serialize generic types efficiently without boxing.NetworkVariableBase.MarkNetworkBehaviourDirtyso that user-created network variable types can mark their containingNetworkBehaviourto be processed by the update loop.Testing and Documentation