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,15 +1,18 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text.Json.Serialization;
using Server.Json;
namespace Server
{
public class NameList
{
public string Type { get; }
[JsonPropertyName("type")]
public string Type { get; set; }
public string[] List { get; }
[JsonPropertyName("names")]
public string[] List { get; set; }
public bool ContainsName(string name)
{
@ -20,22 +23,7 @@ namespace Server
return false;
}
public NameList(string type, XmlElement xml)
{
Type = type;
List = xml.InnerText.Split(',');
for (int i = 0; i < List.Length; ++i)
List[i] = Utility.Intern(List[i].Trim());
}
public string GetRandomName()
{
if (List.Length > 0)
return List[Utility.Random(List.Length)];
return "";
}
public string GetRandomName() => List.Length > 0 ? List[Utility.Random(List.Length)] : "";
public static NameList GetNameList(string type)
{
@ -45,53 +33,25 @@ namespace Server
public static string RandomName(string type) => GetNameList(type)?.GetRandomName() ?? "";
private static readonly Dictionary<string, NameList> m_Table;
private static readonly Dictionary<string, NameList> m_Table = new Dictionary<string, NameList>(StringComparer.OrdinalIgnoreCase);
static NameList()
public static void Configure()
{
m_Table = new Dictionary<string, NameList>(StringComparer.OrdinalIgnoreCase);
// TODO: Turn this into a command so it can be updated in-game
string filePath = Path.Combine(Core.BaseDirectory, "Data/names.json");
string filePath = Path.Combine(Core.BaseDirectory, "Data/names.xml");
if (!File.Exists(filePath))
return;
try
List<NameList> nameLists = JsonConfig.Deserialize<List<NameList>>(filePath);
foreach (var nameList in nameLists)
{
Load(filePath);
}
catch (Exception e)
{
Console.WriteLine("Warning: Exception caught loading name lists:");
Console.WriteLine(e);
nameList.FixNames();
m_Table.Add(nameList.Type, nameList);
}
}
private static void Load(string filePath)
private void FixNames()
{
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlElement root = doc["names"];
foreach (XmlElement element in root.GetElementsByTagName("namelist"))
{
string type = element.GetAttribute("type");
if (string.IsNullOrEmpty(type))
continue;
try
{
NameList list = new NameList(type, element);
m_Table[type] = list;
}
catch
{
// ignored
}
}
for (int i = 0; i < List.Length; i++)
List[i] = Utility.Intern(List[i].Trim());
}
}
}

View file

@ -4,7 +4,6 @@ using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using Microsoft.AspNetCore.Connections;
using Server.Network;
namespace Server.Misc
@ -70,16 +69,15 @@ namespace Server.Misc
try
{
NetState ns = e.State;
ConnectionContext s = ns.Connection;
IPEndPoint ipep = (IPEndPoint)s.LocalEndPoint;
IPEndPoint ipep = (IPEndPoint)ns.Connection.LocalEndPoint;
IPAddress localAddress = ipep.Address;
int localPort = ipep.Port;
if (IsPrivateNetwork(localAddress))
{
ipep = (IPEndPoint)s.RemoteEndPoint;
ipep = (IPEndPoint)ns.Connection.RemoteEndPoint;
if (!IsPrivateNetwork(ipep.Address) && m_PublicAddress != null)
localAddress = m_PublicAddress;
}