Skip to content

Commit

Permalink
Swap order of operations in JumpThru.Awake for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
DemoJameson committed May 8, 2023
1 parent f454522 commit 2f4d16d
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Celeste.Mod.mm/Patches/JumpThru.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Mono.Cecil;
using Mono.Cecil.Cil;
using Monocle;
using MonoMod;
using MonoMod.Cil;
using MonoMod.InlineRT;
using MonoMod.Utils;
using System;

namespace Celeste {
internal class patch_JumpThru {
[MonoModIgnore]
[PatchJumpThruAwake]
public extern void Awake(Scene scene);
}
}

namespace MonoMod {
/*
* Patches the JumpThru.Awake method to do:
* if (staticMover.Platform == null && staticMover.IsRiding(this))
* instead of:
* if (staticMover.IsRiding(this) && staticMover.Platform == null),
*
* resulting in many less pointless calls to IsRiding.
*/
[MonoModCustomMethodAttribute(nameof(MonoModRules.PatchJumpThruAwake))]
class PatchJumpThruAwakeAttribute : Attribute { }

static partial class MonoModRules {
public static void PatchJumpThruAwake(ILContext context, CustomAttribute attrib) {
TypeDefinition t_StaticMover = MonoModRule.Modder.FindType("Celeste.StaticMover").Resolve();
FieldDefinition f_platform = t_StaticMover.FindField("Platform");

ILCursor cursor = new(context);
ILLabel label = null;
// find if (staticMover.IsRiding(this))
cursor.GotoNext(MoveType.Before,
instr => instr.MatchLdloc(1),
instr => instr.MatchLdarg(0),
instr => instr.MatchCallvirt("Celeste.StaticMover", "IsRiding"),
instr => instr.MatchBrfalse(out label)
);

// insert mover.Platform != null
cursor.Emit(OpCodes.Ldloc, 1);
cursor.Emit(OpCodes.Ldfld, f_platform);
cursor.Emit(OpCodes.Brtrue_S, label);

// go after the 4 instructions we matched
cursor.Index += 4;
// remove the old mover.Platform != null check, as we now perform it earlier
cursor.RemoveRange(3);
}
}
}

0 comments on commit 2f4d16d

Please sign in to comment.