0
votes

I'm attempting to mod a game, and I'm using Harmony 1.2.0.1. I've been attempting to use a transpiler to add a separate condition to an if statement, however when the method that I am patching runs, the game completely crashes. I can't find any errors in the log, nor can I figure out how to enable Harmony's debugger.

public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator il)
{
    var code = new List<CodeInstruction>(instructions);
    int index = -1;
    Label jumpTo = il.DefineLabel();
    bool labeled = false;
    
    for (int i = 0; i < code.Count - 1; i++)
    {
        if (code[i].opcode == OpCodes.Stloc_2 && code[i + 1].opcode == OpCodes.Ldsfld)
        {
            index = i + 1;
            Debug.Log(i);
        }
        else if (code[i].operand == AccessTools.Field(typeof(Eat), nameof(Eat.lookupDir)) && !labeled)
        {
            code[i - 1].labels.Add(jumpTo);
            labeled = true;
            Debug.Log("label");
        }
    }

    MethodInfo getGameObject = AccessTools.Property(typeof(Component), nameof(Component.gameObject)).GetGetMethod();
    MethodInfo check = AccessTools.Method(typeof(ChangeEatPatch), nameof(ChangeEatPatch.IsObject), new Type[] { typeof(GameObject) });
    
    var instructionsToInsert = new List<CodeInstruction>();
    instructionsToInsert.Add(new CodeInstruction(OpCodes.Ldarg_0));
    instructionsToInsert.Add(new CodeInstruction(OpCodes.Call, getGameObject));
    instructionsToInsert.Add(new CodeInstruction(OpCodes.Callvirt, check));
    instructionsToInsert.Add(new CodeInstruction(OpCodes.Brtrue_S, jumpTo));

    if (index != -1) code.InsertRange(index, instructionsToInsert);
    return code;
}

public static bool IsObject(GameObject go)
{
    return GameObject.tag == "test";
}

I'm pretty sure the crash resides within the Call or CallVirt instructions, as I don't see why any other instructions would be crashing. However, regardless of any syntax I've tried, it still crashes every time I run it. I've looked it up, people just say to either ask for help, or look at other people's patches. That much hasn't been helpful, and it is still crashing. send help