Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows empty string for targeting special MethodType options and doesn't try searching for it with AccessTools #622

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Harmony/Internal/PatchTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ internal static MethodBase GetOriginalMethod(this HarmonyMethod attr)
switch (attr.methodType)
{
case MethodType.Normal:
if (attr.methodName is null)
if (string.IsNullOrEmpty(attr.methodName))
return null;
return AccessTools.DeclaredMethod(attr.declaringType, attr.methodName, attr.argumentTypes);

case MethodType.Getter:
if (attr.methodName is null)
if (string.IsNullOrEmpty(attr.methodName))
return AccessTools.DeclaredIndexerGetter(attr.declaringType, attr.argumentTypes);
return AccessTools.DeclaredPropertyGetter(attr.declaringType, attr.methodName);

case MethodType.Setter:
if (attr.methodName is null)
if (string.IsNullOrEmpty(attr.methodName))
return AccessTools.DeclaredIndexerSetter(attr.declaringType, attr.argumentTypes);
return AccessTools.DeclaredPropertySetter(attr.declaringType, attr.methodName);

Expand All @@ -113,14 +113,14 @@ internal static MethodBase GetOriginalMethod(this HarmonyMethod attr)
.FirstOrDefault();

case MethodType.Enumerator:
if (attr.methodName is null)
if (string.IsNullOrEmpty(attr.methodName))
return null;
var enumMethod = AccessTools.DeclaredMethod(attr.declaringType, attr.methodName, attr.argumentTypes);
return AccessTools.EnumeratorMoveNext(enumMethod);

#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
case MethodType.Async:
if (attr.methodName is null)
if (string.IsNullOrEmpty(attr.methodName))
return null;
var asyncMethod = AccessTools.DeclaredMethod(attr.declaringType, attr.methodName, attr.argumentTypes);
return AccessTools.AsyncMoveNext(asyncMethod);
Expand Down
29 changes: 14 additions & 15 deletions Harmony/Tools/AccessTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@
FileLog.Debug("AccessTools.DeclaredField: type is null");
return null;
}
if (name is null)
if (string.IsNullOrEmpty(name))
{
FileLog.Debug("AccessTools.DeclaredField: name is null");
FileLog.Debug("AccessTools.DeclaredField: name is null/empty");
return null;
}
var fieldInfo = type.GetField(name, allDeclared);
Expand Down Expand Up @@ -183,9 +183,9 @@
FileLog.Debug("AccessTools.Field: type is null");
return null;
}
if (name is null)
if (string.IsNullOrEmpty(name))
{
FileLog.Debug("AccessTools.Field: name is null");
FileLog.Debug("AccessTools.Field: name is null/empty");
return null;
}
var fieldInfo = FindIncludingBaseTypes(type, t => t.GetField(name, all));
Expand Down Expand Up @@ -234,9 +234,9 @@
FileLog.Debug("AccessTools.DeclaredProperty: type is null");
return null;
}
if (name is null)
if (string.IsNullOrEmpty(name))
{
FileLog.Debug("AccessTools.DeclaredProperty: name is null");
FileLog.Debug("AccessTools.DeclaredProperty: name is null/empty");
return null;
}
var property = type.GetProperty(name, allDeclared);
Expand Down Expand Up @@ -338,9 +338,9 @@
FileLog.Debug("AccessTools.Property: type is null");
return null;
}
if (name is null)
if (string.IsNullOrEmpty(name))
{
FileLog.Debug("AccessTools.Property: name is null");
FileLog.Debug("AccessTools.Property: name is null/empty");
return null;
}
var property = FindIncludingBaseTypes(type, t => t.GetProperty(name, all));
Expand Down Expand Up @@ -446,9 +446,9 @@
FileLog.Debug("AccessTools.DeclaredMethod: type is null");
return null;
}
if (name is null)
if (string.IsNullOrEmpty(name))
{
FileLog.Debug("AccessTools.DeclaredMethod: name is null");
FileLog.Debug("AccessTools.DeclaredMethod: name is null/empty");
return null;
}
MethodInfo result;
Expand Down Expand Up @@ -495,9 +495,9 @@
FileLog.Debug("AccessTools.Method: type is null");
return null;
}
if (name is null)
if (string.IsNullOrEmpty(name))
{
FileLog.Debug("AccessTools.Method: name is null");
FileLog.Debug("AccessTools.Method: name is null/empty");
return null;
}
MethodInfo result;
Expand Down Expand Up @@ -857,9 +857,9 @@
FileLog.Debug("AccessTools.Inner: type is null");
return null;
}
if (name is null)
if (string.IsNullOrEmpty(name))
{
FileLog.Debug("AccessTools.Inner: name is null");
FileLog.Debug("AccessTools.Inner: name is null/empty");
return null;
}
return FindIncludingBaseTypes(type, t => t.GetNestedType(name, all));
Expand Down Expand Up @@ -1543,7 +1543,7 @@
/// </para>
/// </remarks>
///
public static DelegateType MethodDelegate<DelegateType>(MethodInfo method, object instance = null, bool virtualCall = true, Type[] delegateArgs = null) where DelegateType : Delegate

Check warning on line 1546 in Harmony/Tools/AccessTools.cs

View workflow job for this annotation

GitHub Actions / Build Binaries (ubuntu, ubuntu-latest, ReleaseRef) / Upload Test Build Output Cache

XML comment has badly formed XML -- 'Expected an end tag for element 'param'.'

Check warning on line 1546 in Harmony/Tools/AccessTools.cs

View workflow job for this annotation

GitHub Actions / Build Binaries (ubuntu, ubuntu-latest, ReleaseRef) / Upload Test Build Output Cache

XML comment has badly formed XML -- 'Expected an end tag for element 'param'.'
{
if (method is null)
throw new ArgumentNullException(nameof(method));
Expand Down Expand Up @@ -1722,7 +1722,6 @@
{
var method = DeclaredMethod(typeColonName);
return MethodDelegate<DelegateType>(method, instance, virtualCall);

}

/// <summary>Creates a delegate for a given delegate definition, attributed with [<see cref="HarmonyLib.HarmonyDelegate"/>]</summary>
Expand All @@ -1735,7 +1734,7 @@
/// <returns>A delegate of given <typeparamref name="DelegateType"/> to the method specified via [<see cref="HarmonyLib.HarmonyDelegate"/>]
/// attributes on <typeparamref name="DelegateType"/></returns>
/// <remarks>
/// This calls <see cref="MethodDelegate{DelegateType}(MethodInfo, object, bool)"/> with the <c>method</c> and <c>virtualCall</c> arguments

Check warning on line 1737 in Harmony/Tools/AccessTools.cs

View workflow job for this annotation

GitHub Actions / Build Binaries (ubuntu, ubuntu-latest, ReleaseRef) / Upload Test Build Output Cache

XML comment has cref attribute 'MethodDelegate{DelegateType}(MethodInfo, object, bool)' that could not be resolved

Check warning on line 1737 in Harmony/Tools/AccessTools.cs

View workflow job for this annotation

GitHub Actions / Build Binaries (ubuntu, ubuntu-latest, ReleaseRef) / Upload Test Build Output Cache

XML comment has cref attribute 'MethodDelegate{DelegateType}(MethodInfo, object, bool)' that could not be resolved
/// determined from the [<see cref="HarmonyLib.HarmonyDelegate"/>] attributes on <typeparamref name="DelegateType"/>,
/// and the given <paramref name="instance"/> (for closed instance delegates).
/// </remarks>
Expand Down
Loading