misto_fixes-Deep_North_Compat icon

Deep North Compat

Restores two Valheim 1.0 method overloads so mods built for the older game keep working. Needed by some older mods.

Last updated a week ago
Total downloads 452
Total rating 0 
Categories Mods
Dependency string misto_fixes-Deep_North_Compat-1.0.0
Dependants 1 other package depends on this package

This mod requires the following mods to function

denikson-BepInExPack_Valheim-5.4.2350 icon
denikson-BepInExPack_Valheim

BepInEx pack for Valheim. Preconfigured with the correct entry point for mods and preferred defaults for the community.

Preferred version: 5.4.2350

README

Deep_North_Compat

A tiny BepInEx patcher for Valheim 1.0 (Deep North). It re-adds two game methods whose signatures 1.0 changed by adding optional trailing parameters, so mods built for the older game keep working without modifying their files:

Restored (old) overload Forwards to (1.0)
Character.Message(type, msg, amount, icon) Message(type, msg, amount, icon, log = false)
SEMan.AddStatusEffect(hash, resetTime, itemLevel, skillLevel) same + variant = 0

Needed by older mods such as Smoothbrain's Mining, Ranching and ComfortTweaks. Once those authors update, this package is no longer needed. It does nothing if the game already has the old overload, and a failure in it never stops the game from starting.

Caveat: adding an overload makes lookups of that method by name only ambiguous, so this shims just these two. Mods that patch these methods without argument types could be affected.

AI disclosure: this patcher was written with AI assistance (Claude, by Anthropic) and then tested on a live Valheim 1.0 dedicated server.

MIT licensed. Source (the whole patcher):

MistookenCompat.cs
// BepInEx preloader patcher: restores API overloads that Valheim 1.0 (Deep North) removed by ADDING trailing
// parameters, so mods built for the older game keep working without modifying their DLLs. Own code; does not
// redistribute any mod. Each restored overload forwards to the new method, filling the new trailing parameters
// with their declared defaults (or default(T)). Only the whitelisted methods below are touched.
// CAUTION: every added overload makes name-only reflection/Harmony lookups of that method ambiguous.
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;

public static class MistookenCompatPatcher
{
    // Type, method, old parameter types (full names) that mods still call.
    static readonly string[][] Shims = new string[][]
    {
        new string[] { "Character", "Message", "MessageHud/MessageType", "System.String", "System.Int32", "UnityEngine.Sprite" },
        new string[] { "SEMan", "AddStatusEffect", "System.Int32", "System.Boolean", "System.Int32", "System.Single" },
        // Not shimmed on purpose: Piece.SetCreator, EffectList.Create, PlayerProfile.IncrementStat. Up-to-date mods
        // (e.g. PlantEverything) patch those BY NAME; a second overload makes Harmony's lookup ambiguous and breaks them.
    };

    public static IEnumerable<string> TargetDLLs
    {
        get { yield return "assembly_valheim.dll"; }
    }

    public static void Patch(AssemblyDefinition assembly)
    {
        foreach (string[] s in Shims)
        {
            try { AddForwardingOverload(assembly.MainModule, s[0], s[1], s.Skip(2).ToArray()); }
            catch (Exception) { /* a failed shim must never stop the game from starting */ }
        }
    }

    static bool ParamsMatch(MethodDefinition m, string[] oldTypes, int count)
    {
        if (m.Parameters.Count != count) return false;
        for (int i = 0; i < oldTypes.Length && i < count; i++)
            if (m.Parameters[i].ParameterType.FullName != oldTypes[i]) return false;
        return true;
    }

    static void AddForwardingOverload(ModuleDefinition module, string typeName, string methodName, string[] oldTypes)
    {
        TypeDefinition type = module.GetType(typeName);
        if (type == null) return;
        if (type.Methods.Any(m => m.Name == methodName && ParamsMatch(m, oldTypes, oldTypes.Length))) return; // already there

        MethodDefinition target = type.Methods.FirstOrDefault(m =>
            m.Name == methodName && m.Parameters.Count > oldTypes.Length && ParamsMatch(m, oldTypes, m.Parameters.Count));
        if (target == null) return;

        MethodAttributes attrs = MethodAttributes.Public | MethodAttributes.HideBySig;
        if (target.IsStatic) attrs |= MethodAttributes.Static;
        MethodDefinition shim = new MethodDefinition(methodName, attrs, target.ReturnType);
        shim.HasThis = !target.IsStatic;
        for (int i = 0; i < oldTypes.Length; i++)
            shim.Parameters.Add(new ParameterDefinition(target.Parameters[i].Name, ParameterAttributes.None, target.Parameters[i].ParameterType));

        shim.Body.InitLocals = true;
        ILProcessor il = shim.Body.GetILProcessor();
        if (!target.IsStatic) il.Emit(OpCodes.Ldarg_0);
        for (int i = 0; i < oldTypes.Length; i++) il.Emit(OpCodes.Ldarg, shim.Parameters[i]);
        for (int i = oldTypes.Length; i < target.Parameters.Count; i++) EmitDefault(shim, il, target.Parameters[i]);
        il.Emit(target.IsStatic || !target.IsVirtual ? OpCodes.Call : OpCodes.Callvirt, target);
        il.Emit(OpCodes.Ret);
        type.Methods.Add(shim);
    }

    static void EmitDefault(MethodDefinition shim, ILProcessor il, ParameterDefinition p)
    {
        if (p.HasConstant && p.Constant != null)
        {
            object c = p.Constant;
            if (c is bool) { il.Emit(((bool)c) ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0); return; }
            if (c is int) { il.Emit(OpCodes.Ldc_I4, (int)c); return; }
            if (c is short) { il.Emit(OpCodes.Ldc_I4, (int)(short)c); return; }
            if (c is byte) { il.Emit(OpCodes.Ldc_I4, (int)(byte)c); return; }
            if (c is float) { il.Emit(OpCodes.Ldc_R4, (float)c); return; }
        }
        TypeDefinition def = p.ParameterType.Resolve();
        if (def != null && def.IsValueType)
        {
            VariableDefinition loc = new VariableDefinition(p.ParameterType);
            shim.Body.Variables.Add(loc);
            il.Emit(OpCodes.Ldloca, loc);
            il.Emit(OpCodes.Initobj, p.ParameterType);
            il.Emit(OpCodes.Ldloc, loc);
        }
        else il.Emit(OpCodes.Ldnull);
    }
}