【发布时间】: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