update mirror

This commit is contained in:
JohnCorby 2022-01-22 17:08:29 -08:00
parent 965fe056c5
commit c131228ca1
11 changed files with 38 additions and 58 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -13,7 +13,7 @@ namespace Mirror.Weaver
NetworkWriter networkWriter = new NetworkWriter(); NetworkWriter networkWriter = new NetworkWriter();
networkWriter.Write(thrusting); networkWriter.Write(thrusting);
networkWriter.WritePackedUInt32((uint)spin); networkWriter.WritePackedUInt32((uint)spin);
base.SendCommandInternal(cmdName, networkWriter, cmdName); base.SendCommandInternal(cmdName, networkWriter, channel);
} }
public void CallCmdThrust(float thrusting, int spin) public void CallCmdThrust(float thrusting, int spin)
@ -44,17 +44,14 @@ namespace Mirror.Weaver
if (!NetworkBehaviourProcessor.WriteArguments(worker, writers, Log, md, RemoteCallType.Command, ref WeavingFailed)) if (!NetworkBehaviourProcessor.WriteArguments(worker, writers, Log, md, RemoteCallType.Command, ref WeavingFailed))
return null; return null;
string cmdName = md.Name;
int channel = commandAttr.GetField("channel", 0); int channel = commandAttr.GetField("channel", 0);
bool requiresAuthority = commandAttr.GetField("requiresAuthority", true); bool requiresAuthority = commandAttr.GetField("requiresAuthority", true);
// invoke internal send and return // invoke internal send and return
// load 'base.' to call the SendCommand function with // load 'base.' to call the SendCommand function with
worker.Emit(OpCodes.Ldarg_0); worker.Emit(OpCodes.Ldarg_0);
worker.Emit(OpCodes.Ldtoken, td); // pass full function name to avoid ClassA.Func <-> ClassB.Func collisions
// invokerClass worker.Emit(OpCodes.Ldstr, md.FullName);
worker.Emit(OpCodes.Call, weaverTypes.getTypeFromHandleReference);
worker.Emit(OpCodes.Ldstr, cmdName);
// writer // writer
worker.Emit(OpCodes.Ldloc_0); worker.Emit(OpCodes.Ldloc_0);
worker.Emit(OpCodes.Ldc_I4, channel); worker.Emit(OpCodes.Ldc_I4, channel);

View File

@ -292,20 +292,20 @@ namespace Mirror.Weaver
for (int i = 0; i < commands.Count; ++i) for (int i = 0; i < commands.Count; ++i)
{ {
CmdResult cmdResult = commands[i]; CmdResult cmdResult = commands[i];
GenerateRegisterCommandDelegate(cctorWorker, weaverTypes.registerCommandDelegateReference, commandInvocationFuncs[i], cmdResult); GenerateRegisterCommandDelegate(cctorWorker, weaverTypes.registerCommandReference, commandInvocationFuncs[i], cmdResult);
} }
// register all client rpcs in cctor // register all client rpcs in cctor
for (int i = 0; i < clientRpcs.Count; ++i) for (int i = 0; i < clientRpcs.Count; ++i)
{ {
ClientRpcResult clientRpcResult = clientRpcs[i]; ClientRpcResult clientRpcResult = clientRpcs[i];
GenerateRegisterRemoteDelegate(cctorWorker, weaverTypes.registerRpcDelegateReference, clientRpcInvocationFuncs[i], clientRpcResult.method.Name); GenerateRegisterRemoteDelegate(cctorWorker, weaverTypes.registerRpcReference, clientRpcInvocationFuncs[i], clientRpcResult.method.FullName);
} }
// register all target rpcs in cctor // register all target rpcs in cctor
for (int i = 0; i < targetRpcs.Count; ++i) for (int i = 0; i < targetRpcs.Count; ++i)
{ {
GenerateRegisterRemoteDelegate(cctorWorker, weaverTypes.registerRpcDelegateReference, targetRpcInvocationFuncs[i], targetRpcs[i].Name); GenerateRegisterRemoteDelegate(cctorWorker, weaverTypes.registerRpcReference, targetRpcInvocationFuncs[i], targetRpcs[i].FullName);
} }
// add final 'Ret' instruction to cctor // add final 'Ret' instruction to cctor
@ -358,22 +358,25 @@ namespace Mirror.Weaver
// This generates code like: // This generates code like:
NetworkBehaviour.RegisterCommandDelegate(base.GetType(), "CmdThrust", new NetworkBehaviour.CmdDelegate(ShipControl.InvokeCmdCmdThrust)); NetworkBehaviour.RegisterCommandDelegate(base.GetType(), "CmdThrust", new NetworkBehaviour.CmdDelegate(ShipControl.InvokeCmdCmdThrust));
*/ */
void GenerateRegisterRemoteDelegate(ILProcessor worker, MethodReference registerMethod, MethodDefinition func, string cmdName)
// pass full function name to avoid ClassA.Func <-> ClassB.Func collisions
void GenerateRegisterRemoteDelegate(ILProcessor worker, MethodReference registerMethod, MethodDefinition func, string functionFullName)
{ {
worker.Emit(OpCodes.Ldtoken, netBehaviourSubclass); worker.Emit(OpCodes.Ldtoken, netBehaviourSubclass);
worker.Emit(OpCodes.Call, weaverTypes.getTypeFromHandleReference); worker.Emit(OpCodes.Call, weaverTypes.getTypeFromHandleReference);
worker.Emit(OpCodes.Ldstr, cmdName); worker.Emit(OpCodes.Ldstr, functionFullName);
worker.Emit(OpCodes.Ldnull); worker.Emit(OpCodes.Ldnull);
worker.Emit(OpCodes.Ldftn, func); worker.Emit(OpCodes.Ldftn, func);
worker.Emit(OpCodes.Newobj, weaverTypes.CmdDelegateConstructor); worker.Emit(OpCodes.Newobj, weaverTypes.RemoteCallDelegateConstructor);
// //
worker.Emit(OpCodes.Call, registerMethod); worker.Emit(OpCodes.Call, registerMethod);
} }
void GenerateRegisterCommandDelegate(ILProcessor worker, MethodReference registerMethod, MethodDefinition func, CmdResult cmdResult) void GenerateRegisterCommandDelegate(ILProcessor worker, MethodReference registerMethod, MethodDefinition func, CmdResult cmdResult)
{ {
string cmdName = cmdResult.method.Name; // pass full function name to avoid ClassA.Func <-> ClassB.Func collisions
string cmdName = cmdResult.method.FullName;
bool requiresAuthority = cmdResult.requiresAuthority; bool requiresAuthority = cmdResult.requiresAuthority;
worker.Emit(OpCodes.Ldtoken, netBehaviourSubclass); worker.Emit(OpCodes.Ldtoken, netBehaviourSubclass);
@ -382,7 +385,7 @@ namespace Mirror.Weaver
worker.Emit(OpCodes.Ldnull); worker.Emit(OpCodes.Ldnull);
worker.Emit(OpCodes.Ldftn, func); worker.Emit(OpCodes.Ldftn, func);
worker.Emit(OpCodes.Newobj, weaverTypes.CmdDelegateConstructor); worker.Emit(OpCodes.Newobj, weaverTypes.RemoteCallDelegateConstructor);
// requiresAuthority ? 1 : 0 // requiresAuthority ? 1 : 0
worker.Emit(requiresAuthority ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0); worker.Emit(requiresAuthority ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
@ -1043,7 +1046,6 @@ namespace Mirror.Weaver
return false; return false;
} }
// if not SenderConnection And not TargetRpc NetworkConnection first param // if not SenderConnection And not TargetRpc NetworkConnection first param
if (!isSenderConnection && isNetworkConnection && !(callType == RemoteCallType.TargetRpc && firstParam)) if (!isSenderConnection && isNetworkConnection && !(callType == RemoteCallType.TargetRpc && firstParam))
{ {
@ -1129,13 +1131,6 @@ namespace Mirror.Weaver
return; return;
} }
if (names.Contains(md.Name))
{
Log.Error($"Duplicate ClientRpc name {md.Name}", md);
WeavingFailed = true;
return;
}
bool includeOwner = clientRpcAttr.GetField("includeOwner", true); bool includeOwner = clientRpcAttr.GetField("includeOwner", true);
names.Add(md.Name); names.Add(md.Name);
@ -1168,12 +1163,6 @@ namespace Mirror.Weaver
if (!ValidateRemoteCallAndParameters(md, RemoteCallType.TargetRpc, ref WeavingFailed)) if (!ValidateRemoteCallAndParameters(md, RemoteCallType.TargetRpc, ref WeavingFailed))
return; return;
if (names.Contains(md.Name))
{
Log.Error($"Duplicate Target Rpc name {md.Name}", md);
WeavingFailed = true;
return;
}
names.Add(md.Name); names.Add(md.Name);
targetRpcs.Add(md); targetRpcs.Add(md);
@ -1198,13 +1187,6 @@ namespace Mirror.Weaver
if (!ValidateRemoteCallAndParameters(md, RemoteCallType.Command, ref WeavingFailed)) if (!ValidateRemoteCallAndParameters(md, RemoteCallType.Command, ref WeavingFailed))
return; return;
if (names.Contains(md.Name))
{
Log.Error($"Duplicate Command name {md.Name}", md);
WeavingFailed = true;
return;
}
bool requiresAuthority = commandAttr.GetField("requiresAuthority", true); bool requiresAuthority = commandAttr.GetField("requiresAuthority", true);
names.Add(md.Name); names.Add(md.Name);

View File

@ -74,17 +74,14 @@ namespace Mirror.Weaver
if (!NetworkBehaviourProcessor.WriteArguments(worker, writers, Log, md, RemoteCallType.ClientRpc, ref WeavingFailed)) if (!NetworkBehaviourProcessor.WriteArguments(worker, writers, Log, md, RemoteCallType.ClientRpc, ref WeavingFailed))
return null; return null;
string rpcName = md.Name;
int channel = clientRpcAttr.GetField("channel", 0); int channel = clientRpcAttr.GetField("channel", 0);
bool includeOwner = clientRpcAttr.GetField("includeOwner", true); bool includeOwner = clientRpcAttr.GetField("includeOwner", true);
// invoke SendInternal and return // invoke SendInternal and return
// this // this
worker.Emit(OpCodes.Ldarg_0); worker.Emit(OpCodes.Ldarg_0);
worker.Emit(OpCodes.Ldtoken, td); // pass full function name to avoid ClassA.Func <-> ClassB.Func collisions
// invokerClass worker.Emit(OpCodes.Ldstr, md.FullName);
worker.Emit(OpCodes.Call, weaverTypes.getTypeFromHandleReference);
worker.Emit(OpCodes.Ldstr, rpcName);
// writer // writer
worker.Emit(OpCodes.Ldloc_0); worker.Emit(OpCodes.Ldloc_0);
worker.Emit(OpCodes.Ldc_I4, channel); worker.Emit(OpCodes.Ldc_I4, channel);

View File

@ -105,8 +105,6 @@ namespace Mirror.Weaver
if (!NetworkBehaviourProcessor.WriteArguments(worker, writers, Log, md, RemoteCallType.TargetRpc, ref WeavingFailed)) if (!NetworkBehaviourProcessor.WriteArguments(worker, writers, Log, md, RemoteCallType.TargetRpc, ref WeavingFailed))
return null; return null;
string rpcName = md.Name;
// invoke SendInternal and return // invoke SendInternal and return
// this // this
worker.Emit(OpCodes.Ldarg_0); worker.Emit(OpCodes.Ldarg_0);
@ -120,10 +118,8 @@ namespace Mirror.Weaver
// null // null
worker.Emit(OpCodes.Ldnull); worker.Emit(OpCodes.Ldnull);
} }
worker.Emit(OpCodes.Ldtoken, td); // pass full function name to avoid ClassA.Func <-> ClassB.Func collisions
// invokerClass worker.Emit(OpCodes.Ldstr, md.FullName);
worker.Emit(OpCodes.Call, weaverTypes.getTypeFromHandleReference);
worker.Emit(OpCodes.Ldstr, rpcName);
// writer // writer
worker.Emit(OpCodes.Ldloc_0); worker.Emit(OpCodes.Ldloc_0);
worker.Emit(OpCodes.Ldc_I4, targetRpcAttr.GetField("channel", 0)); worker.Emit(OpCodes.Ldc_I4, targetRpcAttr.GetField("channel", 0));

View File

@ -21,7 +21,6 @@ namespace Mirror.Weaver
WeaverTypes weaverTypes; WeaverTypes weaverTypes;
SyncVarAccessLists syncVarAccessLists; SyncVarAccessLists syncVarAccessLists;
IAssemblyResolver Resolver;
AssemblyDefinition CurrentAssembly; AssemblyDefinition CurrentAssembly;
Writers writers; Writers writers;
Readers readers; Readers readers;
@ -138,7 +137,6 @@ namespace Mirror.Weaver
modified = false; modified = false;
try try
{ {
Resolver = resolver;
CurrentAssembly = assembly; CurrentAssembly = assembly;
// fix "No writer found for ..." error // fix "No writer found for ..." error

View File

@ -1,5 +1,6 @@
using System; using System;
using Mono.Cecil; using Mono.Cecil;
using UnityEditor;
using UnityEngine; using UnityEngine;
namespace Mirror.Weaver namespace Mirror.Weaver
@ -15,7 +16,7 @@ namespace Mirror.Weaver
public MethodReference NetworkClientConnectionReference; public MethodReference NetworkClientConnectionReference;
public MethodReference CmdDelegateConstructor; public MethodReference RemoteCallDelegateConstructor;
public MethodReference NetworkServerGetActive; public MethodReference NetworkServerGetActive;
public MethodReference NetworkServerGetLocalClientActive; public MethodReference NetworkServerGetLocalClientActive;
@ -41,8 +42,8 @@ namespace Mirror.Weaver
public MethodReference syncVarNetworkBehaviourEqualReference; public MethodReference syncVarNetworkBehaviourEqualReference;
public MethodReference setSyncVarNetworkBehaviourReference; public MethodReference setSyncVarNetworkBehaviourReference;
public MethodReference getSyncVarNetworkBehaviourReference; public MethodReference getSyncVarNetworkBehaviourReference;
public MethodReference registerCommandDelegateReference; public MethodReference registerCommandReference;
public MethodReference registerRpcDelegateReference; public MethodReference registerRpcReference;
public MethodReference getTypeFromHandleReference; public MethodReference getTypeFromHandleReference;
public MethodReference logErrorReference; public MethodReference logErrorReference;
public MethodReference logWarningReference; public MethodReference logWarningReference;
@ -77,11 +78,11 @@ namespace Mirror.Weaver
TypeReference NetworkClientType = Import(typeof(NetworkClient)); TypeReference NetworkClientType = Import(typeof(NetworkClient));
NetworkClientGetActive = Resolvers.ResolveMethod(NetworkClientType, assembly, Log, "get_active", ref WeavingFailed); NetworkClientGetActive = Resolvers.ResolveMethod(NetworkClientType, assembly, Log, "get_active", ref WeavingFailed);
TypeReference cmdDelegateReference = Import<RemoteCalls.CmdDelegate>(); TypeReference RemoteCallDelegateType = Import<RemoteCalls.RemoteCallDelegate>();
CmdDelegateConstructor = Resolvers.ResolveMethod(cmdDelegateReference, assembly, Log, ".ctor", ref WeavingFailed); RemoteCallDelegateConstructor = Resolvers.ResolveMethod(RemoteCallDelegateType, assembly, Log, ".ctor", ref WeavingFailed);
TypeReference NetworkBehaviourType = Import<NetworkBehaviour>(); TypeReference NetworkBehaviourType = Import<NetworkBehaviour>();
TypeReference RemoteCallHelperType = Import(typeof(RemoteCalls.RemoteCallHelper)); TypeReference RemoteProcedureCallsType = Import(typeof(RemoteCalls.RemoteProcedureCalls));
TypeReference ScriptableObjectType = Import<UnityEngine.ScriptableObject>(); TypeReference ScriptableObjectType = Import<UnityEngine.ScriptableObject>();
@ -112,8 +113,8 @@ namespace Mirror.Weaver
setSyncVarNetworkBehaviourReference = Resolvers.ResolveMethod(NetworkBehaviourType, assembly, Log, "SetSyncVarNetworkBehaviour", ref WeavingFailed); setSyncVarNetworkBehaviourReference = Resolvers.ResolveMethod(NetworkBehaviourType, assembly, Log, "SetSyncVarNetworkBehaviour", ref WeavingFailed);
getSyncVarNetworkBehaviourReference = Resolvers.ResolveMethod(NetworkBehaviourType, assembly, Log, "GetSyncVarNetworkBehaviour", ref WeavingFailed); getSyncVarNetworkBehaviourReference = Resolvers.ResolveMethod(NetworkBehaviourType, assembly, Log, "GetSyncVarNetworkBehaviour", ref WeavingFailed);
registerCommandDelegateReference = Resolvers.ResolveMethod(RemoteCallHelperType, assembly, Log, "RegisterCommandDelegate", ref WeavingFailed); registerCommandReference = Resolvers.ResolveMethod(RemoteProcedureCallsType, assembly, Log, "RegisterCommand", ref WeavingFailed);
registerRpcDelegateReference = Resolvers.ResolveMethod(RemoteCallHelperType, assembly, Log, "RegisterRpcDelegate", ref WeavingFailed); registerRpcReference = Resolvers.ResolveMethod(RemoteProcedureCallsType, assembly, Log, "RegisterRpc", ref WeavingFailed);
TypeReference unityDebug = Import(typeof(UnityEngine.Debug)); TypeReference unityDebug = Import(typeof(UnityEngine.Debug));
// these have multiple methods with same name, so need to check parameters too // these have multiple methods with same name, so need to check parameters too
@ -145,6 +146,17 @@ namespace Mirror.Weaver
}), }),
ref WeavingFailed); ref WeavingFailed);
/*
// [InitializeOnLoadMethod]
// 'UnityEditor' is not available in builds.
// we can only import this attribute if we are in an Editor assembly.
if (Helpers.IsEditorAssembly(assembly))
{
TypeReference initializeOnLoadMethodAttributeRef = Import(typeof(InitializeOnLoadMethodAttribute));
initializeOnLoadMethodAttribute = initializeOnLoadMethodAttributeRef.Resolve();
}
*/
// [RuntimeInitializeOnLoadMethod] // [RuntimeInitializeOnLoadMethod]
TypeReference runtimeInitializeOnLoadMethodAttributeRef = Import(typeof(RuntimeInitializeOnLoadMethodAttribute)); TypeReference runtimeInitializeOnLoadMethodAttributeRef = Import(typeof(RuntimeInitializeOnLoadMethodAttribute));
runtimeInitializeOnLoadMethodAttribute = runtimeInitializeOnLoadMethodAttributeRef.Resolve(); runtimeInitializeOnLoadMethodAttribute = runtimeInitializeOnLoadMethodAttributeRef.Resolve();

View File

@ -250,7 +250,6 @@ namespace Mirror.Weaver
// Find all fields in type and write them // Find all fields in type and write them
bool WriteAllFields(TypeReference variable, ILProcessor worker, ref bool WeavingFailed) bool WriteAllFields(TypeReference variable, ILProcessor worker, ref bool WeavingFailed)
{ {
uint fields = 0;
foreach (FieldDefinition field in variable.FindAllPublicFields()) foreach (FieldDefinition field in variable.FindAllPublicFields())
{ {
MethodReference writeFunc = GetWriteFunc(field.FieldType, ref WeavingFailed); MethodReference writeFunc = GetWriteFunc(field.FieldType, ref WeavingFailed);
@ -259,7 +258,6 @@ namespace Mirror.Weaver
FieldReference fieldRef = assembly.MainModule.ImportReference(field); FieldReference fieldRef = assembly.MainModule.ImportReference(field);
fields++;
worker.Emit(OpCodes.Ldarg_0); worker.Emit(OpCodes.Ldarg_0);
worker.Emit(OpCodes.Ldarg_1); worker.Emit(OpCodes.Ldarg_1);
worker.Emit(OpCodes.Ldfld, fieldRef); worker.Emit(OpCodes.Ldfld, fieldRef);