mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
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<Mirror.CommandAttribute>()) { ... } ```
This commit is contained in:
parent
7919942110
commit
07d290fd8c
@ -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<T>(this TypeReference td) => Is(td, typeof(T));
|
||||
|
||||
// removes <T> 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<TInterface>(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<TInterface>())
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -170,10 +173,9 @@ public static MethodReference MakeHostInstanceGeneric(this MethodReference self,
|
||||
|
||||
public static CustomAttribute GetCustomAttribute<TAttribute>(this ICustomAttributeProvider method)
|
||||
{
|
||||
Type t = typeof(TAttribute);
|
||||
foreach (CustomAttribute ca in method.CustomAttributes)
|
||||
{
|
||||
if (ca.AttributeType.FullName == t.FullName)
|
||||
if (ca.AttributeType.Is<TAttribute>())
|
||||
return ca;
|
||||
}
|
||||
return null;
|
||||
@ -181,9 +183,8 @@ public static CustomAttribute GetCustomAttribute<TAttribute>(this ICustomAttribu
|
||||
|
||||
public static bool HasCustomAttribute<TAttribute>(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<TAttribute>());
|
||||
}
|
||||
|
||||
public static T GetField<T>(this CustomAttribute ca, string field, T defaultValue)
|
||||
|
@ -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<Mirror.CommandAttribute>())
|
||||
{
|
||||
Weaver.Error($"Command {md.Name} must be declared inside a NetworkBehaviour", md);
|
||||
}
|
||||
|
||||
if (ca.AttributeType.FullName == typeof(Mirror.ClientRpcAttribute).FullName)
|
||||
if (ca.AttributeType.Is<Mirror.ClientRpcAttribute>())
|
||||
{
|
||||
Weaver.Error($"ClientRpc {md.Name} must be declared inside a NetworkBehaviour", md);
|
||||
}
|
||||
|
||||
if (ca.AttributeType.FullName == typeof(Mirror.TargetRpcAttribute).FullName)
|
||||
if (ca.AttributeType.Is<Mirror.TargetRpcAttribute>())
|
||||
{
|
||||
Weaver.Error($"TargetRpc {md.Name} must be declared inside a NetworkBehaviour", md);
|
||||
}
|
||||
|
@ -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
|
||||
/// <returns></returns>
|
||||
static bool IsNetworkIdentityField(FieldDefinition syncVar)
|
||||
{
|
||||
return syncVar.FieldType.FullName == typeof(UnityEngine.GameObject).FullName ||
|
||||
syncVar.FieldType.FullName == typeof(Mirror.NetworkIdentity).FullName;
|
||||
return syncVar.FieldType.Is<UnityEngine.GameObject>() ||
|
||||
syncVar.FieldType.Is<Mirror.NetworkIdentity>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -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<float>())
|
||||
{
|
||||
worker.Append(worker.Create(OpCodes.Conv_R4));
|
||||
}
|
||||
else if (param.ParameterType.FullName == typeof(double).FullName)
|
||||
else if (param.ParameterType.Is<double>())
|
||||
{
|
||||
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<System.Collections.IEnumerator>())
|
||||
{
|
||||
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<Mirror.NetworkConnection>();
|
||||
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<Mirror.NetworkConnectionToClient>()
|
||||
|| type.Resolve().IsDerivedFrom<Mirror.NetworkConnectionToClient>();
|
||||
}
|
||||
|
||||
@ -973,19 +974,19 @@ void ProcessMethods()
|
||||
{
|
||||
foreach (CustomAttribute ca in md.CustomAttributes)
|
||||
{
|
||||
if (ca.AttributeType.FullName == typeof(Mirror.CommandAttribute).FullName)
|
||||
if (ca.AttributeType.Is<Mirror.CommandAttribute>())
|
||||
{
|
||||
ProcessCommand(names, md, ca);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ca.AttributeType.FullName == typeof(Mirror.TargetRpcAttribute).FullName)
|
||||
if (ca.AttributeType.Is<Mirror.TargetRpcAttribute>())
|
||||
{
|
||||
ProcessTargetRpc(names, md, ca);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ca.AttributeType.FullName == typeof(Mirror.ClientRpcAttribute).FullName)
|
||||
if (ca.AttributeType.Is<Mirror.ClientRpcAttribute>())
|
||||
{
|
||||
ProcessClientRpc(names, md, ca);
|
||||
break;
|
||||
|
@ -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;
|
||||
|
@ -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<UnityEngine.GameObject>())
|
||||
{
|
||||
// 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<Mirror.NetworkIdentity>())
|
||||
{
|
||||
// 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<UnityEngine.GameObject>())
|
||||
{
|
||||
// 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<Mirror.NetworkIdentity>())
|
||||
{
|
||||
// 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<UnityEngine.GameObject>())
|
||||
{
|
||||
// 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<Mirror.NetworkIdentity>())
|
||||
{
|
||||
// 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<UnityEngine.GameObject>() ||
|
||||
fd.FieldType.Is<Mirror.NetworkIdentity>())
|
||||
{
|
||||
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<UnityEngine.GameObject>() ||
|
||||
fd.FieldType.Is<Mirror.NetworkIdentity>())
|
||||
{
|
||||
Weaver.WeaveLists.replacementGetterProperties[fd] = get;
|
||||
}
|
||||
|
@ -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<Mirror.NetworkConnection>();
|
||||
}
|
||||
|
||||
public static MethodDefinition ProcessTargetRpcInvoke(TypeDefinition td, MethodDefinition md, MethodDefinition rpcCallFunc)
|
||||
|
@ -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<UnityEngine.Object>())
|
||||
{
|
||||
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<UnityEngine.ScriptableObject>())
|
||||
{
|
||||
Weaver.Error($"Cannot generate reader for {variableReference.Name}. Use a supported type or provide a custom reader", variableReference);
|
||||
return null;
|
||||
|
@ -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<Mirror.NetworkBehaviour>())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
@ -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<UnityEngine.Object>())
|
||||
{
|
||||
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<UnityEngine.ScriptableObject>())
|
||||
{
|
||||
Weaver.Error($"Cannot generate writer for {variableReference.Name}. Use a supported type or provide a custom writer", variableReference);
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user