Skip to content

Commit

Permalink
perf(Weaver): passing in format string
Browse files Browse the repository at this point in the history
dont waste performance creating strings for every method unless they have the attribute
  • Loading branch information
James-Frowen committed Sep 16, 2021
1 parent 6a69cdb commit 6c40fd6
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Assets/Mirage/Weaver/Processors/AttributeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ static bool IgnoreMethod(MethodDefinition md)

void ProcessMethodAttributes(MethodDefinition md, FoundType foundType)
{
InjectGuard<ServerAttribute>(md, foundType, IsServer, "[Server] function '" + md.FullName + "' called on client");
InjectGuard<ClientAttribute>(md, foundType, IsClient, "[Client] function '" + md.FullName + "' called on server");
InjectGuard<HasAuthorityAttribute>(md, foundType, HasAuthority, "[Has Authority] function '" + md.FullName + "' called on player without authority");
InjectGuard<LocalPlayerAttribute>(md, foundType, IsLocalPlayer, "[Local Player] function '" + md.FullName + "' called on nonlocal player");
InjectGuard<ServerAttribute>(md, foundType, IsServer, "[Server] function '{0}' called on client");
InjectGuard<ClientAttribute>(md, foundType, IsClient, "[Client] function '{0}' called on server");
InjectGuard<HasAuthorityAttribute>(md, foundType, HasAuthority, "[Has Authority] function '{0}' called on player without authority");
InjectGuard<LocalPlayerAttribute>(md, foundType, IsLocalPlayer, "[Local Player] function '{0}' called on nonlocal player");
CheckAttribute<ServerRpcAttribute>(md, foundType);
CheckAttribute<ClientRpcAttribute>(md, foundType);
}
Expand All @@ -117,7 +117,7 @@ void CheckAttribute<TAttribute>(MethodDefinition md, FoundType foundType)
}
}

void InjectGuard<TAttribute>(MethodDefinition md, FoundType foundType, MethodReference predicate, string message)
void InjectGuard<TAttribute>(MethodDefinition md, FoundType foundType, MethodReference predicate, string format)
{
CustomAttribute attribute = md.GetCustomAttribute<TAttribute>();
if (attribute == null)
Expand Down Expand Up @@ -147,6 +147,7 @@ void InjectGuard<TAttribute>(MethodDefinition md, FoundType foundType, MethodRef
worker.InsertBefore(top, worker.Create(OpCodes.Brtrue, top));
if (throwError)
{
string message = string.Format(format, md.Name);
worker.InsertBefore(top, worker.Create(OpCodes.Ldstr, message));
worker.InsertBefore(top, worker.Create(OpCodes.Newobj, () => new MethodInvocationException("")));
worker.InsertBefore(top, worker.Create(OpCodes.Throw));
Expand Down

0 comments on commit 6c40fd6

Please sign in to comment.