mirror of
https://github.com/kwsch/NHSE.git
synced 2026-08-28 13:54:24 -05:00
Minor clean
handle compiler/roslyn messages
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user