Difference between revisions of "Modding:Migrate to Harmony 2.0"

From Stardew Valley Wiki
Jump to navigation Jump to search
(+ release notes, reorganize a bit, minor changes)
(+ troubleshooting > "You can only patch implemented methods/constructors")
Line 19: Line 19:
  
 
That's it! Otherwise usage should be identical.
 
That's it! Otherwise usage should be identical.
 +
 +
==Troubleshooting==
 +
==="You can only patch implemented methods/constructors"===
 +
Consider this example:
 +
<source lang="C#">
 +
public class GameLocation
 +
{
 +
  public virtual void cleanupBeforePlayerExit() {}
 +
}
 +
 +
public class Farm : GameLocation {}
 +
</source>
 +
 +
<tt>Farm.cleanupBeforePlayerExit</tt> doesn't exist, so <tt>Farm</tt> inherits it from <tt>GameLocation</tt>. Harmony 1.x would let you patch <tt>Farm.cleanupBeforePlayerExit</tt>, but in Harmony 2.x you must target the actual method (<tt>GameLocation.cleanupBeforePlayerExit</tt> in this example).
  
 
[[Category:Modding]]
 
[[Category:Modding]]

Revision as of 00:37, 6 May 2020

Index

The following describes the upcoming SMAPI 3.x, and may change before release.

This page is for modders. Players: see Modding:Mod compatibility instead.

This page explains how to update your mods for compatibility with Harmony 2.0. This only applies to mods which use Harmony directly; this is discouraged in most cases, isn't officially part of SMAPI's public API, and isn't subject to SMAPI's normal versioning policy.

What's new?

See the Harmony 2.0 release notes and Harmony 2.0 documentation for more info.

Migration steps

  1. Make sure you follow best practices outlined in the Harmony guide. In particular, use the EnableHarmony option (don't reference the Harmony DLL directly) and use the code API.
  2. Change using Harmony; to using HarmonyLib;.
  3. Change HarmonyInstance harmony = HarmonyInstance.Create("your mod id"); to Harmony harmony = new Harmony("your mod id");.
  4. Recompile the mod.

That's it! Otherwise usage should be identical.

Troubleshooting

"You can only patch implemented methods/constructors"

Consider this example:

public class GameLocation
{
   public virtual void cleanupBeforePlayerExit() {}
}

public class Farm : GameLocation {}

Farm.cleanupBeforePlayerExit doesn't exist, so Farm inherits it from GameLocation. Harmony 1.x would let you patch Farm.cleanupBeforePlayerExit, but in Harmony 2.x you must target the actual method (GameLocation.cleanupBeforePlayerExit in this example).