Use YamlDotNet to load patrons JSON file.

So that i don't have to add Newtonsoft.Json stuff to the sandbox whitelist.

YAML is a JSON superset after all...
This commit is contained in:
Pieter-Jan Briers
2020-11-24 00:14:23 +01:00
parent 82ecdea509
commit fcb5787d33

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Content.Client.UserInterface.Stylesheets; using Content.Client.UserInterface.Stylesheets;
using Newtonsoft.Json;
using Robust.Client.Credits; using Robust.Client.Credits;
using Robust.Client.Interfaces.ResourceManagement; using Robust.Client.Interfaces.ResourceManagement;
using Robust.Client.UserInterface; using Robust.Client.UserInterface;
@@ -13,6 +12,7 @@ using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Maths; using Robust.Shared.Maths;
using Robust.Shared.Utility; using Robust.Shared.Utility;
using YamlDotNet.RepresentationModel;
namespace Content.Client.UserInterface namespace Content.Client.UserInterface
{ {
@@ -82,7 +82,7 @@ namespace Content.Client.UserInterface
var margin = new MarginContainer {MarginLeftOverride = 2, MarginTopOverride = 2}; var margin = new MarginContainer {MarginLeftOverride = 2, MarginTopOverride = 2};
var vBox = new VBoxContainer(); var vBox = new VBoxContainer();
margin.AddChild(vBox); margin.AddChild(vBox);
var patrons = ReadJson<PatronEntry[]>("/Credits/Patrons.json"); var patrons = LoadPatrons();
Button patronButton; Button patronButton;
vBox.AddChild(patronButton = new Button vBox.AddChild(patronButton = new Button
@@ -117,6 +117,16 @@ namespace Content.Client.UserInterface
patronsList.AddChild(margin); patronsList.AddChild(margin);
} }
private IEnumerable<PatronEntry> LoadPatrons()
{
var yamlStream = _resourceManager.ContentFileReadYaml(new ResourcePath("/Credits/Patrons.json"));
var sequence = (YamlSequenceNode) yamlStream.Documents[0].RootNode;
return sequence
.Cast<YamlMappingNode>()
.Select(m => new PatronEntry(m["Name"].AsString(), m["Tier"].AsString()));
}
private void PopulateCredits(Control contributorsList) private void PopulateCredits(Control contributorsList)
{ {
Button contributeButton; Button contributeButton;
@@ -141,6 +151,7 @@ namespace Content.Client.UserInterface
}); });
var first = true; var first = true;
void AddSection(string title, string path, bool markup = false) void AddSection(string title, string path, bool markup = false)
{ {
if (!first) if (!first)
@@ -190,29 +201,16 @@ namespace Content.Client.UserInterface
} }
} }
private T ReadJson<T>(string path)
{
var serializer = new JsonSerializer();
using var stream = _resourceManager.ContentFileRead(path);
using var streamReader = new StreamReader(stream);
using var jsonTextReader = new JsonTextReader(streamReader);
return serializer.Deserialize<T>(jsonTextReader)!;
}
[JsonObject(ItemRequired = Required.Always)]
private sealed class PatronEntry private sealed class PatronEntry
{ {
public string Name { get; set; } = default!; public string Name { get; }
public string Tier { get; set; } = default!; public string Tier { get; }
}
[JsonObject(ItemRequired = Required.Always)] public PatronEntry(string name, string tier)
private sealed class OpenSourceLicense {
{ Name = name;
public string Name { get; set; } = default!; Tier = tier;
public string License { get; set; } = default!; }
} }
} }
} }