Survivalist: Invisible Strain

Survivalist: Invisible Strain

Not enough ratings
How to make Mods with Custom Code
By Bob
Use HarmonyLib to write mods that tweak the game's code.
   
Award
Favorite
Favorited
Unfavorite
Making a mod with custom code
I'm going to assume you have some familiarity with c# programming, and it also helps to have read this guide which explains how to set up your own mod.

First, you need to have a c# compiler and IDE, I use Microsoft Visual Studio 2022 Community Edition[my.visualstudio.com]. Install and run it, and go to File / New Project, Visual C#, Class Library (.NET Framework). Create a project called MyDLL (or whatever):


We use HarmonyLib[harmony.pardeike.net] to hook onto the Survivalist code. Download 0Harmony.dll from their site[github.com] and place it in your MyDLL folder. In Visual Studio, right-click References, Browse, and select 0Harmony.dll to add it as a reference.

Visual Studio will have created one file in your project called Class1, rename it Main and replace their code with the following code:

using HarmonyLib; public class Main { public static Harmony HarmonyInstance; public static void Load() { HarmonyInstance = new Harmony("MyDLL"); HarmonyInstance.PatchAll(); } public static void Unload() { if (HarmonyInstance != null) { HarmonyInstance.UnpatchAll(); } } }

When the game loads your dll it will be looking for a class called Main and functions called Load and Unload. It's important that you implement Unload as well as Load, otherwise the game won't be able to unload your mod if people turn it off (unless they restart).

To be able refer to the game's functions, you'll need to add references to its dlls as well. You can find them by right-clicking the game in Steam, going to Properties, Local Files, Browse. They will be in Survivalist Invisible Strain_Data\Managed. You seem to need:

Assembly-CSharp.dll
Assembly-CSharp-firstpass.dll
netstandard.dll
UnityEngine.dll
UnityEngine.CoreModule.dll



Now you can use HarmonyLib to override some of the game's functions. It works by allowing you to add Prefix and Postfix functions - read the Harmony docs for more info, but here's a quick example:

[HarmonyPatch(typeof(ZombieSpawnPoint), "SpawnEnemyGroup")] static class ZombieSpawnPoint__SpawnEnemyGroup__Patch { static void Postfix(ref Community __result, ref ZombieSpawnPoint __instance) { // Add body armor to all the ambient zombies foreach (Character zombie in __result.Members) { EquipmentPrototype proto = GameImpl.Instance.PickRandomClothing("Military", ClothingType.BodyArmor, true, Session.Instance.DeterministicRand); if (proto != null) { Equipment armor = Equipment.Spawn(proto); zombie.Inventory.Add(zombie, armor); } } } }

Now go to Build -> Build Solution. it should produce a file called MyDLL.dll in C:\MyDLL\MyDLL\bin\Debug.

Create a Mod or Story for the game following this guide. Then in the Editor go to Open Folder, and in that folder create a directory called DLLs. Place your dll in here.

You'll also need 0Harmony.dll, you could copy it into that folder but it's perhaps better to go to Story Settings and add Harmony 2.0.4 as a Dependency (you may need to Subscribe to it first in the workshop). This is a project set up by a community member which means multiple different mods can use that without each bundling their own copy of the dll with them. (Note: set that up first, before copying MyDLL.dll into the folder)

Now start a new game, select your mod, and look for some zombies!
1 Comments
Simo_Shiver 16 Jan, 2023 @ 10:58am 
What c# version :guard: