summaryrefslogtreecommitdiff
path: root/src/SMAPI.Internal.Patching/HarmonyPatcher.cs
blob: fc239fd27ea451c03f30c1c6e02a5b70839c32a9 (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
#nullable disable

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;
        }
    }
}