Weaver.ResolveMethod moved to Resolvers

This commit is contained in:
vis2k 2019-01-02 12:52:43 +01:00
parent b5eb487c65
commit feb6a9e01d
4 changed files with 99 additions and 85 deletions

View File

@ -80,6 +80,7 @@
<Compile Include="Processors\TargetRpcProcessor.cs" /> <Compile Include="Processors\TargetRpcProcessor.cs" />
<Compile Include="Processors\SyncListStructProcessor.cs" /> <Compile Include="Processors\SyncListStructProcessor.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Resolvers.cs" />
<Compile Include="Weaver.cs" /> <Compile Include="Weaver.cs" />
<Compile Include="Helpers.cs" /> <Compile Include="Helpers.cs" />
<Compile Include="CompilationFinishedHook.cs" /> <Compile Include="CompilationFinishedHook.cs" />

View File

@ -51,7 +51,7 @@ public static MethodDefinition ProcessEventInvoke(TypeDefinition td, EventDefini
cmdWorker.Append(cmdWorker.Create(OpCodes.Ldfld, eventField)); cmdWorker.Append(cmdWorker.Create(OpCodes.Ldfld, eventField));
// read the event arguments // read the event arguments
MethodReference invoke = Weaver.ResolveMethod(eventField.FieldType, "Invoke"); MethodReference invoke = Resolvers.ResolveMethod(eventField.FieldType, Weaver.scriptDef, "Invoke");
if (!NetworkBehaviourProcessor.ProcessNetworkReaderParameters(td, invoke.Resolve(), cmdWorker, false)) if (!NetworkBehaviourProcessor.ProcessNetworkReaderParameters(td, invoke.Resolve(), cmdWorker, false))
return null; return null;
@ -66,7 +66,7 @@ public static MethodDefinition ProcessEventInvoke(TypeDefinition td, EventDefini
public static MethodDefinition ProcessEventCall(TypeDefinition td, EventDefinition ed, CustomAttribute ca) public static MethodDefinition ProcessEventCall(TypeDefinition td, EventDefinition ed, CustomAttribute ca)
{ {
MethodReference invoke = Weaver.ResolveMethod(ed.EventType, "Invoke"); MethodReference invoke = Resolvers.ResolveMethod(ed.EventType, Weaver.scriptDef, "Invoke");
MethodDefinition evt = new MethodDefinition("Call" + ed.Name, MethodAttributes.Public | MethodDefinition evt = new MethodDefinition("Call" + ed.Name, MethodAttributes.Public |
MethodAttributes.HideBySig, MethodAttributes.HideBySig,
Weaver.voidType); Weaver.voidType);

View File

@ -0,0 +1,41 @@
// all the resolve functions for the weaver
// NOTE: these functions should be made extensions, but right now they still
// make heavy use of Weaver.fail and we'd have to check each one's return
// value for null otherwise.
// (original FieldType.Resolve returns null if not found too, so
// exceptions would be a bit inconsistent here)
using Mono.Cecil;
namespace Mirror.Weaver
{
public static class Resolvers
{
public static MethodReference ResolveMethod(TypeReference tr, AssemblyDefinition scriptDef, string name)
{
//Console.WriteLine("ResolveMethod " + t.ToString () + " " + name);
if (tr == null)
{
Log.Error("Type missing for " + name);
Weaver.fail = true;
return null;
}
foreach (MethodDefinition methodRef in tr.Resolve().Methods)
{
if (methodRef.Name == name)
{
return scriptDef.MainModule.ImportReference(methodRef);
}
}
Log.Error("ResolveMethod failed " + tr.Name + "::" + name + " " + tr.Resolve());
// why did it fail!?
foreach (MethodDefinition methodRef in tr.Resolve().Methods)
{
Log.Error("Method " + methodRef.Name);
}
Weaver.fail = true;
return null;
}
}
}

View File

@ -778,7 +778,7 @@ static void ConfirmGeneratedCodeClass(ModuleDefinition moduleDef)
const MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; const MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
var method = new MethodDefinition(".ctor", methodAttributes, voidType); var method = new MethodDefinition(".ctor", methodAttributes, voidType);
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, ResolveMethod(objectType, ".ctor"))); method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, Resolvers.ResolveMethod(objectType, scriptDef, ".ctor")));
method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
lists.generateContainerClass.Methods.Add(method); lists.generateContainerClass.Methods.Add(method);
@ -1052,34 +1052,6 @@ public static MethodReference ResolveMethodInParents(TypeReference t, string nam
return ResolveMethodInParents(t.Resolve().BaseType, name); return ResolveMethodInParents(t.Resolve().BaseType, name);
} }
public static MethodReference ResolveMethod(TypeReference t, string name)
{
//Console.WriteLine("ResolveMethod " + t.ToString () + " " + name);
if (t == null)
{
Log.Error("Type missing for " + name);
fail = true;
return null;
}
foreach (var methodRef in t.Resolve().Methods)
{
if (methodRef.Name == name)
{
return scriptDef.MainModule.ImportReference(methodRef);
}
}
Log.Error("ResolveMethod failed " + t.Name + "::" + name + " " + t.Resolve());
// why did it fail!?
foreach (var methodRef in t.Resolve().Methods)
{
Log.Error("Method " + methodRef.Name);
}
fail = true;
return null;
}
static MethodReference ResolveMethodWithArg(TypeReference t, string name, TypeReference argType) static MethodReference ResolveMethodWithArg(TypeReference t, string name, TypeReference argType)
{ {
foreach (var methodRef in t.Resolve().Methods) foreach (var methodRef in t.Resolve().Methods)
@ -1257,34 +1229,34 @@ static void SetupTargetTypes()
NetworkReaderType = m_UNetAssemblyDefinition.MainModule.GetType("Mirror.NetworkReader"); NetworkReaderType = m_UNetAssemblyDefinition.MainModule.GetType("Mirror.NetworkReader");
NetworkReaderDef = NetworkReaderType.Resolve(); NetworkReaderDef = NetworkReaderType.Resolve();
NetworkReaderCtor = ResolveMethod(NetworkReaderDef, ".ctor"); NetworkReaderCtor = Resolvers.ResolveMethod(NetworkReaderDef, scriptDef, ".ctor");
NetworkWriterType = m_UNetAssemblyDefinition.MainModule.GetType("Mirror.NetworkWriter"); NetworkWriterType = m_UNetAssemblyDefinition.MainModule.GetType("Mirror.NetworkWriter");
NetworkWriterDef = NetworkWriterType.Resolve(); NetworkWriterDef = NetworkWriterType.Resolve();
NetworkWriterCtor = ResolveMethod(NetworkWriterDef, ".ctor"); NetworkWriterCtor = Resolvers.ResolveMethod(NetworkWriterDef, scriptDef, ".ctor");
NetworkServerGetActive = ResolveMethod(NetworkServerType, "get_active"); NetworkServerGetActive = Resolvers.ResolveMethod(NetworkServerType, scriptDef, "get_active");
NetworkServerGetLocalClientActive = ResolveMethod(NetworkServerType, "get_localClientActive"); NetworkServerGetLocalClientActive = Resolvers.ResolveMethod(NetworkServerType, scriptDef, "get_localClientActive");
NetworkClientGetActive = ResolveMethod(NetworkClientType, "get_active"); NetworkClientGetActive = Resolvers.ResolveMethod(NetworkClientType, scriptDef, "get_active");
NetworkReaderReadInt32 = ResolveMethod(NetworkReaderType, "ReadInt32"); NetworkReaderReadInt32 = Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadInt32");
NetworkWriterWriteInt32 = ResolveMethodWithArg(NetworkWriterType, "Write", int32Type); NetworkWriterWriteInt32 = ResolveMethodWithArg(NetworkWriterType, "Write", int32Type);
NetworkWriterWriteInt16 = ResolveMethodWithArg(NetworkWriterType, "Write", int16Type); NetworkWriterWriteInt16 = ResolveMethodWithArg(NetworkWriterType, "Write", int16Type);
NetworkReaderReadPacked32 = ResolveMethod(NetworkReaderType, "ReadPackedUInt32"); NetworkReaderReadPacked32 = Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadPackedUInt32");
NetworkReaderReadPacked64 = ResolveMethod(NetworkReaderType, "ReadPackedUInt64"); NetworkReaderReadPacked64 = Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadPackedUInt64");
NetworkReaderReadByte = ResolveMethod(NetworkReaderType, "ReadByte"); NetworkReaderReadByte = Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadByte");
NetworkWriterWritePacked32 = ResolveMethod(NetworkWriterType, "WritePackedUInt32"); NetworkWriterWritePacked32 = Resolvers.ResolveMethod(NetworkWriterType, scriptDef, "WritePackedUInt32");
NetworkWriterWritePacked64 = ResolveMethod(NetworkWriterType, "WritePackedUInt64"); NetworkWriterWritePacked64 = Resolvers.ResolveMethod(NetworkWriterType, scriptDef, "WritePackedUInt64");
NetworkReadUInt16 = ResolveMethod(NetworkReaderType, "ReadUInt16"); NetworkReadUInt16 = Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadUInt16");
NetworkWriteUInt16 = ResolveMethodWithArg(NetworkWriterType, "Write", uint16Type); NetworkWriteUInt16 = ResolveMethodWithArg(NetworkWriterType, "Write", uint16Type);
CmdDelegateReference = m_UNetAssemblyDefinition.MainModule.GetType("Mirror.NetworkBehaviour/CmdDelegate"); CmdDelegateReference = m_UNetAssemblyDefinition.MainModule.GetType("Mirror.NetworkBehaviour/CmdDelegate");
CmdDelegateConstructor = ResolveMethod(CmdDelegateReference, ".ctor"); CmdDelegateConstructor = Resolvers.ResolveMethod(CmdDelegateReference, scriptDef, ".ctor");
scriptDef.MainModule.ImportReference(gameObjectType); scriptDef.MainModule.ImportReference(gameObjectType);
scriptDef.MainModule.ImportReference(transformType); scriptDef.MainModule.ImportReference(transformType);
@ -1314,46 +1286,46 @@ static void SetupTargetTypes()
ComponentType = m_UnityAssemblyDefinition.MainModule.GetType("UnityEngine.Component"); ComponentType = m_UnityAssemblyDefinition.MainModule.GetType("UnityEngine.Component");
ClientSceneType = m_UNetAssemblyDefinition.MainModule.GetType("Mirror.ClientScene"); ClientSceneType = m_UNetAssemblyDefinition.MainModule.GetType("Mirror.ClientScene");
FindLocalObjectReference = ResolveMethod(ClientSceneType, "FindLocalObject"); FindLocalObjectReference = Resolvers.ResolveMethod(ClientSceneType, scriptDef, "FindLocalObject");
ReadyConnectionReference = ResolveMethod(ClientSceneType, "get_readyConnection"); ReadyConnectionReference = Resolvers.ResolveMethod(ClientSceneType, scriptDef, "get_readyConnection");
// get specialized GetComponent<NetworkIdentity>() // get specialized GetComponent<NetworkIdentity>()
getComponentReference = ResolveMethodGeneric(ComponentType, "GetComponent", NetworkIdentityType); getComponentReference = ResolveMethodGeneric(ComponentType, "GetComponent", NetworkIdentityType);
getUNetIdReference = ResolveMethod(unetViewTmp, "get_netId"); getUNetIdReference = Resolvers.ResolveMethod(unetViewTmp, scriptDef, "get_netId");
gameObjectInequality = ResolveMethod(unityObjectType, "op_Inequality"); gameObjectInequality = Resolvers.ResolveMethod(unityObjectType, scriptDef, "op_Inequality");
UBehaviourIsServer = ResolveMethod(NetworkBehaviourType, "get_isServer"); UBehaviourIsServer = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "get_isServer");
setSyncVarReference = ResolveMethod(NetworkBehaviourType, "SetSyncVar"); setSyncVarReference = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "SetSyncVar");
setSyncVarHookGuard = ResolveMethod(NetworkBehaviourType, "set_syncVarHookGuard"); setSyncVarHookGuard = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "set_syncVarHookGuard");
getSyncVarHookGuard = ResolveMethod(NetworkBehaviourType, "get_syncVarHookGuard"); getSyncVarHookGuard = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "get_syncVarHookGuard");
setSyncVarGameObjectReference = ResolveMethod(NetworkBehaviourType, "SetSyncVarGameObject"); setSyncVarGameObjectReference = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "SetSyncVarGameObject");
registerCommandDelegateReference = ResolveMethod(NetworkBehaviourType, "RegisterCommandDelegate"); registerCommandDelegateReference = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "RegisterCommandDelegate");
registerRpcDelegateReference = ResolveMethod(NetworkBehaviourType, "RegisterRpcDelegate"); registerRpcDelegateReference = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "RegisterRpcDelegate");
registerEventDelegateReference = ResolveMethod(NetworkBehaviourType, "RegisterEventDelegate"); registerEventDelegateReference = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "RegisterEventDelegate");
getTypeReference = ResolveMethod(objectType, "GetType"); getTypeReference = Resolvers.ResolveMethod(objectType, scriptDef, "GetType");
getTypeFromHandleReference = ResolveMethod(typeType, "GetTypeFromHandle"); getTypeFromHandleReference = Resolvers.ResolveMethod(typeType, scriptDef, "GetTypeFromHandle");
logErrorReference = ResolveMethod(m_UnityAssemblyDefinition.MainModule.GetType("UnityEngine.Debug"), "LogError"); logErrorReference = Resolvers.ResolveMethod(m_UnityAssemblyDefinition.MainModule.GetType("UnityEngine.Debug"), scriptDef, "LogError");
logWarningReference = ResolveMethod(m_UnityAssemblyDefinition.MainModule.GetType("UnityEngine.Debug"), "LogWarning"); logWarningReference = Resolvers.ResolveMethod(m_UnityAssemblyDefinition.MainModule.GetType("UnityEngine.Debug"), scriptDef, "LogWarning");
sendCommandInternal = ResolveMethod(NetworkBehaviourType, "SendCommandInternal"); sendCommandInternal = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "SendCommandInternal");
sendRpcInternal = ResolveMethod(NetworkBehaviourType, "SendRPCInternal"); sendRpcInternal = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "SendRPCInternal");
sendTargetRpcInternal = ResolveMethod(NetworkBehaviourType, "SendTargetRPCInternal"); sendTargetRpcInternal = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "SendTargetRPCInternal");
sendEventInternal = ResolveMethod(NetworkBehaviourType, "SendEventInternal"); sendEventInternal = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "SendEventInternal");
SyncObjectType = scriptDef.MainModule.ImportReference(SyncObjectType); SyncObjectType = scriptDef.MainModule.ImportReference(SyncObjectType);
InitSyncObjectReference = ResolveMethod(NetworkBehaviourType, "InitSyncObject"); InitSyncObjectReference = Resolvers.ResolveMethod(NetworkBehaviourType, scriptDef, "InitSyncObject");
} }
static void SetupReadFunctions() static void SetupReadFunctions()
{ {
lists.readFuncs = new Dictionary<string, MethodReference> lists.readFuncs = new Dictionary<string, MethodReference>
{ {
{ singleType.FullName, ResolveMethod(NetworkReaderType, "ReadSingle") }, { singleType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadSingle") },
{ doubleType.FullName, ResolveMethod(NetworkReaderType, "ReadDouble") }, { doubleType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadDouble") },
{ boolType.FullName, ResolveMethod(NetworkReaderType, "ReadBoolean") }, { boolType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadBoolean") },
{ stringType.FullName, ResolveMethod(NetworkReaderType, "ReadString") }, { stringType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadString") },
{ int64Type.FullName, NetworkReaderReadPacked64 }, { int64Type.FullName, NetworkReaderReadPacked64 },
{ uint64Type.FullName, NetworkReaderReadPacked64 }, { uint64Type.FullName, NetworkReaderReadPacked64 },
{ int32Type.FullName, NetworkReaderReadPacked32 }, { int32Type.FullName, NetworkReaderReadPacked32 },
@ -1363,22 +1335,22 @@ static void SetupReadFunctions()
{ byteType.FullName, NetworkReaderReadPacked32 }, { byteType.FullName, NetworkReaderReadPacked32 },
{ sbyteType.FullName, NetworkReaderReadPacked32 }, { sbyteType.FullName, NetworkReaderReadPacked32 },
{ charType.FullName, NetworkReaderReadPacked32 }, { charType.FullName, NetworkReaderReadPacked32 },
{ decimalType.FullName, ResolveMethod(NetworkReaderType, "ReadDecimal") }, { decimalType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadDecimal") },
{ vector2Type.FullName, ResolveMethod(NetworkReaderType, "ReadVector2") }, { vector2Type.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadVector2") },
{ vector3Type.FullName, ResolveMethod(NetworkReaderType, "ReadVector3") }, { vector3Type.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadVector3") },
{ vector4Type.FullName, ResolveMethod(NetworkReaderType, "ReadVector4") }, { vector4Type.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadVector4") },
{ colorType.FullName, ResolveMethod(NetworkReaderType, "ReadColor") }, { colorType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadColor") },
{ color32Type.FullName, ResolveMethod(NetworkReaderType, "ReadColor32") }, { color32Type.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadColor32") },
{ quaternionType.FullName, ResolveMethod(NetworkReaderType, "ReadQuaternion") }, { quaternionType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadQuaternion") },
{ rectType.FullName, ResolveMethod(NetworkReaderType, "ReadRect") }, { rectType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadRect") },
{ planeType.FullName, ResolveMethod(NetworkReaderType, "ReadPlane") }, { planeType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadPlane") },
{ rayType.FullName, ResolveMethod(NetworkReaderType, "ReadRay") }, { rayType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadRay") },
{ matrixType.FullName, ResolveMethod(NetworkReaderType, "ReadMatrix4x4") }, { matrixType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadMatrix4x4") },
{ guidType.FullName, ResolveMethod(NetworkReaderType, "ReadGuid") }, { guidType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadGuid") },
{ gameObjectType.FullName, ResolveMethod(NetworkReaderType, "ReadGameObject") }, { gameObjectType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadGameObject") },
{ NetworkIdentityType.FullName, ResolveMethod(NetworkReaderType, "ReadNetworkIdentity") }, { NetworkIdentityType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadNetworkIdentity") },
{ transformType.FullName, ResolveMethod(NetworkReaderType, "ReadTransform") }, { transformType.FullName, Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadTransform") },
{ "System.Byte[]", ResolveMethod(NetworkReaderType, "ReadBytesAndSize") }, { "System.Byte[]", Resolvers.ResolveMethod(NetworkReaderType, scriptDef, "ReadBytesAndSize") },
}; };
} }