-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from SnipUndercover/spike-refill-controller
Implement Spike Refill Controllers
- Loading branch information
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Celeste.Mod.Entities; | ||
using Microsoft.Xna.Framework; | ||
using Mono.Cecil.Cil; | ||
using Monocle; | ||
using MonoMod.Cil; | ||
using MonoMod.RuntimeDetour; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Celeste.Mod.MaxHelpingHand.Entities { | ||
[Tracked] | ||
[CustomEntity("MaxHelpingHand/SpikeRefillController")] | ||
public class SpikeRefillController : Entity { | ||
private static readonly MethodInfo m_OrigUpdate = typeof(Player).GetMethod("orig_Update"); | ||
private static ILHook Hook_OrigUpdate; | ||
|
||
public string Flag { get; private set; } | ||
public bool FlagInverted { get; private set; } | ||
|
||
private bool IsFlagSatisfied => string.IsNullOrWhiteSpace(Flag) || (FlagInverted ^ SceneAs<Level>().Session.GetFlag(Flag)); | ||
|
||
public SpikeRefillController(EntityData data, Vector2 offset) : base(data.Position + offset) { | ||
Flag = data.Attr("flag"); | ||
FlagInverted = data.Bool("flagInverted"); | ||
} | ||
|
||
public static void Load() { | ||
Hook_OrigUpdate = new ILHook(m_OrigUpdate, HookSpikeRefillPrevention); | ||
} | ||
|
||
public static void Unload() { | ||
Hook_OrigUpdate?.Dispose(); | ||
} | ||
|
||
private static void HookSpikeRefillPrevention(ILContext il) { | ||
ILCursor cursor = new ILCursor(il); | ||
|
||
if (!cursor.TryGotoNext(MoveType.After, instr => instr.MatchCall("Monocle.Entity", "System.Boolean CollideCheck<Celeste.Spikes>(Microsoft.Xna.Framework.Vector2)"))) { | ||
Logger.Log(LogLevel.Error, "MaxHelpingHand/SpikeRefillController", | ||
$"Could not find CollideCheck<Spikes> in {il.Method.FullName}!"); | ||
return; | ||
} | ||
|
||
Logger.Log(LogLevel.Verbose, "MaxHelpingHand/SpikeRefillController", | ||
$"Hooking CollideCheck<Spikes> in {il.Method.FullName} @ {InstructionToString(cursor.Next)}"); | ||
|
||
cursor.Emit(OpCodes.Ldarg_0); | ||
cursor.EmitDelegate(OverrideRefillPreventionInSpikes); | ||
} | ||
|
||
private static bool OverrideRefillPreventionInSpikes(bool collidedWithSpikes, Player self) { | ||
// return true to prevent refills | ||
if (!collidedWithSpikes) | ||
return collidedWithSpikes; | ||
|
||
return !(self.Scene.Tracker.GetEntity<SpikeRefillController>()?.IsFlagSatisfied ?? false); | ||
} | ||
|
||
// stringifying branch instructions crashes because of monomod, very cool | ||
private static string InstructionToString(Instruction instr) { | ||
Instruction toStringify = instr; | ||
if (instr.Operand is ILLabel target) { | ||
toStringify = Instruction.Create(toStringify.OpCode, target.Target); | ||
toStringify.Offset = instr.Offset; | ||
} | ||
return toStringify.ToString(); | ||
} | ||
} | ||
} |
Binary file added
BIN
+449 Bytes
...cs/Atlases/Gameplay/objects/MaxHelpingHand/spikeRefillController/controller.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
local spikeRefillController = {} | ||
|
||
spikeRefillController.name = "MaxHelpingHand/SpikeRefillController" | ||
spikeRefillController.justification = { 0.5, 0.5 } | ||
spikeRefillController.placements = { | ||
name = 'normal', | ||
data = { | ||
flag = "", | ||
flagInverted = false | ||
} | ||
} | ||
|
||
spikeRefillController.texture = "objects/MaxHelpingHand/spikeRefillController/controller" | ||
|
||
return spikeRefillController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters