fix(cleanup): Consolidate the duplicate code in Properties, PropsGump, and PropsConfig (#540)

This commit is contained in:
Kamron Batman 2021-04-14 22:58:56 -07:00 • committed by GitHub
parent 98ce65083a
commit dd2933a9ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 335 additions and 761 deletions

View file

@ -6,39 +6,15 @@ using System.Text;
using Server.Items;
using CPA = Server.CommandPropertyAttribute;
using static Server.Attributes;
using static Server.Types;
namespace Server.Commands
{
public static class Add
{
private static readonly Type m_EntityType = typeof(IEntity);
private static readonly Type m_ConstructibleType = typeof(ConstructibleAttribute);
private static readonly Type m_EnumType = typeof(Enum);
private static readonly Type m_TypeType = typeof(Type);
private static readonly Type m_ParsableType = typeof(ParsableAttribute);
private static readonly Type[] m_ParseTypes = { typeof(string) };
private static readonly object[] m_ParseArgs = new object[1];
private static readonly Type[] m_SignedNumerics =
{
typeof(long),
typeof(int),
typeof(short),
typeof(sbyte)
};
private static readonly Type[] m_UnsignedNumerics =
{
typeof(ulong),
typeof(uint),
typeof(ushort),
typeof(byte)
};
public static void Initialize()
{
CommandSystem.Register("Tile", AccessLevel.GameMaster, Tile_OnCommand);
@ -193,7 +169,7 @@ namespace Server.Commands
}
else
{
var attr = Properties.GetCPA(thisProp);
var attr = GetCPA(thisProp);
if (attr == null)
{
@ -759,56 +735,21 @@ namespace Server.Commands
InternalAvg_OnCommand(e, true);
}
public static bool IsEntity(Type t) => m_EntityType.IsAssignableFrom(t);
public static bool IsEnum(Type type) => type.IsSubclassOf(OfEnum);
public static bool IsConstructible(ConstructorInfo ctor, AccessLevel accessLevel)
{
var attrs = ctor.GetCustomAttributes(m_ConstructibleType, false);
public static bool IsType(Type type) => type == OfType || type.IsSubclassOf(OfType);
return attrs.Length != 0 && accessLevel >= ((ConstructibleAttribute)attrs[0]).AccessLevel;
}
public static bool IsEnum(Type type) => type.IsSubclassOf(m_EnumType);
public static bool IsType(Type type) => type == m_TypeType || type.IsSubclassOf(m_TypeType);
public static bool IsParsable(Type type) => type.IsDefined(m_ParsableType, false);
public static bool IsParsable(Type type) => type.IsDefined(OfParsable, false);
public static object ParseParsable(Type type, string value)
{
var method = type.GetMethod("Parse", m_ParseTypes);
var method = type.GetMethod("Parse", ParseTypes);
m_ParseArgs[0] = value;
return method?.Invoke(null, m_ParseArgs);
}
public static bool IsSignedNumeric(Type type)
{
for (var i = 0; i < m_SignedNumerics.Length; ++i)
{
if (type == m_SignedNumerics[i])
{
return true;
}
}
return false;
}
public static bool IsUnsignedNumeric(Type type)
{
for (var i = 0; i < m_UnsignedNumerics.Length; ++i)
{
if (type == m_UnsignedNumerics[i])
{
return true;
}
}
return false;
}
private enum TileZType
{
Start,