Adds property sorting for json output (#266)

This commit is contained in:
Kamron Batman 2020-09-26 22:38:50 -07:00 committed by GitHub
parent ddc58ec707
commit 12e935e4f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 228 additions and 130 deletions

View file

@ -138,17 +138,6 @@ namespace Server
Save(); Save();
} }
public static T GetMetadata<T>(string key) where T : class
{
m_Settings.metadata.TryGetValue(key, out var value);
return value as T;
}
public static void SetMetadata(string key, object value)
{
m_Settings.metadata[key] = value;
}
// If mock is enabled we skip the console readline. // If mock is enabled we skip the console readline.
public static void Load(bool mocked = false) public static void Load(bool mocked = false)
{ {
@ -291,14 +280,14 @@ namespace Server
internal class ServerSettings internal class ServerSettings
{ {
[JsonPropertyName("dataDirectories")] public List<string> dataDirectories { get; set; } = new List<string>(); [JsonPropertyName("dataDirectories")]
public List<string> dataDirectories { get; set; } = new List<string>();
[JsonPropertyName("listeners")] public List<IPEndPoint> listeners { get; set; } = new List<IPEndPoint>(); [JsonPropertyName("listeners")]
public List<IPEndPoint> listeners { get; set; } = new List<IPEndPoint>();
[JsonPropertyName("settings")] [JsonPropertyName("settings")]
public Dictionary<string, string> settings { get; set; } = new Dictionary<string, string>(); public SortedDictionary<string, string> settings { get; set; } = new SortedDictionary<string, string>();
[JsonExtensionData] public Dictionary<string, object> metadata { get; set; } = new Dictionary<string, object>();
} }
} }
} }

View file

@ -859,7 +859,7 @@ namespace Server.Items
if (v == 0) if (v == 0)
{ {
@group.Add(b); group.Add(b);
} }
else else
{ {
@ -971,7 +971,7 @@ namespace Server.Items
if (v == 0) if (v == 0)
{ {
@group.Add(b); group.Add(b);
} }
else else
{ {
@ -1087,7 +1087,7 @@ namespace Server.Items
if (v == 0) if (v == 0)
{ {
@group.Add(b); group.Add(b);
} }
else else
{ {
@ -1404,7 +1404,7 @@ namespace Server.Items
if (v == 0) if (v == 0)
{ {
@group.Add(b); group.Add(b);
} }
else else
{ {
@ -1465,7 +1465,7 @@ namespace Server.Items
if (v == 0) if (v == 0)
{ {
@group.Add(b); group.Add(b);
} }
else else
{ {
@ -1522,7 +1522,7 @@ namespace Server.Items
if (v == 0) if (v == 0)
{ {
@group.Add(b); group.Add(b);
} }
else else
{ {

View file

@ -16,6 +16,7 @@
using System; using System;
using System.Buffers; using System.Buffers;
using System.IO; using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
@ -33,7 +34,8 @@ namespace Server.Json
WriteIndented = true, WriteIndented = true,
AllowTrailingCommas = true, AllowTrailingCommas = true,
IgnoreNullValues = true, IgnoreNullValues = true,
ReadCommentHandling = JsonCommentHandling.Skip ReadCommentHandling = JsonCommentHandling.Skip,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
}; };
options.Converters.Add(new MapConverterFactory()); options.Converters.Add(new MapConverterFactory());
@ -64,8 +66,13 @@ namespace Server.Json
return JsonSerializer.Deserialize<T>(text, options ?? DefaultOptions); return JsonSerializer.Deserialize<T>(text, options ?? DefaultOptions);
} }
public static string Serialize(object value, JsonSerializerOptions options = null) =>
JsonSerializer.Serialize(value, options ?? DefaultOptions);
public static void Serialize(string filePath, object value, JsonSerializerOptions options = null) public static void Serialize(string filePath, object value, JsonSerializerOptions options = null)
{ {
var contents = Serialize(value, options);
if (File.Exists(filePath)) if (File.Exists(filePath))
{ {
File.Delete(filePath); File.Delete(filePath);
@ -73,7 +80,7 @@ namespace Server.Json
Directory.CreateDirectory(Path.GetDirectoryName(filePath)); Directory.CreateDirectory(Path.GetDirectoryName(filePath));
File.WriteAllText(filePath, JsonSerializer.Serialize(value, options ?? DefaultOptions)); File.WriteAllText(filePath, contents);
} }
public static T ToObject<T>(this ref Utf8JsonReader reader, JsonSerializerOptions options = null) => public static T ToObject<T>(this ref Utf8JsonReader reader, JsonSerializerOptions options = null) =>

View file

@ -0,0 +1,100 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: JsonPropertySorter.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
namespace Server.Json
{
public static class JsonUtilities
{
public static string SortByPropertyName(string jsonStr)
{
using JsonDocument doc = JsonDocument.Parse(jsonStr);
return SortByPropertyName(doc.RootElement);
}
public static string SortByPropertyName(JsonElement je)
{
// TODO: Better way to do this than a stream?
using var ms = new MemoryStream();
JsonWriterOptions opts = new JsonWriterOptions
{
Indented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
using (var writer = new Utf8JsonWriter(ms, opts))
{
WriteJsonElementSorted(je, writer);
}
ms.TryGetBuffer(out var buffer);
return Encoding.UTF8.GetString(buffer);
}
private static void WriteJsonElementSorted(JsonElement je, Utf8JsonWriter writer)
{
switch(je.ValueKind)
{
case JsonValueKind.Object:
writer.WriteStartObject();
// TODO: This is slow, can make it faster?
foreach (JsonProperty x in je.EnumerateObject().OrderBy(prop => prop.Name))
{
writer.WritePropertyName(x.Name);
WriteJsonElementSorted(x.Value, writer);
}
writer.WriteEndObject();
break;
case JsonValueKind.Array:
writer.WriteStartArray();
foreach(JsonElement x in je.EnumerateArray())
{
WriteJsonElementSorted(x, writer);
}
writer.WriteEndArray();
break;
case JsonValueKind.Number:
writer.WriteNumberValue(je.GetDouble());
break;
case JsonValueKind.String:
// Escape the string
writer.WriteStringValue(je.GetString());
break;
case JsonValueKind.Null:
writer.WriteNullValue();
break;
case JsonValueKind.True:
writer.WriteBooleanValue(true);
break;
case JsonValueKind.False:
writer.WriteBooleanValue(false);
break;
case JsonValueKind.Undefined: // Don't write anything
break;
default:
throw new NotImplementedException($"Kind: {je.ValueKind}");
}
}
}
}

View file

@ -649,8 +649,8 @@ namespace Server
public virtual bool OnTarget(Mobile m, Target t, object o) => Parent?.OnTarget(m, t, o) != false; public virtual bool OnTarget(Mobile m, Target t, object o) => Parent?.OnTarget(m, t, o) != false;
public virtual bool OnCombatantChange(Mobile m, Mobile old, Mobile @new) => public virtual bool OnCombatantChange(Mobile m, Mobile old, Mobile newMobile) =>
Parent?.OnCombatantChange(m, old, @new) != false; Parent?.OnCombatantChange(m, old, newMobile) != false;
public virtual bool AllowHousing(Mobile from, Point3D p) => Parent?.AllowHousing(from, p) != false; public virtual bool AllowHousing(Mobile from, Point3D p) => Parent?.AllowHousing(from, p) != false;

View file

@ -25,16 +25,16 @@ namespace Server.Configurations
{ {
private const string m_RelPath = "Configuration/email-settings.json"; private const string m_RelPath = "Configuration/email-settings.json";
public static bool EmailEnabled { get; private set; }
public static MailboxAddress FromAddress { get; private set; }
public static MailboxAddress CrashAddress { get; private set; } public static MailboxAddress CrashAddress { get; private set; }
public static MailboxAddress SpeechLogPageAddress { get; private set; } public static bool EmailEnabled { get; private set; }
public static string EmailServer { get; private set; }
public static int EmailPort { get; private set; } public static int EmailPort { get; private set; }
public static string EmailServerUsername { get; private set; }
public static string EmailServerPassword { get; private set; }
public static int EmailSendRetryCount { get; private set; } // seconds public static int EmailSendRetryCount { get; private set; } // seconds
public static int EmailSendRetryDelay { get; private set; } // seconds public static int EmailSendRetryDelay { get; private set; } // seconds
public static string EmailServer { get; private set; }
public static string EmailServerUsername { get; private set; }
public static string EmailServerPassword { get; private set; }
public static MailboxAddress FromAddress { get; private set; }
public static MailboxAddress SpeechLogPageAddress { get; private set; }
public static void Configure() public static void Configure()
{ {

View file

@ -173,7 +173,7 @@ namespace Server.Engines.BulkOrders
if (points >= group.Points) if (points >= group.Points)
{ {
return @group; return group;
} }
} }

View file

@ -498,7 +498,7 @@ namespace Server.Engines.Craft
if (index >= 0 && index < group.CraftItems.Count) if (index >= 0 && index < group.CraftItems.Count)
{ {
CraftItem(@group.CraftItems[index]); CraftItem(group.CraftItems[index]);
} }
} }
@ -519,7 +519,7 @@ namespace Server.Engines.Craft
if (index >= 0 && index < group.CraftItems.Count) if (index >= 0 && index < group.CraftItems.Count)
{ {
m_From.SendGump(new CraftGumpItem(m_From, system, @group.CraftItems[index], m_Tool)); m_From.SendGump(new CraftGumpItem(m_From, system, group.CraftItems[index], m_Tool));
} }
} }

View file

@ -133,7 +133,7 @@ namespace Server.Guilds
if (pm == null || !IsMember(pm, guild) || !pm.GuildRank.GetFlag(RankFlags.CanInvitePlayer)) if (pm == null || !IsMember(pm, guild) || !pm.GuildRank.GetFlag(RankFlags.CanInvitePlayer))
{ {
pm.SendLocalizedMessage(503301); // You don't have permission to do that. from.SendLocalizedMessage(503301); // You don't have permission to do that.
} }
else if (targ == null) else if (targ == null)
{ {

View file

@ -179,11 +179,11 @@ namespace Server.Gumps
/* /*
private static bool PrevLabel = OldStyle, NextLabel = OldStyle; private static bool PrevLabel = OldStyle, NextLabel = OldStyle;
private static readonly int PrevLabelOffsetX = PrevWidth + 1; private static readonly int PrevLabelOffsetX = PrevWidth + 1;
private static readonly int PrevLabelOffsetY = 0; private static readonly int PrevLabelOffsetY = 0;
private static readonly int NextLabelOffsetX = -29; private static readonly int NextLabelOffsetX = -29;
private static readonly int NextLabelOffsetY = 0; private static readonly int NextLabelOffsetY = 0;
* */ * */
@ -306,108 +306,110 @@ namespace Server.Gumps
AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID); AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID);
} }
if (group == selected) if (group != selected)
{ {
var indentMaskX = BorderSize; continue;
var indentMaskY = y + EntryHeight + OffsetSize; }
for (var j = 0; j < group.Skills.Length; ++j) var indentMaskX = BorderSize;
{ var indentMaskY = y + EntryHeight + OffsetSize;
var sk = target.Skills[group.Skills[j]];
x = BorderSize + OffsetSize; for (var j = 0; j < group!.Skills.Length; ++j)
y += EntryHeight + OffsetSize; {
var sk = target.Skills[group.Skills[j]];
x += OffsetSize; x = BorderSize + OffsetSize;
x += IndentWidth; y += EntryHeight + OffsetSize;
AddImageTiled(x, y, PrevWidth, EntryHeight, HeaderGumpID); x += OffsetSize;
x += IndentWidth;
AddButton(x + PrevOffsetX, y + PrevOffsetY, 0x15E1, 0x15E5, GetButtonID(1, j)); AddImageTiled(x, y, PrevWidth, EntryHeight, HeaderGumpID);
x += PrevWidth + OffsetSize; AddButton(x + PrevOffsetX, y + PrevOffsetY, 0x15E1, 0x15E5, GetButtonID(1, j));
x -= OldStyle ? OffsetSize : 0; x += PrevWidth + OffsetSize;
AddImageTiled( x -= OldStyle ? OffsetSize : 0;
x,
y,
emptyWidth + (OldStyle ? OffsetSize * 2 : 0) - OffsetSize - IndentWidth,
EntryHeight,
EntryGumpID
);
AddLabel(x + TextOffsetX, y, TextHue, sk == null ? "(null)" : sk.Name);
x += emptyWidth + (OldStyle ? OffsetSize * 2 : 0) - OffsetSize - IndentWidth;
x += OffsetSize;
if (SetGumpID != 0)
{
AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID);
}
if (sk != null)
{
int buttonID1, buttonID2;
int xOffset, yOffset;
switch (sk.Lock)
{
default:
buttonID1 = 0x983;
buttonID2 = 0x983;
xOffset = 6;
yOffset = 4;
break;
case SkillLock.Down:
buttonID1 = 0x985;
buttonID2 = 0x985;
xOffset = 6;
yOffset = 4;
break;
case SkillLock.Locked:
buttonID1 = 0x82C;
buttonID2 = 0x82C;
xOffset = 5;
yOffset = 2;
break;
}
AddButton(x + xOffset, y + yOffset, buttonID1, buttonID2, GetButtonID(2, j));
y += 1;
x -= OffsetSize;
x -= 1;
x -= 50;
AddImageTiled(x, y, 50, EntryHeight - 2, OffsetGumpID);
x += 1;
y += 1;
AddImageTiled(x, y, 48, EntryHeight - 4, EntryGumpID);
AddLabelCropped(
x + TextOffsetX,
y - 1,
48 - TextOffsetX,
EntryHeight - 3,
TextHue,
sk.Base.ToString("F1")
);
y -= 2;
}
}
AddImageTiled( AddImageTiled(
indentMaskX, x,
indentMaskY, y,
IndentWidth + OffsetSize, emptyWidth + (OldStyle ? OffsetSize * 2 : 0) - OffsetSize - IndentWidth,
group.Skills.Length * (EntryHeight + OffsetSize) - (i < m_Groups.Length - 1 ? OffsetSize : 0), EntryHeight,
BackGumpID + 4 EntryGumpID
); );
AddLabel(x + TextOffsetX, y, TextHue, sk == null ? "(null)" : sk.Name);
x += emptyWidth + (OldStyle ? OffsetSize * 2 : 0) - OffsetSize - IndentWidth;
x += OffsetSize;
if (SetGumpID != 0)
{
AddImageTiled(x, y, SetWidth, EntryHeight, SetGumpID);
}
if (sk != null)
{
int buttonID1, buttonID2;
int xOffset, yOffset;
switch (sk.Lock)
{
default:
buttonID1 = 0x983;
buttonID2 = 0x983;
xOffset = 6;
yOffset = 4;
break;
case SkillLock.Down:
buttonID1 = 0x985;
buttonID2 = 0x985;
xOffset = 6;
yOffset = 4;
break;
case SkillLock.Locked:
buttonID1 = 0x82C;
buttonID2 = 0x82C;
xOffset = 5;
yOffset = 2;
break;
}
AddButton(x + xOffset, y + yOffset, buttonID1, buttonID2, GetButtonID(2, j));
y += 1;
x -= OffsetSize;
x -= 1;
x -= 50;
AddImageTiled(x, y, 50, EntryHeight - 2, OffsetGumpID);
x += 1;
y += 1;
AddImageTiled(x, y, 48, EntryHeight - 4, EntryGumpID);
AddLabelCropped(
x + TextOffsetX,
y - 1,
48 - TextOffsetX,
EntryHeight - 3,
TextHue,
sk.Base.ToString("F1")
);
y -= 2;
}
} }
AddImageTiled(
indentMaskX,
indentMaskY,
IndentWidth + OffsetSize,
group.Skills.Length * (EntryHeight + OffsetSize) - (i < m_Groups.Length - 1 ? OffsetSize : 0),
BackGumpID + 4
);
} }
} }

View file

@ -120,7 +120,7 @@ namespace Server
for (var j = 0; !contains && j < group.Length; ++j) for (var j = 0; !contains && j < group.Length; ++j)
{ {
contains = @group[j].IsAssignableFrom(type); contains = group[j].IsAssignableFrom(type);
} }
if (contains) if (contains)

View file

@ -226,7 +226,7 @@ namespace Server.Spells.Necromancy
if (contains) if (contains)
{ {
return @group; return group;
} }
} }