From 3c59a5fc6a2f924c56cf27fdac16654539f00ec5 Mon Sep 17 00:00:00 2001 From: zhiyang7 Date: Wed, 8 Feb 2023 18:04:24 +0800 Subject: [PATCH] Compatibility fix --- .../RewriteFacades/ISoundBankMethods.cs | 9 +++ .../RewriteFacades/OptionsElementMethods.cs | 16 ++++ src/SMAPI/Framework/SCore.cs | 4 + src/SMAPI/Metadata/InstructionMetadata.cs | 31 +------ src/SMAPI/Patches/StringPatcher.cs | 81 +++++++++++++++++++ src/SMAPI/SMAPI.csproj | 1 + 6 files changed, 114 insertions(+), 28 deletions(-) create mode 100644 src/SMAPI/Framework/ModLoading/RewriteFacades/ISoundBankMethods.cs create mode 100644 src/SMAPI/Framework/ModLoading/RewriteFacades/OptionsElementMethods.cs create mode 100644 src/SMAPI/Patches/StringPatcher.cs diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/ISoundBankMethods.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/ISoundBankMethods.cs new file mode 100644 index 00000000..881cc0e9 --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/ISoundBankMethods.cs @@ -0,0 +1,9 @@ +using Microsoft.Xna.Framework.Audio; +using StardewValley; + +namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades; + +public interface ISoundBankMethods : ISoundBank +{ + CueDefinition GetCueDefinition(string name); +} diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/OptionsElementMethods.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/OptionsElementMethods.cs new file mode 100644 index 00000000..dd13f06e --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/OptionsElementMethods.cs @@ -0,0 +1,16 @@ +using Microsoft.Xna.Framework.Graphics; +using StardewValley.Menus; + +namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades; + +public class OptionsElementMethods : OptionsElement +{ + public OptionsElementMethods(string label) : base(label) + { + } + + public virtual void draw(SpriteBatch b, int slotX, int slotY, IClickableMenu context = null) + { + base.draw(b, slotX, slotY); + } +} diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 66bcdaa2..1bd4142c 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -293,6 +293,10 @@ namespace StardewModdingAPI.Framework MiniMonoModHotfix.Apply(); HarmonyPatcher.Apply("SMAPI", this.Monitor, new Game1Patcher(this.Reflection, this.OnLoadStageChanged), +#if SMAPI_FOR_MOBILE + new StringPatcher(this.Reflection), + new ThreadSilenceExitPatch(this.Monitor), +#endif new TitleMenuPatcher(this.OnLoadStageChanged) ); // new GamePatcher(this.Monitor).Apply( diff --git a/src/SMAPI/Metadata/InstructionMetadata.cs b/src/SMAPI/Metadata/InstructionMetadata.cs index 2a2c3bd1..36331087 100644 --- a/src/SMAPI/Metadata/InstructionMetadata.cs +++ b/src/SMAPI/Metadata/InstructionMetadata.cs @@ -58,34 +58,6 @@ namespace StardewModdingAPI.Metadata #if SMAPI_FOR_MOBILE // module rewrite for .Net 5 runtime assemblies - // yield return new TypeModuleReferenceRewriter("System", - // new [] - // { - // "System.Runtime" - // }, - // new [] - // { - // "System.Collections.Generic.ISet`1" - // }, typeof(System.Collections.Generic.ISet<>).Assembly); - // yield return new TypeModuleReferenceRewriter("System", - // new [] - // { - // "System.Runtime" - // }, - // new [] - // { - // "System.Collections.Generic.HashSet`1" - // }, typeof(System.Collections.Generic.HashSet<>).Assembly); - // yield return new TypeModuleReferenceRewriter("System.Runtime", - // new [] - // { - // "System.Runtime" - // }, - // new [] - // { - // "System.Collections.Generic.IReadOnlySet`1" - // }, typeof(System.Collections.Generic.IReadOnlySet<>).Assembly); - yield return new ModuleReferenceRewriter("System.*", "System.", new Version(5,0), new[] { typeof(System.Collections.CollectionBase).Assembly, @@ -94,6 +66,7 @@ namespace StardewModdingAPI.Metadata typeof(System.Xml.XmlDocument).Assembly, typeof(System.Xml.Linq.XComment).Assembly, typeof(System.Collections.Generic.IReadOnlySet<>).Assembly, + typeof(System.Data.DataTable).Assembly, }); yield return new TypeFieldToAnotherTypePropertyRewriter(typeof(Game1), typeof(Game1Methods), "onScreenMenus", "onScreenMenus"); @@ -119,6 +92,8 @@ namespace StardewModdingAPI.Metadata yield return new MethodParentRewriter(typeof(IClickableMenu), typeof(IClickableMenuMethods)); 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)); //Constructor Rewrites yield return new MethodParentRewriter(typeof(MapPage), typeof(MapPageMethods)); diff --git a/src/SMAPI/Patches/StringPatcher.cs b/src/SMAPI/Patches/StringPatcher.cs new file mode 100644 index 00000000..f879fcc7 --- /dev/null +++ b/src/SMAPI/Patches/StringPatcher.cs @@ -0,0 +1,81 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using HarmonyLib; +using StardewModdingAPI.Enums; +using StardewModdingAPI.Framework.Reflection; +using StardewModdingAPI.Internal.Patching; +using StardewValley; +using StardewValley.Menus; +using StardewValley.Minigames; + +namespace StardewModdingAPI.Patches +{ + /// Harmony patches for which notify SMAPI for save load stages. + /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")] + internal class StringPatcher : BasePatcher + { + /********* + ** Fields + *********/ + /// Simplifies access to private code. + private static Reflector Reflection = null!; // initialized in constructor + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// Simplifies access to private code. + /// A callback to invoke when the load stage changes. + public StringPatcher(Reflector reflection) + { + StringPatcher.Reflection = reflection; + } + + /// + public override void Apply(Harmony harmony, IMonitor monitor) + { + // detect CreatedInitialLocations and SaveAddedLocations + harmony.Patch( + original: this.RequireMethod(nameof(string.Split), new []{typeof(char), typeof(StringSplitOptions)}), + prefix: this.GetHarmonyMethod(nameof(StringPatcher.Before_Split)) + ); + harmony.Patch( + original: this.RequireMethod(nameof(string.Split), new []{typeof(string), typeof(StringSplitOptions)}), + prefix: this.GetHarmonyMethod(nameof(StringPatcher.Before_Split)) + ); + harmony.Patch( + original: this.RequireMethod(nameof(string.Split), new []{typeof(char), typeof(int), typeof(StringSplitOptions)}), + prefix: this.GetHarmonyMethod(nameof(StringPatcher.Before_Split)) + ); + harmony.Patch( + original: this.RequireMethod(nameof(string.Split), new []{typeof(string), typeof(int), typeof(StringSplitOptions)}), + prefix: this.GetHarmonyMethod(nameof(StringPatcher.Before_Split)) + ); + } + + + /********* + ** Private methods + *********/ + /// The method to call before . + /// Returns whether to execute the original method. + /// This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments. + private static bool Before_Split(string __instance, ref StringSplitOptions options) + { + if (3 == (uint)options) + { + options = StringSplitOptions.RemoveEmptyEntries; + } + else if(2 == (uint)options) + { + options = StringSplitOptions.None; + } + return true; + } + + } +} diff --git a/src/SMAPI/SMAPI.csproj b/src/SMAPI/SMAPI.csproj index 311376d5..a5cf652b 100644 --- a/src/SMAPI/SMAPI.csproj +++ b/src/SMAPI/SMAPI.csproj @@ -141,6 +141,7 @@ +