Some cleanup of contains keys and try get values (#246)

This commit is contained in:
Kamron Batman 2020-09-13 20:20:44 -07:00 • committed by GitHub
parent 9cbb0cfd23
commit a236617ad9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
69 changed files with 3508 additions and 350 deletions

View file

@ -172,7 +172,9 @@ namespace Server.Gumps
m_Object = o;
if (o != null)
{
m_Type = o.GetType();
}
m_List = list;
m_Stack = stack;
@ -189,7 +191,9 @@ namespace Server.Gumps
var lastIndex = page * EntryCount + count - 1;
if (lastIndex >= 0 && lastIndex < m_List.Count && m_List[lastIndex] == null)
{
--count;
}
var totalHeight = OffsetSize + (EntryHeight + OffsetSize) * (count + 1);
@ -210,24 +214,33 @@ namespace Server.Gumps
var emptyWidth = TotalWidth - PrevWidth - NextWidth - OffsetSize * 4 - (OldStyle ? SetWidth + OffsetSize : 0);
if (OldStyle)
{
AddImageTiled(x, y, TotalWidth - OffsetSize * 3 - SetWidth, EntryHeight, HeaderGumpID);
}
else
{
AddImageTiled(x, y, PrevWidth, EntryHeight, HeaderGumpID);
}
if (page > 0)
{
AddButton(x + PrevOffsetX, y + PrevOffsetY, PrevButtonID1, PrevButtonID2, 1);
if (PrevLabel)
{
AddLabel(x + PrevLabelOffsetX, y + PrevLabelOffsetY, TextHue, "Previous");
}
}
x += PrevWidth + OffsetSize;
if (!OldStyle)
{
AddImageTiled(x, y, emptyWidth, EntryHeight, HeaderGumpID);
}
if (TypeLabel && m_Type != null)
{
AddHtml(
x,
y,
@ -235,18 +248,23 @@ namespace Server.Gumps
EntryHeight,
$"<BASEFONT COLOR=#FAFAFA><CENTER>{m_Type.Name}</CENTER></BASEFONT>"
);
}
x += emptyWidth + OffsetSize;
if (!OldStyle)
{
AddImageTiled(x, y, NextWidth, EntryHeight, HeaderGumpID);
}
if ((page + 1) * EntryCount < m_List.Count)
{
AddButton(x + NextOffsetX, y + NextOffsetY, NextButtonID1, NextButtonID2, 2, GumpButtonType.Reply, 1);
if (NextLabel)
{
AddLabel(x + NextLabelOffsetX, y + NextLabelOffsetY, TextHue, "Next");
}
}
for (int i = 0, index = page * EntryCount; i < count && index < m_List.Count; ++i, ++index)
@ -267,7 +285,9 @@ namespace Server.Gumps
x += TypeWidth + OffsetSize;
if (SetGumpID != 0)
{
AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID);
}
}
else if (o is PropertyInfo prop)
{
@ -279,13 +299,17 @@ namespace Server.Gumps
x += ValueWidth + OffsetSize;
if (SetGumpID != 0)
{
AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID);
}
var cpa = GetCPA(prop);
if ((!prop.GetType().IsValueType || prop.CanWrite) && cpa != null &&
m_Mobile.AccessLevel >= cpa.WriteLevel && !cpa.ReadOnly)
{
AddButton(x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + 3);
}
}
}
}
@ -315,14 +339,18 @@ namespace Server.Gumps
case 1: // Previous
{
if (m_Page > 0)
{
from.SendGump(new PropertiesGump(from, m_Object, m_Stack, m_List, m_Page - 1));
}
break;
}
case 2: // Next
{
if ((m_Page + 1) * EntryCount < m_List.Count)
{
from.SendGump(new PropertiesGump(from, m_Object, m_Stack, m_List, m_Page + 1));
}
break;
}
@ -335,13 +363,17 @@ namespace Server.Gumps
var prop = m_List[index] as PropertyInfo;
if (prop == null)
{
return;
}
var attr = GetCPA(prop);
if (prop.GetType().IsValueType && !prop.CanWrite || attr == null ||
from.AccessLevel < attr.WriteLevel || attr.ReadOnly)
{
return;
}
var type = prop.PropertyType;
@ -454,9 +486,13 @@ namespace Server.Gumps
var obj = prop.GetValue(m_Object, null);
if (obj != null)
{
from.SendGump(new PropertiesGump(from, obj, m_Stack, new StackEntry(m_Object, prop)));
}
else
{
from.SendGump(new PropertiesGump(from, m_Object, m_Stack, m_List, m_Page));
}
}
}
@ -470,7 +506,9 @@ namespace Server.Gumps
var list = new object[a.Length];
for (var i = 0; i < list.Length; ++i)
{
list[i] = a.GetValue(i);
}
return list;
}
@ -480,15 +518,21 @@ namespace Server.Gumps
public static void OnValueChanged(object obj, PropertyInfo prop, Stack<StackEntry> stack)
{
if (stack == null || stack.Count == 0)
{
return;
}
if (!prop.PropertyType.IsValueType)
{
return;
}
var peek = stack.Peek();
if (peek.m_Property.CanWrite)
{
peek.m_Property.SetValue(peek.m_Object, obj, null);
}
}
private static string[] GetCustomEnumNames(Type type)
@ -496,10 +540,14 @@ namespace Server.Gumps
var attrs = type.GetCustomAttributes(typeofCustomEnum, false);
if (attrs.Length == 0)
{
return Array.Empty<string>();
}
if (!(attrs[0] is CustomEnumAttribute ce))
{
return Array.Empty<string>();
}
return ce.Names;
}
@ -512,8 +560,12 @@ namespace Server.Gumps
private static bool IsType(Type type, Type[] check)
{
for (var i = 0; i < check.Length; ++i)
{
if (IsType(type, check[i]))
{
return true;
}
}
return false;
}
@ -534,27 +586,68 @@ namespace Server.Gumps
public static string ValueToString(object o)
{
if (o == null) return "-null-";
if (o is string s) return $"\"{s}\"";
if (o is bool) return o.ToString();
if (o is char c) return $"0x{(int)c:X} '{c}'";
if (o == null)
{
return "-null-";
}
if (o is string s)
{
return $"\"{s}\"";
}
if (o is bool)
{
return o.ToString();
}
if (o is char c)
{
return $"0x{(int)c:X} '{c}'";
}
if (o is Serial serial)
{
if (serial.IsValid)
{
if (serial.IsItem) return $"(I) 0x{serial.Value:X}";
if (serial.IsMobile) return $"(M) 0x{serial.Value:X}";
if (serial.IsItem)
{
return $"(I) 0x{serial.Value:X}";
}
if (serial.IsMobile)
{
return $"(M) 0x{serial.Value:X}";
}
}
return $"(?) 0x{serial.Value:X}";
}
if (o is byte || o is sbyte || o is short || o is ushort || o is int || o is uint || o is long || o is ulong)
{
return $"{o} (0x{o:X})";
if (o is Mobile mobile) return $"(M) 0x{mobile.Serial.Value:X} \"{mobile.Name}\"";
if (o is Item item) return $"(I) 0x{item.Serial.Value:X}";
if (o is Type type) return type.Name;
if (o is TextDefinition definition) return definition.Format(true);
}
if (o is Mobile mobile)
{
return $"(M) 0x{mobile.Serial.Value:X} \"{mobile.Name}\"";
}
if (o is Item item)
{
return $"(I) 0x{item.Serial.Value:X}";
}
if (o is Type type)
{
return type.Name;
}
if (o is TextDefinition definition)
{
return definition.Format(true);
}
return o.ToString();
}
@ -564,7 +657,9 @@ namespace Server.Gumps
var list = new List<object>();
if (m_Type == null)
{
return list;
}
var props = m_Type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
@ -575,10 +670,14 @@ namespace Server.Gumps
var kvp = groups[i];
if (!HasAttribute(kvp.Key, typeofNoSort, false))
{
kvp.Value.Sort(PropertySorter.Instance);
}
if (i != 0)
{
list.Add(null);
}
list.Add(kvp.Key);
list.AddRange(kvp.Value);
@ -592,7 +691,10 @@ namespace Server.Gumps
var attrs = prop.GetCustomAttributes(typeofCPA, false);
if (attrs.Length > 0)
{
return attrs[0] as CPA;
}
return null;
}
@ -617,15 +719,24 @@ namespace Server.Gumps
var baseType = type?.BaseType;
if (baseType == typeofObject || baseType?.GetProperty(prop.Name, prop.PropertyType) == null)
{
break;
}
type = baseType;
}
if (type != null && !groups.ContainsKey(type))
groups[type] = new List<PropertyInfo> { prop };
else
groups[type].Add(prop);
if (type != null)
{
if (groups.TryGetValue(type, out var result))
{
result.Add(prop);
}
else
{
groups[type] = new List<PropertyInfo> { prop };
}
}
}
}
}
@ -638,7 +749,10 @@ namespace Server.Gumps
public static object GetObjectFromString(Type t, string s)
{
if (t == typeof(string)) return s;
if (t == typeof(string))
{
return s;
}
if (t == typeof(byte) || t == typeof(sbyte) || t == typeof(short) || t == typeof(ushort) || t == typeof(int) ||
t == typeof(uint) || t == typeof(long) || t == typeof(ulong))
@ -646,7 +760,9 @@ namespace Server.Gumps
if (s.StartsWith("0x"))
{
if (t == typeof(ulong) || t == typeof(uint) || t == typeof(ushort) || t == typeof(byte))
{
return Convert.ChangeType(Convert.ToUInt64(s.Substring(2), 16), t);
}
return Convert.ChangeType(Convert.ToInt64(s.Substring(2), 16), t);
}
@ -654,7 +770,11 @@ namespace Server.Gumps
return Convert.ChangeType(s, t);
}
if (t == typeof(double) || t == typeof(float)) return Convert.ChangeType(s, t);
if (t == typeof(double) || t == typeof(float))
{
return Convert.ChangeType(s, t);
}
if (t.IsDefined(typeof(ParsableAttribute), false))
{
var parseMethod = t.GetMethod("Parse", new[] { typeof(string) });
@ -667,26 +787,63 @@ namespace Server.Gumps
private static string GetStringFromObject(object o)
{
if (o == null) return "-null-";
if (o is string s) return $"\"{s}\"";
if (o is bool) return o.ToString();
if (o is char c) return $"0x{(int)c:X} '{c}'";
if (o == null)
{
return "-null-";
}
if (o is string s)
{
return $"\"{s}\"";
}
if (o is bool)
{
return o.ToString();
}
if (o is char c)
{
return $"0x{(int)c:X} '{c}'";
}
if (o is Serial serial)
{
if (serial.IsValid)
{
if (serial.IsItem) return $"(I) 0x{serial.Value:X}";
if (serial.IsMobile) return $"(M) 0x{serial.Value:X}";
if (serial.IsItem)
{
return $"(I) 0x{serial.Value:X}";
}
if (serial.IsMobile)
{
return $"(M) 0x{serial.Value:X}";
}
}
return $"(?) 0x{serial.Value:X}";
}
if (o is byte || o is sbyte || o is short || o is ushort || o is int || o is uint || o is long || o is ulong)
{
return $"{o} (0x{o:X})";
if (o is Mobile mobile) return $"(M) 0x{mobile.Serial.Value:X} \"{mobile.Name}\"";
if (o is Item item) return $"(I) 0x{item.Serial.Value:X}";
if (o is Type type) return type.Name;
}
if (o is Mobile mobile)
{
return $"(M) 0x{mobile.Serial.Value:X} \"{mobile.Name}\"";
}
if (o is Item item)
{
return $"(I) 0x{item.Serial.Value:X}";
}
if (o is Type type)
{
return type.Name;
}
return o.ToString();
}
@ -702,9 +859,14 @@ namespace Server.Gumps
public int Compare(PropertyInfo x, PropertyInfo y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null)
{
return -1;
}
return y == null ? 1 : x.Name.CompareTo(x.Name);
}
@ -726,7 +888,9 @@ namespace Server.Gumps
int dist;
for (dist = 0; current != null && current != typeofObject && current != type; ++dist)
{
current = current.BaseType;
}
return dist;
}