Skip to content

Commit

Permalink
Only activate jumpthru hooks when they are used in the map
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie480 committed Aug 22, 2021
1 parent 194fa6a commit 56c4abd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
49 changes: 48 additions & 1 deletion Entities/SidewaysJumpThru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using MonoMod.Cil;
using MonoMod.RuntimeDetour;
using System;
using System.Linq;
using System.Reflection;

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

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

private static bool hooksActive = false;

public static void Load() {
On.Celeste.LevelLoader.ctor += onLevelLoad;
On.Celeste.OverworldLoader.ctor += onOverworldLoad;
}

public static void Unload() {
On.Celeste.LevelLoader.ctor -= onLevelLoad;
On.Celeste.OverworldLoader.ctor -= onOverworldLoad;

deactivateHooks();
}

private static void onLevelLoad(On.Celeste.LevelLoader.orig_ctor orig, LevelLoader self, Session session, Vector2? startPosition) {
orig(self, session, startPosition);

if (session.MapData?.Levels?.Any(level => level.Entities?.Any(entity => entity.Name == "SpringCollab2020/SidewaysJumpThru") ?? false) ?? false) {
activateHooks();
} else {
deactivateHooks();
}
}

private static void onOverworldLoad(On.Celeste.OverworldLoader.orig_ctor orig, OverworldLoader self, Overworld.StartMode startMode, HiresSnow snow) {
orig(self, startMode, snow);

if (startMode != (Overworld.StartMode) (-1)) { // -1 = in-game overworld from the collab utils
deactivateHooks();
}
}

public static void activateHooks() {
if (hooksActive) {
return;
}
hooksActive = true;

Logger.Log(LogLevel.Info, "SpringCollab2020/SidewaysJumpThru", "=== Activating sideways jumpthru hooks");

string updateSpriteMethodToPatch = Everest.Loader.DependencyLoaded(new EverestModuleMetadata { Name = "Everest", Version = new Version(1, 1432) }) ?
"orig_UpdateSprite" : "UpdateSprite";

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

public static void Unload() {
public static void deactivateHooks() {
if (!hooksActive) {
return;
}
hooksActive = false;

Logger.Log(LogLevel.Info, "SpringCollab2020/SidewaysJumpThru", "=== Deactivating sideways jumpthru hooks");

On.Celeste.Actor.MoveHExact -= onActorMoveHExact;
On.Celeste.Platform.MoveHExactCollideSolids -= onPlatformMoveHExactCollideSolids;

Expand Down
50 changes: 48 additions & 2 deletions Entities/UpsideDownJumpThru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,48 @@ class UpsideDownJumpThru : JumpThru {

private static ILHook playerOrigUpdateHook;

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

private static readonly Hitbox normalHitbox = new Hitbox(8f, 11f, -4f, -11f);

public static void Load() {
On.Celeste.LevelLoader.ctor += onLevelLoad;
On.Celeste.OverworldLoader.ctor += onOverworldLoad;
}

public static void Unload() {
On.Celeste.LevelLoader.ctor -= onLevelLoad;
On.Celeste.OverworldLoader.ctor -= onOverworldLoad;
deactivateHooks();
}

private static void onLevelLoad(On.Celeste.LevelLoader.orig_ctor orig, LevelLoader self, Session session, Vector2? startPosition) {
orig(self, session, startPosition);

if (session.MapData?.Levels?.Any(level => level.Entities?.Any(entity => entity.Name == "SpringCollab2020/UpsideDownJumpThru") ?? false) ?? false) {
activateHooks();
} else {
deactivateHooks();
}
}

private static void onOverworldLoad(On.Celeste.OverworldLoader.orig_ctor orig, OverworldLoader self, Overworld.StartMode startMode, HiresSnow snow) {
orig(self, startMode, snow);

if (startMode != (Overworld.StartMode) (-1)) { // -1 = in-game overworld from the collab utils
deactivateHooks();
}
}


public static void activateHooks() {
if (hooksActive) {
return;
}
hooksActive = true;

Logger.Log(LogLevel.Info, "SpringCollab2020/UpsideDownJumpThru", "=== Activating upside-down jumpthru hooks");

using (new DetourContext { Before = { "*" } }) { // these don't always call the orig methods, better apply them first.
// fix general actor/platform behavior to make them comply with jumpthrus.
On.Celeste.Actor.MoveVExact += onActorMoveVExact;
Expand All @@ -50,7 +89,14 @@ public static void Load() {
}
}

public static void Unload() {
public static void deactivateHooks() {
if (!hooksActive) {
return;
}
hooksActive = false;

Logger.Log(LogLevel.Info, "SpringCollab2020/UpsideDownJumpThru", "=== Deactivating upside-down jumpthru hooks");

On.Celeste.Actor.MoveVExact -= onActorMoveVExact;
On.Celeste.Platform.MoveVExactCollideSolids -= onPlatformMoveVExactCollideSolids;

Expand Down

0 comments on commit 56c4abd

Please sign in to comment.