[Ryujinx.Input] Address dotnet-format issues (#5384)

* dotnet format style --severity info

Some changes were manually reverted.

* dotnet format analyzers --serverity info

Some changes have been minimally adapted.

* Restore a few unused methods and variables

* Address dotnet format CA1816 warnings

* Address or silence dotnet format CA1806 and a few CA1854 warnings

* Address most dotnet format whitespace warnings

* Apply dotnet format whitespace formatting

A few of them have been manually reverted and the corresponding warning was silenced

* Add comments to disabled warnings

* Simplify properties and array initialization, Use const when possible, Remove trailing commas

* Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas"

This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e.

* dotnet format whitespace after rebase

* Remove redundant code, convert to auto-properties and fix naming rule violations

* Remove bogus change

* Address review feedback
This commit is contained in:
TSRBerry 2023-06-28 18:23:00 +02:00 committed by GitHub
parent 40f2bd37e3
commit 46b7c905f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 344 additions and 343 deletions

View file

@ -19,7 +19,7 @@ namespace Ryujinx.Input.Motion.CemuHook
{
public class Client : IDisposable
{
public const uint Magic = 0x43555344; // DSUC
public const uint Magic = 0x43555344; // DSUC
public const ushort Version = 1001;
private bool _active;
@ -29,15 +29,15 @@ namespace Ryujinx.Input.Motion.CemuHook
private readonly Dictionary<int, UdpClient> _clients;
private readonly bool[] _clientErrorStatus = new bool[Enum.GetValues<PlayerIndex>().Length];
private readonly long[] _clientRetryTimer = new long[Enum.GetValues<PlayerIndex>().Length];
private NpadManager _npadManager;
private readonly long[] _clientRetryTimer = new long[Enum.GetValues<PlayerIndex>().Length];
private readonly NpadManager _npadManager;
public Client(NpadManager npadManager)
{
_npadManager = npadManager;
_hosts = new Dictionary<int, IPEndPoint>();
_motionData = new Dictionary<int, Dictionary<int, MotionInput>>();
_clients = new Dictionary<int, UdpClient>();
_hosts = new Dictionary<int, IPEndPoint>();
_motionData = new Dictionary<int, Dictionary<int, MotionInput>>();
_clients = new Dictionary<int, UdpClient>();
CloseClients();
}
@ -84,7 +84,7 @@ namespace Ryujinx.Input.Motion.CemuHook
try
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(host), port);
IPEndPoint endPoint = new(IPAddress.Parse(host), port);
client = new UdpClient(host, port);
@ -141,9 +141,9 @@ namespace Ryujinx.Input.Motion.CemuHook
{
lock (_motionData)
{
if (_motionData.ContainsKey(player))
if (_motionData.TryGetValue(player, out Dictionary<int, MotionInput> value))
{
if (_motionData[player].TryGetValue(slot, out input))
if (value.TryGetValue(slot, out input))
{
return true;
}
@ -164,26 +164,26 @@ namespace Ryujinx.Input.Motion.CemuHook
private void Send(byte[] data, int clientId)
{
if (_clients.TryGetValue(clientId, out UdpClient _client))
if (_clients.TryGetValue(clientId, out UdpClient client))
{
if (_client != null && _client.Client != null && _client.Client.Connected)
if (client != null && client.Client != null && client.Client.Connected)
{
try
{
_client?.Send(data, data.Length);
client?.Send(data, data.Length);
}
catch (SocketException socketException)
{
if (!_clientErrorStatus[clientId])
{
Logger.Warning?.PrintMsg(LogClass.Hid, $"Unable to send data request to motion source at {_client.Client.RemoteEndPoint}. Error: {socketException.ErrorCode}");
Logger.Warning?.PrintMsg(LogClass.Hid, $"Unable to send data request to motion source at {client.Client.RemoteEndPoint}. Error: {socketException.ErrorCode}");
}
_clientErrorStatus[clientId] = true;
RemoveClient(clientId);
_client?.Dispose();
client?.Dispose();
SetRetryTimer(clientId);
}
@ -193,7 +193,7 @@ namespace Ryujinx.Input.Motion.CemuHook
RemoveClient(clientId);
_client?.Dispose();
client?.Dispose();
SetRetryTimer(clientId);
}
@ -203,13 +203,13 @@ namespace Ryujinx.Input.Motion.CemuHook
private byte[] Receive(int clientId, int timeout = 0)
{
if (_hosts.TryGetValue(clientId, out IPEndPoint endPoint) && _clients.TryGetValue(clientId, out UdpClient _client))
if (_hosts.TryGetValue(clientId, out IPEndPoint endPoint) && _clients.TryGetValue(clientId, out UdpClient client))
{
if (_client != null && _client.Client != null && _client.Client.Connected)
if (client != null && client.Client != null && client.Client.Connected)
{
_client.Client.ReceiveTimeout = timeout;
client.Client.ReceiveTimeout = timeout;
var result = _client?.Receive(ref endPoint);
var result = client?.Receive(ref endPoint);
if (result.Length > 0)
{
@ -242,9 +242,9 @@ namespace Ryujinx.Input.Motion.CemuHook
public void ReceiveLoop(int clientId)
{
if (_hosts.TryGetValue(clientId, out IPEndPoint endPoint) && _clients.TryGetValue(clientId, out UdpClient _client))
if (_hosts.TryGetValue(clientId, out IPEndPoint endPoint) && _clients.TryGetValue(clientId, out UdpClient client))
{
if (_client != null && _client.Client != null && _client.Client.Connected)
if (client != null && client.Client != null && client.Client.Connected)
{
try
{
@ -271,7 +271,7 @@ namespace Ryujinx.Input.Motion.CemuHook
RemoveClient(clientId);
_client?.Dispose();
client?.Dispose();
SetRetryTimer(clientId);
}
@ -281,7 +281,7 @@ namespace Ryujinx.Input.Motion.CemuHook
RemoveClient(clientId);
_client?.Dispose();
client?.Dispose();
SetRetryTimer(clientId);
}
@ -297,8 +297,8 @@ namespace Ryujinx.Input.Motion.CemuHook
data = data.AsSpan()[16..].ToArray();
using MemoryStream stream = new MemoryStream(data);
using BinaryReader reader = new BinaryReader(stream);
using MemoryStream stream = new(data);
using BinaryReader reader = new(stream);
switch (type)
{
@ -310,18 +310,18 @@ namespace Ryujinx.Input.Motion.CemuHook
case MessageType.Data:
ControllerDataResponse inputData = reader.ReadStruct<ControllerDataResponse>();
Vector3 accelerometer = new Vector3()
Vector3 accelerometer = new()
{
X = -inputData.AccelerometerX,
Y = inputData.AccelerometerZ,
Z = -inputData.AccelerometerY
Z = -inputData.AccelerometerY,
};
Vector3 gyroscrope = new Vector3()
Vector3 gyroscrope = new()
{
X = inputData.GyroscopePitch,
Y = inputData.GyroscopeRoll,
Z = -inputData.GyroscopeYaw
Z = -inputData.GyroscopeYaw,
};
ulong timestamp = inputData.MotionTimestamp;
@ -346,7 +346,7 @@ namespace Ryujinx.Input.Motion.CemuHook
}
else
{
MotionInput input = new MotionInput();
MotionInput input = new();
input.Update(accelerometer, gyroscrope, timestamp, cemuHookConfig.Sensitivity, (float)cemuHookConfig.GyroDeadzone);
@ -355,11 +355,11 @@ namespace Ryujinx.Input.Motion.CemuHook
}
else
{
MotionInput input = new MotionInput();
MotionInput input = new();
input.Update(accelerometer, gyroscrope, timestamp, cemuHookConfig.Sensitivity, (float)cemuHookConfig.GyroDeadzone);
_motionData.Add(clientId, new Dictionary<int, MotionInput>() { { slot, input } });
_motionData.Add(clientId, new Dictionary<int, MotionInput> { { slot, input } });
}
}
else
@ -380,38 +380,37 @@ namespace Ryujinx.Input.Motion.CemuHook
Header header = GenerateHeader(clientId);
using (MemoryStream stream = MemoryStreamManager.Shared.GetStream())
using (BinaryWriter writer = new BinaryWriter(stream))
using MemoryStream stream = MemoryStreamManager.Shared.GetStream();
using BinaryWriter writer = new(stream);
writer.WriteStruct(header);
ControllerInfoRequest request = new()
{
writer.WriteStruct(header);
Type = MessageType.Info,
PortsCount = 4,
};
ControllerInfoRequest request = new ControllerInfoRequest()
{
Type = MessageType.Info,
PortsCount = 4
};
request.PortIndices[0] = (byte)slot;
request.PortIndices[0] = (byte)slot;
writer.WriteStruct(request);
writer.WriteStruct(request);
header.Length = (ushort)(stream.Length - 16);
header.Length = (ushort)(stream.Length - 16);
writer.Seek(6, SeekOrigin.Begin);
writer.Write(header.Length);
writer.Seek(6, SeekOrigin.Begin);
writer.Write(header.Length);
Crc32.Hash(stream.ToArray(), header.Crc32.AsSpan());
Crc32.Hash(stream.ToArray(), header.Crc32.AsSpan());
writer.Seek(8, SeekOrigin.Begin);
writer.Write(header.Crc32.AsSpan());
writer.Seek(8, SeekOrigin.Begin);
writer.Write(header.Crc32.AsSpan());
byte[] data = stream.ToArray();
byte[] data = stream.ToArray();
Send(data, clientId);
}
Send(data, clientId);
}
public unsafe void RequestData(int clientId, int slot)
public void RequestData(int clientId, int slot)
{
if (!_active)
{
@ -420,44 +419,43 @@ namespace Ryujinx.Input.Motion.CemuHook
Header header = GenerateHeader(clientId);
using (MemoryStream stream = MemoryStreamManager.Shared.GetStream())
using (BinaryWriter writer = new BinaryWriter(stream))
using MemoryStream stream = MemoryStreamManager.Shared.GetStream();
using BinaryWriter writer = new(stream);
writer.WriteStruct(header);
ControllerDataRequest request = new()
{
writer.WriteStruct(header);
Type = MessageType.Data,
Slot = (byte)slot,
SubscriberType = SubscriberType.Slot,
};
ControllerDataRequest request = new ControllerDataRequest()
{
Type = MessageType.Data,
Slot = (byte)slot,
SubscriberType = SubscriberType.Slot
};
writer.WriteStruct(request);
writer.WriteStruct(request);
header.Length = (ushort)(stream.Length - 16);
header.Length = (ushort)(stream.Length - 16);
writer.Seek(6, SeekOrigin.Begin);
writer.Write(header.Length);
writer.Seek(6, SeekOrigin.Begin);
writer.Write(header.Length);
Crc32.Hash(stream.ToArray(), header.Crc32.AsSpan());
Crc32.Hash(stream.ToArray(), header.Crc32.AsSpan());
writer.Seek(8, SeekOrigin.Begin);
writer.Write(header.Crc32.AsSpan());
writer.Seek(8, SeekOrigin.Begin);
writer.Write(header.Crc32.AsSpan());
byte[] data = stream.ToArray();
byte[] data = stream.ToArray();
Send(data, clientId);
}
Send(data, clientId);
}
private Header GenerateHeader(int clientId)
private static Header GenerateHeader(int clientId)
{
Header header = new Header()
Header header = new()
{
Id = (uint)clientId,
Id = (uint)clientId,
MagicString = Magic,
Version = Version,
Length = 0
Version = Version,
Length = 0,
};
return header;
@ -465,9 +463,10 @@ namespace Ryujinx.Input.Motion.CemuHook
public void Dispose()
{
GC.SuppressFinalize(this);
_active = false;
CloseClients();
}
}
}
}

View file

@ -16,15 +16,15 @@ namespace Ryujinx.Input.Motion.CemuHook.Protocol
public struct ControllerDataResponse
{
public SharedResponse Shared;
public byte Connected;
public uint PacketId;
public byte ExtraButtons;
public byte MainButtons;
public ushort PSExtraInput;
public ushort LeftStickXY;
public ushort RightStickXY;
public uint DPadAnalog;
public ulong MainButtonsAnalog;
public byte Connected;
public uint PacketId;
public byte ExtraButtons;
public byte MainButtons;
public ushort PSExtraInput;
public ushort LeftStickXY;
public ushort RightStickXY;
public uint DPadAnalog;
public ulong MainButtonsAnalog;
public Array6<byte> Touch1;
public Array6<byte> Touch2;
@ -42,6 +42,6 @@ namespace Ryujinx.Input.Motion.CemuHook.Protocol
{
All,
Slot,
Mac
Mac,
}
}
}

View file

@ -7,7 +7,7 @@ namespace Ryujinx.Input.Motion.CemuHook.Protocol
public struct ControllerInfoResponse
{
public SharedResponse Shared;
private byte _zero;
private readonly byte _zero;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
@ -17,4 +17,4 @@ namespace Ryujinx.Input.Motion.CemuHook.Protocol
public int PortsCount;
public Array4<byte> PortIndices;
}
}
}

View file

@ -12,4 +12,4 @@ namespace Ryujinx.Input.Motion.CemuHook.Protocol
public Array4<byte> Crc32;
public uint Id;
}
}
}

View file

@ -4,6 +4,6 @@
{
Protocol = 0x100000,
Info,
Data
Data,
}
}
}

View file

@ -6,11 +6,11 @@ namespace Ryujinx.Input.Motion.CemuHook.Protocol
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SharedResponse
{
public MessageType Type;
public byte Slot;
public SlotState State;
public MessageType Type;
public byte Slot;
public SlotState State;
public DeviceModelType ModelType;
public ConnectionType ConnectionType;
public ConnectionType ConnectionType;
public Array6<byte> MacAddress;
public BatteryStatus BatteryStatus;
@ -20,21 +20,21 @@ namespace Ryujinx.Input.Motion.CemuHook.Protocol
{
Disconnected,
Reserved,
Connected
Connected,
}
public enum DeviceModelType : byte
{
None,
PartialGyro,
FullGyro
FullGyro,
}
public enum ConnectionType : byte
{
None,
USB,
Bluetooth
Bluetooth,
}
public enum BatteryStatus : byte
@ -46,6 +46,6 @@ namespace Ryujinx.Input.Motion.CemuHook.Protocol
High,
Full,
Charging,
Charged
Charged,
}
}
}

View file

@ -6,19 +6,19 @@ namespace Ryujinx.Input
{
public class MotionInput
{
public ulong TimeStamp { get; set; }
public ulong TimeStamp { get; set; }
public Vector3 Accelerometer { get; set; }
public Vector3 Gyroscrope { get; set; }
public Vector3 Rotation { get; set; }
public Vector3 Gyroscrope { get; set; }
public Vector3 Rotation { get; set; }
private readonly MotionSensorFilter _filter;
public MotionInput()
{
TimeStamp = 0;
TimeStamp = 0;
Accelerometer = new Vector3();
Gyroscrope = new Vector3();
Rotation = new Vector3();
Gyroscrope = new Vector3();
Rotation = new Vector3();
// TODO: RE the correct filter.
_filter = new MotionSensorFilter(0f);
@ -62,4 +62,4 @@ namespace Ryujinx.Input
return degree * (MathF.PI / 180);
}
}
}
}

View file

@ -106,19 +106,19 @@ namespace Ryujinx.Input.Motion
float q1 = Quaternion.W;
// Estimated direction of gravity.
Vector3 gravity = new Vector3()
Vector3 gravity = new()
{
X = 2f * (q2 * q4 - q1 * q3),
Y = 2f * (q1 * q2 + q3 * q4),
Z = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4
Z = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4,
};
// Error is cross product between estimated direction and measured direction of gravity.
Vector3 error = new Vector3()
Vector3 error = new()
{
X = accel.Y * gravity.Z - accel.Z * gravity.Y,
Y = accel.Z * gravity.X - accel.X * gravity.Z,
Z = accel.X * gravity.Y - accel.Y * gravity.X
Z = accel.X * gravity.Y - accel.Y * gravity.X,
};
if (Ki > 0f)
@ -134,7 +134,7 @@ namespace Ryujinx.Input.Motion
gyro += (Kp * error) + (Ki * _intergralError);
// Integrate rate of change of quaternion.
Vector3 delta = new Vector3(q2, q3, q4);
Vector3 delta = new(q2, q3, q4);
q1 += (-q2 * gyro.X - q3 * gyro.Y - q4 * gyro.Z) * (SampleRateCoefficient * SamplePeriod);
q2 += (q1 * gyro.X + delta.Y * gyro.Z - delta.Z * gyro.Y) * (SampleRateCoefficient * SamplePeriod);
@ -142,7 +142,7 @@ namespace Ryujinx.Input.Motion
q4 += (q1 * gyro.Z + delta.X * gyro.Y - delta.Y * gyro.X) * (SampleRateCoefficient * SamplePeriod);
// Normalise quaternion.
Quaternion quaternion = new Quaternion(q2, q3, q4, q1);
Quaternion quaternion = new(q2, q3, q4, q1);
norm = 1f / quaternion.Length();
@ -159,4 +159,4 @@ namespace Ryujinx.Input.Motion
Quaternion = Quaternion.Identity;
}
}
}
}