|
| 1 | +using Celeste.Mod.CollabUtils2.Entities; |
| 2 | +using Celeste.Mod.Entities; |
| 3 | +using Microsoft.Xna.Framework; |
| 4 | +using Mono.Cecil; |
| 5 | +using Mono.Cecil.Cil; |
| 6 | +using Monocle; |
| 7 | +using MonoMod.Cil; |
| 8 | +using MonoMod.RuntimeDetour; |
| 9 | +using System; |
| 10 | + |
| 11 | +namespace Celeste.Mod.CollabUtils2.Triggers { |
| 12 | + [CustomEntity("CollabUtils2/SilverBerryCollectTrigger")] |
| 13 | + [Tracked] |
| 14 | + class SilverBerryCollectTrigger : Trigger { |
| 15 | + private static ILHook strawberryUpdateHook = null; |
| 16 | + |
| 17 | + public static void Load() { |
| 18 | + strawberryUpdateHook = new ILHook(typeof(Strawberry).GetMethod("orig_Update"), hookStrawberryUpdate); |
| 19 | + } |
| 20 | + |
| 21 | + public static void Unload() { |
| 22 | + strawberryUpdateHook?.Dispose(); |
| 23 | + strawberryUpdateHook = null; |
| 24 | + } |
| 25 | + |
| 26 | + public SilverBerryCollectTrigger(EntityData data, Vector2 offset) : base(data, offset) { } |
| 27 | + |
| 28 | + private static void hookStrawberryUpdate(ILContext il) { |
| 29 | + ILCursor cursor = new ILCursor(il); |
| 30 | + |
| 31 | + while (cursor.TryGotoNext( |
| 32 | + instr => instr.OpCode == OpCodes.Callvirt && (instr.Operand as MethodReference)?.FullName == "System.Boolean Monocle.Entity::CollideCheck<Celeste.GoldBerryCollectTrigger>()")) { |
| 33 | + |
| 34 | + Logger.Log("CollabUtils2/SilverBerryCollectTrigger", $"Modding gold berry collect at {cursor.Index} in IL for Strawberry.orig_Update"); |
| 35 | + |
| 36 | + // argument 1 (player) is the same player that is used for the vanilla call, so clone it |
| 37 | + cursor.Emit(OpCodes.Dup); |
| 38 | + |
| 39 | + // argument 2 (orig) will be the result of the vanilla call to CollideCheck<GoldBerryCollectTrigger>() |
| 40 | + cursor.Index++; |
| 41 | + |
| 42 | + // argument 3 (self) is the strawberry that is being checked: the method we are hooking is part of it |
| 43 | + cursor.Emit(OpCodes.Ldarg_0); |
| 44 | + |
| 45 | + cursor.EmitDelegate<Func<Player, bool, Strawberry, bool>>((player, orig, self) => { |
| 46 | + // collect golden berry and derivatives if the player is in a golden berry trigger, OR if it is a silver berry and the player is in a silver berry collect trigger. |
| 47 | + return orig || ((self is SilverBerry) && player.CollideCheck<SilverBerryCollectTrigger>()); |
| 48 | + }); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments