diff --git a/Harmony/Tools/CodeMatcher/CodeMatcher.cs b/Harmony/Tools/CodeMatcher/CodeMatcher.cs index 0db7a6a4..f8d5a00d 100644 --- a/Harmony/Tools/CodeMatcher/CodeMatcher.cs +++ b/Harmony/Tools/CodeMatcher/CodeMatcher.cs @@ -34,6 +34,12 @@ private void SetOutOfBounds(int direction) Pos = direction > 0 ? Length : -1; } + private int PosOrThrow() + { + if (IsInvalid) throw new InvalidOperationException("Current position is out of bounds"); + return Pos; + } + /// Gets the number of code instructions in this matcher /// The count /// @@ -52,27 +58,27 @@ private void SetOutOfBounds(int direction) /// Gets the remaining code instructions /// The remaining count /// - public int Remaining => Length - Math.Max(0, Pos); + public int Remaining => Length - PosOrThrow(); /// Gets the opcode at the current position /// The opcode /// - public ref OpCode Opcode => ref codes[Pos].opcode; + public ref OpCode Opcode => ref codes[PosOrThrow()].opcode; /// Gets the operand at the current position /// The operand /// - public ref object Operand => ref codes[Pos].operand; + public ref object Operand => ref codes[PosOrThrow()].operand; /// Gets the labels at the current position /// The labels /// - public ref List