diff options
Diffstat (limited to 'src/SMAPI/Framework/Patching')
-rw-r--r-- | src/SMAPI/Framework/Patching/GameLocationPatch.cs | 60 | ||||
-rw-r--r-- | src/SMAPI/Framework/Patching/GamePatcher.cs | 45 | ||||
-rw-r--r-- | src/SMAPI/Framework/Patching/IHarmonyPatch.cs | 15 |
3 files changed, 120 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Patching/GameLocationPatch.cs b/src/SMAPI/Framework/Patching/GameLocationPatch.cs new file mode 100644 index 00000000..c0216b8f --- /dev/null +++ b/src/SMAPI/Framework/Patching/GameLocationPatch.cs @@ -0,0 +1,60 @@ +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Reflection; +using Harmony; +using StardewValley; +using xTile.Tiles; + +namespace StardewModdingAPI.Framework.Patching +{ + /// <summary>A Harmony patch for the <see cref="GameLocation.updateSeasonalTileSheets"/> method.</summary> + internal class GameLocationPatch : IHarmonyPatch + { + /********* + ** Accessors + *********/ + /// <summary>A unique name for this patch.</summary> + public string Name => $"{nameof(GameLocation)}.{nameof(GameLocation.updateSeasonalTileSheets)}"; + + + /********* + ** Public methods + *********/ + /// <summary>Apply the Harmony patch.</summary> + /// <param name="harmony">The Harmony instance.</param> + public void Apply(HarmonyInstance harmony) + { + MethodInfo method = AccessTools.Method(typeof(GameLocation), nameof(GameLocation.updateSeasonalTileSheets)); + MethodInfo prefix = AccessTools.Method(this.GetType(), nameof(GameLocationPatch.Prefix)); + + harmony.Patch(method, new HarmonyMethod(prefix), null); + } + + + /********* + ** Private methods + *********/ + /// <summary>An implementation of <see cref="GameLocation.updateSeasonalTileSheets"/> which correctly handles custom map tilesheets.</summary> + /// <param name="__instance">The location instance being patched.</param> + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument name is defined by Harmony.")] + private static bool Prefix(GameLocation __instance) + { + if (!__instance.IsOutdoors || __instance.Name.Equals("Desert")) + return false; + foreach (TileSheet tilesheet in __instance.Map.TileSheets) + { + string imageSource = tilesheet.ImageSource; + string imageFile = Path.GetFileName(imageSource); + if (imageFile.StartsWith("spring_") || imageFile.StartsWith("summer_") || imageFile.StartsWith("fall_") || imageFile.StartsWith("winter_")) + { + string imageDir = Path.GetDirectoryName(imageSource); + if (string.IsNullOrWhiteSpace(imageDir)) + imageDir = "Maps"; + tilesheet.ImageSource = Path.Combine(imageDir, Game1.currentSeason + "_" + imageFile.Split('_')[1]); + } + } + + return false; + } + } +} diff --git a/src/SMAPI/Framework/Patching/GamePatcher.cs b/src/SMAPI/Framework/Patching/GamePatcher.cs new file mode 100644 index 00000000..71ca8e55 --- /dev/null +++ b/src/SMAPI/Framework/Patching/GamePatcher.cs @@ -0,0 +1,45 @@ +using System; +using Harmony; + +namespace StardewModdingAPI.Framework.Patching +{ + /// <summary>Encapsulates applying Harmony patches to the game.</summary> + internal class GamePatcher + { + /********* + ** Properties + *********/ + /// <summary>Encapsulates monitoring and logging.</summary> + private readonly IMonitor Monitor; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="monitor">Encapsulates monitoring and logging.</param> + public GamePatcher(IMonitor monitor) + { + this.Monitor = monitor; + } + + /// <summary>Apply all loaded patches to the game.</summary> + /// <param name="patches">The patches to apply.</param> + public void Apply(params IHarmonyPatch[] patches) + { + HarmonyInstance harmony = HarmonyInstance.Create("io.smapi"); + foreach (IHarmonyPatch patch in patches) + { + try + { + patch.Apply(harmony); + } + catch (Exception ex) + { + this.Monitor.Log($"Couldn't apply runtime patch '{patch.Name}' to the game. Some SMAPI features may not work correctly. See log file for details.", LogLevel.Error); + this.Monitor.Log(ex.GetLogSummary(), LogLevel.Trace); + } + } + } + } +} diff --git a/src/SMAPI/Framework/Patching/IHarmonyPatch.cs b/src/SMAPI/Framework/Patching/IHarmonyPatch.cs new file mode 100644 index 00000000..cb42f40e --- /dev/null +++ b/src/SMAPI/Framework/Patching/IHarmonyPatch.cs @@ -0,0 +1,15 @@ +using Harmony; + +namespace StardewModdingAPI.Framework.Patching +{ + /// <summary>A Harmony patch to apply.</summary> + internal interface IHarmonyPatch + { + /// <summary>A unique name for this patch.</summary> + string Name { get; } + + /// <summary>Apply the Harmony patch.</summary> + /// <param name="harmony">The Harmony instance.</param> + void Apply(HarmonyInstance harmony); + } +} |