Skip to content

Commit 56c4abd

Browse files
committed
Only activate jumpthru hooks when they are used in the map
1 parent 194fa6a commit 56c4abd

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

Entities/SidewaysJumpThru.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using MonoMod.Cil;
77
using MonoMod.RuntimeDetour;
88
using System;
9+
using System.Linq;
910
using System.Reflection;
1011

1112
namespace Celeste.Mod.SpringCollab2020.Entities {
@@ -17,7 +18,46 @@ class SidewaysJumpThru : Entity {
1718

1819
private static FieldInfo actorMovementCounter = typeof(Actor).GetField("movementCounter", BindingFlags.Instance | BindingFlags.NonPublic);
1920

21+
private static bool hooksActive = false;
22+
2023
public static void Load() {
24+
On.Celeste.LevelLoader.ctor += onLevelLoad;
25+
On.Celeste.OverworldLoader.ctor += onOverworldLoad;
26+
}
27+
28+
public static void Unload() {
29+
On.Celeste.LevelLoader.ctor -= onLevelLoad;
30+
On.Celeste.OverworldLoader.ctor -= onOverworldLoad;
31+
32+
deactivateHooks();
33+
}
34+
35+
private static void onLevelLoad(On.Celeste.LevelLoader.orig_ctor orig, LevelLoader self, Session session, Vector2? startPosition) {
36+
orig(self, session, startPosition);
37+
38+
if (session.MapData?.Levels?.Any(level => level.Entities?.Any(entity => entity.Name == "SpringCollab2020/SidewaysJumpThru") ?? false) ?? false) {
39+
activateHooks();
40+
} else {
41+
deactivateHooks();
42+
}
43+
}
44+
45+
private static void onOverworldLoad(On.Celeste.OverworldLoader.orig_ctor orig, OverworldLoader self, Overworld.StartMode startMode, HiresSnow snow) {
46+
orig(self, startMode, snow);
47+
48+
if (startMode != (Overworld.StartMode) (-1)) { // -1 = in-game overworld from the collab utils
49+
deactivateHooks();
50+
}
51+
}
52+
53+
public static void activateHooks() {
54+
if (hooksActive) {
55+
return;
56+
}
57+
hooksActive = true;
58+
59+
Logger.Log(LogLevel.Info, "SpringCollab2020/SidewaysJumpThru", "=== Activating sideways jumpthru hooks");
60+
2161
string updateSpriteMethodToPatch = Everest.Loader.DependencyLoaded(new EverestModuleMetadata { Name = "Everest", Version = new Version(1, 1432) }) ?
2262
"orig_UpdateSprite" : "UpdateSprite";
2363

@@ -48,7 +88,14 @@ public static void Load() {
4888
On.Celeste.Player.NormalUpdate += onPlayerNormalUpdate;
4989
}
5090

51-
public static void Unload() {
91+
public static void deactivateHooks() {
92+
if (!hooksActive) {
93+
return;
94+
}
95+
hooksActive = false;
96+
97+
Logger.Log(LogLevel.Info, "SpringCollab2020/SidewaysJumpThru", "=== Deactivating sideways jumpthru hooks");
98+
5299
On.Celeste.Actor.MoveHExact -= onActorMoveHExact;
53100
On.Celeste.Platform.MoveHExactCollideSolids -= onPlatformMoveHExactCollideSolids;
54101

Entities/UpsideDownJumpThru.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,48 @@ class UpsideDownJumpThru : JumpThru {
2121

2222
private static ILHook playerOrigUpdateHook;
2323

24-
private static readonly Hitbox normalHitbox = new Hitbox(8f, 11f, -4f, -11f);
24+
private static bool hooksActive = false;
2525

26+
private static readonly Hitbox normalHitbox = new Hitbox(8f, 11f, -4f, -11f);
27+
2628
public static void Load() {
29+
On.Celeste.LevelLoader.ctor += onLevelLoad;
30+
On.Celeste.OverworldLoader.ctor += onOverworldLoad;
31+
}
32+
33+
public static void Unload() {
34+
On.Celeste.LevelLoader.ctor -= onLevelLoad;
35+
On.Celeste.OverworldLoader.ctor -= onOverworldLoad;
36+
deactivateHooks();
37+
}
38+
39+
private static void onLevelLoad(On.Celeste.LevelLoader.orig_ctor orig, LevelLoader self, Session session, Vector2? startPosition) {
40+
orig(self, session, startPosition);
41+
42+
if (session.MapData?.Levels?.Any(level => level.Entities?.Any(entity => entity.Name == "SpringCollab2020/UpsideDownJumpThru") ?? false) ?? false) {
43+
activateHooks();
44+
} else {
45+
deactivateHooks();
46+
}
47+
}
48+
49+
private static void onOverworldLoad(On.Celeste.OverworldLoader.orig_ctor orig, OverworldLoader self, Overworld.StartMode startMode, HiresSnow snow) {
50+
orig(self, startMode, snow);
51+
52+
if (startMode != (Overworld.StartMode) (-1)) { // -1 = in-game overworld from the collab utils
53+
deactivateHooks();
54+
}
55+
}
56+
57+
58+
public static void activateHooks() {
59+
if (hooksActive) {
60+
return;
61+
}
62+
hooksActive = true;
63+
64+
Logger.Log(LogLevel.Info, "SpringCollab2020/UpsideDownJumpThru", "=== Activating upside-down jumpthru hooks");
65+
2766
using (new DetourContext { Before = { "*" } }) { // these don't always call the orig methods, better apply them first.
2867
// fix general actor/platform behavior to make them comply with jumpthrus.
2968
On.Celeste.Actor.MoveVExact += onActorMoveVExact;
@@ -50,7 +89,14 @@ public static void Load() {
5089
}
5190
}
5291

53-
public static void Unload() {
92+
public static void deactivateHooks() {
93+
if (!hooksActive) {
94+
return;
95+
}
96+
hooksActive = false;
97+
98+
Logger.Log(LogLevel.Info, "SpringCollab2020/UpsideDownJumpThru", "=== Deactivating upside-down jumpthru hooks");
99+
54100
On.Celeste.Actor.MoveVExact -= onActorMoveVExact;
55101
On.Celeste.Platform.MoveVExactCollideSolids -= onPlatformMoveVExactCollideSolids;
56102

0 commit comments

Comments
 (0)