Skip to content

Commit

Permalink
CreateAndPatchAll - use a counter + target name as default ID instead…
Browse files Browse the repository at this point in the history
… of a GUID

Closes #44
  • Loading branch information
ManlyMarco committed Jan 19, 2024
1 parent e9c72af commit 94da210
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Harmony/Public/Harmony.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void UnpatchAll(string harmonyID = null)
if (HarmonyGlobalSettings.DisallowLegacyGlobalUnpatchAll)
{
Logger.Log(Logger.LogChannel.Warn, () => "Legacy UnpatchAll has been called AND DisallowLegacyGlobalUnpatchAll=true. " +
"Skipping execution of UnpatchAll");
"Skipping execution of UnpatchAll");
return;
}

Expand All @@ -308,7 +308,7 @@ public void UnpatchAll(string harmonyID = null)
if (harmonyID.Length == 0)
{
Logger.Log(Logger.LogChannel.Warn, () => "Legacy UnpatchAll was called with harmonyID=\"\" which is an invalid id. " +
"Skipping execution of UnpatchAll");
"Skipping execution of UnpatchAll");
return;
}

Expand Down Expand Up @@ -414,24 +414,30 @@ public static Dictionary<string, Version> VersionInfo(out Version currentVersion
return PatchProcessor.VersionInfo(out currentVersion);
}

private static int _autoGuidCounter = 100;

/// <summary>Creates a new Harmony instance and applies all patches specified in the type</summary>
/// <param name="type">The type to scan for patches.</param>
/// <param name="harmonyInstanceId">The ID for the Harmony instance to create, which will be used.</param>
/// <param name="harmonyInstanceId">ID of the Harmony instance which will be created. Specify the ID if other plugins may want to interact with your patches.</param>
///
public static Harmony CreateAndPatchAll(Type type, string harmonyInstanceId = null)
{
var harmony = new Harmony(harmonyInstanceId ?? $"harmony-auto-{Guid.NewGuid()}");
if (type == null) throw new ArgumentNullException(nameof(type));

var harmony = new Harmony(harmonyInstanceId ?? $"harmony-auto-{System.Threading.Interlocked.Increment(ref _autoGuidCounter)}-{type.Assembly.GetName().Name}-{type.FullName}");
harmony.PatchAll(type);
return harmony;
}

/// <summary>Applies all patches specified in the assembly</summary>
/// <param name="assembly">The assembly to scan.</param>
/// <param name="harmonyInstanceId">The ID for the Harmony instance to create, which will be used.</param>
/// <param name="harmonyInstanceId">ID of the Harmony instance which will be created. Specify the ID if other plugins may want to interact with your patches.</param>
///
public static Harmony CreateAndPatchAll(Assembly assembly, string harmonyInstanceId = null)
{
var harmony = new Harmony(harmonyInstanceId ?? $"harmony-auto-{Guid.NewGuid()}");
if (assembly == null) throw new ArgumentNullException(nameof(assembly));

var harmony = new Harmony(harmonyInstanceId ?? $"harmony-auto-{System.Threading.Interlocked.Increment(ref _autoGuidCounter)}-{assembly.GetName().Name}");
harmony.PatchAll(assembly);
return harmony;
}
Expand Down

0 comments on commit 94da210

Please sign in to comment.