diff --git a/Harmony/Public/Attributes.cs b/Harmony/Public/Attributes.cs
index ac37b59d..7fe50292 100644
--- a/Harmony/Public/Attributes.cs
+++ b/Harmony/Public/Attributes.cs
@@ -778,30 +778,17 @@ public HarmonyArgument(int index, string name)
}
}
- /// Flags used for optionally patching members that might not exist.
- ///
- [Flags]
- public enum OptionalFlags
- {
- /// Default behavior (throw and abort patching if there are no matches).
- ///
- None = 0,
-
- /// Do not throw an exception and abort the patching process if no matches are found (a warning is logged instead).
- ///
- AllowNoMatches = 1 << 1,
- }
-
- /// Attribute used for optionally patching members that might not exist.
+ /// Attribute used for optionally patching members that might not exist.
+ /// Harmony patches with this attribute will not throw an exception and abort the patching process if the target member is not found (a warning is logged instead).
///
[AttributeUsage(AttributeTargets.Method)]
public class HarmonyOptional : HarmonyAttribute
{
/// Default constructor
///
- public HarmonyOptional(OptionalFlags flags = OptionalFlags.AllowNoMatches)
+ public HarmonyOptional()
{
- info.optionalFlags = flags;
+ info.optional = true;
}
}
}
diff --git a/Harmony/Public/HarmonyMethod.cs b/Harmony/Public/HarmonyMethod.cs
index c72c9a55..28f8c4b2 100644
--- a/Harmony/Public/HarmonyMethod.cs
+++ b/Harmony/Public/HarmonyMethod.cs
@@ -67,9 +67,9 @@ public class HarmonyMethod
///
public bool? wrapTryCatch;
- /// Flags used for optionally patching members that might not exist.
+ /// Whether to not throw/abort when trying to patch members that do not exist (skip instead).
///
- public OptionalFlags? optionalFlags;
+ public bool? optional;
/// Default constructor
///
diff --git a/Harmony/Public/PatchClassProcessor.cs b/Harmony/Public/PatchClassProcessor.cs
index 5460b67a..48d5f4d3 100644
--- a/Harmony/Public/PatchClassProcessor.cs
+++ b/Harmony/Public/PatchClassProcessor.cs
@@ -132,7 +132,7 @@ void ReversePatch(ref MethodBase lastOriginal)
if (lastOriginal is null)
{
- if (IsPatchOptional(patchMethod))
+ if (patchMethod.info.optional == true)
{
Logger.Log(Logger.LogChannel.Warn, () => $"Skipping optional reverse patch {patchMethod.info.method.FullDescription()} - target method not found");
continue;
@@ -185,7 +185,7 @@ List PatchWithAttributes(ref MethodBase lastOriginal)
lastOriginal = patchMethod.info.GetOriginalMethod();
if (lastOriginal is null)
{
- if (IsPatchOptional(patchMethod))
+ if (patchMethod.info.optional == true)
{
Logger.Log(Logger.LogChannel.Warn, () => $"Skipping optional patch {patchMethod.info.method.FullDescription()} - target method not found");
continue;
@@ -205,13 +205,6 @@ List PatchWithAttributes(ref MethodBase lastOriginal)
return jobs.GetReplacements();
}
- static bool IsPatchOptional(AttributePatch patchMethod)
- {
- var optionalFlags = patchMethod.info.optionalFlags;
- var isOptional = optionalFlags != null && optionalFlags.Value.Has(OptionalFlags.AllowNoMatches);
- return isOptional;
- }
-
void ProcessPatchJob(PatchJobs.Job job)
{
MethodInfo replacement = default;
diff --git a/HarmonyTests/Patching/Assets/Specials.cs b/HarmonyTests/Patching/Assets/Specials.cs
index 4eb1a0a6..68d786f1 100644
--- a/HarmonyTests/Patching/Assets/Specials.cs
+++ b/HarmonyTests/Patching/Assets/Specials.cs
@@ -147,18 +147,20 @@ public class OptionalPatch
[HarmonyPostfix, HarmonyOptional, HarmonyPatch(typeof(OptionalPatch), nameof(NotEnumerator), MethodType.Async)]
public static void Test5() => throw new InvalidOperationException();
+ [HarmonyPostfix]
+ [HarmonyOptional]
+ [HarmonyPatch(typeof(OptionalPatch), "missing_method1")]
+ [HarmonyPatch(typeof(OptionalPatch), nameof(NotEnumerator), MethodType.Normal)]
+ [HarmonyPatch(typeof(OptionalPatch), "missing_method2")]
+ public static void Test6() => throw new InvalidOperationException();
+
private void NotEnumerator() => throw new InvalidOperationException();
}
public static class OptionalPatchNone
{
- [HarmonyPrefix]
- [HarmonyOptional(OptionalFlags.None)]
- [HarmonyPatch(typeof(OptionalPatch), "missing_method")]
- public static void Prefix()
- {
- throw new InvalidOperationException();
- }
+ [HarmonyPrefix, HarmonyPatch(typeof(OptionalPatch), "missing_method")]
+ public static void Prefix() => throw new InvalidOperationException();
}
public static class SafeWrapPatch