Search code examples
c#harmony

Harmony.PatchAll 'Cannot convert from System.Type' to 'System.Reflection.Assembly'


I'm here trying to repurpose a lethal company mod and using Harmony to patch the files, but when I try to build the dll file on vsc i keep getting this same error. I'll send the code. The error occurs at line 65 to 68.

using System;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GokuBracken.Patches;
using HarmonyLib;
using UnityEngine;

namespace GokuBracken.Core
{
    // Token: 0x0200000B RID: 11
    [BepInPlugin("", "", "")]
    public class GokuBrackenBase : BaseUnityPlugin
    {
        // Token: 0x1700001A RID: 26
        // (get) Token: 0x0600004C RID: 76 RVA: 0x000022D6 File Offset: 0x000004D6
        // (set) Token: 0x0600004D RID: 77 RVA: 0x000022DD File Offset: 0x000004DD
        internal static GokuBrackenBase Instance
        {
            get
            {
                return GokuBrackenBase._instance;
            }
            set
            {
                if (GokuBrackenBase._instance == null)
                {
                    GokuBrackenBase._instance = value;
                    return;
                }
                UnityEngine.Object.Destroy(value);
            }
        }

        // Token: 0x1700001B RID: 27
        // (get) Token: 0x0600004E RID: 78 RVA: 0x000022F9 File Offset: 0x000004F9
        public static ManualLogSource LogSource
        {
            get
            {
                return GokuBrackenBase.Instance.Logger;
            }
        }

        // Token: 0x1700001C RID: 28
        // (get) Token: 0x0600004F RID: 79 RVA: 0x00002305 File Offset: 0x00000505
        public static ConfigFile ModConfig
        {
            get
            {
                return GokuBrackenBase.Instance.Config;
            }
        }

        // Token: 0x06000050 RID: 80 RVA: 0x00002DD8 File Offset: 0x00000FD8
        private void Awake()
        {
            GokuBrackenBase.Instance = this;
            base.Logger.LogInfo("Initializing config...");
            ConfigManager.InitializeConfig();
            base.Logger.LogInfo("Loading asset bundle...");
            Assets.PopulateAssets();
            base.Logger.LogInfo("");
            base.Logger.LogInfo("");
            this.Harmony.PatchAll(typeof(GokuBrackenBase));
            this.Harmony.PatchAll(typeof(EnemyPatches));
            this.Harmony.PatchAll(typeof(TerminalPatches));
            this.Harmony.PatchAll(typeof(RoundManagementPatches));
        }

        // Token: 0x04000018 RID: 24
        private static GokuBrackenBase _instance;

        // Token: 0x04000019 RID: 25
        private readonly Harmony Harmony = new Harmony("");
    }
}

the specific lines that error are:

            this.Harmony.PatchAll(typeof(GokuBrackenBase));
            this.Harmony.PatchAll(typeof(EnemyPatches));
            this.Harmony.PatchAll(typeof(TerminalPatches));
            this.Harmony.PatchAll(typeof(RoundManagementPatches));

note that EnemyPatches, TerminalPatches, and RoundManagementPatches are cs files. I tried using Assembly.GetExecutingAssembly and GetCallingAssembly, but it still gives me a similar error.


Solution

  • PatchAll(Assembly)
    searches an assembly for Harmony annotations and uses them to create patches

    The output of typeof(anything) is a System.Type, a type does have a property that you can use to access the assembly, but if all your classes are .cs files in the same project then they will all return the same assembly, which means you normally only need one of these PatchAll invocations:

    this.Harmony.PatchAll(typeof(GokuBrackenBase).Assembly);
    

    I tried using Assembly.GetExecutingAssembly and GetCallingAssembly, but it still gives me a similar error.

    This error would be different to what you have posted and might be due to not having any annotations in your code files to configure your monkey patches.

    The first step should be to read the Harmony documentation, generally for modding there are other options, Harmony provides many advanced capabilities but you will need a better understanding of C# to get the most out of it as the documentation assumes an intermediate understanding of this type of development.