Skip to content

Commit

Permalink
feat: Use Server Client attribute outside of NetworkBehaviour (#2150)
Browse files Browse the repository at this point in the history
* adding generated test for other baseclasses

* removing errors when attribute is not in networkbehaviour

* temp weaver tests

* updating weaver tests for monobehaviour

* adding weaver tests for non-networkbehaviour

* moving where serverclient attributes are processed

* removing un-used code

* regenerate tests
  • Loading branch information
James-Frowen committed Aug 21, 2020
1 parent c6fa49c commit eec49fa
Show file tree
Hide file tree
Showing 15 changed files with 4,182 additions and 55 deletions.
18 changes: 0 additions & 18 deletions Assets/Mirror/Editor/Weaver/Processors/MonoBehaviourProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,6 @@ static void ProcessMethods(TypeDefinition td)
{
Weaver.Error($"TargetRpc {md.Name} must be declared inside a NetworkBehaviour", md);
}

string attributeName = ca.Constructor.DeclaringType.ToString();

switch (attributeName)
{
case "Mirror.ServerAttribute":
Weaver.Error($"Server method {md.Name} must be declared inside a NetworkBehaviour", md);
break;
case "Mirror.ServerCallbackAttribute":
Weaver.Error($"ServerCallback method {md.Name} must be declared inside a NetworkBehaviour", md);
break;
case "Mirror.ClientAttribute":
Weaver.Error($"Client method {md.Name} must be declared inside a NetworkBehaviour", md);
break;
case "Mirror.ClientCallbackAttribute":
Weaver.Error($"ClientCallback method {md.Name} must be declared inside a NetworkBehaviour", md);
break;
}
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions Assets/Mirror/Editor/Weaver/Processors/PropertySiteProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void ProcessSiteClass(TypeDefinition td)
//Console.WriteLine(" ProcessSiteClass " + td);
foreach (MethodDefinition md in td.Methods)
{
ProcessSiteMethod(td, md);
ProcessSiteMethod(md);
}

foreach (TypeDefinition nested in td.NestedTypes)
Expand All @@ -50,7 +50,7 @@ static void ProcessSiteClass(TypeDefinition td)
}
}

static void ProcessSiteMethod(TypeDefinition td, MethodDefinition md)
static void ProcessSiteMethod(MethodDefinition md)
{
// process all references to replaced members with properties
//Weaver.DLog(td, " ProcessSiteMethod " + md);
Expand All @@ -62,18 +62,11 @@ static void ProcessSiteMethod(TypeDefinition td, MethodDefinition md)

if (md.IsAbstract)
{
if (ServerClientAttributeProcessor.HasServerClientAttribute(md))
{
Weaver.Error("Server or Client Attributes can't be added to abstract method. Server and Client Attributes are not inherited so they need to be applied to the override methods instead.", md);
}
return;
}

if (md.Body != null && md.Body.Instructions != null)
{
// TODO move this to NetworkBehaviourProcessor
ServerClientAttributeProcessor.ProcessMethodAttributes(td, md);

for (int iCount = 0; iCount < md.Body.Instructions.Count;)
{
Instruction instr = md.Body.Instructions[iCount];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,44 @@ namespace Mirror.Weaver
/// </summary>
static class ServerClientAttributeProcessor
{
public static bool ProcessSiteClass(TypeDefinition td)
{
bool modified = false;
foreach (MethodDefinition md in td.Methods)
{
modified |= ProcessSiteMethod(td, md);
}

foreach (TypeDefinition nested in td.NestedTypes)
{
modified |= ProcessSiteClass(nested);
}
return modified;
}

static bool ProcessSiteMethod(TypeDefinition td, MethodDefinition md)
{
if (md.Name == ".cctor" ||
md.Name == NetworkBehaviourProcessor.ProcessedFunctionName ||
md.Name.StartsWith(Weaver.InvokeRpcPrefix))
return false;

if (md.IsAbstract)
{
if (HasServerClientAttribute(md))
{
Weaver.Error("Server or Client Attributes can't be added to abstract method. Server and Client Attributes are not inherited so they need to be applied to the override methods instead.", md);
}
return false;
}

if (md.Body != null && md.Body.Instructions != null)
{
return ProcessMethodAttributes(td, md);
}
return false;
}

public static bool HasServerClientAttribute(MethodDefinition md)
{
foreach (CustomAttribute attr in md.CustomAttributes)
Expand All @@ -26,37 +64,39 @@ public static bool HasServerClientAttribute(MethodDefinition md)
return false;
}

public static void ProcessMethodAttributes(TypeDefinition td, MethodDefinition md)
public static bool ProcessMethodAttributes(TypeDefinition td, MethodDefinition md)
{
bool modified = false;
foreach (CustomAttribute attr in md.CustomAttributes)
{
switch (attr.Constructor.DeclaringType.ToString())
{
case "Mirror.ServerAttribute":
InjectServerGuard(td, md, true);
InjectServerGuard(md, true);
modified = true;
break;
case "Mirror.ServerCallbackAttribute":
InjectServerGuard(td, md, false);
InjectServerGuard(md, false);
modified = true;
break;
case "Mirror.ClientAttribute":
InjectClientGuard(td, md, true);
InjectClientGuard(md, true);
modified = true;
break;
case "Mirror.ClientCallbackAttribute":
InjectClientGuard(td, md, false);
InjectClientGuard(md, false);
modified = true;
break;
default:
break;
}
}

return modified;
}

static void InjectServerGuard(TypeDefinition td, MethodDefinition md, bool logWarning)
static void InjectServerGuard(MethodDefinition md, bool logWarning)
{
if (!Weaver.IsNetworkBehaviour(td))
{
Weaver.Error($"Server method {md.Name} must be declared in a NetworkBehaviour", md);
return;
}
ILProcessor worker = md.Body.GetILProcessor();
Instruction top = md.Body.Instructions[0];

Expand All @@ -72,13 +112,8 @@ static void InjectServerGuard(TypeDefinition td, MethodDefinition md, bool logWa
worker.InsertBefore(top, worker.Create(OpCodes.Ret));
}

static void InjectClientGuard(TypeDefinition td, MethodDefinition md, bool logWarning)
static void InjectClientGuard(MethodDefinition md, bool logWarning)
{
if (!Weaver.IsNetworkBehaviour(td))
{
Weaver.Error($"Client method {md.Name} must be declared in a NetworkBehaviour", md);
return;
}
ILProcessor worker = md.Body.GetILProcessor();
Instruction top = md.Body.Instructions[0];

Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirror/Editor/Weaver/Weaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ static bool WeaveModule(ModuleDefinition moduleDefinition)
{
modified |= WeaveNetworkBehavior(td);
modified |= WeaveMessage(td);
modified |= ServerClientAttributeProcessor.ProcessSiteClass(td);
}
}
watch.Stop();
Expand Down Expand Up @@ -330,7 +331,7 @@ static bool Weave(string assName, AssemblyDefinition unityAssembly, AssemblyDefi
// this must be done for ALL code, not just NetworkBehaviours
try
{
PropertySiteProcessor.ProcessSitesModule(CurrentAssembly.MainModule);
PropertySiteProcessor.ProcessSitesModule(moduleDefinition);
}
catch (Exception e)
{
Expand Down

0 comments on commit eec49fa

Please sign in to comment.