Type Abbreviation utility.

Just to neaten up VV a little.
This commit is contained in:
Pieter-Jan Briers
2020-01-05 01:51:11 +01:00
parent ba390ba466
commit 131a1ee3bd
6 changed files with 193 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ using Robust.Client.ViewVariables.Editors;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Robust.Client.ViewVariables.Instances
@@ -123,7 +124,7 @@ namespace Robust.Client.ViewVariables.Instances
var componentList = _entity.GetAllComponents().OrderBy(c => c.GetType().ToString());
foreach (var component in componentList)
{
var button = new Button {Text = component.GetType().ToString(), TextAlign = Button.AlignMode.Left};
var button = new Button {Text = TypeAbbreviation.Abbreviate(component.GetType().ToString()), TextAlign = Button.AlignMode.Left};
button.OnPressed += args => { ViewVariablesManager.OpenVV(component); };
clientComponents.AddChild(button);
}

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Robust.Server.ViewVariables.Traits
@@ -24,7 +24,7 @@ namespace Robust.Server.ViewVariables.Traits
{
var type = component.GetType();
list.Add(new ViewVariablesBlobEntityComponents.Entry
{Stringified = type.ToString(), Qualified = type.AssemblyQualifiedName});
{Stringified = TypeAbbreviation.Abbreviate(type.ToString()), Qualified = type.AssemblyQualifiedName});
}
return new ViewVariablesBlobEntityComponents

View File

@@ -33,5 +33,10 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Utility\TypeAbbreviations.yaml">
<LogicalName>Robust.Shared.Utility.TypeAbbreviations.yaml</LogicalName>
</EmbeddedResource>
</ItemGroup>
<Import Project="..\MSBuild\Robust.Engine.targets" />
</Project>

View File

@@ -0,0 +1,104 @@
using System;
using System.IO;
using System.Text;
using YamlDotNet.RepresentationModel;
namespace Robust.Shared.Utility
{
public static class TypeAbbreviation
{
private static readonly Abbreviation[] _abbreviations;
static TypeAbbreviation()
{
using var stream =
typeof(TypeAbbreviation).Assembly.GetManifestResourceStream(
"Robust.Shared.Utility.TypeAbbreviations.yaml");
DebugTools.AssertNotNull(stream);
using var streamReader = new StreamReader(stream, EncodingHelpers.UTF8);
var yamlStream = new YamlStream();
yamlStream.Load(streamReader);
var document = yamlStream.Documents[0];
_abbreviations = ParseAbbreviations((YamlSequenceNode) document.RootNode);
}
public static string Abbreviate(ReadOnlySpan<char> name)
{
var sb = new StringBuilder();
Abbreviate(name, _abbreviations, sb);
return sb.ToString();
}
private static void Abbreviate(ReadOnlySpan<char> name, Abbreviation[] abbreviations, StringBuilder output)
{
foreach (var abbr in abbreviations)
{
if (!name.StartsWith(abbr.Long))
{
continue;
}
output.Append(abbr.Short);
name = name[abbr.Long.Length..];
if (abbr.SubAbbreviations.Length != 0)
{
Abbreviate(name, abbr.SubAbbreviations, output);
// Return so nested call can handle appending final name.
return;
}
// Break to append rest of name.
break;
}
output.Append(name);
}
private static Abbreviation[] ParseAbbreviations(YamlSequenceNode sequence)
{
var array = new Abbreviation[sequence.Children.Count];
for (var i = 0; i < array.Length; i++)
{
var subNode = (YamlMappingNode)sequence[i];
// TODO: Maybe allow opting out of those mandatory periods at the end.
var longName = subNode.GetNode("long").AsString() + ".";
var shortName = subNode.GetNode("short").AsString() + ".";
var sub = Array.Empty<Abbreviation>();
if (subNode.TryGetNode("sub", out YamlSequenceNode node))
{
sub = ParseAbbreviations(node);
}
var abbr = new Abbreviation
{
Long = longName,
Short = shortName,
SubAbbreviations = sub
};
array[i] = abbr;
}
return array;
}
private struct Abbreviation
{
public string Long { get; set; }
public string Short { get; set; }
public Abbreviation[] SubAbbreviations { get; set; }
}
}
}

View File

@@ -0,0 +1,52 @@
- long: "System"
short: "S"
sub:
- long: "Collections"
short: "C"
sub:
- long: "Generic"
short: "G"
- long: "Robust"
short: "R"
sub:
- long: "Shared"
short: "Sh"
sub:
- long: "Maths"
short: "M"
- &GO
long: "GameObjects"
short: "GO"
sub:
- long: "Components"
short: "C"
- long: "Server"
short: "Se"
sub:
- *GO
- long: "Client"
short: "C"
sub:
- *GO
- long: "Content"
short: "C"
sub:
- long: "Shared"
short: "Sh"
sub:
- *GO
- long: "Server"
short: "Se"
sub:
- *GO
- long: "Client"
short: "C"
sub:
- *GO

View File

@@ -0,0 +1,28 @@
using System.Collections.Generic;
using NUnit.Framework;
using Robust.Shared.Utility;
namespace Robust.UnitTesting.Shared.Utility
{
[TestFixture]
[Parallelizable(ParallelScope.Fixtures | ParallelScope.All)]
[TestOf(typeof(TypeAbbreviation))]
public class TypeAbbreviations_Test
{
private static IEnumerable<(string name, string expected)> TestCases { get; } = new[]
{
("Robust.Shared.GameObjects.Foo", "R.Sh.GO.Foo"),
("Robust.Client.GameObjects.Foo", "R.C.GO.Foo"),
("Content.Client.GameObjects.Foo", "C.C.GO.Foo"),
("Robust.Shared.Maths.Vector2", "R.Sh.M.Vector2"),
("System.Collections.Generic.List", "S.C.G.List"),
("System.Math", "S.Math"),
};
[Test]
public void Test([ValueSource(nameof(TestCases))] (string name, string expected) data)
{
Assert.That(TypeAbbreviation.Abbreviate(data.name), Is.EqualTo(data.expected));
}
}
}