internal static InjectionDefinition FindInjectionDefinition(TypeDefinition type,
string name,
MethodDefinition target,
InjectFlags flags,
int[] localVarIDs = null,
params FieldDefinition[] memberReferences)
{
Logger.LogLine(LogMask.GetInjectionMethod, "##### GET INJECTION METHOD BEGIN #####");
Logger.LogLine(
LogMask.GetInjectionMethod,
$"Attempting to get a suitable injection method for {type.Name}.{target?.Name}");
if (string.IsNullOrEmpty(name))
{
Logger.LogLine(LogMask.GetInjectionMethod, "No injection method name specified");
return(null);
}
if (target == null || !target.HasBody)
{
Logger.LogLine(LogMask.GetInjectionMethod, "No target specified or the target has no definition");
return(null);
}
InjectValues hFlags = flags.ToValues();
#if DEBUG
Logger.LogLine(LogMask.GetInjectionMethod, "Patch parameters:");
Logger.LogLine(LogMask.GetInjectionMethod, $"Pass tag: {hFlags.PassTag}");
Logger.LogLine(LogMask.GetInjectionMethod, $"Modify return value: {hFlags.ModifyReturn}");
Logger.LogLine(LogMask.GetInjectionMethod, $"Pass THIS: {hFlags.PassInvokingInstance}");
Logger.LogLine(LogMask.GetInjectionMethod, $"Pass method locals: {hFlags.PassLocals}");
Logger.LogLine(LogMask.GetInjectionMethod, $"Pass member fields: {hFlags.PassFields}");
Logger.LogLine(LogMask.GetInjectionMethod, $"Pass member parameters: {hFlags.PassParameters}");
if (hFlags.PassParameters)
{
Logger.LogLine(
LogMask.GetInjectionMethod,
$"Member parameters are passed by {(hFlags.PassParametersByRef ? "reference" : "value")}");
}
#endif
if (hFlags.PassInvokingInstance && target.IsStatic)
{
Logger.LogLine(
LogMask.GetInjectionMethod,
$"{nameof(hFlags.PassInvokingInstance)} is true, but target is static!");
return(null);
}
if (hFlags.PassFields && (target.IsStatic || memberReferences == null || memberReferences.Length == 0))
{
Logger.LogLine(
LogMask.GetInjectionMethod,
$"{nameof(hFlags.PassFields)} is true, but target is either static or no member references were specified");
return(null);
}
if (hFlags.PassLocals && (!target.Body.HasVariables || localVarIDs == null || localVarIDs.Length == 0))
{
Logger.LogLine(
LogMask.GetInjectionMethod,
$"{nameof(hFlags.PassLocals)} is true, but target either doesn't have any locals or no local IDs were specified");
return(null);
}
int parameterCount = 0;
MethodDefinition injection =
type.Methods.FirstOrDefault(m =>
{
try
{
parameterCount = VerifyInjectionDefinition(m, target, flags, localVarIDs, memberReferences);
}
catch (InjectionDefinitionException e)
{
Logger.LogLine(LogMask.GetInjectionMethod, e.Message);
return(false);
}
return(true);
});
if (injection == null)
{
Logger.LogLine(LogMask.GetInjectionMethod, "Did not find any matching methods!");
return(null);
}
Logger.LogLine(LogMask.GetInjectionMethod, "Found injection method.");
Logger.LogLine(LogMask.GetInjectionMethod, "##### GET INJECTION METHOD END #####");
return(new InjectionDefinition
{
InjectMethod = injection,
InjectTarget = target,
Flags = flags,
MemberReferences = memberReferences,
LocalVarIDs = localVarIDs,
ParameterCount = parameterCount
});
}