|
| 1 | +using Celeste.Mod.Helpers; |
| 2 | +using Mono.Cecil.Cil; |
| 3 | +using MonoMod.Cil; |
| 4 | +using MonoMod.RuntimeDetour; |
| 5 | +using System.Reflection; |
| 6 | + |
| 7 | +namespace Celeste.Mod.CommunalHelper.Entities; |
| 8 | + |
| 9 | +[CustomEntity("CommunalHelper/DisableAutoCameraOffsetController")] |
| 10 | +[Tracked] |
| 11 | +public class DisableAutoCameraOffsetController(EntityData data, Vector2 offset) : Entity(data.Position + offset) |
| 12 | +{ |
| 13 | + private readonly string flag = data.Attr("flag"); |
| 14 | + |
| 15 | + private bool IsActive() |
| 16 | + => string.IsNullOrEmpty(flag) || SceneAs<Level>().Session.GetFlag(flag); |
| 17 | + |
| 18 | + #region Hooks |
| 19 | + |
| 20 | + private static ILHook ilHook_Player_get_CameraTarget; |
| 21 | + |
| 22 | + internal static void Load() |
| 23 | + { |
| 24 | + ilHook_Player_get_CameraTarget = new ILHook(typeof(Player).GetMethod("get_CameraTarget", BindingFlags.Public | BindingFlags.Instance)!, Player_get_CameraTarget); |
| 25 | + } |
| 26 | + |
| 27 | + internal static void Unload() |
| 28 | + { |
| 29 | + ilHook_Player_get_CameraTarget.Dispose(); |
| 30 | + ilHook_Player_get_CameraTarget = null; |
| 31 | + } |
| 32 | + |
| 33 | + private static void Player_get_CameraTarget(ILContext il) |
| 34 | + { |
| 35 | + ILCursor cursor = new(il); |
| 36 | + |
| 37 | + // add check for controller being active to the check for StReflectionFall |
| 38 | + /* |
| 39 | + * IL_0062: ldarg.0 |
| 40 | + * IL_0063: ldfld class Monocle.StateMachine Celeste.Player::StateMachine |
| 41 | + * IL_0068: callvirt instance int32 Monocle.StateMachine::get_State() |
| 42 | + * IL_0032: ldc.i4.s 18 |
| 43 | + * IL_0034: beq.s IL_0062 |
| 44 | + */ |
| 45 | + if (!cursor.TryGotoNextBestFit(MoveType.Before, |
| 46 | + instr => instr.MatchLdarg(0), |
| 47 | + instr => instr.MatchLdfld<Player>("StateMachine"), |
| 48 | + instr => instr.MatchCallvirt<StateMachine>("get_State"), |
| 49 | + instr => instr.MatchLdcI4(Player.StReflectionFall), |
| 50 | + instr => instr.MatchBeq(out _))) |
| 51 | + return; |
| 52 | + |
| 53 | + ILCursor cameraOffsetLabelCursor = cursor.Clone(); |
| 54 | + ILLabel setCameraOffset = cameraOffsetLabelCursor.DefineLabel(); |
| 55 | + |
| 56 | + /* |
| 57 | + * IL_0036: ldloc.1 |
| 58 | + * IL_0037: ldarg.0 |
| 59 | + * IL_0038: ldfld class Celeste.Level Celeste.Player::level |
| 60 | + * IL_003d: ldflda valuetype [FNA]Microsoft.Xna.Framework.Vector2 Celeste.Level::CameraOffset |
| 61 | + */ |
| 62 | + if (!cameraOffsetLabelCursor.TryGotoNextBestFit(MoveType.Before, |
| 63 | + instr => instr.MatchLdloc(1), |
| 64 | + instr => instr.MatchLdarg(0), |
| 65 | + instr => instr.MatchLdfld<Player>("level"), |
| 66 | + instr => instr.MatchLdflda<Level>("CameraOffset"))) |
| 67 | + return; |
| 68 | + |
| 69 | + cameraOffsetLabelCursor.MarkLabel(setCameraOffset); |
| 70 | + |
| 71 | + cursor.Emit(OpCodes.Ldarg_0); |
| 72 | + cursor.EmitDelegate(IsControllerActive); |
| 73 | + cursor.Emit(OpCodes.Brtrue, setCameraOffset); |
| 74 | + |
| 75 | + // skip all the state-specific camera offsets if the controller is active |
| 76 | + /* |
| 77 | + * IL_0062: ldarg.0 |
| 78 | + * IL_0063: ldfld class Monocle.StateMachine Celeste.Player::StateMachine |
| 79 | + * IL_0068: callvirt instance int32 Monocle.StateMachine::get_State() |
| 80 | + * IL_006d: ldc.i4.s 19 |
| 81 | + * IL_006f: bne.un.s IL_00ae |
| 82 | + */ |
| 83 | + if (!cursor.TryGotoNextBestFit(MoveType.Before, |
| 84 | + instr => instr.MatchLdarg(0), |
| 85 | + instr => instr.MatchLdfld<Player>("StateMachine"), |
| 86 | + instr => instr.MatchCallvirt<StateMachine>("get_State"), |
| 87 | + instr => instr.MatchLdcI4(Player.StStarFly), |
| 88 | + instr => instr.MatchBneUn(out _))) |
| 89 | + return; |
| 90 | + |
| 91 | + ILCursor skipStateOffsetLabelCursor = cursor.Clone(); |
| 92 | + ILLabel skipStateOffset = skipStateOffsetLabelCursor.DefineLabel(); |
| 93 | + |
| 94 | + /* |
| 95 | + * IL_013c: ldarg.0 |
| 96 | + * IL_013d: ldflda valuetype [FNA]Microsoft.Xna.Framework.Vector2 Celeste.Player::CameraAnchorLerp |
| 97 | + * IL_0142: call instance float32 [FNA]Microsoft.Xna.Framework.Vector2::Length() |
| 98 | + * IL_0147: ldc.r4 0.0 |
| 99 | + * IL_014c: ble.un IL_024d |
| 100 | + */ |
| 101 | + if (!skipStateOffsetLabelCursor.TryGotoNextBestFit(MoveType.Before, |
| 102 | + instr => instr.MatchLdarg(0), |
| 103 | + instr => instr.MatchLdflda<Player>("CameraAnchorLerp"), |
| 104 | + instr => instr.MatchCall<Vector2>("Length"), |
| 105 | + instr => instr.MatchLdcR4(0f), |
| 106 | + instr => instr.MatchBleUn(out _))) |
| 107 | + return; |
| 108 | + |
| 109 | + skipStateOffsetLabelCursor.MarkLabel(skipStateOffset); |
| 110 | + |
| 111 | + cursor.Emit(OpCodes.Ldarg_0); |
| 112 | + cursor.EmitDelegate(IsControllerActive); |
| 113 | + cursor.Emit(OpCodes.Brtrue, skipStateOffset); |
| 114 | + } |
| 115 | + |
| 116 | + private static bool IsControllerActive(Player player) |
| 117 | + => player.Scene?.Tracker?.GetEntity<DisableAutoCameraOffsetController>() is { } controller && controller.IsActive(); |
| 118 | + |
| 119 | + #endregion |
| 120 | +} |
0 commit comments