【问题标题】:C# Harmony transpiler crashing when using Call or CallVirtC# Harmony 编译器在使用 Call 或 CallVirt 时崩溃
【发布时间】:2022-01-31 03:30:31
【问题描述】:

我正在尝试修改游戏,并且我正在使用 Harmony 1.2.0.1。我一直在尝试使用转译器向 if 语句添加单独的条件,但是当我正在修补的方法运行时,游戏完全崩溃。我在日志中找不到任何错误,也无法弄清楚如何启用 Harmony 的调试器。

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\";
}

我很确定崩溃存在于 Call 或 CallVirt 指令中,因为我不明白为什么任何其他指令会崩溃。但是,无论我尝试过何种语法,每次运行它时它仍然会崩溃。我查了一下,人们只是说要么寻求帮助,要么看看别人的补丁。这么多没有帮助,它仍然在崩溃。发送帮助

    标签: c# patch cil transpiler


    【解决方案1】:

    更新:这只是一个将其变成无限循环的检查。下次我真的需要更仔细地检查IL代码。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2014-09-08
    • 2011-05-08
    相关资源
    最近更新 更多