-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swap order of operations in JumpThru.Awake for better performance
- Loading branch information
1 parent
f454522
commit 2f4d16d
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|