Updates Data to JSON (#159)

This commit is contained in:
Kamron Batman 2020-06-19 13:53:18 -07:00 • committed by GitHub
parent d2f7e08de4
commit 1246a135f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
61 changed files with 18005 additions and 9152 deletions

View file

@ -1,38 +0,0 @@
using System.Xml;
namespace Server.Gumps
{
public class ChildNode : IGoNode
{
public ChildNode(XmlTextReader xml, ParentNode parent)
{
Parent = parent;
Parse(xml);
}
public ParentNode Parent { get; }
public string Name { get; private set; }
public Point3D Location { get; private set; }
private void Parse(XmlTextReader xml)
{
Name = xml.MoveToAttribute("name") ? xml.Value : "empty";
int x = 0, y = 0, z = 0;
if (xml.MoveToAttribute("x"))
x = Utility.ToInt32(xml.Value);
if (xml.MoveToAttribute("y"))
y = Utility.ToInt32(xml.Value);
if (xml.MoveToAttribute("z"))
z = Utility.ToInt32(xml.Value);
Location = new Point3D(x, y, z);
}
}
}

View file

@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
namespace Server.Gumps
{
public class GoCategory
{
public GoCategory Parent { get; set; }
[JsonPropertyName("locations")]
public GoLocation[] Locations { get; set; }
[JsonPropertyName("categories")]
public GoCategory[] Categories { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
}
}

View file

@ -4,11 +4,12 @@ namespace Server.Gumps
{
public class GoGump : Gump
{
public static readonly LocationTree Felucca = new LocationTree("felucca.xml", Map.Felucca);
public static readonly LocationTree Trammel = new LocationTree("trammel.xml", Map.Trammel);
public static readonly LocationTree Ilshenar = new LocationTree("ilshenar.xml", Map.Ilshenar);
public static readonly LocationTree Malas = new LocationTree("malas.xml", Map.Malas);
public static readonly LocationTree Tokuno = new LocationTree("tokuno.xml", Map.Tokuno);
private static LocationTree Felucca;
private static LocationTree Trammel;
private static LocationTree Ilshenar;
private static LocationTree Malas;
private static LocationTree Tokuno;
private static LocationTree TerMur;
public static bool OldStyle = PropsConfig.OldStyle;
@ -61,16 +62,43 @@ namespace Server.Gumps
private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;
private static readonly int BackHeight = BorderSize + TotalHeight + BorderSize;
private readonly ParentNode m_Node;
private readonly GoCategory m_Node;
private readonly int m_Page;
private readonly LocationTree m_Tree;
private GoGump(int page, Mobile from, LocationTree tree, ParentNode node) : base(50, 50)
public static void DisplayTo(Mobile from)
{
LocationTree tree;
if (from.Map == Map.Ilshenar)
tree = Ilshenar ??= new LocationTree("ilshenar", Map.Ilshenar);
else if (from.Map == Map.Felucca)
tree = Felucca ??= new LocationTree("felucca", Map.Felucca);
else if (from.Map == Map.Trammel)
tree = Trammel ??= new LocationTree("trammel", Map.Trammel);
else if (from.Map == Map.Malas)
tree = Malas ??= new LocationTree("malas", Map.Malas);
else if (from.Map == Map.Tokuno)
tree = Tokuno ??= new LocationTree("tokuno", Map.Tokuno);
else
tree = TerMur ??= new LocationTree("termur", Map.TerMur);
if (!tree.LastBranch.TryGetValue(from, out GoCategory branch))
branch = tree.Root;
if (branch != null)
from.SendGump(new GoGump(0, from, tree, branch));
}
private GoGump(int page, Mobile from, LocationTree tree, GoCategory node) : base(50, 50)
{
from.CloseGump<GoGump>();
tree.LastBranch[from] = node;
if (node == tree.Root)
tree.LastBranch.Remove(from);
else
tree.LastBranch[from] = node;
m_Page = page;
m_Tree = tree;
@ -79,7 +107,7 @@ namespace Server.Gumps
int x = BorderSize + OffsetSize;
int y = BorderSize + OffsetSize;
int count = node.Children.Length - page * EntryCount;
int count = node.Categories.Length + node.Locations.Length - page * EntryCount;
if (count < 0)
count = 0;
@ -138,7 +166,7 @@ namespace Server.Gumps
if (!OldStyle)
AddImageTiled(x, y, NextWidth, EntryHeight, HeaderGumpID);
if ((page + 1) * EntryCount < node.Children.Length)
if ((page + 1) * EntryCount < node.Categories.Length + node.Locations.Length)
{
AddButton(x + NextOffsetX, y + NextOffsetY, NextButtonID1, NextButtonID2, 3, GumpButtonType.Reply, 1);
@ -146,13 +174,14 @@ namespace Server.Gumps
AddLabel(x + NextLabelOffsetX, y + NextLabelOffsetY, TextHue, "Next");
}
for (int i = 0, index = page * EntryCount; i < EntryCount && index < node.Children.Length; ++i, ++index)
int totalEntryCount = node.Categories.Length + node.Locations.Length;
for (int i = 0, index = page * EntryCount; i < EntryCount && index < totalEntryCount; ++i, ++index)
{
x = BorderSize + OffsetSize;
y += EntryHeight + OffsetSize;
IGoNode child = node.Children[index];
string name = child.Name;
string name = index >= node.Categories.Length ? node.Locations[index].Name : node.Categories[index].Name;
AddImageTiled(x, y, EntryWidth, EntryHeight, EntryGumpID);
AddLabelCropped(x + TextOffsetX, y, EntryWidth - TextOffsetX, EntryHeight, TextHue, name);
@ -166,28 +195,6 @@ namespace Server.Gumps
}
}
public static void DisplayTo(Mobile from)
{
LocationTree tree;
if (from.Map == Map.Ilshenar)
tree = Ilshenar;
else if (from.Map == Map.Felucca)
tree = Felucca;
else if (from.Map == Map.Trammel)
tree = Trammel;
else if (from.Map == Map.Malas)
tree = Malas;
else
tree = Tokuno;
if (!tree.LastBranch.TryGetValue(from, out ParentNode branch))
branch = tree.Root;
if (branch != null)
from.SendGump(new GoGump(0, from, tree, branch));
}
public override void OnResponse(NetState state, RelayInfo info)
{
Mobile from = state.Mobile;
@ -210,7 +217,7 @@ namespace Server.Gumps
}
case 3:
{
if ((m_Page + 1) * EntryCount < m_Node.Children.Length)
if ((m_Page + 1) * EntryCount < m_Node.Categories.Length + m_Node.Locations.Length)
from.SendGump(new GoGump(m_Page + 1, from, m_Tree, m_Node));
break;
@ -219,14 +226,18 @@ namespace Server.Gumps
{
int index = info.ButtonID - 4;
if (index >= 0 && index < m_Node.Children.Length)
{
IGoNode o = m_Node.Children[index];
if (index < 0)
break;
if (o is ParentNode node)
from.SendGump(new GoGump(0, from, m_Tree, node));
else
from.MoveToWorld(((ChildNode)o).Location, m_Tree.Map);
if (index < m_Node.Categories.Length)
{
from.SendGump(new GoGump(0, from, m_Tree, m_Node.Categories[index]));
}
else
{
index -= m_Node.Categories.Length;
if (index < m_Node.Locations.Length)
from.MoveToWorld(m_Node.Locations[index].Location, m_Tree.Map);
}
break;

View file

@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
namespace Server.Gumps
{
public class GoLocation
{
public GoCategory Parent { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("location")]
public Point3D Location { get; set; }
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Server.Json;
namespace Server.Gumps
{
@ -8,34 +9,50 @@ namespace Server.Gumps
{
public LocationTree(string fileName, Map map)
{
LastBranch = new Dictionary<Mobile, ParentNode>();
LastBranch = new Dictionary<Mobile, GoCategory>();
Map = map;
string path = Path.Combine("Data/Locations/", fileName);
string path = Path.Combine($"Data/Locations/{fileName}.json");
if (File.Exists(path))
if (!File.Exists(path))
{
XmlTextReader xml = new XmlTextReader(new StreamReader(path)) { WhitespaceHandling = WhitespaceHandling.None };
Console.WriteLine("Go Locations: {0} does not exist", path);
return;
}
Root = Parse(xml);
xml.Close();
try
{
Root = JsonConfig.Deserialize<GoCategory>(path);
SetParents(Root);
}
catch (Exception e)
{
Console.WriteLine("Go Locations: Error in deserializing {0}", path);
Console.WriteLine(e);
}
}
public Dictionary<Mobile, ParentNode> LastBranch { get; }
public Dictionary<Mobile, GoCategory> LastBranch { get; }
public Map Map { get; }
public ParentNode Root { get; }
public GoCategory Root { get; }
private ParentNode Parse(XmlTextReader xml)
private static void SetParents(GoCategory parent)
{
xml.Read();
xml.Read();
xml.Read();
// Deserialization may leave these null
parent.Categories ??= Array.Empty<GoCategory>();
parent.Locations ??= Array.Empty<GoLocation>();
return new ParentNode(xml, null);
for (int i = 0; i < parent.Categories.Length; i++)
{
GoCategory category = parent.Categories[i];
category.Parent = parent;
SetParents(category);
}
for (int j = 0; j < parent.Locations.Length; j++)
parent.Locations[j].Parent = parent;
}
}
}

View file

@ -1,53 +0,0 @@
using System;
using System.Collections.Generic;
using System.Xml;
namespace Server.Gumps
{
public interface IGoNode
{
ParentNode Parent { get; }
string Name { get; }
}
public class ParentNode : IGoNode
{
public ParentNode(XmlTextReader xml, ParentNode parent)
{
Parent = parent;
Parse(xml);
}
public ParentNode Parent { get; }
public IGoNode[] Children { get; private set; }
public string Name { get; private set; }
private void Parse(XmlTextReader xml)
{
Name = xml.MoveToAttribute("name") ? xml.Value : "empty";
if (xml.IsEmptyElement)
Children = Array.Empty<IGoNode>();
else
{
List<IGoNode> children = new List<IGoNode>();
while (xml.Read() && (xml.NodeType == XmlNodeType.Element || xml.NodeType == XmlNodeType.Comment))
{
if (xml.NodeType == XmlNodeType.Comment)
continue;
if (xml.Name == "child")
children.Add(new ChildNode(xml, this));
else
children.Add(new ParentNode(xml, this));
}
Children = children.ToArray();
}
}
}
}