Lib.Harmony 2.0.2

There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Lib.Harmony --version 2.0.2
NuGet\Install-Package Lib.Harmony -Version 2.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lib.Harmony" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Lib.Harmony --version 2.0.2
#r "nuget: Lib.Harmony, 2.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Lib.Harmony as a Cake Addin
#addin nuget:?package=Lib.Harmony&version=2.0.2

// Install Lib.Harmony as a Cake Tool
#tool nuget:?package=Lib.Harmony&version=2.0.2

Introduction

Harmony - a library for patching, replacing and decorating .NET methods during runtime.

Prerequisites

Harmony works with all languages that compile to CIL, Microsofts intermediate byte code language. This is foremost the .NET Framework and of course Mono - used by the game engine Unity.

The exception is probably Unity .NET Standard profile, which does not provide the functionality to fully create methods on the fly at runtime.

Bootstrapping and Injection

Harmony does not provide you with a way to run your own code within an application that is not designed to execute foreign code. You need a way to inject at least the few lines that start the Harmony patching and this is usually done with a loader. Here are some common examples of loaders (incomplete):

You need to find your own injection method or choose a game that supports user dll loading (usually called Mods) like for example RimWorld (Wiki).

Dependencies

It has no other dependencies and will most likely work in other environments too. Harmony was tested on PC, Mac and Linux and support 32- and 64-bit. For a typical Unity target, simply set your project to .Net 3.5 or Mono 2.x and include the Harmony dll.

Altering functionality (Patching)

In general, if you want to change how an exising C# application like a game works and you don't have the source code for that application, you have basically two principles to do that:

  1. Alter dll files on disk
  2. Re-point method implementations (hooking)

Depending on the needs and situation, altering dll files is not always a desirable solution. For example

  • it has legal implications
  • it might be blocked by an anti-cheat system
  • it does not coordinate nicely with multiple concurrent changes
  • it has to be done before and outside the original application

Harmony uses a variation of hooking and focuces only on runtime changes that don't affect files on disk:

  • less conflicts with multiple mods
  • supports existing mod loaders
  • changes can be made dynamically/conditionally
  • the patch order can be flexible
  • other mods can be patched too
  • less legal issues

How Harmony works

Where other patch libraries simply allow you to replace the original method, Harmony goes one step further and gives you:

  • A way to keep the original method intact
  • Execute your code before and/or after the original method
  • Modify the original with IL code processors
  • Multiple Harmony patches co-exist and don't conflict with each other

alternate text is missing from this package README image

Limits of runtime patching

note Harmony can't do everything. Make sure you understand the following:

  • With Harmony, you only manipulate methods. This includes constructors and getters/setters.

  • You can only work with methods that have an actual IL code body, which means that they appear in a dissassembler like dnSpy.

  • Methods that are too small might get inlined and your patches will not run.

  • You cannot add fields to classes and you cannot extend enums (they get compiled into ints).

  • Patching generic methods or methods in generic classes is tricky and might not work as expected.

Hello World Example

Original game code:

[!code-csharpexample]

Patching with Harmony annotations:

[!code-csharpexample]

Alternatively, manual patching with reflection:

[!code-csharpexample]

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 is compatible.  netcoreapp3.1 is compatible. 
.NET Framework net35 is compatible.  net40 was computed.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.0

  • .NETCoreApp 3.1

  • .NETFramework 3.5

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.

NuGet packages (69)

Showing the top 5 NuGet packages that depend on Lib.Harmony:

Package Downloads
ClownFish.Nebula.net

简单易用的应用程序开发框架,支持多种架构模式(SaaS多租户/微服务/单体/模块化),内置强大的日志和监控能力。

OpenMod.Core The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Core service implementations for OpenMod

Allure.Xunit

Create beautiful reports from your xUnit.net tests.

WpfPilot

Next generation WPF automation testing.

Allure.SpecFlow

Create beautiful reports from your SpecFlow tests.

GitHub repositories (34)

Showing the top 5 popular GitHub repositories that depend on Lib.Harmony:

Repository Stars
Sonarr/Sonarr
Smart PVR for newsgroup and bittorrent users.
Revolutionary-Games/Thrive
The main repository for the development of the evolution game Thrive.
JetBrains/resharper-unity
Unity support for both ReSharper and Rider
SychicBoy/NETReactorSlayer
An open source (GPLv3) deobfuscator and unpacker for Eziriz .NET Reactor
skyarkhangel/Hardcore-SK
Rimworld Hardcore SK project, our discord server:
Version Downloads Last updated
2.3.3 2,319 3/29/2024
2.2.2 479,627 7/19/2022
1.2.0.1 169,425 8/8/2018