- PanzerKadaverintern
- Posts : 8
Reputation : 0
Join date : 2019-08-31
Modding Tutorial : Use Harmony to patch game's dll
Mon Apr 27, 2020 10:31 pm
Modding Tutorial : Use Harmony to patch game's dll
Hi everyone,
I'm proud to present to you a small tutorial on how to use Harmony (https://github.com/pardeike/Harmony) to patch the game's dll to add/modify game's features.
STEP 0 : Prerequisites
STEP 1 : Preparing the game's dll to load a mod dll
1] Get the last version of dnSpy here : https://github.com/0xd4d/dnSpy/releases
I advise you to get the "dnSpy-netcore-win64.zip" file, as your computer may not have the correct .NET Framework installed
2] Extract dnSpy in a folder, then launch "dnSpy.exe"
3] With dnSpy, open the game dll located at "GameFolder\ProjectHospital_Data\Managed\Assembly-CSharp.dll"
[If you are not used to modifying a dll on the fly with dnSpy, you can skip now and go to step 2. Patched dll download links are provided in the next post of this one]
4] In the dll main namespace, look for TitleScreenController class
5] Overwrite LoadAddons method code with the following code : https://pastebin.com/UbM1whPD
This patched method allows the game's mod loader to look if a mod folder contains a Harmony dll and a ModName.dll and load it.
6] Hit "Save all" button (CTRL+SHIFT+S)
STEP 2 : Set up the mod folder
1] Create a new folder in "GameFolder\ProjectHospital_Data\StreamingAssets\Addons\", named after your mod name. Be sure this name does not contain any space characters.
2] Inside this folder, copy 0Harmony.dll
3] With Visual Studio (Community) create a new Class Library inside the newly created mod folder, named after your mod name (the same name of the previously created folder).
4] Open the AssemblyInfo.cs file and remove the following fields :
5] Delete "Class1.cs" solution's file
6] Create a new class named "ModEntryPoint.cs" and paste the following code : https://pastebin.com/kzw3fQ70
7] Replace <modname>, <modversion>, <gameversion> and <yourpseudo> occurrences with relevant data
8] Inside project's references, add the following references :
9] Inside Project's Settings, change the Application > Build > Output path (both for Debug and Release configurations) to
10] Hit compile to check if everything is alright
11] Inside your mod folder, you should now have two dll : 0Harmony.dll and YourMod.dll (see the following screenshot for reference)
12] Congrats, your mod folder and solution are correctly setup !
STEP 2 : Read the game's dll
1] Using dnSpy, read trough game's dll to locate interesting methods
STEP 3 : Use Harmony patches (Prefix and Postfix methods) to modify the game's features
1] Harmony API : https://harmony.pardeike.net/articles/intro.html
2] See my mod "Doctor Mode : Perks and Cost" as a working example on how to patch game's methods (Link : https://projecthospital.forumotion.com/t2449-modharmony-doctor-mode-perks-and-cost#8862 )
If you have coding question, feel free to use this thread to ask !
Hi everyone,
I'm proud to present to you a small tutorial on how to use Harmony (https://github.com/pardeike/Harmony) to patch the game's dll to add/modify game's features.
STEP 0 : Prerequisites
- Visual studio 2017 or higher. Community (it's free !) works too. Free version available here : https://visualstudio.microsoft.com/fr/vs/
- .NET Frameworks 3.5 installed (you should be able to select this version when installing Visual Studio)
- Last version of dnSpy (see STEP 1)
- Last version of Harmony : https://github.com/pardeike/Harmony/releases
STEP 1 : Preparing the game's dll to load a mod dll
1] Get the last version of dnSpy here : https://github.com/0xd4d/dnSpy/releases
I advise you to get the "dnSpy-netcore-win64.zip" file, as your computer may not have the correct .NET Framework installed
2] Extract dnSpy in a folder, then launch "dnSpy.exe"
3] With dnSpy, open the game dll located at "GameFolder\ProjectHospital_Data\Managed\Assembly-CSharp.dll"
[If you are not used to modifying a dll on the fly with dnSpy, you can skip now and go to step 2. Patched dll download links are provided in the next post of this one]
4] In the dll main namespace, look for TitleScreenController class
5] Overwrite LoadAddons method code with the following code : https://pastebin.com/UbM1whPD
This patched method allows the game's mod loader to look if a mod folder contains a Harmony dll and a ModName.dll and load it.
6] Hit "Save all" button (CTRL+SHIFT+S)
STEP 2 : Set up the mod folder
1] Create a new folder in "GameFolder\ProjectHospital_Data\StreamingAssets\Addons\", named after your mod name. Be sure this name does not contain any space characters.
2] Inside this folder, copy 0Harmony.dll
3] With Visual Studio (Community) create a new Class Library inside the newly created mod folder, named after your mod name (the same name of the previously created folder).
4] Open the AssemblyInfo.cs file and remove the following fields :
- AssemblyTitle
- AssemblyFileVersion
- AssemblyVersion
5] Delete "Class1.cs" solution's file
6] Create a new class named "ModEntryPoint.cs" and paste the following code : https://pastebin.com/kzw3fQ70
7] Replace <modname>, <modversion>, <gameversion> and <yourpseudo> occurrences with relevant data
8] Inside project's references, add the following references :
- 0Harmony.dll
- UnityEngine.CoreModule.dll (GameFolder\ProjectHospital_Data\Managed\)
- Assembly-CSharp.dll (GameFolder\ProjectHospital_Data\Managed\)
9] Inside Project's Settings, change the Application > Build > Output path (both for Debug and Release configurations) to
- Code:
..\
10] Hit compile to check if everything is alright
11] Inside your mod folder, you should now have two dll : 0Harmony.dll and YourMod.dll (see the following screenshot for reference)
12] Congrats, your mod folder and solution are correctly setup !
STEP 2 : Read the game's dll
1] Using dnSpy, read trough game's dll to locate interesting methods
STEP 3 : Use Harmony patches (Prefix and Postfix methods) to modify the game's features
1] Harmony API : https://harmony.pardeike.net/articles/intro.html
2] See my mod "Doctor Mode : Perks and Cost" as a working example on how to patch game's methods (Link : https://projecthospital.forumotion.com/t2449-modharmony-doctor-mode-perks-and-cost#8862 )
If you have coding question, feel free to use this thread to ask !
- PanzerKadaverintern
- Posts : 8
Reputation : 0
Join date : 2019-08-31
Re: Modding Tutorial : Use Harmony to patch game's dll
Mon Apr 27, 2020 10:34 pm
Patched Assembly
Download the corresponding archive and extract the dll inside YourGameFolder\ProjectHospital_Data\Managed
Download the corresponding archive and extract the dll inside YourGameFolder\ProjectHospital_Data\Managed
- 1.2.19.532 hotfix1(last update)
- 1.2.19.532
- mishelle89resident
- Posts : 14
Reputation : 0
Join date : 2020-10-24
Re: Modding Tutorial : Use Harmony to patch game's dll
Sun Nov 29, 2020 7:49 am
Is this tutorial actual?
- jan.oxymorondeveloper
- Posts : 2309
Reputation : 336
Join date : 2018-03-23
Re: Modding Tutorial : Use Harmony to patch game's dll
Tue Dec 01, 2020 4:16 pm
mishelle89 wrote:Is this tutorial actual?
Hi, we're currently working on an official support for Harmony, so I plan to put together an up to date guide in the next few days.
Btw, thanks to @PanzerKadaver for leading the way!
mishelle89 likes this post
- mishelle89resident
- Posts : 14
Reputation : 0
Join date : 2020-10-24
Re: Modding Tutorial : Use Harmony to patch game's dll
Tue Dec 01, 2020 5:25 pm
jan.oxymoron wrote:mishelle89 wrote:Is this tutorial actual?
Hi, we're currently working on an official support for Harmony, so I plan to put together an up to date guide in the next few days.
Btw, thanks to @PanzerKadaver for leading the way!
Sounds good! It would be nice to know name of entry point at least. I tried with "ModEntryPoint", but looks like Start() never called.
Any way I will wait for official tutorial
- jan.oxymorondeveloper
- Posts : 2309
Reputation : 336
Join date : 2018-03-23
Re: Modding Tutorial : Use Harmony to patch game's dll
Tue Dec 01, 2020 5:29 pm
mishelle89 wrote:jan.oxymoron wrote:mishelle89 wrote:Is this tutorial actual?
Hi, we're currently working on an official support for Harmony, so I plan to put together an up to date guide in the next few days.
Btw, thanks to @PanzerKadaver for leading the way!
Sounds good! It would be nice to know name of entry point at least. I tried with "ModEntryPoint", but looks like Start() never called.
Any way I will wait for official tutorial
Hi, you'd still need to modify the game's dll according to the original guide to add the mod-loading code, but I think at this point it's worth it to wait for the official support in the upcoming patch.
Permissions in this forum:
You cannot reply to topics in this forum