Detouring an instance method with HugsLib

Started by Kyna Tiona, January 16, 2017, 08:04:15 PM

Previous topic - Next topic

Kyna Tiona

So I keep getting an "incompatible/corrupted mods" error when trying to detour an instance method, and I'm not sure what I'm doing wrong. I commented out the contents of the method and it still crashed, then uncommented them and replaced the method declaration with something normal and all was fine, so I'm pretty sure it's the function declaration I've messed up. Can anyone spot the mistake here?

using System;
using RimWorld;
using Verse;
using UnityEngine;
using SkillSpeed;
using HugsLib.Source.Detour;

namespace SkillSpeed.Detour
{
    public static class _SkillRecord
    {
        [DetourMethod(typeof(SkillRecord), "Interval")]
        public static void _Interval(this SkillRecord self)
        {
            // Snip
        }
    }
}

RawCode

"this" keyword inside argument section is for extension methods, please do not use any "funny" keywords like "this" or "new" and just do what stated inside library documentation.

provide method you are trying to inject and i will explain step by step how you should do it.

scuba156

Quote from: RawCode on January 17, 2017, 12:39:46 AM
"this" keyword inside argument section is for extension methods, please do not use any "funny" keywords like "this" or "new" and just do what stated inside library documentation.

provide method you are trying to inject and i will explain step by step how you should do it.
The 'this' argument on a detour is used for accessing the variables inside that instance among many, the other option would be writing another method that finds and returns the SkillRecord that needs to be accessed and then work on that, which is pointless when you can just get the instance.

using HugsLib.Source.Detour;
using RimWorld;

namespace test
{
    public static class test
    {
        [DetourMethod(typeof(SkillRecord), "Interval")]
        public static void _Interval(this SkillRecord self)
        {
            Verse.Log.Message("Detoured Interval");
        }
    }
}

The above code detours just fine for me. I would make sure you have the latest HugsLib in both your project references and as an installed RimWorld mod. Try it in a new project in case there is other code causing the issue.