From 07d290fd8cf841f97c325c2a5b467af55b4f7d3e Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Fri, 18 Sep 2020 14:54:51 -0500 Subject: [PATCH] Enhance code readability by using extension method (#2260) # Before This is hard to read: ``` if (ca.AttributeType.FullName == typeof(Mirror.CommandAttribute).FullName) { ... } ``` # After ``` if (ca.AttributeType.Is()) { ... } ``` --- Assets/Mirror/Editor/Weaver/Extensions.cs | 13 ++++++----- .../Processors/MonoBehaviourProcessor.cs | 6 ++--- .../Processors/NetworkBehaviourProcessor.cs | 23 ++++++++++--------- .../ServerClientAttributeProcessor.cs | 2 +- .../Weaver/Processors/SyncVarProcessor.cs | 20 ++++++++-------- .../Weaver/Processors/TargetRpcProcessor.cs | 2 +- Assets/Mirror/Editor/Weaver/Readers.cs | 4 ++-- Assets/Mirror/Editor/Weaver/Weaver.cs | 2 +- Assets/Mirror/Editor/Weaver/Writers.cs | 4 ++-- 9 files changed, 39 insertions(+), 37 deletions(-) diff --git a/Assets/Mirror/Editor/Weaver/Extensions.cs b/Assets/Mirror/Editor/Weaver/Extensions.cs index 09835f66c..382f7c23d 100644 --- a/Assets/Mirror/Editor/Weaver/Extensions.cs +++ b/Assets/Mirror/Editor/Weaver/Extensions.cs @@ -7,6 +7,10 @@ namespace Mirror.Weaver { public static class Extensions { + public static bool Is(this TypeReference td, Type t) => + td.FullName == t.FullName; + + public static bool Is(this TypeReference td) => Is(td, typeof(T)); // removes from class names (if any generic parameters) internal static string StripGenericParametersFromClassName(string className) @@ -67,13 +71,12 @@ public static TypeReference GetEnumUnderlyingType(this TypeDefinition td) public static bool ImplementsInterface(this TypeDefinition td) { TypeDefinition typedef = td; - Type baseInterface = typeof(TInterface); while (typedef != null) { foreach (InterfaceImplementation iface in typedef.Interfaces) { - if (iface.InterfaceType.FullName == baseInterface.FullName) + if (iface.InterfaceType.Is()) return true; } @@ -170,10 +173,9 @@ public static MethodReference MakeHostInstanceGeneric(this MethodReference self, public static CustomAttribute GetCustomAttribute(this ICustomAttributeProvider method) { - Type t = typeof(TAttribute); foreach (CustomAttribute ca in method.CustomAttributes) { - if (ca.AttributeType.FullName == t.FullName) + if (ca.AttributeType.Is()) return ca; } return null; @@ -181,9 +183,8 @@ public static CustomAttribute GetCustomAttribute(this ICustomAttribu public static bool HasCustomAttribute(this ICustomAttributeProvider attributeProvider) { - Type t = typeof(TAttribute); // Linq allocations don't matter in weaver - return attributeProvider.CustomAttributes.Any(attr => attr.AttributeType.FullName == t.FullName); + return attributeProvider.CustomAttributes.Any(attr => attr.AttributeType.Is()); } public static T GetField(this CustomAttribute ca, string field, T defaultValue) diff --git a/Assets/Mirror/Editor/Weaver/Processors/MonoBehaviourProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/MonoBehaviourProcessor.cs index 0f50a1cfb..4b10de314 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/MonoBehaviourProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/MonoBehaviourProcessor.cs @@ -35,17 +35,17 @@ static void ProcessMethods(TypeDefinition td) { foreach (CustomAttribute ca in md.CustomAttributes) { - if (ca.AttributeType.FullName == typeof(Mirror.CommandAttribute).FullName) + if (ca.AttributeType.Is()) { Weaver.Error($"Command {md.Name} must be declared inside a NetworkBehaviour", md); } - if (ca.AttributeType.FullName == typeof(Mirror.ClientRpcAttribute).FullName) + if (ca.AttributeType.Is()) { Weaver.Error($"ClientRpc {md.Name} must be declared inside a NetworkBehaviour", md); } - if (ca.AttributeType.FullName == typeof(Mirror.TargetRpcAttribute).FullName) + if (ca.AttributeType.Is()) { Weaver.Error($"TargetRpc {md.Name} must be declared inside a NetworkBehaviour", md); } diff --git a/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs index 7879161a8..7f15912b4 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Mono.CecilX; using Mono.CecilX.Cil; @@ -517,8 +518,8 @@ void DeserializeField(FieldDefinition syncVar, ILProcessor worker, MethodDefinit /// static bool IsNetworkIdentityField(FieldDefinition syncVar) { - return syncVar.FieldType.FullName == typeof(UnityEngine.GameObject).FullName || - syncVar.FieldType.FullName == typeof(Mirror.NetworkIdentity).FullName; + return syncVar.FieldType.Is() || + syncVar.FieldType.Is(); } /// @@ -844,11 +845,11 @@ public static bool ReadArguments(MethodDefinition method, ILProcessor worker, Re worker.Append(worker.Create(OpCodes.Call, readFunc)); // conversion.. is this needed? - if (param.ParameterType.FullName == typeof(float).FullName) + if (param.ParameterType.Is()) { worker.Append(worker.Create(OpCodes.Conv_R4)); } - else if (param.ParameterType.FullName == typeof(double).FullName) + else if (param.ParameterType.Is()) { worker.Append(worker.Create(OpCodes.Conv_R8)); } @@ -880,12 +881,12 @@ public static bool ValidateRemoteCallAndParameters(MethodDefinition method, Remo // check if a Command/TargetRpc/Rpc function is valid for weaving static bool ValidateFunction(MethodReference md) { - if (md.ReturnType.FullName == typeof(System.Collections.IEnumerator).FullName) + if (md.ReturnType.Is()) { Weaver.Error($"{md.Name} cannot be a coroutine", md); return false; } - if (md.ReturnType.FullName != typeof(void).FullName) + if (!md.ReturnType.Is(typeof(void))) { Weaver.Error($"{md.Name} cannot return a value. Make it void instead", md); return false; @@ -915,7 +916,7 @@ static bool ValidateParameters(MethodReference method, RemoteCallType callType) // validate parameters for a remote function call like Rpc/Cmd static bool ValidateParameter(MethodReference method, ParameterDefinition param, RemoteCallType callType, bool firstParam) { - bool isNetworkConnection = param.ParameterType.FullName == typeof(Mirror.NetworkConnection).FullName; + bool isNetworkConnection = param.ParameterType.Is(); bool isSenderConnection = IsSenderConnection(param, callType); if (param.IsOut) @@ -958,7 +959,7 @@ public static bool IsSenderConnection(ParameterDefinition param, RemoteCallType TypeReference type = param.ParameterType; - return type.FullName == typeof(Mirror.NetworkConnectionToClient).FullName + return type.Is() || type.Resolve().IsDerivedFrom(); } @@ -973,19 +974,19 @@ void ProcessMethods() { foreach (CustomAttribute ca in md.CustomAttributes) { - if (ca.AttributeType.FullName == typeof(Mirror.CommandAttribute).FullName) + if (ca.AttributeType.Is()) { ProcessCommand(names, md, ca); break; } - if (ca.AttributeType.FullName == typeof(Mirror.TargetRpcAttribute).FullName) + if (ca.AttributeType.Is()) { ProcessTargetRpc(names, md, ca); break; } - if (ca.AttributeType.FullName == typeof(Mirror.ClientRpcAttribute).FullName) + if (ca.AttributeType.Is()) { ProcessClientRpc(names, md, ca); break; diff --git a/Assets/Mirror/Editor/Weaver/Processors/ServerClientAttributeProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/ServerClientAttributeProcessor.cs index 5a2e9ba03..989ce1aea 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/ServerClientAttributeProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/ServerClientAttributeProcessor.cs @@ -154,7 +154,7 @@ static void InjectGuardParameters(MethodDefinition md, ILProcessor worker, Instr // this is required to early-out from a function with a return value. static void InjectGuardReturnValue(MethodDefinition md, ILProcessor worker, Instruction top) { - if (md.ReturnType.FullName != typeof(void).FullName) + if (!md.ReturnType.Is(typeof(void))) { md.Body.Variables.Add(new VariableDefinition(md.ReturnType)); md.Body.InitLocals = true; diff --git a/Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs index 2e491485f..b246e4e00 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs @@ -82,7 +82,7 @@ public static MethodDefinition ProcessSyncVarGet(FieldDefinition fd, string orig ILProcessor worker = get.Body.GetILProcessor(); // [SyncVar] GameObject? - if (fd.FieldType.FullName == typeof(UnityEngine.GameObject).FullName) + if (fd.FieldType.Is()) { // return this.GetSyncVarGameObject(ref field, uint netId); // this. @@ -95,7 +95,7 @@ public static MethodDefinition ProcessSyncVarGet(FieldDefinition fd, string orig worker.Append(worker.Create(OpCodes.Ret)); } // [SyncVar] NetworkIdentity? - else if (fd.FieldType.FullName == typeof(Mirror.NetworkIdentity).FullName) + else if (fd.FieldType.Is()) { // return this.GetSyncVarNetworkIdentity(ref field, uint netId); // this. @@ -141,7 +141,7 @@ public static MethodDefinition ProcessSyncVarSet(TypeDefinition td, FieldDefinit worker.Append(worker.Create(OpCodes.Ldarg_1)); // reference to field to set // make generic version of SetSyncVar with field type - if (fd.FieldType.FullName == typeof(UnityEngine.GameObject).FullName) + if (fd.FieldType.Is()) { // reference to netId Field to set worker.Append(worker.Create(OpCodes.Ldarg_0)); @@ -149,7 +149,7 @@ public static MethodDefinition ProcessSyncVarSet(TypeDefinition td, FieldDefinit worker.Append(worker.Create(OpCodes.Call, WeaverTypes.syncVarGameObjectEqualReference)); } - else if (fd.FieldType.FullName == typeof(Mirror.NetworkIdentity).FullName) + else if (fd.FieldType.Is()) { // reference to netId Field to set worker.Append(worker.Create(OpCodes.Ldarg_0)); @@ -191,7 +191,7 @@ public static MethodDefinition ProcessSyncVarSet(TypeDefinition td, FieldDefinit // 8 byte integer aka long worker.Append(worker.Create(OpCodes.Ldc_I8, dirtyBit)); - if (fd.FieldType.FullName == typeof(UnityEngine.GameObject).FullName) + if (fd.FieldType.Is()) { // reference to netId Field to set worker.Append(worker.Create(OpCodes.Ldarg_0)); @@ -199,7 +199,7 @@ public static MethodDefinition ProcessSyncVarSet(TypeDefinition td, FieldDefinit worker.Append(worker.Create(OpCodes.Call, WeaverTypes.setSyncVarGameObjectReference)); } - else if (fd.FieldType.FullName == typeof(Mirror.NetworkIdentity).FullName) + else if (fd.FieldType.Is()) { // reference to netId Field to set worker.Append(worker.Create(OpCodes.Ldarg_0)); @@ -266,8 +266,8 @@ public static void ProcessSyncVar(TypeDefinition td, FieldDefinition fd, Diction // GameObject/NetworkIdentity SyncVars have a new field for netId FieldDefinition netIdField = null; - if (fd.FieldType.FullName == typeof(UnityEngine.GameObject).FullName || - fd.FieldType.FullName == typeof(Mirror.NetworkIdentity).FullName) + if (fd.FieldType.Is() || + fd.FieldType.Is()) { netIdField = new FieldDefinition("___" + fd.Name + "NetId", FieldAttributes.Private, @@ -297,8 +297,8 @@ public static void ProcessSyncVar(TypeDefinition td, FieldDefinition fd, Diction // netId instead // -> only for GameObjects, otherwise an int syncvar's getter would // end up in recursion. - if (fd.FieldType.FullName == typeof(UnityEngine.GameObject).FullName || - fd.FieldType.FullName == typeof(Mirror.NetworkIdentity).FullName) + if (fd.FieldType.Is() || + fd.FieldType.Is()) { Weaver.WeaveLists.replacementGetterProperties[fd] = get; } diff --git a/Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs index e0160e357..92bc571ef 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs @@ -12,7 +12,7 @@ public static class TargetRpcProcessor public static bool HasNetworkConnectionParameter(MethodDefinition md) { return md.Parameters.Count > 0 && - md.Parameters[0].ParameterType.FullName == typeof(Mirror.NetworkConnection).FullName; + md.Parameters[0].ParameterType.Is(); } public static MethodDefinition ProcessTargetRpcInvoke(TypeDefinition td, MethodDefinition md, MethodDefinition rpcCallFunc) diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index fbf4547f3..9dd412c86 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -54,12 +54,12 @@ public static MethodReference GetReadFunc(TypeReference variableReference, int r Weaver.Error($"Cannot generate reader for component type {variableReference.Name}. Use a supported type or provide a custom reader", variableReference); return null; } - if (variableReference.FullName == typeof(UnityEngine.Object).FullName) + if (variableReference.Is()) { Weaver.Error($"Cannot generate reader for {variableReference.Name}. Use a supported type or provide a custom reader", variableReference); return null; } - if (variableReference.FullName == typeof(UnityEngine.ScriptableObject).FullName) + if (variableReference.Is()) { Weaver.Error($"Cannot generate reader for {variableReference.Name}. Use a supported type or provide a custom reader", variableReference); return null; diff --git a/Assets/Mirror/Editor/Weaver/Weaver.cs b/Assets/Mirror/Editor/Weaver/Weaver.cs index 348441b4b..776b974eb 100644 --- a/Assets/Mirror/Editor/Weaver/Weaver.cs +++ b/Assets/Mirror/Editor/Weaver/Weaver.cs @@ -125,7 +125,7 @@ static bool WeaveNetworkBehavior(TypeDefinition td) TypeDefinition parent = td; while (parent != null) { - if (parent.FullName == typeof(Mirror.NetworkBehaviour).FullName) + if (parent.Is()) { break; } diff --git a/Assets/Mirror/Editor/Weaver/Writers.cs b/Assets/Mirror/Editor/Weaver/Writers.cs index c0c9df2d0..b96c793fe 100644 --- a/Assets/Mirror/Editor/Weaver/Writers.cs +++ b/Assets/Mirror/Editor/Weaver/Writers.cs @@ -94,12 +94,12 @@ static MethodDefinition GenerateWriter(TypeReference variableReference, int recu Weaver.Error($"Cannot generate writer for component type {variableReference.Name}. Use a supported type or provide a custom writer", variableReference); return null; } - if (variableReference.FullName == typeof(UnityEngine.Object).FullName) + if (variableReference.Is()) { Weaver.Error($"Cannot generate writer for {variableReference.Name}. Use a supported type or provide a custom writer", variableReference); return null; } - if (variableReference.FullName == typeof(UnityEngine.ScriptableObject).FullName) + if (variableReference.Is()) { Weaver.Error($"Cannot generate writer for {variableReference.Name}. Use a supported type or provide a custom writer", variableReference); return null;