-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breaking: Use SyncLists directly (delete overrides) (#2307)
* feat: Use SyncLists directly Previously, you had to write an intermediary class to use synclists, syncsets and syncdictionaries. The weaver would populate that intermediary class with a serialization and deserialization method This PR gets rid of 90% of the weaver code for synclists. There is no need to generate these methods anymore. Instead the lists use `writer.Write<T>` and `read.read<T>` to serialize their Data. Since there is no code generate in synclists, you can now use the synclists directly instead of subclassing them. BEFORE: ```cs public class MyComponent : NetworkBehaviour { // nonsense class to make the weaver happy class SyncListData : Synclist<Data> {} SyncListData mySyncList; } ``` AFTER: ```cs public class MyComponent : NetworkBehaviour { Synclist<Data> mySyncList; } ``` * linting * feat: Use SyncLists directly (no overrides) Previously, you had to write an intermediary class to use synclists and syncdictionaries. The weaver would populate that intermediary class with a serialization and deserialization method This PR gets rid of 90% of the weaver code for synclists. There is no need to generate these methods anymore. Instead the lists use `writer.Write<T>` and `read.read<T>` to serialize their Data. Since there is no code generate in synclists, you can now use the synclists directly instead of subclassing them. Same as #2305 ,but it removes the deprecated Serialize and Deserialize methods from syncobjects, This way you get a nice compiler error for the code that no longer works, instead of a warning you might accidentally ignore. BEFORE: ```cs public class MyComponent : NetworkBehaviour { // nonsense class to make the weaver happy class SyncListData : Synclist<Data> {} SyncListData mySyncList; } ``` AFTER: ```cs public class MyComponent : NetworkBehaviour { Synclist<Data> mySyncList; } ``` BREAKING CHANGE: Serialize and Deserialize methods in synclists don't do anything anymore * Remove old comment * Fix compilatio error
- Loading branch information
Showing
31 changed files
with
184 additions
and
822 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
57 changes: 1 addition & 56 deletions
57
Assets/Mirror/Editor/Weaver/Processors/GenericArgumentResolver.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,56 +1 @@ | ||
using System; | ||
using Mono.CecilX; | ||
|
||
namespace Mirror.Weaver | ||
{ | ||
public static class GenericArgumentResolver | ||
{ | ||
static TypeReference[] GetGenericArguments(TypeReference tr, Type baseType, TypeReference[] genericArguments) | ||
{ | ||
if (tr == null) | ||
return null; | ||
|
||
TypeReference[] resolvedArguments = new TypeReference[0]; | ||
|
||
if (tr is GenericInstanceType genericInstance) | ||
{ | ||
// this type is a generic instance, for example List<int> | ||
// however, the parameter may be generic itself, for example: | ||
// List<T>. If T is a generic parameter, then look it up in | ||
// from the arguments we got. | ||
resolvedArguments = genericInstance.GenericArguments.ToArray(); | ||
|
||
for (int i = 0; i< resolvedArguments.Length; i++) | ||
{ | ||
TypeReference argument = resolvedArguments[i]; | ||
if (argument is GenericParameter genericArgument) | ||
{ | ||
resolvedArguments[i] = genericArguments[genericArgument.Position]; | ||
} | ||
} | ||
} | ||
|
||
if (tr.Is(baseType)) | ||
return resolvedArguments; | ||
|
||
if (tr.CanBeResolved()) | ||
return GetGenericArguments(tr.Resolve().BaseType, baseType, resolvedArguments); | ||
|
||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Find out what the arguments are to a generic base type | ||
/// </summary> | ||
/// <param name="td"></param> | ||
/// <param name="baseType"></param> | ||
/// <returns></returns> | ||
public static TypeReference[] GetGenericArguments(TypeDefinition td, Type baseType) | ||
{ | ||
if (td == null) | ||
return null; | ||
|
||
return GetGenericArguments(td.BaseType, baseType, new TypeReference[] { }); | ||
} | ||
} | ||
} | ||
// Removed Oct 1 2020 |
29 changes: 1 addition & 28 deletions
29
Assets/Mirror/Editor/Weaver/Processors/SyncDictionaryProcessor.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,28 +1 @@ | ||
using Mono.CecilX; | ||
|
||
namespace Mirror.Weaver | ||
{ | ||
/// <summary> | ||
/// generates OnSerialize/OnDeserialize for SyncLists | ||
/// </summary> | ||
static class SyncDictionaryProcessor | ||
{ | ||
/// <summary> | ||
/// Generates serialization methods for synclists | ||
/// </summary> | ||
/// <param name="td">The synclist class</param> | ||
public static void Process(TypeDefinition td) | ||
{ | ||
TypeReference []arguments = GenericArgumentResolver.GetGenericArguments(td, typeof(SyncDictionary<,>)); | ||
if (arguments != null) | ||
{ | ||
SyncObjectProcessor.GenerateSerialization(td, arguments[0], typeof(SyncDictionary<,>), "SerializeKey", "DeserializeKey"); | ||
SyncObjectProcessor.GenerateSerialization(td, arguments[1], typeof(SyncDictionary<,>), "SerializeItem", "DeserializeItem"); | ||
} | ||
else | ||
{ | ||
Weaver.Error($"Could not find generic arguments for SyncDictionary in {td.Name}", td); | ||
} | ||
} | ||
} | ||
} | ||
// Removed Oct 1 2020 |
30 changes: 1 addition & 29 deletions
30
Assets/Mirror/Editor/Weaver/Processors/SyncListProcessor.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,29 +1 @@ | ||
using System; | ||
using Mono.CecilX; | ||
|
||
namespace Mirror.Weaver | ||
{ | ||
/// <summary> | ||
/// generates OnSerialize/OnDeserialize for SyncLists | ||
/// </summary> | ||
static class SyncListProcessor | ||
{ | ||
/// <summary> | ||
/// Generates serialization methods for synclists | ||
/// </summary> | ||
/// <param name="td">The synclist class</param> | ||
/// <param name="mirrorBaseType">the base SyncObject td inherits from</param> | ||
public static void Process(TypeDefinition td, Type mirrorBaseType) | ||
{ | ||
TypeReference [] arguments = GenericArgumentResolver.GetGenericArguments(td, mirrorBaseType); | ||
if (arguments != null) | ||
{ | ||
SyncObjectProcessor.GenerateSerialization(td, arguments[0], mirrorBaseType, "SerializeItem", "DeserializeItem"); | ||
} | ||
else | ||
{ | ||
Weaver.Error($"Could not find generic arguments for {mirrorBaseType.Name} in {td}", td); | ||
} | ||
} | ||
} | ||
} | ||
// Removed Oct 1 2020 |
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.