diff --git a/NHSE.Injection/Injector/AutoInjector.cs b/NHSE.Injection/Injector/AutoInjector.cs index 8b2583d..93573e8 100644 --- a/NHSE.Injection/Injector/AutoInjector.cs +++ b/NHSE.Injection/Injector/AutoInjector.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; namespace NHSE.Injection { @@ -29,18 +30,35 @@ public InjectionResult Read(bool force = false) { if ((!AutoInjectEnabled && !force) || !Injector.Connected) return InjectionResult.Skipped; - var result = Injector.Read(); - AfterRead(result); - return result; + + try + { + var result = Injector.Read(); + AfterRead(result); + return result; + } + catch (IndexOutOfRangeException ex) + { + Debug.WriteLine(ex.Message); + return InjectionResult.FailConnectionError; + } } public InjectionResult Write(bool force = false) { if ((!AutoInjectEnabled && !force) || !Injector.Connected) return InjectionResult.Skipped; - var result = Injector.Write(); - AfterWrite(result); - return result; + try + { + var result = Injector.Write(); + AfterWrite(result); + return result; + } + catch (IndexOutOfRangeException ex) + { + Debug.WriteLine(ex.Message); + return InjectionResult.FailConnectionError; + } } public void SetWriteOffset(in uint offset) diff --git a/NHSE.Injection/Injector/InjectionResult.cs b/NHSE.Injection/Injector/InjectionResult.cs index 1edc42f..8fa9eff 100644 --- a/NHSE.Injection/Injector/InjectionResult.cs +++ b/NHSE.Injection/Injector/InjectionResult.cs @@ -5,6 +5,7 @@ public enum InjectionResult Skipped, Success, FailValidate, + FailConnectionError, Same, } -} \ No newline at end of file +} diff --git a/NHSE.WinForms/Subforms/SysBot/SysBotUI.cs b/NHSE.WinForms/Subforms/SysBot/SysBotUI.cs index 82602b5..add7f78 100644 --- a/NHSE.WinForms/Subforms/SysBot/SysBotUI.cs +++ b/NHSE.WinForms/Subforms/SysBot/SysBotUI.cs @@ -50,8 +50,9 @@ private void B_WriteCurrent_Click(object sender, EventArgs e) try { var result = Injector.Write(true); - if (result != InjectionResult.Success) - WinFormsUtil.Alert(result.ToString()); + if (result == InjectionResult.Success) + return; + WinFormsUtil.Alert(result.ToString()); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) @@ -59,6 +60,7 @@ private void B_WriteCurrent_Click(object sender, EventArgs e) { WinFormsUtil.Error(ex.Message); } + TurnOffAuto(); } private void B_ReadCurrent_Click(object sender, EventArgs e) @@ -66,8 +68,9 @@ private void B_ReadCurrent_Click(object sender, EventArgs e) try { var result = Injector.Read(true); - if (result != InjectionResult.Success) - WinFormsUtil.Alert(result.ToString()); + if (result == InjectionResult.Success) + return; + WinFormsUtil.Alert(result.ToString()); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) @@ -75,6 +78,15 @@ private void B_ReadCurrent_Click(object sender, EventArgs e) { WinFormsUtil.Error(ex.Message); } + TurnOffAuto(); + } + + private void TurnOffAuto() + { + if (CHK_AutoRead.Checked) + CHK_AutoRead.Checked = false; + if (CHK_AutoWrite.Checked) + CHK_AutoWrite.Checked = false; } private void CHK_AutoWrite_CheckedChanged(object sender, EventArgs e) => Injector.AutoInjectEnabled = CHK_AutoWrite.Checked;