fix: Removes BodyValue and fixes modifying Body and BodyMod in props gump. (#743)

* Removes BodyValue
* Removes BodyPicker gump
* Fixes setting Body and BodyMod in props gump
* Props will now look for an `implicit cast` in order to find a way to allow you to modify values with the props gump.
* Fixed `[self set body 0x190` so it works too.
This commit is contained in:
Kamron Batman 2021-08-28 22:56:00 -07:00 • committed by GitHub
parent 244fc6a136
commit bf20800382
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 175 additions and 1477 deletions

View file

@ -471,13 +471,32 @@ namespace Server.Gumps
private static bool HasAttribute(Type type, Type check, bool inherit) =>
type.GetCustomAttributes(check, inherit).Length > 0;
private static bool HasImplicitCastTo(Type type, Type check)
{
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
{
if (method.Name != "op_Implicit" || !IsType(method.ReturnType, check))
{
continue;
}
var parameters = method.GetParameters();
if (parameters.Length == 1 && IsType(parameters[0].ParameterType, type))
{
return true;
}
}
return false;
}
private static bool IsType(Type type, Type check) => type == check || type.IsSubclassOf(check);
private static bool IsType(Type type, Type[] check)
{
for (var i = 0; i < check.Length; ++i)
{
if (IsType(type, check[i]))
if (IsType(type, check[i]) || HasImplicitCastTo(type, check[i]))
{
return true;
}

View file

@ -1,395 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Server.Commands;
using Server.Network;
namespace Server.Gumps
{
public enum ModelBodyType
{
Invalid = -1,
Monsters,
Sea,
Animals,
Human,
Equipment
}
public class SetBodyGump : Gump
{
private const int LabelColor32 = 0xFFFFFF;
private const int SelectedColor32 = 0x8080FF;
private const int TextColor32 = 0xFFFFFF;
private static List<InternalEntry> m_Monster, m_Animal, m_Sea, m_Human;
private readonly Mobile m_Mobile;
private readonly object m_Object;
private readonly List<InternalEntry> m_OurList;
private readonly int m_OurPage;
private readonly ModelBodyType m_OurType;
private readonly PropertyInfo m_Property;
private readonly PropertiesGump m_PropertiesGump;
public SetBodyGump(
PropertyInfo prop, Mobile mobile, object o, PropertiesGump propertiesGump,
int ourPage = 0, List<InternalEntry> ourList = null, ModelBodyType ourType = ModelBodyType.Invalid
)
: base(20, 30)
{
m_PropertiesGump = propertiesGump;
m_Property = prop;
m_Mobile = mobile;
m_Object = o;
m_OurPage = ourPage;
m_OurList = ourList;
m_OurType = ourType;
AddPage(0);
AddBackground(0, 0, 525, 328, 5054);
AddImageTiled(10, 10, 505, 20, 0xA40);
AddAlphaRegion(10, 10, 505, 20);
AddImageTiled(10, 35, 505, 283, 0xA40);
AddAlphaRegion(10, 35, 505, 283);
AddTypeButton(10, 10, 1, "Monster", ModelBodyType.Monsters);
AddTypeButton(130, 10, 2, "Animal", ModelBodyType.Animals);
AddTypeButton(250, 10, 3, "Marine", ModelBodyType.Sea);
AddTypeButton(370, 10, 4, "Human", ModelBodyType.Human);
AddImage(480, 12, 0x25EA);
AddImage(497, 12, 0x25E6);
if (ourList == null)
{
AddLabel(15, 40, 0x480, "Choose a body type above.");
}
else if (ourList.Count == 0)
{
AddLabel(15, 40, 0x480, "The server must have UO:3D installed to use this feature.");
}
else
{
for (int i = 0, index = ourPage * 12; i < 12 && index >= 0 && index < ourList.Count; ++i, ++index)
{
var entry = ourList[index];
var itemID = entry.ItemID;
var bounds = ItemBounds.Table[itemID & 0x3FFF];
var x = 15 + i % 4 * 125;
var y = 40 + i / 4 * 93;
AddItem(x + (120 - bounds.Width) / 2 - bounds.X, y + (69 - bounds.Height) / 2 - bounds.Y, itemID);
AddButton(x + 6, y + 66, 0x98D, 0x98D, 7 + index);
x += 6;
y += 67;
AddHtml(x + 0, y - 1, 108, 21, Center(entry.DisplayName));
AddHtml(x + 0, y + 1, 108, 21, Center(entry.DisplayName));
AddHtml(x - 1, y + 0, 108, 21, Center(entry.DisplayName));
AddHtml(x + 1, y + 0, 108, 21, Center(entry.DisplayName));
AddHtml(x + 0, y + 0, 108, 21, Color(Center(entry.DisplayName), TextColor32));
}
if (ourPage > 0)
{
AddButton(480, 12, 0x15E3, 0x15E7, 5);
}
if ((ourPage + 1) * 12 < ourList.Count)
{
AddButton(497, 12, 0x15E1, 0x15E5, 6);
}
}
}
public string Center(string text) => $"<CENTER>{text}</CENTER>";
public string Color(string text, int color) => $"<BASEFONT COLOR=#{color:X6}>{text}</BASEFONT>";
public void AddTypeButton(int x, int y, int buttonID, string text, ModelBodyType type)
{
var isSelection = m_OurType == type;
AddButton(x, y - 1, isSelection ? 4006 : 4005, 4007, buttonID);
AddHtml(x + 35, y, 200, 20, Color(text, isSelection ? SelectedColor32 : LabelColor32));
}
public override void OnResponse(NetState sender, RelayInfo info)
{
var index = info.ButtonID - 1;
if (index == -1)
{
m_PropertiesGump.SendPropertiesGump();
}
else if (index >= 0 && index < 4)
{
if (m_Monster == null)
{
LoadLists();
}
ModelBodyType type;
List<InternalEntry> list;
switch (index)
{
default:
type = ModelBodyType.Monsters;
list = m_Monster;
break;
case 1:
type = ModelBodyType.Animals;
list = m_Animal;
break;
case 2:
type = ModelBodyType.Sea;
list = m_Sea;
break;
case 3:
type = ModelBodyType.Human;
list = m_Human;
break;
}
m_Mobile.SendGump(new SetBodyGump(m_Property, m_Mobile, m_Object, m_PropertiesGump, 0, list, type));
}
else if (m_OurList != null)
{
index -= 4;
if (index == 0 && m_OurPage > 0)
{
m_Mobile.SendGump(
new SetBodyGump(
m_Property,
m_Mobile,
m_Object,
m_PropertiesGump,
m_OurPage - 1,
m_OurList,
m_OurType
)
);
}
else if (index == 1 && (m_OurPage + 1) * 12 < m_OurList.Count)
{
m_Mobile.SendGump(
new SetBodyGump(
m_Property,
m_Mobile,
m_Object,
m_PropertiesGump,
m_OurPage + 1,
m_OurList,
m_OurType
)
);
}
else
{
index -= 2;
if (index >= 0 && index < m_OurList.Count)
{
try
{
var entry = m_OurList[index];
CommandLogging.LogChangeProperty(m_Mobile, m_Object, m_Property.Name, entry.Body.ToString());
m_Property.SetValue(m_Object, entry.Body, null);
m_PropertiesGump.OnValueChanged(m_Object, m_Property);
}
catch
{
m_Mobile.SendMessage("An exception was caught. The property may not have changed.");
}
m_Mobile.SendGump(
new SetBodyGump(
m_Property,
m_Mobile,
m_Object,
m_PropertiesGump,
m_OurPage,
m_OurList,
m_OurType
)
);
}
}
}
}
public static List<BodyEntry> LoadBodies()
{
var list = new List<BodyEntry>();
var path = Path.Combine(Core.BaseDirectory, "Data/models.txt");
if (File.Exists(path))
{
using var ip = new StreamReader(path);
string line;
while ((line = ip.ReadLine()) != null)
{
line = line.Trim();
if (line.Length == 0 || line.StartsWithOrdinal("#"))
{
continue;
}
var split = line.Split('\t');
if (split.Length >= 9)
{
Body body = Utility.ToInt32(split[0]);
var type = (ModelBodyType)Utility.ToInt32(split[1]);
var name = split[8];
var entry = new BodyEntry(body, type, name);
if (!list.Contains(entry))
{
list.Add(entry);
}
}
}
}
return list;
}
private static void LoadLists()
{
m_Monster = new List<InternalEntry>();
m_Animal = new List<InternalEntry>();
m_Sea = new List<InternalEntry>();
m_Human = new List<InternalEntry>();
var entries = LoadBodies();
for (var i = 0; i < entries.Count; ++i)
{
var oldEntry = entries[i];
var bodyID = oldEntry.Body.BodyID;
if (((Body)bodyID).IsEmpty)
{
continue;
}
List<InternalEntry> list;
switch (oldEntry.BodyType)
{
default: continue;
case ModelBodyType.Monsters:
list = m_Monster;
break;
case ModelBodyType.Animals:
list = m_Animal;
break;
case ModelBodyType.Sea:
list = m_Sea;
break;
case ModelBodyType.Human:
list = m_Human;
break;
}
var itemID = ShrinkTable.Lookup(bodyID, -1);
if (itemID != -1)
{
list.Add(new InternalEntry(bodyID, itemID, oldEntry.Name));
}
}
m_Monster.Sort();
m_Animal.Sort();
m_Sea.Sort();
m_Human.Sort();
}
public class InternalEntry : IComparable<InternalEntry>
{
private static readonly string[] m_GroupNames =
{
"ogres_", "ettins_", "walking_dead_", "gargoyles_",
"orcs_", "flails_", "daemons_", "arachnids_",
"dragons_", "elementals_", "serpents_", "gazers_",
"liche_", "spirits_", "harpies_", "headless_",
"lizard_race_", "mongbat_", "rat_race_", "scorpions_",
"trolls_", "slimes_", "skeletons_", "ethereals_",
"terathan_", "imps_", "cyclops_", "krakens_",
"frogs_", "ophidians_", "centaurs_", "mages_",
"fey_race_", "genies_", "paladins_", "shadowlords_",
"succubi_", "lizards_", "rodents_", "birds_",
"bovines_", "bruins_", "canines_", "deer_",
"equines_", "felines_", "fowl_", "gorillas_",
"kirin_", "llamas_", "ostards_", "porcines_",
"ruminants_", "walrus_", "dolphins_", "sea_horse_",
"sea_serpents_", "character_", "h_", "titans_"
};
public InternalEntry(int body, int itemID, string name)
{
Body = body;
ItemID = itemID;
Name = name;
DisplayName = name.ToLower();
for (var i = 0; i < m_GroupNames.Length; ++i)
{
if (DisplayName.StartsWithOrdinal(m_GroupNames[i]))
{
DisplayName = DisplayName[m_GroupNames[i].Length..];
break;
}
}
DisplayName = DisplayName.Replace('_', ' ');
}
public int Body { get; }
public int ItemID { get; }
public string Name { get; }
public string DisplayName { get; }
public int CompareTo(InternalEntry comp)
{
var v = string.CompareOrdinal(Name, comp.Name);
return v == 0 ? Body.CompareTo(comp.Body) : v;
}
}
public record BodyEntry
{
public BodyEntry(Body body, ModelBodyType bodyType, string name)
{
Body = body;
BodyType = bodyType;
Name = name;
}
public Body Body { get; }
public ModelBodyType BodyType { get; }
public string Name { get; }
}
}
}

View file

@ -19,7 +19,7 @@ namespace Server.Gumps
private readonly Mobile m_Mobile;
private readonly object m_Object;
private readonly PropertyInfo m_Property;
private PropertiesGump m_PropertiesGump;
private readonly PropertiesGump m_PropertiesGump;
public SetGump(PropertyInfo prop, Mobile from, object o, PropertiesGump propertiesGump) : base(GumpOffsetX, GumpOffsetY)
{
@ -30,7 +30,6 @@ namespace Server.Gumps
var canNull = !prop.PropertyType.IsValueType;
var canDye = prop.IsDefined(typeof(HueAttribute), false);
var isBody = prop.IsDefined(typeof(BodyAttribute), false);
var val = prop.GetValue(m_Object, null);
var initialText = val switch
@ -46,16 +45,14 @@ namespace Server.Gumps
0,
0,
BackWidth,
BackHeight + (canNull ? EntryHeight + OffsetSize : 0) + (canDye ? EntryHeight + OffsetSize : 0) +
(isBody ? EntryHeight + OffsetSize : 0),
BackHeight + (canNull ? EntryHeight + OffsetSize : 0) + (canDye ? EntryHeight + OffsetSize : 0),
BackGumpID
);
AddImageTiled(
BorderSize,
BorderSize,
TotalWidth - (OldStyle ? SetWidth + OffsetSize : 0),
TotalHeight + (canNull ? EntryHeight + OffsetSize : 0) + (canDye ? EntryHeight + OffsetSize : 0) +
(isBody ? EntryHeight + OffsetSize : 0),
TotalHeight + (canNull ? EntryHeight + OffsetSize : 0) + (canDye ? EntryHeight + OffsetSize : 0),
OffsetGumpID
);
@ -118,23 +115,6 @@ namespace Server.Gumps
AddButton(x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, 3);
}
if (isBody)
{
x = BorderSize + OffsetSize;
y += EntryHeight + OffsetSize;
AddImageTiled(x, y, EntryWidth, EntryHeight, EntryGumpID);
AddLabelCropped(x + TextOffsetX, y, EntryWidth - TextOffsetX, EntryHeight, TextHue, "Body Picker");
x += EntryWidth + OffsetSize;
if (SetGumpID != 0)
{
AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID);
}
AddButton(x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, 4);
}
}
@ -186,16 +166,6 @@ namespace Server.Gumps
m_Mobile.SendHuePicker(new InternalPicker(m_Property, m_Mobile, m_Object, m_PropertiesGump));
break;
}
case 4: // Body Picker
{
toSet = null;
shouldSet = false;
shouldSend = false;
m_Mobile.SendGump(new SetBodyGump(m_Property, m_Mobile, m_Object, m_PropertiesGump));
break;
}
default: