Skip to content

Commit

Permalink
Allows empty string for targeting special MethodType options and does…
Browse files Browse the repository at this point in the history
…n't try searching for it with AccessTools
  • Loading branch information
Banane9 committed Aug 30, 2024
1 parent efb41b4 commit b7ba4b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
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.DeclaredIndexer(attr.declaringType, attr.argumentTypes).GetGetMethod(true);
return AccessTools.DeclaredProperty(attr.declaringType, attr.methodName).GetGetMethod(true);

case MethodType.Setter:
if (attr.methodName is null)
if (string.IsNullOrEmpty(attr.methodName))
return AccessTools.DeclaredIndexer(attr.declaringType, attr.argumentTypes).GetSetMethod(true);
return AccessTools.DeclaredProperty(attr.declaringType, attr.methodName).GetSetMethod(true);

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
36 changes: 19 additions & 17 deletions Harmony/Tools/AccessTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public static FieldInfo DeclaredField(Type type, string name)
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 @@ public static FieldInfo Field(Type type, string name)
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 @@ public static PropertyInfo DeclaredProperty(Type type, string name)
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 @@ public static PropertyInfo Property(Type type, string name)
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 @@ public static MethodInfo DeclaredMethod(Type type, string name, Type[] parameter
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 @@ public static MethodInfo Method(Type type, string name, Type[] parameters = null
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 @@ -578,6 +578,7 @@ public static MethodInfo EnumeratorMoveNext(MethodBase method)
}

#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP

/// <summary>Gets the <see cref="IAsyncStateMachine.MoveNext"/> method of an async method's state machine</summary>
/// <param name="method">Async method that creates the state machine internally</param>
/// <returns>The internal <see cref="IAsyncStateMachine.MoveNext"/> method of the async state machine or <b>null</b> if no valid async method is detected</returns>
Expand Down Expand Up @@ -607,6 +608,7 @@ public static MethodInfo AsyncMoveNext(MethodBase method)

return asyncMethodBody;
}

#endif

/// <summary>Gets the names of all method that are declared in a type</summary>
Expand Down Expand Up @@ -857,9 +859,9 @@ public static Type Inner(Type type, string name)
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 @@ -1699,7 +1701,6 @@ public static DelegateType MethodDelegate<DelegateType>(string typeColonName, ob
{
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 Down Expand Up @@ -1842,12 +1843,12 @@ public static T CreateInstance<T>()
/// A cache for the <see cref="ICollection{T}.Add"/> or similar Add methods for different types.
/// </summary>
///
static readonly Dictionary<Type, FastInvokeHandler> addHandlerCache = [];
private static readonly Dictionary<Type, FastInvokeHandler> addHandlerCache = [];

#if NET35
static readonly ReaderWriterLock addHandlerCacheLock = new();
#else
static readonly ReaderWriterLockSlim addHandlerCacheLock = new(LockRecursionPolicy.SupportsRecursion);
private static readonly ReaderWriterLockSlim addHandlerCacheLock = new(LockRecursionPolicy.SupportsRecursion);
#endif

/// <summary>Makes a deep copy of any object</summary>
Expand Down Expand Up @@ -2057,6 +2058,7 @@ public static bool IsFloatingPoint(Type type)
/// <returns>True if instance is of nullable type, false if not</returns>
///
#pragma warning disable IDE0060

public static bool IsOfNullableType<T>(T instance) => Nullable.GetUnderlyingType(typeof(T)) is not null;

/// <summary>Tests whether a type or member is static, as defined in C#</summary>
Expand Down

0 comments on commit b7ba4b4

Please sign in to comment.