Clean up: Bugs, LINQ, constructors, default values, and null checks (#18)

This commit is contained in:
Kamron Batman 2019-03-11 14:29:59 -07:00 • committed by GitHub
parent e748af4430
commit 513e54f70e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1094 changed files with 15913 additions and 21892 deletions

View file

@ -190,7 +190,7 @@ namespace Server.Commands
if (!IsConstructible(ctor, from.AccessLevel))
continue;
int totalParams = 0;
// Handle optional constructors
@ -250,7 +250,7 @@ namespace Server.Commands
if (IsParsable(type)) return ParseParsable(type, value);
object obj = value;
if (value != null && value.StartsWith("0x"))
if (value?.StartsWith("0x") == true)
{
if (IsSignedNumeric(type))
obj = Convert.ToInt64(value.Substring(2), 16);
@ -262,7 +262,7 @@ namespace Server.Commands
if (obj == null && !type.IsValueType)
return null;
return Convert.ChangeType(obj, type);
}
catch
@ -455,7 +455,7 @@ namespace Server.Commands
private static void Internal_OnCommand(CommandEventArgs e, bool outline)
{
Mobile from = e.Mobile;
if (e.Length >= 1)
BoundingBoxPicker.Begin(from, (map, start, end) =>
TileBox_Callback(from, map, start, end, new TileState(TileZType.Start, 0, e.Arguments, outline)));
@ -511,7 +511,7 @@ namespace Server.Commands
private static void InternalZ_OnCommand(CommandEventArgs e, bool outline)
{
Mobile from = e.Mobile;
if (e.Length >= 2)
{
string[] subArgs = new string[e.Length - 1];
@ -532,7 +532,7 @@ namespace Server.Commands
private static void InternalAvg_OnCommand(CommandEventArgs e, bool outline)
{
Mobile from = e.Mobile;
if (e.Length >= 1)
BoundingBoxPicker.Begin(from, (map, start, end) =>
TileBox_Callback(from, map, start, end, new TileState(TileZType.MapAverage, 0, e.Arguments, outline)));
@ -701,4 +701,4 @@ namespace Server.Commands
}
}
}
}
}

View file

@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Server.Commands.Generic;
@ -308,7 +307,7 @@ namespace Server.Commands
for (int i = m_Batch.BatchCommands.Count - 1; i >= 0; --i)
{
BatchCommand sc = (BatchCommand)m_Batch.BatchCommands[i];
BatchCommand sc = m_Batch.BatchCommands[i];
entry = info.GetTextEntry(1 + i * 2);

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Server.Engines.Quests.Haven;
using Server.Engines.Quests.Necro;
using Server.Items;
@ -429,7 +430,7 @@ namespace Server.Commands
int indexOf = m_Params[i].IndexOf('=');
if (indexOf >= 0)
sp.AddEntry(m_Params[i].Substring(++indexOf), 100, 1);
sp.AddEntry(m_Params[i].Substring(++indexOf));
}
else if (m_Params[i].StartsWith("MinDelay"))
{
@ -901,12 +902,11 @@ namespace Server.Commands
{
eable = map.GetItemsInRange(new Point3D(x, y, z), 0);
foreach (Item item in eable)
if (item.Z == z && item.ItemID == itemID)
{
eable.Free();
return true;
}
if (eable.Any(item => item.Z == z && item.ItemID == itemID))
{
eable.Free();
return true;
}
}
eable.Free();
@ -989,7 +989,7 @@ namespace Server.Commands
{
List<DecorationList> list = new List<DecorationList>();
DecorationList v;
while ((v = Read(ip)) != null)
list.Add(v);
@ -1066,11 +1066,9 @@ namespace Server.Commands
{
public DecorationEntry(string line)
{
string x, y, z;
Pop(out x, ref line);
Pop(out y, ref line);
Pop(out z, ref line);
Pop(out string x, ref line);
Pop(out string y, ref line);
Pop(out string z, ref line);
Location = new Point3D(Utility.ToInt32(x), Utility.ToInt32(y), Utility.ToInt32(z));
Extra = line;
@ -1096,4 +1094,4 @@ namespace Server.Commands
}
}
}
}
}

View file

@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Server.Engines.Quests.Haven;
using Server.Engines.Quests.Necro;
using Server.Items;
@ -427,7 +428,7 @@ namespace Server.Commands
int indexOf = m_Params[i].IndexOf('=');
if (indexOf >= 0)
sp.AddEntry(m_Params[i].Substring(++indexOf), 100, 1);
sp.AddEntry(m_Params[i].Substring(++indexOf));
}
else if (m_Params[i].StartsWith("MinDelay"))
{
@ -899,12 +900,11 @@ namespace Server.Commands
{
eable = map.GetItemsInRange(new Point3D(x, y, z), 0);
foreach (Item item in eable)
if (item.Z == z && item.ItemID == itemID)
{
eable.Free();
return true;
}
if (eable.Any(item => item.Z == z && item.ItemID == itemID))
{
eable.Free();
return true;
}
}
eable.Free();
@ -1064,11 +1064,9 @@ namespace Server.Commands
{
public DecorationEntryMag(string line)
{
string x, y, z;
Pop(out x, ref line);
Pop(out y, ref line);
Pop(out z, ref line);
Pop(out string x, ref line);
Pop(out string y, ref line);
Pop(out string z, ref line);
Location = new Point3D(Utility.ToInt32(x), Utility.ToInt32(y), Utility.ToInt32(z));
Extra = line;
@ -1094,4 +1092,4 @@ namespace Server.Commands
}
}
}
}
}

View file

@ -229,11 +229,9 @@ namespace Server.Commands
StringBuilder nameBuilder = new StringBuilder(rootType);
StringBuilder fnamBuilder = new StringBuilder("docs/types/" + SanitizeType(rootType));
StringBuilder linkBuilder;
if (DontLink(type)) //if ( DontLink( rootType ) )
linkBuilder = new StringBuilder("<font color=\"blue\">" + rootType + "</font>");
else
linkBuilder =
new StringBuilder("<a href=\"" + "@directory@" + rootType + "-T-.html\">" + rootType + "</a>");
linkBuilder = DontLink(type) ?
new StringBuilder("<font color=\"blue\">" + rootType + "</font>") :
new StringBuilder("<a href=\"" + "@directory@" + rootType + "-T-.html\">" + rootType + "</a>");
nameBuilder.Append("&lt;");
fnamBuilder.Append("-");
@ -272,10 +270,7 @@ namespace Server.Commands
}
}
if (name == null)
typeName = type.Name;
else
typeName = name;
typeName = name ?? type.Name;
if (fnam == null) fileName = "docs/types/" + SanitizeType(type.Name) + ".html";
else fileName = fnam + ".html";
@ -300,7 +295,8 @@ namespace Server.Commands
{
bool anonymousType = name.Contains("<");
StringBuilder sb = new StringBuilder(name);
for (int i = 0; i < ReplaceChars.Length; ++i) sb.Replace(ReplaceChars[i], '-');
for (int i = 0; i < ReplaceChars.Length; ++i)
sb.Replace(ReplaceChars[i], '-');
if (anonymousType) return "(Anonymous-Type)" + sb;
return sb.ToString();
@ -418,7 +414,7 @@ namespace Server.Commands
MethodInfo getMethod = prop.GetGetMethod();
MethodInfo setMethod = prop.GetGetMethod();
return getMethod != null && getMethod.IsStatic || setMethod != null && setMethod.IsStatic;
return getMethod?.IsStatic == true || setMethod?.IsStatic == true;
}
return false;
@ -426,13 +422,7 @@ namespace Server.Commands
private string GetNameFrom(ConstructorInfo ctor, PropertyInfo prop, MethodInfo method)
{
if (ctor != null)
return ctor.DeclaringType?.Name ?? "";
if (prop != null)
return prop.Name;
if (method != null)
return method.Name;
return "";
return ctor?.DeclaringType?.Name ?? prop?.Name ?? method?.Name ?? "";
}
}
@ -440,14 +430,8 @@ namespace Server.Commands
{
public int Compare(TypeInfo x, TypeInfo y)
{
if (x == null && y == null)
return 0;
if (x == null)
return -1;
if (y == null)
return 1;
return x.TypeName.CompareTo(y.TypeName);
return x == null && y == null ? 0 : x == null ? -1 : y == null ? 1 :
x.TypeName.CompareTo(y.TypeName);
}
}
@ -2463,7 +2447,7 @@ namespace Server.Commands
int extendCount = 0;
if (baseType != null && baseType != typeof(object) && baseType != typeof(ValueType) && !baseType.IsPrimitive)
if (baseType != typeof(object) && baseType != typeof(ValueType) && baseType?.IsPrimitive == false)
{
typeHtml.Write(" : ");
@ -2575,7 +2559,7 @@ namespace Server.Commands
MethodInfo getMethod = pi.GetGetMethod();
MethodInfo setMethod = pi.GetSetMethod();
if (getMethod != null && getMethod.IsStatic || setMethod != null && setMethod.IsStatic)
if (getMethod?.IsStatic == true || setMethod?.IsStatic == true)
html.Write(StaticString);
html.Write(GetPair(pi.PropertyType, pi.Name, false));

View file

@ -175,7 +175,7 @@ namespace Server.Commands
ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
return ctor != null && ctor.IsDefined(typeofConstructible, false);
return ctor?.IsDefined(typeofConstructible, false) == true;
}
private static void AddTypes(Assembly asm, List<Type> types)
@ -399,4 +399,4 @@ namespace Server.Commands
return list.ToArray();
}
}
}
}

View file

@ -105,7 +105,7 @@ namespace Server.Commands.Generic
PropertyInfo[] chain = Properties.GetPropertyInfoChain(m_From, obj.GetType(), m_Columns[i],
PropertyAccess.Read, ref failReason);
if (chain != null && chain.Length > 0)
if (chain?.Length > 0)
{
m_Columns[i] = "";
@ -565,4 +565,4 @@ namespace Server.Commands.Generic
}
}
}
}
}

View file

@ -90,93 +90,93 @@ namespace Server.Commands.Generic
public void Acquire(TypeBuilder typeBuilder, ILGenerator il, string fieldName)
{
if (Value is string toParse)
{
if (!Type.IsValueType && toParse == "null")
{
Value = null;
}
else if (Type == typeof(string))
{
if (toParse == @"@""null""")
toParse = "null";
if (!(Value is string toParse))
return;
Value = toParse;
}
else if (Type.IsEnum)
if (!Type.IsValueType && toParse == "null")
{
Value = null;
}
else if (Type == typeof(string))
{
if (toParse == @"@""null""")
toParse = "null";
Value = toParse;
}
else if (Type.IsEnum)
{
Value = Enum.Parse(Type, toParse, true);
}
else
{
MethodInfo parseMethod;
object[] parseArgs;
MethodInfo parseNumber = Type.GetMethod(
"Parse",
BindingFlags.Public | BindingFlags.Static,
null,
new[] { typeof(string), typeof(NumberStyles) },
null
);
if (parseNumber != null)
{
Value = Enum.Parse(Type, toParse, true);
NumberStyles style = NumberStyles.Integer;
if (Insensitive.StartsWith(toParse, "0x"))
{
style = NumberStyles.HexNumber;
toParse = toParse.Substring(2);
}
parseMethod = parseNumber;
parseArgs = new object[] { toParse, style };
}
else
{
MethodInfo parseMethod;
object[] parseArgs;
MethodInfo parseNumber = Type.GetMethod(
MethodInfo parseGeneral = Type.GetMethod(
"Parse",
BindingFlags.Public | BindingFlags.Static,
null,
new[] { typeof(string), typeof(NumberStyles) },
new[] { typeof(string) },
null
);
if (parseNumber != null)
{
NumberStyles style = NumberStyles.Integer;
parseMethod = parseGeneral;
parseArgs = new object[] { toParse };
}
if (Insensitive.StartsWith(toParse, "0x"))
{
style = NumberStyles.HexNumber;
toParse = toParse.Substring(2);
}
if (parseMethod != null)
{
Value = parseMethod.Invoke(null, parseArgs);
parseMethod = parseNumber;
parseArgs = new object[] { toParse, style };
}
else
if (!Type.IsPrimitive)
{
MethodInfo parseGeneral = Type.GetMethod(
"Parse",
BindingFlags.Public | BindingFlags.Static,
null,
new[] { typeof(string) },
null
Field = typeBuilder.DefineField(
fieldName,
Type,
FieldAttributes.Private | FieldAttributes.InitOnly
);
parseMethod = parseGeneral;
parseArgs = new object[] { toParse };
}
if (parseMethod != null)
{
Value = parseMethod.Invoke(null, parseArgs);
if (!Type.IsPrimitive)
{
Field = typeBuilder.DefineField(
fieldName,
Type,
FieldAttributes.Private | FieldAttributes.InitOnly
);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldstr, toParse);
if (parseArgs.Length == 2) // dirty evil hack :-(
il.Emit(OpCodes.Ldc_I4, (int)parseArgs[1]);
il.Emit(OpCodes.Call, parseMethod);
il.Emit(OpCodes.Stfld, Field);
}
}
else
{
throw new InvalidOperationException(
$"Unable to convert string \"{Value}\" into type '{Type}'."
);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldstr, toParse);
if (parseArgs.Length == 2) // dirty evil hack :-(
il.Emit(OpCodes.Ldc_I4, (int)parseArgs[1]);
il.Emit(OpCodes.Call, parseMethod);
il.Emit(OpCodes.Stfld, Field);
}
}
else
{
throw new InvalidOperationException(
$"Unable to convert string \"{Value}\" into type '{Type}'."
);
}
}
}
}
@ -542,4 +542,4 @@ namespace Server.Commands.Generic
return (IConditional)Activator.CreateInstance(conditionalType);
}
}
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Server.Commands.Generic
{
@ -36,28 +37,15 @@ namespace Server.Commands.Generic
if (!CheckObjectTypes(from, command, ext, out bool items, out bool mobiles))
return;
IPooledEnumerable<IEntity> eable;
if (items || mobiles)
eable = map.GetObjectsInBounds(rect, items, mobiles);
else
if (!(items || mobiles))
return;
eable.Free();
IPooledEnumerable<IEntity> eable = map.GetObjectsInBounds(rect, items, mobiles);
List<object> objs = new List<object>();
foreach (IEntity obj in eable)
{
if (mobiles && obj is Mobile && !BaseCommand.IsAccessible(from, obj))
continue;
if (ext.IsValid(obj))
objs.Add(obj);
}
List<object> objs = eable.Where(obj => !mobiles || !(obj is Mobile) || BaseCommand.IsAccessible(from, obj))
.Where(obj => ext.IsValid(obj)).Cast<object>().ToList();
eable.Free();
ext.Filter(objs);
RunCommand(from, objs, command, args);
@ -68,4 +56,4 @@ namespace Server.Commands.Generic
}
}
}
}
}

View file

@ -299,7 +299,7 @@ namespace Server.Commands
for (int i = 0; i < pets.Count; ++i)
{
Mobile pet = (Mobile)pets[i];
Mobile pet = pets[i];
if (pet is IMount mount)
mount.Rider = null; // make sure it's dismounted
@ -410,18 +410,20 @@ namespace Server.Commands
private static bool FixMap(ref Map map, ref Point3D loc, Item item)
{
return map == null || map == Map.Internal && item.RootParent is Mobile m && FixMap(ref map, ref loc, m);
return map != null && map != Map.Internal || item.RootParent is Mobile m && FixMap(ref map, ref loc, m);
}
private static bool FixMap(ref Map map, ref Point3D loc, Mobile m)
{
if (map == null || map == Map.Internal)
bool validMap = map != null && map != Map.Internal;
if (!validMap)
{
map = m.LogoutMap;
loc = m.LogoutLocation;
}
return map != null && map != Map.Internal;
return validMap;
}
[Usage("Go [name | serial | (x y [z]) | (deg min (N | S) deg min (E | W))]")]
@ -676,7 +678,7 @@ namespace Server.Commands
{
Mobile m = state.Mobile;
if (m != null && m.AccessLevel >= ac)
if (m?.AccessLevel >= ac)
m.SendMessage(hue, message);
}
}
@ -957,4 +959,4 @@ namespace Server.Commands
}
}
}
}
}

View file

@ -337,12 +337,7 @@ namespace Server.Commands
public class CommandInfoGump : Gump
{
public CommandInfoGump(CommandInfo info)
: this(info, 320, 200)
{
}
public CommandInfoGump(CommandInfo info, int width, int height)
public CommandInfoGump(CommandInfo info, int width = 320, int height = 200)
: base(300, 50)
{
AddPage(0);
@ -352,7 +347,7 @@ namespace Server.Commands
//AddImageTiled( 10, 10, width - 20, 20, 2624 );
//AddAlphaRegion( 10, 10, width - 20, 20 );
//AddHtmlLocalized( 10, 10, width - 20, 20, header, headerColor, false, false );
AddHtml(10, 10, width - 20, 20, Color(Center(info.Name), 0xFF0000), false, false);
AddHtml(10, 10, width - 20, 20, Color(Center(info.Name), 0xFF0000));
//AddImageTiled( 10, 40, width - 20, height - 80, 2624 );
//AddAlphaRegion( 10, 40, width - 20, height - 80 );
@ -365,7 +360,7 @@ namespace Server.Commands
string[] aliases = info.Aliases;
if (aliases != null && aliases.Length != 0)
if (aliases?.Length > 0)
{
sb.Append($"Alias{(aliases.Length == 1 ? "" : "es")}: ");
@ -404,4 +399,4 @@ namespace Server.Commands
}
}
}
}
}

View file

@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

View file

@ -16,7 +16,7 @@ namespace Server.Commands
ReadWrite = Read | Write
}
public class Properties
public static class Properties
{
private static Type typeofCPA = typeof(CPA);
@ -174,10 +174,7 @@ namespace Server.Commands
{
PropertyInfo[] chain = GetPropertyInfoChain(from, obj.GetType(), propertyName, access, ref failReason);
if (chain == null)
return null;
return GetPropertyInfo(ref obj, chain, ref failReason);
return chain == null ? null : GetPropertyInfo(ref obj, chain, ref failReason);
}
public static PropertyInfo GetPropertyInfo(ref object obj, PropertyInfo[] chain, ref string failReason)
@ -216,21 +213,19 @@ namespace Server.Commands
PropertyInfo p = GetPropertyInfo(ref o, chain, ref failReason);
if (p == null)
return failReason;
return InternalGetValue(o, p, chain);
return p == null ? failReason : InternalGetValue(o, p, chain);
}
public static string IncreaseValue(Mobile from, object o, string[] args)
{
Type type = o.GetType();
// Type type = o.GetType();
object[] realObjs = new object[args.Length / 2];
PropertyInfo[] realProps = new PropertyInfo[args.Length / 2];
int[] realValues = new int[args.Length / 2];
bool positive = false, negative = false;
bool positive = false;
bool negative = false;
for (int i = 0; i < realProps.Length; ++i)
{
@ -315,9 +310,9 @@ namespace Server.Commands
if (value == null)
toString = "null";
else if (IsNumeric(type))
toString = string.Format("{0} (0x{0:X})", value);
toString = $"{value} (0x{value:X})";
else if (IsChar(type))
toString = string.Format("'{0}' ({1} [0x{1:X}])", value, (int)value);
toString = $"'{value}' ({(int)value} [0x{(int)value:X}])";
else if (IsString(type))
toString = (string)value == "null" ? @"@""null""" : $"\"{value}\"";
else if (IsText(type))
@ -348,10 +343,7 @@ namespace Server.Commands
string failReason = "";
PropertyInfo p = GetPropertyInfo(from, ref o, name, PropertyAccess.Write, ref failReason);
if (p == null)
return failReason;
return InternalSetValue(from, logObject, o, p, name, value, true);
return p == null ? failReason : InternalSetValue(from, logObject, o, p, name, value, true);
}
private static bool IsSerial(Type t)
@ -524,10 +516,7 @@ namespace Server.Commands
object toSet = null;
string result = ConstructFromString(p.PropertyType, o, value, ref toSet);
if (result != null)
return result;
return SetDirect(from, logobj, o, p, pname, toSet, shouldLog);
return result ?? SetDirect(from, logobj, o, p, pname, toSet, shouldLog);
}
public static string InternalSetValue(object o, PropertyInfo p, string value)
@ -535,10 +524,7 @@ namespace Server.Commands
object toSet = null;
string result = ConstructFromString(p.PropertyType, o, value, ref toSet);
if (result != null)
return result;
return SetDirect(o, p, toSet);
return result ?? SetDirect(o, p, toSet);
}
private class PropsTarget : Target
@ -639,18 +625,14 @@ namespace Server
public abstract class ClearanceException : AccessException
{
protected AccessLevel m_NeededAccess;
protected AccessLevel m_PlayerAccess;
public ClearanceException(Property property, AccessLevel playerAccess, AccessLevel neededAccess, string accessType)
: base(property,
$"You must be at least {Mobile.GetAccessLevelName(neededAccess)} to {accessType} this property.")
{
}
public AccessLevel PlayerAccess => m_PlayerAccess;
public AccessLevel NeededAccess => m_NeededAccess;
public AccessLevel PlayerAccess{ get; set; }
public AccessLevel NeededAccess{ get; set; }
}
public sealed class ReadAccessException : ClearanceException
@ -801,4 +783,4 @@ namespace Server
return prop;
}
}
}
}

View file

@ -81,7 +81,7 @@ namespace Server.Commands
break; // Tokuno Islands
}
for (int j = 0; maps != null && j < maps.Length; ++j)
for (int j = 0; maps?.Length >= j; ++j)
Add_Static(e.m_ItemID, e.m_Location, maps[j], e.m_Text);
}
@ -145,4 +145,4 @@ namespace Server.Commands
}
}
}
}
}

View file

@ -1,4 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Server.Commands;