support mapping fields to a different type in FieldReplaceRewriter

This commit is contained in:
Jesse Plamondon-Willard 2020-08-24 21:39:50 -04:00
parent 94b8262692
commit 1bd67baae1
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
2 changed files with 20 additions and 6 deletions

View File

@ -25,6 +25,10 @@
* For SMAPI developers: * For SMAPI developers:
* The web API now returns an update alert in two new cases: any newer unofficial update (previously only shown if the mod was incompatible), and a newer prerelease version if the installed non-prerelease version is broken (previously only shown if the installed version was prerelease). * The web API now returns an update alert in two new cases: any newer unofficial update (previously only shown if the mod was incompatible), and a newer prerelease version if the installed non-prerelease version is broken (previously only shown if the installed version was prerelease).
* Internal refactoring to simplify game updates:
* Reorganised SMAPI core to reduce coupling to `Game1` and make it easier to navigate.
* `FieldToPropertyRewriter` now auto-rewrites broken field references into properties if possible, so we no longer need to map fields manually.
* `FieldReplaceRewriter` now supports mapping to a different target type.
* Internal refactoring to simplify future game updates. * Internal refactoring to simplify future game updates.
## 3.6.2 ## 3.6.2

View File

@ -25,18 +25,28 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
/********* /*********
** Public methods ** Public methods
*********/ *********/
/// <summary>Construct an instance.</summary>
/// <param name="fromType">The type whose field to rewrite.</param>
/// <param name="fromFieldName">The field name to rewrite.</param>
/// <param name="toType">The new type which will have the field.</param>
/// <param name="toFieldName">The new field name to reference.</param>
public FieldReplaceRewriter(Type fromType, string fromFieldName, Type toType, string toFieldName)
: base(defaultPhrase: $"{fromType.FullName}.{fromFieldName} field")
{
this.Type = fromType;
this.FromFieldName = fromFieldName;
this.ToField = toType.GetField(toFieldName);
if (this.ToField == null)
throw new InvalidOperationException($"The {toType.FullName} class doesn't have a {toFieldName} field.");
}
/// <summary>Construct an instance.</summary> /// <summary>Construct an instance.</summary>
/// <param name="type">The type whose field to rewrite.</param> /// <param name="type">The type whose field to rewrite.</param>
/// <param name="fromFieldName">The field name to rewrite.</param> /// <param name="fromFieldName">The field name to rewrite.</param>
/// <param name="toFieldName">The new field name to reference.</param> /// <param name="toFieldName">The new field name to reference.</param>
public FieldReplaceRewriter(Type type, string fromFieldName, string toFieldName) public FieldReplaceRewriter(Type type, string fromFieldName, string toFieldName)
: base(defaultPhrase: $"{type.FullName}.{fromFieldName} field") : this(type, fromFieldName, type, toFieldName)
{ {
this.Type = type;
this.FromFieldName = fromFieldName;
this.ToField = type.GetField(toFieldName);
if (this.ToField == null)
throw new InvalidOperationException($"The {type.FullName} class doesn't have a {toFieldName} field.");
} }
/// <summary>Rewrite a CIL instruction reference if needed.</summary> /// <summary>Rewrite a CIL instruction reference if needed.</summary>