TorchAPI / Torch

An extensible modding framework and improved client/DS for Space Engineers. Still a work in progress!
Apache License 2.0
146 stars 76 forks source link

Fix for MyComponentBase.Serialize error #542

Closed Allen-Wrench closed 1 year ago

Allen-Wrench commented 1 year ago

Im not familiar with the Torch patching methods, but here is a transpiler that works with harmony. It just adds a null check where it should be... Feel free to ignore this if you already have another solution.

[HarmonyTranspiler]
[HarmonyPatch(typeof(MyComponentBase), "Serialize")]
public static IEnumerable<CodeInstruction> SerializeFix(IEnumerable<CodeInstruction> instructions, ILGenerator gen)
{
    Label label = gen.DefineLabel();
    LocalBuilder local = gen.DeclareLocal(typeof(MyObjectBuilder_ComponentBase));

    foreach (CodeInstruction code in instructions)
    {
        if (code.opcode == OpCodes.Dup)
        {
                yield return new CodeInstruction(OpCodes.Stloc_0);
                yield return new CodeInstruction(OpCodes.Ldloc_0);
                yield return new CodeInstruction(OpCodes.Brfalse, label);
            yield return new CodeInstruction(OpCodes.Ldloc_0);
        }
            else if (code.opcode == OpCodes.Ret)
            {
                yield return new CodeInstruction(OpCodes.Ldloc_0).WithLabels(label);
            yield return code;
            }
            else
            {
                yield return code;
            }
    }
}
Bishbash777 commented 1 year ago

Thank you for this. Keen have already acknowledged the error so if I got a spare few, I will implement if I come to find that the next hotfix its not near