Skip to content

Commit

Permalink
Switch from using flags to a bool
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Feb 7, 2024
1 parent baa51ff commit c3b8070
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 35 deletions.
21 changes: 4 additions & 17 deletions Harmony/Public/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -778,30 +778,17 @@ public HarmonyArgument(int index, string name)
}
}

/// <summary> Flags used for optionally patching members that might not exist.</summary>
///
[Flags]
public enum OptionalFlags
{
/// <summary>Default behavior (throw and abort patching if there are no matches).</summary>
///
None = 0,

/// <summary> Do not throw an exception and abort the patching process if no matches are found (a warning is logged instead).</summary>
///
AllowNoMatches = 1 << 1,
}

/// <summary>Attribute used for optionally patching members that might not exist.</summary>
/// <summary>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).</summary>
///
[AttributeUsage(AttributeTargets.Method)]
public class HarmonyOptional : HarmonyAttribute
{
/// <summary>Default constructor</summary>
///
public HarmonyOptional(OptionalFlags flags = OptionalFlags.AllowNoMatches)
public HarmonyOptional()
{
info.optionalFlags = flags;
info.optional = true;
}
}
}
4 changes: 2 additions & 2 deletions Harmony/Public/HarmonyMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public class HarmonyMethod
///
public bool? wrapTryCatch;

/// <summary>Flags used for optionally patching members that might not exist.</summary>
/// <summary>Whether to not throw/abort when trying to patch members that do not exist (skip instead).</summary>
///
public OptionalFlags? optionalFlags;
public bool? optional;

/// <summary>Default constructor</summary>
///
Expand Down
11 changes: 2 additions & 9 deletions Harmony/Public/PatchClassProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -185,7 +185,7 @@ List<MethodInfo> 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;
Expand All @@ -205,13 +205,6 @@ List<MethodInfo> 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<MethodInfo>.Job job)
{
MethodInfo replacement = default;
Expand Down
16 changes: 9 additions & 7 deletions HarmonyTests/Patching/Assets/Specials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c3b8070

Please sign in to comment.