summaryrefslogtreecommitdiff
path: root/src/SMAPI.Internal.Patching/HarmonyPatcher.cs
blob: 6f30c241ae41848b2f380c9270f4858d5b36432c (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
using System;
using HarmonyLib;

namespace StardewModdingAPI.Internal.Patching
{
    /// <summary>Simplifies applying <see cref="IPatcher"/> instances to the game.</summary>
    internal static class HarmonyPatcher
    {
        /*********
        ** Public methods
        *********/
        /// <summary>Apply the given Harmony patchers.</summary>
        /// <param name="id">The mod ID applying the patchers.</param>
        /// <param name="monitor">The monitor with which to log any errors.</param>
        /// <param name="patchers">The patchers to apply.</param>
        public static Harmony Apply(string id, IMonitor monitor, params IPatcher[] patchers)
        {
            Harmony harmony = new(id);

            foreach (IPatcher patcher in patchers)
            {
                try
                {
                    patcher.Apply(harmony, monitor);
                }
                catch (Exception ex)
                {
                    monitor.Log($"Couldn't apply runtime patch '{patcher.GetType().Name}' to the game. Some SMAPI features may not work correctly. See log file for details.", LogLevel.Error);
                    monitor.Log($"Technical details:\n{ex.GetLogSummary()}");
                }
            }

            return harmony;
        }
    }
}