summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs
blob: be98a666efb487865e706275379a6650d3bc022c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using HarmonyLib;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.Framework.ModLoading.Framework;
using StardewModdingAPI.Framework.ModLoading.RewriteFacades;

namespace StardewModdingAPI.Framework.ModLoading.Rewriters
{
    /// <summary>Rewrites Harmony 1.x assembly references to work with Harmony 2.x.</summary>
    internal class Harmony1AssemblyRewriter : BaseInstructionHandler
    {
        /*********
        ** Fields
        *********/
        /// <summary>Whether any Harmony 1.x types were replaced.</summary>
        private bool ReplacedTypes;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        public Harmony1AssemblyRewriter()
            : base(defaultPhrase: "Harmony 1.x") { }

        /// <summary>Rewrite a type reference if needed.</summary>
        /// <param name="module">The assembly module containing the instruction.</param>
        /// <param name="type">The type definition to handle.</param>
        /// <param name="replaceWith">Replaces the type reference with a new one.</param>
        /// <returns>Returns whether the type was changed.</returns>
        public override bool Handle(ModuleDefinition module, TypeReference type, Action<TypeReference> replaceWith)
        {
            // rewrite Harmony 1.x type to Harmony 2.0 type
            if (type.Scope is AssemblyNameReference scope && scope.Name == "0Harmony" && scope.Version.Major == 1)
            {
                Type targetType = this.GetMappedType(type);
                replaceWith(module.ImportReference(targetType));
                this.MarkRewritten();
                this.ReplacedTypes = true;
                return true;
            }

            return false;
        }

        /// <summary>Rewrite a CIL instruction reference if needed.</summary>
        /// <param name="module">The assembly module containing the instruction.</param>
        /// <param name="cil">The CIL processor.</param>
        /// <param name="instruction">The CIL instruction to handle.</param>
        /// <param name="replaceWith">Replaces the CIL instruction with a new one.</param>
        /// <returns>Returns whether the instruction was changed.</returns>
        public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action<Instruction> replaceWith)
        {
            // rewrite Harmony 1.x methods to Harmony 2.0
            MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
            if (this.TryRewriteMethodsToFacade(module, methodRef))
                return true;

            // rewrite renamed fields
            FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
            if (fieldRef != null)
            {
                if (fieldRef.DeclaringType.FullName == "HarmonyLib.HarmonyMethod" && fieldRef.Name == "prioritiy")
                    fieldRef.Name = nameof(HarmonyMethod.priority);
            }

            return false;
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Rewrite methods to use Harmony facades if needed.</summary>
        /// <param name="module">The assembly module containing the method reference.</param>
        /// <param name="methodRef">The method reference to map.</param>
        private bool TryRewriteMethodsToFacade(ModuleDefinition module, MethodReference methodRef)
        {
            if (!this.ReplacedTypes)
                return false; // not Harmony (or already using Harmony 2.0)

            // get facade type
            Type toType;
            switch (methodRef?.DeclaringType.FullName)
            {
                case "HarmonyLib.Harmony":
                    toType = typeof(HarmonyInstanceMethods);
                    break;

                case "HarmonyLib.AccessTools":
                    toType = typeof(AccessToolsMethods);
                    break;

                default:
                    return false;
            }

            // map if there's a matching method
            if (RewriteHelper.HasMatchingSignature(toType, methodRef))
            {
                methodRef.DeclaringType = module.ImportReference(toType);
                return true;
            }

            return false;
        }

        /// <summary>Get an equivalent Harmony 2.x type.</summary>
        /// <param name="type">The Harmony 1.x method.</param>
        private Type GetMappedType(TypeReference type)
        {
            // main Harmony object
            if (type.FullName == "Harmony.HarmonyInstance")
                return typeof(Harmony);

            // other objects
            string fullName = type.FullName.Replace("Harmony.", "HarmonyLib.");
            string targetName = typeof(Harmony).AssemblyQualifiedName.Replace(typeof(Harmony).FullName, fullName);
            return Type.GetType(targetName, throwOnError: true);
        }
    }
}