Compatibility fix for mods with input box

This commit is contained in:
ZaneYork 2020-03-19 18:20:40 +08:00
parent 6451102075
commit 43c02b4ee6
5 changed files with 82 additions and 4 deletions

View File

@ -20,7 +20,7 @@ namespace StardewModdingAPI
** Public
****/
/// <summary>SMAPI's current semantic version.</summary>
public static ISemanticVersion ApiVersion { get; } = new Toolkit.SemanticVersion("3.3.2.3", allowNonStandard: true);
public static ISemanticVersion ApiVersion { get; } = new Toolkit.SemanticVersion("3.3.2.4", allowNonStandard: true);
/// <summary>The minimum supported version of Stardew Valley.</summary>
public static ISemanticVersion MinimumGameVersion { get; } = new GameVersion("1.4.5");

View File

@ -0,0 +1,66 @@
using System;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.Framework.ModLoading.Finders;
namespace StardewModdingAPI.Framework.ModLoading.Rewriters
{
internal class TypePropertyToAnotherTypeMethodRewriter : PropertyFinder
{
/*********
** Fields
*********/
/// <summary>The type whose field to which references should be rewritten to.</summary>
private readonly Type ToType;
/// <summary>The property name.</summary>
private readonly string GetterName;
/// <summary>The property name.</summary>
private readonly string SetterName;
/// <summary>The property name.</summary>
private readonly string PropertyName;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="type">The type whose field to which references should be rewritten.</param>
/// <param name="propertyName">The field name to rewrite.</param>
/// <param name="targetPropertyName">The property name (if different).</param>
public TypePropertyToAnotherTypeMethodRewriter(Type type, Type toType, string propertyName, string targetGetter = null, string targetSetter = null)
: base(type.FullName, propertyName, InstructionHandleResult.None)
{
this.ToType = toType;
this.PropertyName = propertyName;
this.GetterName = targetGetter;
this.SetterName = targetSetter;
}
/// <summary>Perform the predefined logic for an instruction if applicable.</summary>
/// <param name="module">The assembly module containing the instruction.</param>
/// <param name="cil">The CIL processor.</param>
/// <param name="instruction">The instruction to handle.</param>
/// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
/// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
if (!this.IsMatch(instruction))
return InstructionHandleResult.None;
MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
if (this.GetterName != null && methodRef.Name == "get_" + this.PropertyName)
{
methodRef = module.ImportReference(this.ToType.GetMethod(this.GetterName));
cil.Replace(instruction, cil.Create(OpCodes.Callvirt, methodRef));
return InstructionHandleResult.Rewritten;
}
if(this.SetterName != null && methodRef.Name == "set_" + this.PropertyName)
{
methodRef = module.ImportReference(this.ToType.GetMethod(this.SetterName));
cil.Replace(instruction, cil.Create(OpCodes.Callvirt, methodRef));
return InstructionHandleResult.Rewritten;
}
return InstructionHandleResult.None;
}
}
}

View File

@ -1,7 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
using StardewValley.Menus;
#pragma warning disable 1591 // missing documentation
@ -9,10 +8,20 @@ namespace StardewModdingAPI.Framework.RewriteFacades
{
public class TextBoxMethods : TextBox
{
public static void SelectedSetter(TextBox textBox, bool value)
{
if(!textBox.Selected && value)
{
typeof(TextBox).GetMethod("ShowAndroidKeyboard", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(textBox, new object[] { });
textBox.Selected = value;
}
else
textBox.Selected = value;
}
public TextBoxMethods(Texture2D textboxTexture, Texture2D caretTexture, SpriteFont font, Color textColor)
: base(textboxTexture, caretTexture, font, textColor, true, false)
{
}
}
}

View File

@ -72,6 +72,8 @@ namespace StardewModdingAPI.Metadata
yield return new TypeFieldToAnotherTypePropertyRewriter(typeof(AnimalQueryMenu), typeof(AnimalQueryMenuMethods), "allowReproductionButton", "AllowReproductionButtonProp");
yield return new TypeFieldToAnotherTypePropertyRewriter(typeof(AnimalQueryMenu), typeof(AnimalQueryMenuMethods), "sellButton", "SellButtonProp");
yield return new TypeFieldToAnotherTypePropertyRewriter(typeof(AnimalQueryMenu), typeof(AnimalQueryMenuMethods), "moveHomeButton", "MoveHomeButtonProp");
// TextBox fix
yield return new TypePropertyToAnotherTypeMethodRewriter(typeof(TextBox), typeof(TextBoxMethods), "Selected", null, "SelectedSetter");
// Rewrite Missing Type
yield return new TypeReferenceRewriter("StardewValley.Menus.CraftingPage", typeof(CraftingPageMobile));

View File

@ -246,6 +246,7 @@
<Compile Include="Framework\ModLoading\Rewriters\MethodParentRewriter.cs" />
<Compile Include="Framework\ModLoading\Rewriters\PropertyToFieldRewriter.cs" />
<Compile Include="Framework\ModLoading\Rewriters\StaticFieldToConstantRewriter.cs" />
<Compile Include="Framework\ModLoading\Rewriters\TypePropertyToAnotherTypeMethodRewriter.cs" />
<Compile Include="Framework\ModLoading\Rewriters\TypeFieldToAnotherTypePropertyRewriter.cs" />
<Compile Include="Framework\ModLoading\Rewriters\TypeFieldToAnotherTypeFieldRewriter.cs" />
<Compile Include="Framework\ModLoading\Rewriters\TypeReferenceRewriter.cs" />