|
| 1 | +using Celeste.Mod.Entities; |
| 2 | +using Microsoft.Xna.Framework; |
| 3 | +using Mono.Cecil.Cil; |
| 4 | +using Monocle; |
| 5 | +using MonoMod.Cil; |
| 6 | +using MonoMod.RuntimeDetour; |
| 7 | +using System; |
| 8 | +using System.Reflection; |
| 9 | + |
| 10 | +namespace Celeste.Mod.MaxHelpingHand.Entities { |
| 11 | + [Tracked] |
| 12 | + [CustomEntity("MaxHelpingHand/SpikeRefillController")] |
| 13 | + public class SpikeRefillController : Entity { |
| 14 | + private static readonly MethodInfo m_OrigUpdate = typeof(Player).GetMethod("orig_Update"); |
| 15 | + private static ILHook Hook_OrigUpdate; |
| 16 | + |
| 17 | + public string Flag { get; private set; } |
| 18 | + public bool FlagInverted { get; private set; } |
| 19 | + |
| 20 | + private bool IsFlagSatisfied => string.IsNullOrWhiteSpace(Flag) || (FlagInverted ^ SceneAs<Level>().Session.GetFlag(Flag)); |
| 21 | + |
| 22 | + public SpikeRefillController(EntityData data, Vector2 offset) : base(data.Position + offset) { |
| 23 | + Flag = data.Attr("flag"); |
| 24 | + FlagInverted = data.Bool("flagInverted"); |
| 25 | + } |
| 26 | + |
| 27 | + public static void Load() { |
| 28 | + Hook_OrigUpdate = new ILHook(m_OrigUpdate, HookSpikeRefillPrevention); |
| 29 | + } |
| 30 | + |
| 31 | + public static void Unload() { |
| 32 | + Hook_OrigUpdate?.Dispose(); |
| 33 | + } |
| 34 | + |
| 35 | + private static void HookSpikeRefillPrevention(ILContext il) { |
| 36 | + ILCursor cursor = new ILCursor(il); |
| 37 | + |
| 38 | + if (!cursor.TryGotoNext(MoveType.After, instr => instr.MatchCall("Monocle.Entity", "System.Boolean CollideCheck<Celeste.Spikes>(Microsoft.Xna.Framework.Vector2)"))) { |
| 39 | + Logger.Log(LogLevel.Error, "MaxHelpingHand/SpikeRefillController", |
| 40 | + $"Could not find CollideCheck<Spikes> in {il.Method.FullName}!"); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + Logger.Log(LogLevel.Verbose, "MaxHelpingHand/SpikeRefillController", |
| 45 | + $"Hooking CollideCheck<Spikes> in {il.Method.FullName} @ {InstructionToString(cursor.Next)}"); |
| 46 | + |
| 47 | + cursor.Emit(OpCodes.Ldarg_0); |
| 48 | + cursor.EmitDelegate(OverrideRefillPreventionInSpikes); |
| 49 | + } |
| 50 | + |
| 51 | + private static bool OverrideRefillPreventionInSpikes(bool collidedWithSpikes, Player self) { |
| 52 | + // return true to prevent refills |
| 53 | + if (!collidedWithSpikes) |
| 54 | + return collidedWithSpikes; |
| 55 | + |
| 56 | + return !(self.Scene.Tracker.GetEntity<SpikeRefillController>()?.IsFlagSatisfied ?? false); |
| 57 | + } |
| 58 | + |
| 59 | + // stringifying branch instructions crashes because of monomod, very cool |
| 60 | + private static string InstructionToString(Instruction instr) { |
| 61 | + Instruction toStringify = instr; |
| 62 | + if (instr.Operand is ILLabel target) { |
| 63 | + toStringify = Instruction.Create(toStringify.OpCode, target.Target); |
| 64 | + toStringify.Offset = instr.Offset; |
| 65 | + } |
| 66 | + return toStringify.ToString(); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments