The adventures of a newbie modder
Warning, this is not exactly a tutorial per se, more of a journey.
If this is not the right section, feel free to move it accordingly.
If this is not the right section, feel free to move it accordingly.

I'm a huge fan of 7 Days, it's the first game I've played since I ditched my Dreamcast, 20 years ago and honestly, it's pretty much the only game I play.
When A17 dropped, I was hyped AF, but the game had changed so much from previous iterations, that it just didn't click.
So I started modding the game. But just XML modding.
I'm a web developer and along the way, I've coded in C# for like a year.
I kept reading about modding using C# and stuff, but this was a bit unreal for me.
I've never decompiled a dll or anything like that and what I did on the job was far from game development.
All of this was still in the realm of magic for me.
But you guessed it, it was enough to pique my interest.
I've always wanted to dive into it at some point, but you know, tomorrow.
After reading this post https://7daystodie.com/forums/showthread.php?121830-Powered-Workstations
I decided it was time to kick myself in the rear and start trying.
First thing I did was to go to https://7d2dsdx.github.io/Tutorials/index.html
I downloaded the latest release of SDX (0.7.1)
I started reading the docs and tried to build the Cube Mod but got a System.BadImageFormatException (in Mono.Cecil.PE.ImageReader.ReadImage()).
I assumed something somewhere didn't play well with A17. (Bummer! =/)
However, I've read something about DMT on the forum, so I went to https://7d2dmods.github.io/HarmonyDocs/
I downloaded the latest release and started reading the docs.
Cool thing is, it's backward compatible with SDX so you can have everything you had with SDX plus new goodies. (Nice!)
I encourage anyone who wants to start modding with C# to read both, they're invaluable resources.
Phase 1:
I started with something preexisting from the docs:
Code:
// Sneak Damage pop up
[HarmonyPatch(typeof(EntityPlayerLocal))]
[HarmonyPatch("NotifySneakDamage")]
public class SphereII_ClearUI_NotifySneakDamage
{
static bool Prefix()
{
return false;
}
}
I needed to see the actual code, so I downloaded a free decompiler (I chose Progress by Telerik but more on that later).
This is the original method targeted by the example script:
Code:
public void NotifySneakDamage(float multiplier)
{
this.sneakDamageText = string.Format(Localization.Get("sneakDamageBonus", string.Empty), multiplier.ToCultureInvariantString("f1")).ToUpper();
this.sneakDamageBlendTimer.FadeIn();
}
Code:
[HarmonyPatch(typeof(EntityPlayerLocal))]
[HarmonyPatch("NotifySneakDamage")]
public class Sneaky_SneakDamage
{
static void Prefix()
{
this.sneakDamageText = "Sneaky!";
this.sneakDamageBlendTimer.FadeIn();
}
}
Reading the docs further, I saw that Harmony could inject a reference to the instance called __instance.
See here: https://7d2dmods.github.io/HarmonyDocs/index.htm?PrefixandPostfix.html
So next iteration:
Code:
[HarmonyPatch(typeof(EntityPlayerLocal))]
[HarmonyPatch("NotifySneakDamage")]
public class Sneaky_SneakDamage
{
static void Prefix(EntityPlayerLocal __instance)
{
__instance.sneakDamageText = "Sneaky!";
__instance.sneakDamageBlendTimer.FadeIn();
}
}
Reading the docs further (you should read it first), I understood that you could reference a private member by prefixing it with 3 underscores.
So here it goes:
Code:
using Harmony;
using System.Reflection;
using UnityEngine;
using DMT;
public class Sneaky
{
public class Sneaky_Init : IHarmony
{
public void Start()
{
Debug.Log(" Loading Patch: " + GetType().ToString());
var harmony = HarmonyInstance.Create(GetType().ToString());
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
}
[HarmonyPatch(typeof(EntityPlayerLocal))]
[HarmonyPatch("NotifySneakDamage")]
public class Sneaky_SneakDamage
{
static void Prefix(EntityPlayerLocal __instance, ref string ___sneakDamageText)
{
___sneakDamageText = "Sneaky!";
__instance.sneakDamageBlendTimer.FadeIn();
}
}
}
OK, this works, time for phase 2.