Modding:Migrate to Harmony 2.0
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.
For modders
What's new?
See the Harmony 2.0 release notes and Harmony 2.0 documentation for more info. Harmony 2.0 also has stricter validation in general, so invalid patches that would previously work (e.g. setting __result to the wrong type) will now cause errors.
Migration steps
- 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. - Change
using Harmony;
tousing HarmonyLib;
. - Change
HarmonyInstance harmony = HarmonyInstance.Create("your mod id");
toHarmony harmony = new Harmony("your mod id");
. - 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).
Mod compatibility
SMAPI automatically rewrites most Harmony 1.x mods for compatibility, though some mods may break due to Harmony 2.0 changes that can't be handled automatically (e.g. stricter validation). This rewriting will be removed in SMAPI 4.0, so mod authors should update their code when possible even if it still works for now.