Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateAndPatchAll - use a counter + target name as default ID instead of a GUID #103

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading