From 896c1195d31b2e193a7c301db8d17b7d72895560 Mon Sep 17 00:00:00 2001 From: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com> Date: Tue, 26 Sep 2023 08:18:06 +0200 Subject: [PATCH 1/2] Improve CodeMatcher exceptions on invalid positions --- Harmony/Tools/CodeMatcher/CodeMatcher.cs | 52 ++++++++++++++++-------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/Harmony/Tools/CodeMatcher/CodeMatcher.cs b/Harmony/Tools/CodeMatcher/CodeMatcher.cs index 0db7a6a4..a2dbc3dd 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