Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: generic arguments resolution (#2300)
* fix: generic arguments lookup The weaver was not able to figure out the synclist type in code like this: ```cs public class SomeList<G, T> : SyncList<T> { } public class SomeListInt : SomeList<string, int> { } ``` This code fixes that problem, and greatly reduces the complexity of argument lookup. * linting
- Loading branch information
Showing
8 changed files
with
52 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 33 additions & 84 deletions
117
Assets/Mirror/Editor/Weaver/Processors/GenericArgumentResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,56 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using Mono.CecilX; | ||
|
||
namespace Mirror.Weaver | ||
{ | ||
public class GenericArgumentResolver | ||
public static class GenericArgumentResolver | ||
{ | ||
readonly Stack<TypeReference> stack = new Stack<TypeReference>(); | ||
readonly int maxGenericArgument; | ||
|
||
public GenericArgumentResolver(int maxGenericArgument) | ||
static TypeReference[] GetGenericArguments(TypeReference tr, Type baseType, TypeReference[] genericArguments) | ||
{ | ||
this.maxGenericArgument = maxGenericArgument; | ||
} | ||
if (tr == null) | ||
return null; | ||
|
||
public bool GetGenericFromBaseClass(TypeDefinition td, int genericArgument, TypeReference baseType, out TypeReference itemType) | ||
{ | ||
itemType = null; | ||
if (GetGenericBaseType(td, baseType, out GenericInstanceType parent)) | ||
{ | ||
TypeReference arg = parent.GenericArguments[genericArgument]; | ||
if (arg.IsGenericParameter) | ||
{ | ||
itemType = FindParameterInStack(td, genericArgument); | ||
} | ||
else | ||
{ | ||
itemType = Weaver.CurrentAssembly.MainModule.ImportReference(arg); | ||
} | ||
} | ||
TypeReference[] resolvedArguments = new TypeReference[0]; | ||
|
||
return itemType != null; | ||
} | ||
|
||
TypeReference FindParameterInStack(TypeDefinition td, int genericArgument) | ||
{ | ||
while (stack.Count > 0) | ||
if (tr is GenericInstanceType genericInstance) | ||
{ | ||
TypeReference next = stack.Pop(); | ||
// this type is a generic instance, for example List<int> | ||
// however, the parameter may be generic itself, for example: | ||
// List<T>. If T is a generic parameter, then look it up in | ||
// from the arguments we got. | ||
resolvedArguments = genericInstance.GenericArguments.ToArray(); | ||
|
||
if (!(next is GenericInstanceType genericType)) | ||
for (int i = 0; i< resolvedArguments.Length; i++) | ||
{ | ||
// if type is not GenericInstanceType something has gone wrong | ||
return null; | ||
TypeReference argument = resolvedArguments[i]; | ||
if (argument is GenericParameter genericArgument) | ||
{ | ||
resolvedArguments[i] = genericArguments[genericArgument.Position]; | ||
} | ||
} | ||
} | ||
|
||
if (genericType.GenericArguments.Count < genericArgument) | ||
{ | ||
// if less than `genericArgument` then we didnt find generic argument | ||
return null; | ||
} | ||
if (tr.Is(baseType)) | ||
return resolvedArguments; | ||
|
||
if (genericType.GenericArguments.Count > maxGenericArgument) | ||
{ | ||
// if greater than `genericArgument` it is hard to know which generic arg we want | ||
// See SyncListGenericInheritanceWithMultipleGeneric test | ||
Weaver.Error($"Type {td.Name} has too many generic arguments in base class {next}", td); | ||
return null; | ||
} | ||
if (tr.CanBeResolved()) | ||
return GetGenericArguments(tr.Resolve().BaseType, baseType, resolvedArguments); | ||
|
||
TypeReference genericArg = genericType.GenericArguments[genericArgument]; | ||
if (!genericArg.IsGenericParameter) | ||
{ | ||
// if not generic, successfully found type | ||
return Weaver.CurrentAssembly.MainModule.ImportReference(genericArg); | ||
} | ||
} | ||
|
||
// nothing left in stack, something went wrong | ||
return null; | ||
} | ||
|
||
bool GetGenericBaseType(TypeDefinition td, TypeReference baseType, out GenericInstanceType found) | ||
/// <summary> | ||
/// Find out what the arguments are to a generic base type | ||
/// </summary> | ||
/// <param name="td"></param> | ||
/// <param name="baseType"></param> | ||
/// <returns></returns> | ||
public static TypeReference[] GetGenericArguments(TypeDefinition td, Type baseType) | ||
{ | ||
stack.Clear(); | ||
TypeReference parent = td.BaseType; | ||
found = null; | ||
|
||
while (parent != null) | ||
{ | ||
string parentName = parent.FullName; | ||
|
||
// strip generic <T> parameters from class name (if any) | ||
parentName = Extensions.StripGenericParametersFromClassName(parentName); | ||
|
||
if (parentName == baseType.FullName) | ||
{ | ||
found = parent as GenericInstanceType; | ||
break; | ||
} | ||
|
||
try | ||
{ | ||
stack.Push(parent); | ||
parent = parent.Resolve().BaseType; | ||
} | ||
catch (AssemblyResolutionException) | ||
{ | ||
// this can happen for plugins. | ||
break; | ||
} | ||
} | ||
if (td == null) | ||
return null; | ||
|
||
return found != null; | ||
return GetGenericArguments(td.BaseType, baseType, new TypeReference[] { }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters