From 5ede85d6933cfc7adec27e20c299b9f9d344dfb6 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 6 Jun 2020 12:56:44 -0700 Subject: [PATCH] Minor clean handle compiler/roslyn messages --- NHSE.Injection/SysBot/USBBot.cs | 55 ++++++------------- .../Subforms/SysBot/USBBotController.cs | 37 +++---------- 2 files changed, 26 insertions(+), 66 deletions(-) diff --git a/NHSE.Injection/SysBot/USBBot.cs b/NHSE.Injection/SysBot/USBBot.cs index d46fecf..e59b918 100644 --- a/NHSE.Injection/SysBot/USBBot.cs +++ b/NHSE.Injection/SysBot/USBBot.cs @@ -1,12 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading; using LibUsbDotNet; -using LibUsbDotNet.Info; using LibUsbDotNet.Main; -using NHSE.Core; namespace NHSE.Injection { @@ -24,7 +19,6 @@ public bool Connect() { lock (_sync) { - // Find and open the usb device. //SwDevice = UsbDevice.OpenUsbDevice(SwFinder); foreach (UsbRegistry ur in UsbDevice.AllDevices) @@ -44,22 +38,20 @@ public bool Connect() SwDevice.Close(); SwDevice.Open(); - - IUsbDevice? wholeUsbDevice = SwDevice as IUsbDevice; - if (!ReferenceEquals(wholeUsbDevice, null)) + if (SwDevice is IUsbDevice wholeUsbDevice) { // This is a "whole" USB device. Before it can be used, // the desired configuration and interface must be selected. // Select config #1 - bool res = wholeUsbDevice.SetConfiguration(1); + wholeUsbDevice.SetConfiguration(1); // Claim interface #0. bool resagain = wholeUsbDevice.ClaimInterface(0); - if (resagain == false) + if (!resagain) { wholeUsbDevice.ReleaseInterface(0); - resagain = wholeUsbDevice.ClaimInterface(0); + wholeUsbDevice.ClaimInterface(0); } } else @@ -71,7 +63,7 @@ public bool Connect() // open read write endpoints 1. reader = SwDevice.OpenEndpointReader(ReadEndpointID.Ep01); writer = SwDevice.OpenEndpointWriter(WriteEndpointID.Ep01); - + Connected = true; return true; } @@ -86,17 +78,13 @@ public void Disconnect() if (SwDevice.IsOpen) { IUsbDevice? wholeUsbDevice = SwDevice as IUsbDevice; - if (!ReferenceEquals(wholeUsbDevice, null)) - { - wholeUsbDevice.ReleaseInterface(0); - } + wholeUsbDevice?.ReleaseInterface(0); SwDevice.Close(); } } - if (!ReferenceEquals(reader, null)) - reader.Dispose(); - if (!ReferenceEquals(writer, null)) - writer.Dispose(); + + reader?.Dispose(); + writer?.Dispose(); Connected = false; } } @@ -104,37 +92,31 @@ public void Disconnect() private int ReadInternal(byte[] buffer) { byte[] sizeOfReturn = new byte[4]; - ErrorCode ec = ErrorCode.None; - int lenNew = 0; - int lenVal = 0; //read size, no error checking as of yet, should be the required 368 bytes - if (!ReferenceEquals(reader, null)) - ec = reader.Read(sizeOfReturn, 5000, out lenNew); - else + if (reader == null) throw new Exception("USB writer is null, you may have disconnected the device during previous function"); + reader.Read(sizeOfReturn, 5000, out _); + //read stack - reader.Read(buffer, 5000, out lenVal); + reader.Read(buffer, 5000, out var lenVal); return lenVal; } private int SendInternal(byte[] buffer) { - int l; - uint pack = (uint)buffer.Length + 2; - ErrorCode ec; - if (!ReferenceEquals(writer, null)) - ec = writer.Write(BitConverter.GetBytes(pack), 2000, out l); - else + if (writer == null) throw new Exception("USB writer is null, you may have disconnected the device during previous function"); + uint pack = (uint)buffer.Length + 2; + var ec = writer.Write(BitConverter.GetBytes(pack), 2000, out _); if (ec != ErrorCode.None) { Disconnect(); throw new Exception(UsbDevice.LastErrorString); } - ec = writer.Write(buffer, 2000, out l); + ec = writer.Write(buffer, 2000, out var l); if (ec != ErrorCode.None) { Disconnect(); @@ -160,7 +142,7 @@ public byte[] ReadBytes(uint offset, int length) // give it time to push data back Thread.Sleep((length / 256) + 100); - + var buffer = new byte[length]; var _ = ReadInternal(buffer); //return Decoder.ConvertHexByteStringToBytes(buffer); @@ -178,6 +160,5 @@ public void WriteBytes(byte[] data, uint offset) Thread.Sleep((data.Length / 256) + 100); } } - } } diff --git a/NHSE.WinForms/Subforms/SysBot/USBBotController.cs b/NHSE.WinForms/Subforms/SysBot/USBBotController.cs index eac201e..0c8b2ee 100644 --- a/NHSE.WinForms/Subforms/SysBot/USBBotController.cs +++ b/NHSE.WinForms/Subforms/SysBot/USBBotController.cs @@ -1,35 +1,17 @@ using System; -using System.Collections.Generic; -using System.Windows.Forms; -using System.Text; -using System.Threading.Tasks; -using LibUsbDotNet; -using LibUsbDotNet.Info; -using LibUsbDotNet.Main; -using System.Collections.ObjectModel; -using System.Net.Sockets; -using System.Threading; using NHSE.Injection; -using NHSE.WinForms.Properties; namespace NHSE.WinForms { public class USBBotController { - public readonly USBBot Bot = new USBBot(); - public USBBotController() - { - - } - public bool Connect() { - bool retvalue = false; try { - retvalue = Bot.Connect(); + return Bot.Connect(); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) @@ -38,8 +20,6 @@ public bool Connect() WinFormsUtil.Error(ex.Message); return false; } - - return retvalue; } public void Disconnect() @@ -48,15 +28,14 @@ public void Disconnect() } //todo: this - public uint GetDefaultOffset() - { - return Settings.Default.SysBotPouchOffset; - } + //public uint GetDefaultOffset() + //{ + // return Settings.Default.SysBotPouchOffset; + //} - public void PopPrompt() - { - - } + //public void PopPrompt() + //{ + //} public void WriteBytes(byte[] data, uint offset) {