ServerClientAttributeProcessor: remove static weaverTypes reference

This commit is contained in:
vis2k 2021-08-18 23:03:42 +08:00
parent 3f9c40cafd
commit c89cbd02c5
2 changed files with 17 additions and 17 deletions

View File

@ -6,22 +6,22 @@ namespace Mirror.Weaver
{ {
static class ServerClientAttributeProcessor static class ServerClientAttributeProcessor
{ {
public static bool Process(TypeDefinition td) public static bool Process(WeaverTypes weaverTypes, TypeDefinition td)
{ {
bool modified = false; bool modified = false;
foreach (MethodDefinition md in td.Methods) foreach (MethodDefinition md in td.Methods)
{ {
modified |= ProcessSiteMethod(md); modified |= ProcessSiteMethod(weaverTypes, md);
} }
foreach (TypeDefinition nested in td.NestedTypes) foreach (TypeDefinition nested in td.NestedTypes)
{ {
modified |= Process(nested); modified |= Process(weaverTypes, nested);
} }
return modified; return modified;
} }
static bool ProcessSiteMethod(MethodDefinition md) static bool ProcessSiteMethod(WeaverTypes weaverTypes, MethodDefinition md)
{ {
if (md.Name == ".cctor" || if (md.Name == ".cctor" ||
md.Name == NetworkBehaviourProcessor.ProcessedFunctionName || md.Name == NetworkBehaviourProcessor.ProcessedFunctionName ||
@ -40,7 +40,7 @@ static bool ProcessSiteMethod(MethodDefinition md)
if (md.Body != null && md.Body.Instructions != null) if (md.Body != null && md.Body.Instructions != null)
{ {
return ProcessMethodAttributes(md); return ProcessMethodAttributes(weaverTypes, md);
} }
return false; return false;
} }
@ -63,50 +63,50 @@ public static bool HasServerClientAttribute(MethodDefinition md)
return false; return false;
} }
public static bool ProcessMethodAttributes(MethodDefinition md) public static bool ProcessMethodAttributes(WeaverTypes weaverTypes, MethodDefinition md)
{ {
if (md.HasCustomAttribute<ServerAttribute>()) if (md.HasCustomAttribute<ServerAttribute>())
InjectServerGuard(md, true); InjectServerGuard(weaverTypes, md, true);
else if (md.HasCustomAttribute<ServerCallbackAttribute>()) else if (md.HasCustomAttribute<ServerCallbackAttribute>())
InjectServerGuard(md, false); InjectServerGuard(weaverTypes, md, false);
else if (md.HasCustomAttribute<ClientAttribute>()) else if (md.HasCustomAttribute<ClientAttribute>())
InjectClientGuard(md, true); InjectClientGuard(weaverTypes, md, true);
else if (md.HasCustomAttribute<ClientCallbackAttribute>()) else if (md.HasCustomAttribute<ClientCallbackAttribute>())
InjectClientGuard(md, false); InjectClientGuard(weaverTypes, md, false);
else else
return false; return false;
return true; return true;
} }
static void InjectServerGuard(MethodDefinition md, bool logWarning) static void InjectServerGuard(WeaverTypes weaverTypes, MethodDefinition md, bool logWarning)
{ {
ILProcessor worker = md.Body.GetILProcessor(); ILProcessor worker = md.Body.GetILProcessor();
Instruction top = md.Body.Instructions[0]; Instruction top = md.Body.Instructions[0];
worker.InsertBefore(top, worker.Create(OpCodes.Call, Weaver.weaverTypes.NetworkServerGetActive)); worker.InsertBefore(top, worker.Create(OpCodes.Call, weaverTypes.NetworkServerGetActive));
worker.InsertBefore(top, worker.Create(OpCodes.Brtrue, top)); worker.InsertBefore(top, worker.Create(OpCodes.Brtrue, top));
if (logWarning) if (logWarning)
{ {
worker.InsertBefore(top, worker.Create(OpCodes.Ldstr, $"[Server] function '{md.FullName}' called when server was not active")); worker.InsertBefore(top, worker.Create(OpCodes.Ldstr, $"[Server] function '{md.FullName}' called when server was not active"));
worker.InsertBefore(top, worker.Create(OpCodes.Call, Weaver.weaverTypes.logWarningReference)); worker.InsertBefore(top, worker.Create(OpCodes.Call, weaverTypes.logWarningReference));
} }
InjectGuardParameters(md, worker, top); InjectGuardParameters(md, worker, top);
InjectGuardReturnValue(md, worker, top); InjectGuardReturnValue(md, worker, top);
worker.InsertBefore(top, worker.Create(OpCodes.Ret)); worker.InsertBefore(top, worker.Create(OpCodes.Ret));
} }
static void InjectClientGuard(MethodDefinition md, bool logWarning) static void InjectClientGuard(WeaverTypes weaverTypes, MethodDefinition md, bool logWarning)
{ {
ILProcessor worker = md.Body.GetILProcessor(); ILProcessor worker = md.Body.GetILProcessor();
Instruction top = md.Body.Instructions[0]; Instruction top = md.Body.Instructions[0];
worker.InsertBefore(top, worker.Create(OpCodes.Call, Weaver.weaverTypes.NetworkClientGetActive)); worker.InsertBefore(top, worker.Create(OpCodes.Call, weaverTypes.NetworkClientGetActive));
worker.InsertBefore(top, worker.Create(OpCodes.Brtrue, top)); worker.InsertBefore(top, worker.Create(OpCodes.Brtrue, top));
if (logWarning) if (logWarning)
{ {
worker.InsertBefore(top, worker.Create(OpCodes.Ldstr, $"[Client] function '{md.FullName}' called when client was not active")); worker.InsertBefore(top, worker.Create(OpCodes.Ldstr, $"[Client] function '{md.FullName}' called when client was not active"));
worker.InsertBefore(top, worker.Create(OpCodes.Call, Weaver.weaverTypes.logWarningReference)); worker.InsertBefore(top, worker.Create(OpCodes.Call, weaverTypes.logWarningReference));
} }
InjectGuardParameters(md, worker, top); InjectGuardParameters(md, worker, top);

View File

@ -101,7 +101,7 @@ static bool WeaveModule(ModuleDefinition moduleDefinition)
if (td.IsClass && td.BaseType.CanBeResolved()) if (td.IsClass && td.BaseType.CanBeResolved())
{ {
modified |= WeaveNetworkBehavior(td); modified |= WeaveNetworkBehavior(td);
modified |= ServerClientAttributeProcessor.Process(td); modified |= ServerClientAttributeProcessor.Process(weaverTypes, td);
} }
} }
watch.Stop(); watch.Stop();