Skip to content

Commit 87ea840

Browse files
authored
Merge pull request #622 from Banane9/allow-empty-for-special-targets
Allows empty string for targeting special MethodType options and doesn't try searching for it with AccessTools
2 parents 5872fce + 0b13d7b commit 87ea840

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

Harmony/Internal/PatchTools.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ internal static MethodBase GetOriginalMethod(this HarmonyMethod attr)
9090
switch (attr.methodType)
9191
{
9292
case MethodType.Normal:
93-
if (attr.methodName is null)
93+
if (string.IsNullOrEmpty(attr.methodName))
9494
return null;
9595
return AccessTools.DeclaredMethod(attr.declaringType, attr.methodName, attr.argumentTypes);
9696

9797
case MethodType.Getter:
98-
if (attr.methodName is null)
98+
if (string.IsNullOrEmpty(attr.methodName))
9999
return AccessTools.DeclaredIndexerGetter(attr.declaringType, attr.argumentTypes);
100100
return AccessTools.DeclaredPropertyGetter(attr.declaringType, attr.methodName);
101101

102102
case MethodType.Setter:
103-
if (attr.methodName is null)
103+
if (string.IsNullOrEmpty(attr.methodName))
104104
return AccessTools.DeclaredIndexerSetter(attr.declaringType, attr.argumentTypes);
105105
return AccessTools.DeclaredPropertySetter(attr.declaringType, attr.methodName);
106106

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

115115
case MethodType.Enumerator:
116-
if (attr.methodName is null)
116+
if (string.IsNullOrEmpty(attr.methodName))
117117
return null;
118118
var enumMethod = AccessTools.DeclaredMethod(attr.declaringType, attr.methodName, attr.argumentTypes);
119119
return AccessTools.EnumeratorMoveNext(enumMethod);
120120

121121
#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
122122
case MethodType.Async:
123-
if (attr.methodName is null)
123+
if (string.IsNullOrEmpty(attr.methodName))
124124
return null;
125125
var asyncMethod = AccessTools.DeclaredMethod(attr.declaringType, attr.methodName, attr.argumentTypes);
126126
return AccessTools.AsyncMoveNext(asyncMethod);

Harmony/Tools/AccessTools.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ public static FieldInfo DeclaredField(Type type, string name)
149149
FileLog.Debug("AccessTools.DeclaredField: type is null");
150150
return null;
151151
}
152-
if (name is null)
152+
if (string.IsNullOrEmpty(name))
153153
{
154-
FileLog.Debug("AccessTools.DeclaredField: name is null");
154+
FileLog.Debug("AccessTools.DeclaredField: name is null/empty");
155155
return null;
156156
}
157157
var fieldInfo = type.GetField(name, allDeclared);
@@ -183,9 +183,9 @@ public static FieldInfo Field(Type type, string name)
183183
FileLog.Debug("AccessTools.Field: type is null");
184184
return null;
185185
}
186-
if (name is null)
186+
if (string.IsNullOrEmpty(name))
187187
{
188-
FileLog.Debug("AccessTools.Field: name is null");
188+
FileLog.Debug("AccessTools.Field: name is null/empty");
189189
return null;
190190
}
191191
var fieldInfo = FindIncludingBaseTypes(type, t => t.GetField(name, all));
@@ -234,9 +234,9 @@ public static PropertyInfo DeclaredProperty(Type type, string name)
234234
FileLog.Debug("AccessTools.DeclaredProperty: type is null");
235235
return null;
236236
}
237-
if (name is null)
237+
if (string.IsNullOrEmpty(name))
238238
{
239-
FileLog.Debug("AccessTools.DeclaredProperty: name is null");
239+
FileLog.Debug("AccessTools.DeclaredProperty: name is null/empty");
240240
return null;
241241
}
242242
var property = type.GetProperty(name, allDeclared);
@@ -338,9 +338,9 @@ public static PropertyInfo Property(Type type, string name)
338338
FileLog.Debug("AccessTools.Property: type is null");
339339
return null;
340340
}
341-
if (name is null)
341+
if (string.IsNullOrEmpty(name))
342342
{
343-
FileLog.Debug("AccessTools.Property: name is null");
343+
FileLog.Debug("AccessTools.Property: name is null/empty");
344344
return null;
345345
}
346346
var property = FindIncludingBaseTypes(type, t => t.GetProperty(name, all));
@@ -446,9 +446,9 @@ public static MethodInfo DeclaredMethod(Type type, string name, Type[] parameter
446446
FileLog.Debug("AccessTools.DeclaredMethod: type is null");
447447
return null;
448448
}
449-
if (name is null)
449+
if (string.IsNullOrEmpty(name))
450450
{
451-
FileLog.Debug("AccessTools.DeclaredMethod: name is null");
451+
FileLog.Debug("AccessTools.DeclaredMethod: name is null/empty");
452452
return null;
453453
}
454454
MethodInfo result;
@@ -495,9 +495,9 @@ public static MethodInfo Method(Type type, string name, Type[] parameters = null
495495
FileLog.Debug("AccessTools.Method: type is null");
496496
return null;
497497
}
498-
if (name is null)
498+
if (string.IsNullOrEmpty(name))
499499
{
500-
FileLog.Debug("AccessTools.Method: name is null");
500+
FileLog.Debug("AccessTools.Method: name is null/empty");
501501
return null;
502502
}
503503
MethodInfo result;
@@ -857,9 +857,9 @@ public static Type Inner(Type type, string name)
857857
FileLog.Debug("AccessTools.Inner: type is null");
858858
return null;
859859
}
860-
if (name is null)
860+
if (string.IsNullOrEmpty(name))
861861
{
862-
FileLog.Debug("AccessTools.Inner: name is null");
862+
FileLog.Debug("AccessTools.Inner: name is null/empty");
863863
return null;
864864
}
865865
return FindIncludingBaseTypes(type, t => t.GetNestedType(name, all));
@@ -1722,7 +1722,6 @@ public static DelegateType MethodDelegate<DelegateType>(string typeColonName, ob
17221722
{
17231723
var method = DeclaredMethod(typeColonName);
17241724
return MethodDelegate<DelegateType>(method, instance, virtualCall);
1725-
17261725
}
17271726

17281727
/// <summary>Creates a delegate for a given delegate definition, attributed with [<see cref="HarmonyLib.HarmonyDelegate"/>]</summary>

0 commit comments

Comments
 (0)