Skip to content

Commit

Permalink
Fix self patching
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jul 24, 2018
1 parent a1cf7fc commit 463db2c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions Harmony/Tools/SelfPatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ namespace Harmony.Tools
{
internal class SelfPatching
{
static readonly string upgradeToLatestVersionFullName = typeof(UpgradeToLatestVersion).FullName;
static int GetVersion(MethodInfo method)
{
var attribute = method.GetCustomAttributes(false).OfType<UpgradeToLatestVersion>().FirstOrDefault();
return attribute?.version ?? -1;
var attribute = method.GetCustomAttributes(false)
.Where(attr => attr.GetType().FullName == upgradeToLatestVersionFullName)
.FirstOrDefault();
if (attribute == null)
return -1;
return Traverse.Create(attribute).Field("version").GetValue<int>();
}

static string MethodKey(MethodInfo method)
Expand Down Expand Up @@ -42,25 +47,30 @@ static bool IsHarmonyAssembly(Assembly assembly)
public static void PatchOldHarmonyMethods()
{
var potentialMethodsToUpgrade = new Dictionary<string, MethodInfo>();
typeof(SelfPatching).Assembly.GetTypes()
var ourAssembly = typeof(SelfPatching).Assembly;
ourAssembly.GetTypes()
.SelectMany(type => type.GetMethods(AccessTools.all))
.Where(method => method.GetCustomAttributes(false).Any(attr => attr is UpgradeToLatestVersion))
.Do(method => potentialMethodsToUpgrade.Add(MethodKey(method), method));

AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly => IsHarmonyAssembly(assembly))
.Where(assembly => IsHarmonyAssembly(assembly) && assembly != ourAssembly)
.SelectMany(assembly => assembly.GetTypes())
.SelectMany(type => type.GetMethods(AccessTools.all))
.Select(method =>
{
potentialMethodsToUpgrade.TryGetValue(MethodKey(method), out var newMethod);
return new KeyValuePair<MethodInfo, MethodInfo>(method, newMethod);
})
.Do(pair =>
.DoIf(pair => pair.Value != null, pair =>
{
var oldMethod = pair.Key;
var oldVersion = GetVersion(oldMethod);
var newMethod = pair.Value;
if (newMethod != null && GetVersion(oldMethod) < GetVersion(newMethod))
var newVersion = GetVersion(newMethod);
if (oldVersion != -1 && newVersion != -1 && oldVersion < newVersion)
{
if (patchedMethods.Contains(oldMethod) == false)
{
Expand Down

0 comments on commit 463db2c

Please sign in to comment.