Skip to content

Commit

Permalink
Add and apply stylecop rules
Browse files Browse the repository at this point in the history
  • Loading branch information
eXpl0it3r committed Nov 10, 2023
1 parent 3421ab7 commit c1d6e2d
Show file tree
Hide file tree
Showing 111 changed files with 1,753 additions and 1,378 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,14 @@ dotnet_naming_style.begins_with_i.capitalization = pascal_case

# ReSharper properties
resharper_place_accessorholder_attribute_on_same_line = false

[*Packet*.cs]
dotnet_diagnostic.SA1201.severity = none

[BinaryWriter.cs]
dotnet_diagnostic.CS0675.severity = none
dotnet_diagnostic.CS0414.severity = none

[BinaryReader.cs]
dotnet_diagnostic.CS0169.severity = none
dotnet_diagnostic.CS0414.severity = none
19 changes: 9 additions & 10 deletions Lib/Core.Data/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ protected Database(string name, string dbName, string connString)
DbName = dbName;
ConnectionString = connString;
}

public string Name { get; }
public string DbName { get; }

protected DbTransaction CurrentTransaction { get; private set; }
protected DbConnection Connection { get; private set; }
private string ConnectionString { get; }
public string Name { get; }
public string DbName { get; }


public async Task OpenConnection()
{
Expand Down Expand Up @@ -113,13 +113,12 @@ public async Task<T> ExecuteScalar<T>(string sql, IEnumerable<IDataParameter> pa
return ret == DBNull.Value ? default : (T)ret;
}


public string DelimDatabase(string val) { return Delim(val, DelimType.Database); }
public string DelimSchema(string val) { return Delim(val, DelimType.Schema); }
public string DelimTable(string val) { return Delim(val, DelimType.Table); }
public string DelimColumn(string val) { return Delim(val, DelimType.Column); }
public string DelimParameter(string val) { return Delim(val, DelimType.Parameter); }
public string DelimString(string val) { return Delim(val, DelimType.String); }
public string DelimDatabase(string val) => Delim(val, DelimType.Database);
public string DelimSchema(string val) => Delim(val, DelimType.Schema);
public string DelimTable(string val) => Delim(val, DelimType.Table);
public string DelimColumn(string val) => Delim(val, DelimType.Column);
public string DelimParameter(string val) => Delim(val, DelimType.Parameter);
public string DelimString(string val) => Delim(val, DelimType.String);
public abstract string Delim(string val, DelimType dt);

public void Dispose()
Expand Down
11 changes: 5 additions & 6 deletions Lib/Core.Data/EntityGuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ public readonly struct EntityGuid
{
public readonly ulong Full;

public byte ServerId { get; private init; }
public uint Timestamp { get; private init; }
public uint Counter { get; private init; }
public byte Type { get; private init; }


public EntityGuid(byte serverId, uint timestamp, uint counter, byte type)
{
ServerId = serverId;
Expand All @@ -26,6 +20,11 @@ public EntityGuid(byte serverId, uint timestamp, uint counter, byte type)
Type;
}

public byte ServerId { get; private init; }
public uint Timestamp { get; private init; }
public uint Counter { get; private init; }
public byte Type { get; private init; }

public static EntityGuid Parse(ulong guid)
{
EntityGuid entityGuid = new() { ServerId = (byte)(guid >> 56), Timestamp = (uint)(((guid >> 32) & 0x00FFFFFF) << 8), Counter = (uint)((guid & 0xFFFFFF00) >> 8), Type = (byte)(guid & 0xFF) };
Expand Down
8 changes: 4 additions & 4 deletions Lib/Shared.Common/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public static class DateTimeExtensions
{
private static DateTime? _epoch;

public static DateTime Epoch => CheckedEpochUtc.ToLocalTime();

public static DateTime EpochUtc => CheckedEpochUtc;

private static DateTime CheckedEpochUtc
{
get
Expand All @@ -16,10 +20,6 @@ private static DateTime CheckedEpochUtc
}
}

public static DateTime Epoch => CheckedEpochUtc.ToLocalTime();

public static DateTime EpochUtc => CheckedEpochUtc;

public static double UnixTimestamp(this DateTime dateTime)
{
return (dateTime - CheckedEpochUtc.ToLocalTime()).TotalSeconds;
Expand Down
6 changes: 3 additions & 3 deletions Lib/Shared.Common/LocationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Win32;
using System;
using System;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace Shared.Common;

Expand Down Expand Up @@ -51,7 +51,7 @@ private static string FindFirefallByShellCommand()
}

using var shellKey = Registry.ClassesRoot.OpenSubKey("firefall\\shell\\open\\command");
var firefallLaunchCommand = (string)shellKey?.GetValue("");
var firefallLaunchCommand = (string)shellKey?.GetValue(string.Empty);
if (firefallLaunchCommand == null)
{
return null;
Expand Down
4 changes: 2 additions & 2 deletions Lib/Shared.Common/SerilogTheme.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Serilog.Sinks.SystemConsole.Themes;
using System;
using System;
using System.Collections.Generic;
using Serilog.Sinks.SystemConsole.Themes;

namespace Shared.Common;

Expand Down
174 changes: 87 additions & 87 deletions Lib/Shared.Udp/Deserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,90 @@ public static T Read<T>(ref ReadOnlyMemory<byte> data)
return (T)Read(ref data, typeof(T));
}

public static object ReadPrimitive(ref ReadOnlyMemory<byte> data, Type type)
{
ReadOnlySpan<byte> span;

if (typeof(byte) == type)
{
span = data[..1].Span;
data = data[1..];
return span[0];
}

if (typeof(char) == type)
{
span = data[..1].Span;
data = data[1..];
return Encoding.ASCII.GetChars(span.ToArray())[0];
}

if (typeof(short) == type)
{
span = data[..2].Span;
data = data[2..];
return BinaryPrimitives.ReadInt16LittleEndian(span);
}

if (typeof(ushort) == type)
{
span = data[..2].Span;
data = data[2..];
return BinaryPrimitives.ReadUInt16LittleEndian(span);
}

if (typeof(int) == type)
{
span = data[..4].Span;
data = data[4..];
return BinaryPrimitives.ReadInt32LittleEndian(span);
}

if (typeof(uint) == type)
{
span = data[..4].Span;
data = data[4..];
return BinaryPrimitives.ReadUInt32LittleEndian(span);
}

if (typeof(long) == type)
{
span = data[..8].Span;
data = data[8..];
return BinaryPrimitives.ReadInt64LittleEndian(span);
}

if (typeof(ulong) == type)
{
span = data[..8].Span;
data = data[8..];
return BinaryPrimitives.ReadUInt64LittleEndian(span);
}

if (typeof(Half) == type)
{
span = data[..2].Span;
data = data[2..];
return BinaryPrimitives.ReadUInt16LittleEndian(span);
}

if (typeof(float) == type)
{
span = data[..4].Span;
data = data[4..];
return MemoryMarshal.Cast<byte, float>(span)[0];
}

if (typeof(double) == type)
{
span = data[..8].Span;
data = data[8..];
return MemoryMarshal.Cast<byte, double>(span)[0];
}

throw new Exception();
}

private static unsafe object Read(ref ReadOnlyMemory<byte> data, Type type, IEnumerable<Attribute> attributes = null)
{
attributes = attributes?.ToList() ?? type.GetCustomAttributes();
Expand All @@ -69,7 +153,7 @@ private static unsafe object Read(ref ReadOnlyMemory<byte> data, Type type, IEnu
return null;
}

data = data[Marshal.SizeOf(exists.ExistsType)..];
data = data[Marshal.SizeOf(exists.ExistsType) ..];
}

if (typeof(IEnumerable).IsAssignableFrom(type) && type.GenericTypeArguments is { Length: > 0 })
Expand All @@ -78,7 +162,7 @@ private static unsafe object Read(ref ReadOnlyMemory<byte> data, Type type, IEnu
if (prefixLength != null)
{
l = (int)Convert.ChangeType(Read(ref data, prefixLength.LengthType), typeof(int));
data = data[Marshal.SizeOf(prefixLength.LengthType)..];
data = data[Marshal.SizeOf(prefixLength.LengthType) ..];
}
else if (length != null)
{
Expand Down Expand Up @@ -108,7 +192,7 @@ private static unsafe object Read(ref ReadOnlyMemory<byte> data, Type type, IEnu

l++; // null terminator

ret = Encoding.ASCII.GetString(data[..(l - 1)].Span.ToArray());
ret = Encoding.ASCII.GetString(data[.. (l - 1)].Span.ToArray());
data = data[l..];
}
else if (type.IsClass)
Expand Down Expand Up @@ -137,90 +221,6 @@ private static unsafe object Read(ref ReadOnlyMemory<byte> data, Type type, IEnu
return ret;
}

public static object ReadPrimitive(ref ReadOnlyMemory<byte> data, Type type)
{
ReadOnlySpan<byte> span;

if (typeof(byte) == type)
{
span = data[..1].Span;
data = data[1..];
return span[0];
}

if (typeof(char) == type)
{
span = data[..1].Span;
data = data[1..];
return Encoding.ASCII.GetChars(span.ToArray())[0];
}

if (typeof(short) == type)
{
span = data[..2].Span;
data = data[2..];
return BinaryPrimitives.ReadInt16LittleEndian(span);
}

if (typeof(ushort) == type)
{
span = data[..2].Span;
data = data[2..];
return BinaryPrimitives.ReadUInt16LittleEndian(span);
}

if (typeof(int) == type)
{
span = data[..4].Span;
data = data[4..];
return BinaryPrimitives.ReadInt32LittleEndian(span);
}

if (typeof(uint) == type)
{
span = data[..4].Span;
data = data[4..];
return BinaryPrimitives.ReadUInt32LittleEndian(span);
}

if (typeof(long) == type)
{
span = data[..8].Span;
data = data[8..];
return BinaryPrimitives.ReadInt64LittleEndian(span);
}

if (typeof(ulong) == type)
{
span = data[..8].Span;
data = data[8..];
return BinaryPrimitives.ReadUInt64LittleEndian(span);
}

if (typeof(Half) == type)
{
span = data[..2].Span;
data = data[2..];
return BinaryPrimitives.ReadUInt16LittleEndian(span);
}

if (typeof(float) == type)
{
span = data[..4].Span;
data = data[4..];
return MemoryMarshal.Cast<byte, float>(span)[0];
}

if (typeof(double) == type)
{
span = data[..8].Span;
data = data[8..];
return MemoryMarshal.Cast<byte, double>(span)[0];
}

throw new Exception();
}

private static object ReadClass(ref ReadOnlyMemory<byte> data, Type type)
{
var properties = from property in type.GetFields()
Expand Down
6 changes: 3 additions & 3 deletions Lib/Shared.Udp/ExistsPrefixAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace Shared.Udp;

public class ExistsPrefixAttribute : Attribute
{
public readonly Type ExistsType;
public readonly object TrueValue;

public ExistsPrefixAttribute(Type t, object trueVal)
{
ExistsType = t;
TrueValue = trueVal;
}

public Type ExistsType { get; }
public object TrueValue { get; }
}
4 changes: 2 additions & 2 deletions Lib/Shared.Udp/LengthPrefixedAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Shared.Udp;

public sealed class LengthPrefixedAttribute : Attribute
{
public readonly Type LengthType;

public LengthPrefixedAttribute(Type t)
{
LengthType = t;
}

public Type LengthType { get; }
}
11 changes: 6 additions & 5 deletions Lib/Shared.Udp/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ public struct Packet
{
public readonly IPEndPoint RemoteEndpoint;
public readonly ReadOnlyMemory<byte> PacketData;
public int CurrentPosition { get; private set; }
public int TotalBytes => PacketData.Length;
public int BytesRemaining => TotalBytes - CurrentPosition;
public DateTime Received { get; set; }


public Packet(IPEndPoint ep, ReadOnlyMemory<byte> data, DateTime? received = null)
{
RemoteEndpoint = ep;
PacketData = data;
CurrentPosition = 0;
this.Received = received ?? DateTime.Now;
}

public int CurrentPosition { get; private set; }
public int TotalBytes => PacketData.Length;
public int BytesRemaining => TotalBytes - CurrentPosition;
public DateTime Received { get; set; }

/*public T ReadBE<T>() where T : struct {
var len = Unsafe.SizeOf<T>();
Expand Down
Loading

0 comments on commit c1d6e2d

Please sign in to comment.