From bf0903b1730d75b7170ca91aaa11c3c173813aca Mon Sep 17 00:00:00 2001 From: yangzhi Date: Mon, 26 Jun 2023 16:41:39 +0800 Subject: [PATCH] Compatibility fix --- .../Framework/Commands/Other/ZoomCommand.cs | 53 ------------------- .../SMAPI.Mods.ConsoleCommands.csproj | 8 +-- .../RewriteFacades/ISoundBankMethods.cs | 24 --------- .../RewriteFacades/SoundBankMethods.cs | 37 +++++++++++++ src/SMAPI/Metadata/InstructionMetadata.cs | 4 +- 5 files changed, 44 insertions(+), 82 deletions(-) delete mode 100644 src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ZoomCommand.cs delete mode 100644 src/SMAPI/Framework/ModLoading/RewriteFacades/ISoundBankMethods.cs create mode 100644 src/SMAPI/Framework/ModLoading/RewriteFacades/SoundBankMethods.cs diff --git a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ZoomCommand.cs b/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ZoomCommand.cs deleted file mode 100644 index a1b394c3..00000000 --- a/src/SMAPI.Mods.ConsoleCommands/Framework/Commands/Other/ZoomCommand.cs +++ /dev/null @@ -1,53 +0,0 @@ -#if SMAPI_FOR_MOBILE -using System.Linq; -using System.Reflection; -using StardewValley; -using StardewValley.Mobile; - -namespace StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands.Other -{ - /// A command which sends a debug command to the game. - internal class ZoomCommand : TrainerCommand - { - /********* - ** Public methods - *********/ - /// Construct an instance. - public ZoomCommand() - : base("zoom", "Modify game's zoom level.\n\nUsage: zoom \n- zoomLevel: the target zoomLevel (a number).\nFor example, 'zoom 1.5' set zoom level to 1.5 * NativeZoomLevel.") { } - - /// Handle the command. - /// Writes messages to the console and log file. - /// The command name. - /// The command arguments. - public override void Handle(IMonitor monitor, string command, ArgumentParser args) - { - // submit command - decimal zoomLevel; - if (!args.Any()) - { - zoomLevel = 1.0m; - } - else if (!args.TryGetDecimal(0, "zoomLevel", out zoomLevel, min: 0.1m, max: 10m)) - return; - object viewport = typeof(Game1).GetField("viewport", BindingFlags.Static | BindingFlags.Public).GetValue(null); - PropertyInfo x = viewport.GetType().GetProperty("X"); - PropertyInfo y = viewport.GetType().GetProperty("Y"); - int oldX = (int)x.GetValue(viewport); - int oldY = (int)y.GetValue(viewport); - FieldInfo _lastPinchZoomLevel = typeof(PinchZoom).GetField("_lastPinchZoomLevel", BindingFlags.Instance | BindingFlags.NonPublic); - FieldInfo _pinchZoomLevel = typeof(PinchZoom).GetField("_pinchZoomLevel", BindingFlags.Instance | BindingFlags.NonPublic); - Game1.options.zoomLevel = Game1.NativeZoomLevel * (float)zoomLevel; - float oldZoom = (float)_lastPinchZoomLevel.GetValue(PinchZoom.Instance); - _lastPinchZoomLevel.SetValue(PinchZoom.Instance, _pinchZoomLevel.GetValue(PinchZoom.Instance)); - _pinchZoomLevel.SetValue(PinchZoom.Instance, Game1.options.zoomLevel); - Game1.game1.refreshWindowSettings(); - PinchZoom.Instance.Center(); - WeatherDebrisManager.Instance.RepositionOnZoomChange(oldX, oldY, (int)x.GetValue(viewport), (int)y.GetValue(viewport), oldZoom, Game1.options.zoomLevel); - RainManager.Instance.UpdateRainPositionForPinchZoom((float)(oldX - (int)x.GetValue(viewport)), (float)(oldY - (int)y.GetValue(viewport))); - // show result - monitor.Log("Zoom level changed.", LogLevel.Info); - } - } -} -#endif diff --git a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj index 4ab4851c..1ae7609a 100644 --- a/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj +++ b/src/SMAPI.Mods.ConsoleCommands/SMAPI.Mods.ConsoleCommands.csproj @@ -30,16 +30,16 @@ - ..\..\build\StardewValleyAndroidStuff\base_1.4.5.145\assemblies\MonoGame.Framework.dll + ..\..\build\StardewValleyAndroidStuff\base_1.5.6.39\assemblies\MonoGame.Framework.dll - ..\SMAPI\bin\Release\StardewModdingAPI.dll + ..\SMAPI\bin\Debug\StardewModdingAPI.dll - ..\..\build\StardewValleyAndroidStuff\base_1.4.5.151\assemblies_decrypt\StardewValley.dll + ..\..\build\StardewValleyAndroidStuff\base_1.5.6.39\assemblies\StardewValley.dll - ..\..\build\StardewValleyAndroidStuff\base_1.4.5.151\assemblies_decrypt\StardewValley.GameData.dll + ..\..\build\StardewValleyAndroidStuff\base_1.5.6.39\assemblies\StardewValley.GameData.dll diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/ISoundBankMethods.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/ISoundBankMethods.cs deleted file mode 100644 index 07e785e0..00000000 --- a/src/SMAPI/Framework/ModLoading/RewriteFacades/ISoundBankMethods.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Reflection; -using Microsoft.Xna.Framework.Audio; -using StardewValley; - -namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades; - -public interface ISoundBankMethods : ISoundBank -{ - void AddCue(CueDefinition cueDefinition) - { - SoundBank soundBank = (SoundBank)this.GetType() - .GetField("soundBank", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) - ?.GetValue(this); - soundBank?.AddCue(cueDefinition); - } - - CueDefinition GetCueDefinition(string name) - { - SoundBank soundBank = (SoundBank)this.GetType() - .GetField("soundBank", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) - ?.GetValue(this); - return soundBank?.GetCueDefinition(name); - } -} diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/SoundBankMethods.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/SoundBankMethods.cs new file mode 100644 index 00000000..79e051bd --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/SoundBankMethods.cs @@ -0,0 +1,37 @@ +using System.Reflection; +using HarmonyLib; +using Microsoft.Xna.Framework.Audio; +using StardewValley; + +namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades; + +public static class SoundBankMethods +{ + public static void AddCue(this ISoundBank iSoundBank, CueDefinition cueDefinition) + { + if (iSoundBank is SoundBankWrapper soundBankWrapper) + { + SoundBank soundBank = (SoundBank)typeof(SoundBankWrapper) + .GetField("soundBank", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) + ?.GetValue(soundBankWrapper); + soundBank?.AddCue(cueDefinition); + } + else + AccessTools.Method(iSoundBank.GetType(), "AddCue", new[] { typeof(CueDefinition) }) + ?.Invoke(iSoundBank, new[] { cueDefinition }); + } + + public static CueDefinition GetCueDefinition(this ISoundBank iSoundBank, string name) + { + if (iSoundBank is SoundBankWrapper soundBankWrapper) + { + SoundBank soundBank = (SoundBank)typeof(SoundBankWrapper) + .GetField("soundBank", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) + ?.GetValue(soundBankWrapper); + return soundBank?.GetCueDefinition(name); + } + + return (CueDefinition)AccessTools.Method(iSoundBank.GetType(), "GetCueDefinition", new[] { typeof(string) }) + ?.Invoke(iSoundBank, new[] { name }); + } +} diff --git a/src/SMAPI/Metadata/InstructionMetadata.cs b/src/SMAPI/Metadata/InstructionMetadata.cs index 4d851a77..aaabf6d1 100644 --- a/src/SMAPI/Metadata/InstructionMetadata.cs +++ b/src/SMAPI/Metadata/InstructionMetadata.cs @@ -94,7 +94,9 @@ namespace StardewModdingAPI.Metadata yield return new MethodParentRewriter(typeof(SpriteText), typeof(SpriteTextMethods)); yield return new MethodParentRewriter(typeof(Utility), typeof(UtilityMethods)); yield return new MethodParentRewriter(typeof(OptionsElement), typeof(OptionsElementMethods)); - yield return new MethodParentRewriter(typeof(ISoundBank), typeof(ISoundBankMethods)); + + yield return new MethodToAnotherStaticMethodRewriter(typeof(ISoundBank), (method) => method.Name == nameof(SoundBankMethods.AddCue), typeof(SoundBankMethods), "AddCue"); + yield return new MethodToAnotherStaticMethodRewriter(typeof(ISoundBank), (method) => method.Name == nameof(SoundBankMethods.GetCueDefinition), typeof(SoundBankMethods), "GetCueDefinition"); //Constructor Rewrites yield return new MethodParentRewriter(typeof(MapPage), typeof(MapPageMethods));