From 64966e5943f96a8bf1cbfc87f626ee9c9f81c273 Mon Sep 17 00:00:00 2001 From: AnakinRaW Date: Sat, 22 Jun 2024 14:18:59 +0200 Subject: [PATCH] reorganize xml parsing --- .../Xml/IPetroglyphXmlFileParserFactory.cs | 5 +-- .../Xml/ParserNotFoundException.cs | 18 +++++++++ .../Xml/Parsers/Data/GameObjectParser.cs | 40 ++++++++++++------- .../Xml/Parsers/Data/SfxEventParser.cs | 19 ++++----- .../Xml/Parsers/XmlObjectParser.cs | 27 +++++++------ .../Xml/PetroglyphXmlParserFactory.cs | 4 +- .../Parsers/IPetroglyphXmlElementParser.cs | 17 ++------ .../Parsers/IPetroglyphXmlFileParser.cs | 4 +- .../Parsers/IPetroglyphXmlParser.cs | 13 ++++++ .../Parsers/PetroglyphXmlElementParser.cs | 8 ++++ .../Parsers/PetroglyphXmlFileParser.cs | 8 +--- .../Parsers/PetroglyphXmlParser.cs | 7 +--- .../PetroglyphXmlPrimitiveElementParser.cs | 3 ++ .../Parsers/PetroglyphXmlPrimitiveParser.cs | 14 ------- .../CommaSeparatedStringKeyValueListParser.cs | 2 +- .../Primitives/PetroglyphXmlStringParser.cs | 2 +- 16 files changed, 101 insertions(+), 90 deletions(-) create mode 100644 src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/ParserNotFoundException.cs create mode 100644 src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlParser.cs create mode 100644 src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlPrimitiveElementParser.cs delete mode 100644 src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlPrimitiveParser.cs diff --git a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/IPetroglyphXmlFileParserFactory.cs b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/IPetroglyphXmlFileParserFactory.cs index ac389ae..e70c889 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/IPetroglyphXmlFileParserFactory.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/IPetroglyphXmlFileParserFactory.cs @@ -1,11 +1,8 @@ -using System; -using PG.StarWarsGame.Files.XML.Parsers; +using PG.StarWarsGame.Files.XML.Parsers; namespace PG.StarWarsGame.Engine.Xml; public interface IPetroglyphXmlFileParserFactory { IPetroglyphXmlFileParser GetFileParser(); - - IPetroglyphXmlFileParser GetFileParser(Type type); } \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/ParserNotFoundException.cs b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/ParserNotFoundException.cs new file mode 100644 index 0000000..19bc797 --- /dev/null +++ b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/ParserNotFoundException.cs @@ -0,0 +1,18 @@ +using System; + +namespace PG.StarWarsGame.Engine.Xml; + +public sealed class ParserNotFoundException : Exception +{ + public override string Message { get; } + + public ParserNotFoundException(Type type) + { + Message = $"The parser for the type {type} was not found."; + } + + public ParserNotFoundException(string tag) + { + Message = $"The parser for the tag {tag} was not found."; + } +} \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/Data/GameObjectParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/Data/GameObjectParser.cs index f8325b8..bd93f11 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/Data/GameObjectParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/Data/GameObjectParser.cs @@ -1,31 +1,44 @@ using System; using System.Xml.Linq; -using Microsoft.Extensions.DependencyInjection; using PG.Commons.Hashing; using PG.StarWarsGame.Engine.DataTypes; using PG.StarWarsGame.Files.XML; +using PG.StarWarsGame.Files.XML.Parsers; +using PG.StarWarsGame.Files.XML.Parsers.Primitives; namespace PG.StarWarsGame.Engine.Xml.Parsers.Data; public sealed class GameObjectParser(IServiceProvider serviceProvider) : XmlObjectParser(serviceProvider) { - private readonly ICrc32HashingService _crc32Hashing = serviceProvider.GetRequiredService(); - - protected override bool IsTagSupported(string tag) - { - throw new NotImplementedException(); - } - - public override GameObject Parse(XElement element) + protected override IPetroglyphXmlElementParser? GetParser(string tag) { - throw new NotSupportedException(); + switch (tag) + { + case "Land_Terrain_Model_Mapping": + return CommaSeparatedStringKeyValueListParser.Instance; + case "Galactic_Model_Name": + case "Destroyed_Galactic_Model_Name": + case "Land_Model_Name": + case "Space_Model_Name": + case "Model_Name": + case "Tactical_Model_Name": + case "Galactic_Fleet_Override_Model_Name": + case "GUI_Model_Name": + case "GUI_Model": + case "Land_Model_Anim_Override_Name": + case "xxxSpace_Model_Name": + case "Damaged_Smoke_Asset_Name": + return PetroglyphXmlStringParser.Instance; + default: + return null; + } } public override GameObject Parse(XElement element, IReadOnlyValueListDictionary parsedElements, out Crc32 nameCrc) { var properties = ToKeyValuePairList(element); var name = GetNameAttributeValue(element); - nameCrc = _crc32Hashing.GetCrc32Upper(name.AsSpan(), PGConstants.PGCrc32Encoding); + nameCrc = HashingService.GetCrc32Upper(name.AsSpan(), PGConstants.PGCrc32Encoding); var type = GetTagName(element); var objectType = EstimateType(type); var gameObject = new GameObject(type, name, nameCrc, objectType, properties, XmlLocationInfo.FromElement(element)); @@ -83,8 +96,5 @@ private static GameObjectType EstimateType(string tagName) }; } - public string GetTagName(XElement element) - { - return element.Name.LocalName; - } + public override GameObject Parse(XElement element) => throw new NotSupportedException(); } \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/Data/SfxEventParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/Data/SfxEventParser.cs index 46a85c7..ad00da6 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/Data/SfxEventParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/Data/SfxEventParser.cs @@ -1,18 +1,18 @@ using System; using System.Xml.Linq; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using PG.Commons.Hashing; using PG.StarWarsGame.Engine.DataTypes; using PG.StarWarsGame.Files.XML; +using PG.StarWarsGame.Files.XML.Parsers; namespace PG.StarWarsGame.Engine.Xml.Parsers.Data; public sealed class SfxEventParser(IServiceProvider serviceProvider) : XmlObjectParser(serviceProvider) { - private readonly ICrc32HashingService _hashingService = serviceProvider.GetRequiredService(); - - private readonly ILogger? _logger = serviceProvider.GetService()?.CreateLogger(typeof(SfxEventParser)); + protected override IPetroglyphXmlElementParser? GetParser(string tag) + { + return null; + } public override SfxEvent Parse( XElement element, @@ -20,7 +20,7 @@ public override SfxEvent Parse( out Crc32 nameCrc) { var name = GetNameAttributeValue(element); - nameCrc = _hashingService.GetCrc32Upper(name.AsSpan(), PGConstants.PGCrc32Encoding); + nameCrc = HashingService.GetCrc32Upper(name.AsSpan(), PGConstants.PGCrc32Encoding); var valueList = new ValueListDictionary(); @@ -37,7 +37,7 @@ public override SfxEvent Parse( if (tagName.Equals("Use_Preset")) { var presetName = parser.Parse(child) as string; - var presetNameCrc = _hashingService.GetCrc32Upper(presetName.AsSpan(), PGConstants.PGCrc32Encoding); + var presetNameCrc = HashingService.GetCrc32Upper(presetName.AsSpan(), PGConstants.PGCrc32Encoding); if (presetNameCrc == default || !parsedElements.TryGetFirstValue(presetNameCrc, out var preset)) { // Unable to find Preset @@ -59,9 +59,4 @@ public override SfxEvent Parse(XElement element) { throw new NotSupportedException(); } - - protected override bool IsTagSupported(string tag) - { - throw new NotImplementedException(); - } } \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/XmlObjectParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/XmlObjectParser.cs index cd2a202..239eb97 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/XmlObjectParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/XmlObjectParser.cs @@ -1,25 +1,31 @@ using System; using System.Xml.Linq; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using PG.Commons.Hashing; using PG.StarWarsGame.Engine.DataTypes; using PG.StarWarsGame.Files.XML; using PG.StarWarsGame.Files.XML.Parsers; namespace PG.StarWarsGame.Engine.Xml.Parsers; -public abstract class XmlObjectParser(IServiceProvider serviceProvider) : PetroglyphXmlElementParser where T : XmlObject +public abstract class XmlObjectParser : PetroglyphXmlElementParser where T : XmlObject { - protected IServiceProvider ServiceProvider { get; } = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); + protected IServiceProvider ServiceProvider { get; } - protected abstract bool IsTagSupported(string tag); + protected ILogger? Logger { get; } + + protected ICrc32HashingService HashingService { get; } - protected IPetroglyphXmlElementParser? GetParser(string tag) + protected XmlObjectParser(IServiceProvider serviceProvider) { - if (!IsTagSupported(tag)) - return null; - - return null; + ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); + Logger = serviceProvider.GetService()?.CreateLogger(GetType()); + HashingService = serviceProvider.GetRequiredService(); } + protected abstract IPetroglyphXmlElementParser? GetParser(string tag); + protected ValueListDictionary ToKeyValuePairList(XElement element) { var keyValuePairList = new ValueListDictionary(); @@ -37,9 +43,4 @@ protected ValueListDictionary ToKeyValuePairList(XElement elemen } return keyValuePairList; } -} - -internal interface IXmlParserFactory -{ - } \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/PetroglyphXmlParserFactory.cs b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/PetroglyphXmlParserFactory.cs index c780ec1..0341eb6 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/PetroglyphXmlParserFactory.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/PetroglyphXmlParserFactory.cs @@ -15,7 +15,7 @@ public IPetroglyphXmlFileParser GetFileParser() return (IPetroglyphXmlFileParser)GetFileParser(typeof(T)); } - public IPetroglyphXmlFileParser GetFileParser(Type type) + private IPetroglyphXmlFileParser GetFileParser(Type type) { if (type == typeof(XmlFileContainer)) return new XmlFileContainerParser(serviceProvider); @@ -29,6 +29,6 @@ public IPetroglyphXmlFileParser GetFileParser(Type type) if (type == typeof(SfxEvent)) return new SfxEventFileParser(serviceProvider); - throw new NotImplementedException($"The parser for the type {type} is not yet implemented."); + throw new ParserNotFoundException(type); } } \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlElementParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlElementParser.cs index 8fb31c6..e699b80 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlElementParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlElementParser.cs @@ -1,16 +1,5 @@ -using System.Xml.Linq; -using PG.Commons.Hashing; +namespace PG.StarWarsGame.Files.XML.Parsers; -namespace PG.StarWarsGame.Files.XML.Parsers; +public interface IPetroglyphXmlElementParser : IPetroglyphXmlParser; -public interface IPetroglyphXmlElementParser -{ - public object Parse(XElement element); -} - -public interface IPetroglyphXmlElementParser : IPetroglyphXmlElementParser -{ - public new T Parse(XElement element); - - public T Parse(XElement element, IReadOnlyValueListDictionary parsedElements, out Crc32 nameCrc); -} \ No newline at end of file +public interface IPetroglyphXmlElementParser : IPetroglyphXmlElementParser, IPetroglyphXmlParser; \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlFileParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlFileParser.cs index 817a13c..2c81137 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlFileParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlFileParser.cs @@ -3,12 +3,12 @@ namespace PG.StarWarsGame.Files.XML.Parsers; -public interface IPetroglyphXmlFileParser : IPetroglyphXmlElementParser +public interface IPetroglyphXmlFileParser : IPetroglyphXmlParser { public object? ParseFile(Stream stream); } -public interface IPetroglyphXmlFileParser : IPetroglyphXmlElementParser, IPetroglyphXmlFileParser +public interface IPetroglyphXmlFileParser : IPetroglyphXmlParser, IPetroglyphXmlFileParser { public new T ParseFile(Stream stream); diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlParser.cs new file mode 100644 index 0000000..cd8a607 --- /dev/null +++ b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/IPetroglyphXmlParser.cs @@ -0,0 +1,13 @@ +using System.Xml.Linq; + +namespace PG.StarWarsGame.Files.XML.Parsers; + +public interface IPetroglyphXmlParser +{ + public object Parse(XElement element); +} + +public interface IPetroglyphXmlParser : IPetroglyphXmlParser +{ + public new T Parse(XElement element); +} \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlElementParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlElementParser.cs index 14d3ce5..8ab4eee 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlElementParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlElementParser.cs @@ -1,14 +1,22 @@ using System.Linq; using System.Xml.Linq; +using PG.Commons.Hashing; namespace PG.StarWarsGame.Files.XML.Parsers; public abstract class PetroglyphXmlElementParser : PetroglyphXmlParser { + protected string GetTagName(XElement element) + { + return element.Name.LocalName; + } + protected string GetNameAttributeValue(XElement element) { var nameAttribute = element.Attributes() .FirstOrDefault(a => a.Name.LocalName == "Name"); return nameAttribute is null ? string.Empty : nameAttribute.Value; } + + public abstract T Parse(XElement element, IReadOnlyValueListDictionary parsedElements, out Crc32 nameCrc); } \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlFileParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlFileParser.cs index ab44289..828d49e 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlFileParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlFileParser.cs @@ -7,8 +7,7 @@ namespace PG.StarWarsGame.Files.XML.Parsers; -public abstract class PetroglyphXmlFileParser(IServiceProvider serviceProvider) - : PetroglyphXmlParser, IPetroglyphXmlFileParser +public abstract class PetroglyphXmlFileParser(IServiceProvider serviceProvider) : PetroglyphXmlParser, IPetroglyphXmlFileParser { protected IServiceProvider ServiceProvider { get; } = serviceProvider; @@ -27,11 +26,6 @@ public void ParseFile(Stream xmlStream, IValueListDictionary parsedEnt Parse(root, parsedEntries); } - public sealed override T Parse(XElement element, IReadOnlyValueListDictionary parsedElements, out Crc32 nameCrc) - { - throw new NotSupportedException(); - } - protected abstract void Parse(XElement element, IValueListDictionary parsedElements); private XElement? GetRootElement(Stream xmlStream) diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlParser.cs index 2032cfb..c0181dd 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlParser.cs @@ -1,15 +1,12 @@ using System.Xml.Linq; -using PG.Commons.Hashing; namespace PG.StarWarsGame.Files.XML.Parsers; -public abstract class PetroglyphXmlParser : IPetroglyphXmlElementParser +public abstract class PetroglyphXmlParser : IPetroglyphXmlParser { - public abstract T Parse(XElement element, IReadOnlyValueListDictionary parsedElements, out Crc32 nameCrc); - public abstract T Parse(XElement element); - object IPetroglyphXmlElementParser.Parse(XElement element) + object IPetroglyphXmlParser.Parse(XElement element) { return Parse(element); } diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlPrimitiveElementParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlPrimitiveElementParser.cs new file mode 100644 index 0000000..b481d17 --- /dev/null +++ b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlPrimitiveElementParser.cs @@ -0,0 +1,3 @@ +namespace PG.StarWarsGame.Files.XML.Parsers; + +public abstract class PetroglyphXmlPrimitiveElementParser : PetroglyphXmlParser, IPetroglyphXmlElementParser; \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlPrimitiveParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlPrimitiveParser.cs deleted file mode 100644 index 5af70bd..0000000 --- a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/PetroglyphXmlPrimitiveParser.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Xml.Linq; -using PG.Commons.Hashing; - -namespace PG.StarWarsGame.Files.XML.Parsers; - -public abstract class PetroglyphXmlPrimitiveParser : PetroglyphXmlParser -{ - public sealed override T Parse(XElement element, IReadOnlyValueListDictionary parsedElements, - out Crc32 nameCrc) - { - throw new NotSupportedException(); - } -} \ No newline at end of file diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/Primitives/CommaSeparatedStringKeyValueListParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/Primitives/CommaSeparatedStringKeyValueListParser.cs index 307e7b0..a300719 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/Primitives/CommaSeparatedStringKeyValueListParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/Primitives/CommaSeparatedStringKeyValueListParser.cs @@ -6,7 +6,7 @@ namespace PG.StarWarsGame.Files.XML.Parsers.Primitives; // Used e.g, by // Format: Key, Value, Key, Value // There might be arbitrary spaces, tabs and newlines -public sealed class CommaSeparatedStringKeyValueListParser : PetroglyphXmlPrimitiveParser> +public sealed class CommaSeparatedStringKeyValueListParser : PetroglyphXmlPrimitiveElementParser> { public static readonly CommaSeparatedStringKeyValueListParser Instance = new(); diff --git a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/Primitives/PetroglyphXmlStringParser.cs b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/Primitives/PetroglyphXmlStringParser.cs index 90e72c0..b5551ec 100644 --- a/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/Primitives/PetroglyphXmlStringParser.cs +++ b/src/PetroglyphTools/PG.StarWarsGame.Files.XML/Parsers/Primitives/PetroglyphXmlStringParser.cs @@ -2,7 +2,7 @@ namespace PG.StarWarsGame.Files.XML.Parsers.Primitives; -public sealed class PetroglyphXmlStringParser : PetroglyphXmlPrimitiveParser +public sealed class PetroglyphXmlStringParser : PetroglyphXmlPrimitiveElementParser { public static readonly PetroglyphXmlStringParser Instance = new();