Adds style cop (#109)

This commit is contained in:
Kamron Batman 2020-04-26 00:16:02 -07:00 committed by GitHub
parent 3e715bcb60
commit 556a17aba8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1725 changed files with 33831 additions and 38046 deletions

View file

@ -141,7 +141,7 @@ namespace Server
{
_ipAddressTable ??= new Dictionary<IPAddress, IPAddress>();
if (!_ipAddressTable.TryGetValue(ipAddress, out IPAddress interned))
if (!_ipAddressTable.TryGetValue(ipAddress, out var interned))
{
interned = ipAddress;
_ipAddressTable[ipAddress] = interned;
@ -157,7 +157,7 @@ namespace Server
public static bool IsValidIP(string text)
{
IPMatch(text, IPAddress.None, out bool valid);
IPMatch(text, IPAddress.None, out var valid);
return valid;
}
@ -169,14 +169,14 @@ namespace Server
if (str == null)
return "";
bool hasOpen = str.IndexOf('<') >= 0;
bool hasClose = str.IndexOf('>') >= 0;
bool hasPound = str.IndexOf('#') >= 0;
var hasOpen = str.IndexOf('<') >= 0;
var hasClose = str.IndexOf('>') >= 0;
var hasPound = str.IndexOf('#') >= 0;
if (!hasOpen && !hasClose && !hasPound)
return str;
StringBuilder sb = new StringBuilder(str);
var sb = new StringBuilder(str);
if (hasOpen)
sb.Replace('<', '(');
@ -193,59 +193,24 @@ namespace Server
public static bool IPMatchCIDR(string cidr, IPAddress ip)
{
if (ip == null || ip.AddressFamily == AddressFamily.InterNetworkV6)
return false; //Just worry about IPv4 for now
return false; // Just worry about IPv4 for now
var bytes = new byte[4];
var split = cidr.Split('.');
var cidrBits = false;
var cidrLength = 0;
/*
string[] str = cidr.Split( '/' );
if ( str.Length != 2 )
return false;
/* **************************************************
IPAddress cidrPrefix;
if ( !IPAddress.TryParse( str[0], out cidrPrefix ) )
return false;
* */
/*
string[] dotSplit = str[0].Split( '.' );
if ( dotSplit.Length != 4 ) //At this point and time, and for speed sake, we'll only worry about IPv4
return false;
byte[] bytes = new byte[4];
for ( int i = 0; i < 4; i++ )
for (var i = 0; i < 4; i++)
{
byte.TryParse( dotSplit[i], out bytes[i] );
}
var part = 0;
uint cidrPrefix = OrderedAddressValue( bytes );
var partBase = 10;
int cidrLength = Utility.ToInt32( str[1] );
//The below solution is the fastest solution of the three
var pattern = split[i];
*/
byte[] bytes = new byte[4];
string[] split = cidr.Split('.');
bool cidrBits = false;
int cidrLength = 0;
for (int i = 0; i < 4; i++)
{
int part = 0;
int partBase = 10;
string pattern = split[i];
for (int j = 0; j < pattern.Length; j++)
for (var j = 0; j < pattern.Length; j++)
{
char c = pattern[j];
var c = pattern[j];
if (c == 'x' || c == 'X')
{
@ -253,7 +218,7 @@ namespace Server
}
else if (c >= '0' && c <= '9')
{
int offset = c - '0';
var offset = c - '0';
if (cidrBits)
{
@ -268,7 +233,7 @@ namespace Server
}
else if (c >= 'a' && c <= 'f')
{
int offset = 10 + (c - 'a');
var offset = 10 + (c - 'a');
if (cidrBits)
{
@ -283,7 +248,7 @@ namespace Server
}
else if (c >= 'A' && c <= 'F')
{
int offset = 10 + (c - 'A');
var offset = 10 + (c - 'A');
if (cidrBits)
{
@ -298,7 +263,7 @@ namespace Server
}
else if (c == '/')
{
if (cidrBits || i != 3) //If there's two '/' or the '/' isn't in the last byte
if (cidrBits || i != 3) // If there's two '/' or the '/' isn't in the last byte
return false;
partBase = 10;
@ -318,12 +283,12 @@ namespace Server
public static bool IPMatchCIDR(IPAddress cidrPrefix, IPAddress ip, int cidrLength)
{
//Ignore IPv6 for now
// Ignore IPv6 for now
if (cidrPrefix == null || ip == null || cidrPrefix.AddressFamily == AddressFamily.InterNetworkV6)
return false;
uint cidrValue = SwapUnsignedInt((uint)GetLongAddressValue(cidrPrefix));
uint ipValue = SwapUnsignedInt((uint)GetLongAddressValue(ip));
var cidrValue = SwapUnsignedInt((uint)GetLongAddressValue(cidrPrefix));
var ipValue = SwapUnsignedInt((uint)GetLongAddressValue(ip));
return IPMatchCIDR(cidrValue, ipValue, cidrLength);
}
@ -333,17 +298,17 @@ namespace Server
if (ip == null || ip.AddressFamily == AddressFamily.InterNetworkV6)
return false;
uint ipValue = SwapUnsignedInt((uint)GetLongAddressValue(ip));
var ipValue = SwapUnsignedInt((uint)GetLongAddressValue(ip));
return IPMatchCIDR(cidrPrefixValue, ipValue, cidrLength);
}
public static bool IPMatchCIDR(uint cidrPrefixValue, uint ipValue, int cidrLength)
{
if (cidrLength <= 0 || cidrLength >= 32) //if invalid cidr Length, just compare IPs
if (cidrLength <= 0 || cidrLength >= 32) // if invalid cidr Length, just compare IPs
return cidrPrefixValue == ipValue;
uint mask = uint.MaxValue << 32 - cidrLength;
var mask = uint.MaxValue << (32 - cidrLength);
return (cidrPrefixValue & mask) == (ipValue & mask);
}
@ -353,33 +318,33 @@ namespace Server
if (bytes.Length != 4)
return 0;
return (uint)(bytes[0] << 0x18 | bytes[1] << 0x10 | bytes[2] << 8 | bytes[3]) & 0xffffffff;
return (uint)((bytes[0] << 0x18) | (bytes[1] << 0x10) | (bytes[2] << 8) | bytes[3]) & 0xffffffff;
}
private static uint SwapUnsignedInt(uint source) =>
(source & 0x000000FF) << 0x18
| (source & 0x0000FF00) << 8
| (source & 0x00FF0000) >> 8
| (source & 0xFF000000) >> 0x18;
((source & 0x000000FF) << 0x18)
| ((source & 0x0000FF00) << 8)
| ((source & 0x00FF0000) >> 8)
| ((source & 0xFF000000) >> 0x18);
public static bool TryConvertIPv6toIPv4(ref IPAddress address)
{
if (!Socket.OSSupportsIPv6 || address.AddressFamily == AddressFamily.InterNetwork)
return true;
byte[] addr = address.GetAddressBytes();
if (addr.Length == 16) //sanity 0 - 15 //10 11 //12 13 14 15
var addr = address.GetAddressBytes();
if (addr.Length == 16) // sanity 0 - 15 //10 11 //12 13 14 15
{
if (addr[10] != 0xFF || addr[11] != 0xFF)
return false;
for (int i = 0; i < 10; i++)
for (var i = 0; i < 10; i++)
if (addr[i] != 0)
return false;
byte[] v4Addr = new byte[4];
var v4Addr = new byte[4];
for (int i = 0; i < 4; i++) v4Addr[i] = addr[12 + i];
for (var i = 0; i < 4; i++) v4Addr[i] = addr[12 + i];
address = new IPAddress(v4Addr);
return true;
@ -392,9 +357,9 @@ namespace Server
{
valid = true;
string[] split = val.Split('.');
var split = val.Split('.');
for (int i = 0; i < 4; ++i)
for (var i = 0; i < 4; ++i)
{
int lowPart, highPart;
@ -405,7 +370,7 @@ namespace Server
}
else
{
string pattern = split[i];
var pattern = split[i];
if (pattern == "*")
{
@ -417,13 +382,13 @@ namespace Server
lowPart = 0;
highPart = 0;
bool highOnly = false;
int lowBase = 10;
int highBase = 10;
var highOnly = false;
var lowBase = 10;
var highBase = 10;
for (int j = 0; j < pattern.Length; ++j)
for (var j = 0; j < pattern.Length; ++j)
{
char c = pattern[j];
var c = pattern[j];
if (c == '?')
{
@ -448,7 +413,7 @@ namespace Server
}
else if (c >= '0' && c <= '9')
{
int offset = c - '0';
var offset = c - '0';
if (!highOnly)
{
@ -461,7 +426,7 @@ namespace Server
}
else if (c >= 'a' && c <= 'f')
{
int offset = 10 + (c - 'a');
var offset = 10 + (c - 'a');
if (!highOnly)
{
@ -474,7 +439,7 @@ namespace Server
}
else if (c >= 'A' && c <= 'F')
{
int offset = 10 + (c - 'A');
var offset = 10 + (c - 'A');
if (!highOnly)
{
@ -487,13 +452,13 @@ namespace Server
}
else
{
valid = false; //high & lowp art would be 0 if it got to here.
valid = false; // high & lowp art would be 0 if it got to here.
}
}
}
}
int b = (byte)(GetAddressValue(ip) >> i * 8);
int b = (byte)(GetAddressValue(ip) >> (i * 8));
if (b < lowPart || b > highPart)
return false;
@ -502,7 +467,8 @@ namespace Server
return true;
}
public static bool IPMatchClassC(IPAddress ip1, IPAddress ip2) => (GetAddressValue(ip1) & 0xFFFFFF) == (GetAddressValue(ip2) & 0xFFFFFF);
public static bool IPMatchClassC(IPAddress ip1, IPAddress ip2) =>
(GetAddressValue(ip1) & 0xFFFFFF) == (GetAddressValue(ip2) & 0xFFFFFF);
public static int InsensitiveCompare(string first, string second) => Insensitive.Compare(first, second);
@ -510,11 +476,11 @@ namespace Server
public static Direction GetDirection(IPoint2D from, IPoint2D to)
{
int dx = to.X - from.X;
int dy = to.Y - from.Y;
var dx = to.X - from.X;
var dy = to.Y - from.Y;
int adx = Math.Abs(dx);
int ady = Math.Abs(dy);
var adx = Math.Abs(dx);
var ady = Math.Abs(dy);
if (adx >= ady * 3) return dx > 0 ? Direction.East : Direction.West;
@ -525,55 +491,6 @@ namespace Server
return dy > 0 ? Direction.Left : Direction.Up;
}
/* Should probably be rewritten to use an ITile interface
public static bool CanMobileFit( int z, StaticTile[] tiles )
{
int checkHeight = 15;
int checkZ = z;
for ( int i = 0; i < tiles.Length; ++i )
{
StaticTile tile = tiles[i];
if ( ((checkZ + checkHeight) > tile.Z && checkZ < (tile.Z + tile.Height))*/
/* || (tile.Z < (checkZ + checkHeight) && (tile.Z + tile.Height) > checkZ)*/ /* )
{
return false;
}
else if ( checkHeight == 0 && tile.Height == 0 && checkZ == tile.Z )
{
return false;
}
}
return true;
}
public static bool IsInContact( StaticTile check, StaticTile[] tiles )
{
int checkHeight = check.Height;
int checkZ = check.Z;
for ( int i = 0; i < tiles.Length; ++i )
{
StaticTile tile = tiles[i];
if ( ((checkZ + checkHeight) > tile.Z && checkZ < (tile.Z + tile.Height))*/
/* || (tile.Z < (checkZ + checkHeight) && (tile.Z + tile.Height) > checkZ)*/ /* )
{
return true;
}
else if ( checkHeight == 0 && tile.Height == 0 && checkZ == tile.Z )
{
return true;
}
}
return false;
}
*/
public static object GetArrayCap(Array array, int index, object emptyValue = null)
{
if (array.Length > 0)
@ -588,7 +505,8 @@ namespace Server
return emptyValue;
}
public static SkillName RandomSkill() => m_AllSkills[Random(m_AllSkills.Length - (Core.ML ? 0 : Core.SE ? 1 : Core.AOS ? 3 : 6))];
public static SkillName RandomSkill() =>
m_AllSkills[Random(m_AllSkills.Length - (Core.ML ? 0 : Core.SE ? 1 : Core.AOS ? 3 : 6))];
public static SkillName RandomCombatSkill() => m_CombatSkills[Random(m_CombatSkills.Length)];
@ -598,21 +516,21 @@ namespace Server
{
if (bottom.m_X < top.m_X)
{
int swap = top.m_X;
var swap = top.m_X;
top.m_X = bottom.m_X;
bottom.m_X = swap;
}
if (bottom.m_Y < top.m_Y)
{
int swap = top.m_Y;
var swap = top.m_Y;
top.m_Y = bottom.m_Y;
bottom.m_Y = swap;
}
if (bottom.m_Z < top.m_Z)
{
int swap = top.m_Z;
var swap = top.m_Z;
top.m_Z = bottom.m_Z;
bottom.m_Z = swap;
}
@ -629,19 +547,19 @@ namespace Server
output.WriteLine(" 0 1 2 3 4 5 6 7 8 9 A B C D E F");
output.WriteLine(" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
int byteIndex = 0;
var byteIndex = 0;
int whole = length >> 4;
int rem = length & 0xF;
var whole = length >> 4;
var rem = length & 0xF;
for (int i = 0; i < whole; ++i, byteIndex += 16)
for (var i = 0; i < whole; ++i, byteIndex += 16)
{
StringBuilder bytes = new StringBuilder(49);
StringBuilder chars = new StringBuilder(16);
var bytes = new StringBuilder(49);
var chars = new StringBuilder(16);
for (int j = 0; j < 16; ++j)
for (var j = 0; j < 16; ++j)
{
int c = input.ReadByte();
var c = input.ReadByte();
bytes.Append(c.ToString("X2"));
@ -665,13 +583,13 @@ namespace Server
if (rem != 0)
{
StringBuilder bytes = new StringBuilder(49);
StringBuilder chars = new StringBuilder(rem);
var bytes = new StringBuilder(49);
var chars = new StringBuilder(rem);
for (int j = 0; j < 16; ++j)
for (var j = 0; j < 16; ++j)
if (j < rem)
{
int c = input.ReadByte();
var c = input.ReadByte();
bytes.Append(c.ToString("X2"));
@ -727,7 +645,7 @@ namespace Server
{
if (bound1 > bound2)
{
int i = bound1;
var i = bound1;
bound1 = bound2;
bound2 = i;
}
@ -771,31 +689,38 @@ namespace Server
public static List<TOutput> SafeConvertList<TInput, TOutput>(List<TInput> list) where TOutput : class
{
List<TOutput> output = new List<TOutput>(list.Capacity);
if ((list?.Capacity ?? 0) == 0)
return new List<TOutput>();
var output = new List<TOutput>(list.Capacity);
output.AddRange(list.OfType<TOutput>());
return output;
}
#region To[Something]
public static bool ToBoolean(string value)
{
bool.TryParse(value, out bool b);
#pragma warning disable CA1806 // Do not ignore method results
bool.TryParse(value, out var b);
#pragma warning restore CA1806 // Do not ignore method results
return b;
}
public static double ToDouble(string value)
{
double.TryParse(value, out double d);
#pragma warning disable CA1806 // Do not ignore method results
double.TryParse(value, out var d);
#pragma warning restore CA1806 // Do not ignore method results
return d;
}
public static TimeSpan ToTimeSpan(string value)
{
TimeSpan.TryParse(value, out TimeSpan t);
#pragma warning disable CA1806 // Do not ignore method results
TimeSpan.TryParse(value, out var t);
#pragma warning restore CA1806 // Do not ignore method results
return t;
}
@ -804,10 +729,12 @@ namespace Server
{
int i;
#pragma warning disable CA1806 // Do not ignore method results
if (value.StartsWith("0x"))
int.TryParse(value.Substring(2), NumberStyles.HexNumber, null, out i);
else
int.TryParse(value, out i);
#pragma warning restore CA1806 // Do not ignore method results
return i;
}
@ -816,17 +743,25 @@ namespace Server
{
uint i;
#pragma warning disable CA1806 // Do not ignore method results
if (value.StartsWith("0x"))
uint.TryParse(value.Substring(2), NumberStyles.HexNumber, null, out i);
else
uint.TryParse(value, out i);
#pragma warning restore CA1806 // Do not ignore method results
return i;
}
#endregion
public static bool ToInt32(string value, out int i) =>
value.StartsWith("0x")
? int.TryParse(value.Substring(2), NumberStyles.HexNumber, null, out i)
: int.TryParse(value, out i);
#region Get[Something]
public static bool ToUInt32(string value, out uint i) =>
value.StartsWith("0x")
? uint.TryParse(value.Substring(2), NumberStyles.HexNumber, null, out i)
: uint.TryParse(value, out i);
public static double GetXMLDouble(string doubleString, double defaultValue)
{
@ -836,7 +771,7 @@ namespace Server
}
catch
{
return double.TryParse(doubleString, out double val) ? val : defaultValue;
return double.TryParse(doubleString, out var val) ? val : defaultValue;
}
}
@ -848,7 +783,7 @@ namespace Server
}
catch
{
return int.TryParse(intString, out int val) ? val : defaultValue;
return int.TryParse(intString, out var val) ? val : defaultValue;
}
}
@ -860,7 +795,7 @@ namespace Server
}
catch
{
return uint.TryParse(uintString, out uint val) ? val : defaultValue;
return uint.TryParse(uintString, out var val) ? val : defaultValue;
}
}
@ -872,7 +807,7 @@ namespace Server
}
catch
{
return DateTime.TryParse(dateTimeString, out DateTime d) ? d : defaultValue;
return DateTime.TryParse(dateTimeString, out var d) ? d : defaultValue;
}
}
@ -884,7 +819,7 @@ namespace Server
}
catch
{
return DateTimeOffset.TryParse(dateTimeOffsetString, out DateTimeOffset d) ? d : defaultValue;
return DateTimeOffset.TryParse(dateTimeOffsetString, out var d) ? d : defaultValue;
}
}
@ -900,7 +835,8 @@ namespace Server
}
}
public static string GetAttribute(XmlElement node, string attributeName, string defaultValue = null) => node?.Attributes[attributeName]?.Value ?? defaultValue;
public static string GetAttribute(XmlElement node, string attributeName, string defaultValue = null) =>
node?.Attributes[attributeName]?.Value ?? defaultValue;
public static string GetText(XmlElement node, string defaultValue) => node == null ? defaultValue : node.InnerText;
@ -908,10 +844,6 @@ namespace Server
public static long GetLongAddressValue(IPAddress address) => BitConverter.ToInt64(address.GetAddressBytes(), 0);
#endregion
#region In[...]Range
public static bool InRange(Point3D p1, Point3D p2, int range) =>
p1.m_X >= p2.m_X - range
&& p1.m_X <= p2.m_X + range
@ -936,17 +868,13 @@ namespace Server
&& p1.Y >= p2.Y - 18
&& p1.Y <= p2.Y + 18;
#endregion
#region Random
//4d6+8 would be: Utility.Dice( 4, 6, 8 )
// 4d6+8 would be: Utility.Dice( 4, 6, 8 )
public static int Dice(uint numDice, uint numSides, int bonus)
{
int total = 0;
uint sides = numSides;
var total = 0;
var sides = numSides;
for (int i = 0; i < numDice; ++i)
for (var i = 0; i < numDice; ++i)
total += (int)RandomImpl.Next(sides) + 1;
return total + bonus;
@ -954,11 +882,11 @@ namespace Server
public static void Shuffle<T>(IList<T> list)
{
int count = list.Count;
for (int i = count - 1; i > 0; i--)
var count = list.Count;
for (var i = count - 1; i > 0; i--)
{
int r = (int)RandomImpl.Next((uint)count);
T swap = list[r];
var r = (int)RandomImpl.Next((uint)count);
var swap = list[r];
list[r] = list[i];
list[i] = swap;
}
@ -974,12 +902,14 @@ namespace Server
{
if (min > max)
{
int copy = min;
var copy = min;
min = max;
max = copy;
}
else if (min == max)
{
return min;
}
return min + (int)RandomImpl.Next((uint)(max - min + 1));
}
@ -995,7 +925,7 @@ namespace Server
public static int Random(int count) => (int)RandomImpl.Next((uint)count);
public static int Random(uint count) => (int)RandomImpl.Next(count);
public static uint Random(uint count) => RandomImpl.Next(count);
public static void RandomBytes(byte[] buffer) => RandomImpl.GetBytes(buffer);
@ -1003,10 +933,6 @@ namespace Server
public static double RandomDouble() => RandomImpl.NextDouble();
#endregion
#region Random Hues
/// <summary>
/// Random pink, blue, green, orange, red or yellow hue
/// </summary>
@ -1101,9 +1027,6 @@ namespace Server
/// Random hue from 0x62, 0x71, 0x03, 0x0D, 0x13, 0x1C, 0x21, 0x30, 0x37, 0x3A, 0x44, 0x59
/// </summary>
public static int RandomBrightHue() =>
RandomDouble() < 0.1 ? RandomList(0x62, 0x71) :
RandomList(0x03, 0x0D, 0x13, 0x1C, 0x21, 0x30, 0x37, 0x3A, 0x44, 0x59);
#endregion
RandomDouble() < 0.1 ? RandomList(0x62, 0x71) : RandomList(0x03, 0x0D, 0x13, 0x1C, 0x21, 0x30, 0x37, 0x3A, 0x44, 0x59);
}
}