From b0780ca565f57a6acae2e3c0a064f25f8b98d1f7 Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 21 Nov 2019 01:44:04 +0100 Subject: [PATCH] ZoneCodeGenerator: Add a preprocessor extracting a member chain to get the name of the asset in the template --- .../Information/StructureInformation.cs | 5 +- .../Generating/Templates/ZoneLoad.stg | 4 +- .../Parsing/CommandFile/CommandFileReader.cs | 1 + .../PostProcessor/PostProcessorAssetName.cs | 56 +++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/ZoneCodeGenerator/Parsing/CommandFile/PostProcessor/PostProcessorAssetName.cs diff --git a/src/ZoneCodeGenerator/Domain/Information/StructureInformation.cs b/src/ZoneCodeGenerator/Domain/Information/StructureInformation.cs index 2b917bcb..9a65927f 100644 --- a/src/ZoneCodeGenerator/Domain/Information/StructureInformation.cs +++ b/src/ZoneCodeGenerator/Domain/Information/StructureInformation.cs @@ -32,8 +32,8 @@ namespace ZoneCodeGenerator.Domain.Information public bool IsLeaf { get; set; } - public bool HasNameMember => Type.Members.Any(variable => variable.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase)); - + public List NameChain { get; set; } + public StructureInformation(DataTypeWithMembers type) { AssetEnumEntry = null; @@ -45,6 +45,7 @@ namespace ZoneCodeGenerator.Domain.Information Usages = new List(); OrderedMembers = new List(); IsLeaf = true; + NameChain = null; } public override string ToString() diff --git a/src/ZoneCodeGenerator/Generating/Templates/ZoneLoad.stg b/src/ZoneCodeGenerator/Generating/Templates/ZoneLoad.stg index 905d2a98..299b5eb3 100644 --- a/src/ZoneCodeGenerator/Generating/Templates/ZoneLoad.stg +++ b/src/ZoneCodeGenerator/Generating/Templates/ZoneLoad.stg @@ -248,8 +248,8 @@ void $LoaderClassName(context.Asset)$::Load($context.Asset.Type.FullName$** pAss GetNameMethod(context) ::= << std::string $LoaderClassName(context.Asset)$::GetAssetName($context.Asset.Type.FullName$* p$context.Asset.Type.Name$) { -$if(context.Asset.HasNameMember)$ - return p$context.Asset.Type.Name$->name; +$if(context.Asset.NameChain)$ + return p$context.Asset.Type.Name$->$first(context.Asset.NameChain):{member | $member.Member.Name$}$$rest(context.Asset.NameChain):{member | .$member.Member.Name$}$; $else$ return "$context.Asset.Type.Name$"; $endif$ diff --git a/src/ZoneCodeGenerator/Parsing/CommandFile/CommandFileReader.cs b/src/ZoneCodeGenerator/Parsing/CommandFile/CommandFileReader.cs index 089376a8..fb609615 100644 --- a/src/ZoneCodeGenerator/Parsing/CommandFile/CommandFileReader.cs +++ b/src/ZoneCodeGenerator/Parsing/CommandFile/CommandFileReader.cs @@ -13,6 +13,7 @@ namespace ZoneCodeGenerator.Parsing.CommandFile { private static readonly IDataPostProcessor[] postProcessors = { + new PostProcessorAssetName(), new PostProcessorDefaultBlock(), new PostProcessorUsages(), new PostProcessorLeafs() diff --git a/src/ZoneCodeGenerator/Parsing/CommandFile/PostProcessor/PostProcessorAssetName.cs b/src/ZoneCodeGenerator/Parsing/CommandFile/PostProcessor/PostProcessorAssetName.cs new file mode 100644 index 00000000..84e0c995 --- /dev/null +++ b/src/ZoneCodeGenerator/Parsing/CommandFile/PostProcessor/PostProcessorAssetName.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using System.Linq; +using ZoneCodeGenerator.Domain; +using ZoneCodeGenerator.Domain.Information; +using ZoneCodeGenerator.Persistence; + +namespace ZoneCodeGenerator.Parsing.CommandFile.PostProcessor +{ + class PostProcessorAssetName : IDataPostProcessor + { + private const string NameMemberName = "name"; + + private static List FindNameMember(StructureInformation information) + { + var nameMemberInformation = + information.OrderedMembers.FirstOrDefault(memberInformation => + memberInformation.Member.Name.Equals(NameMemberName)); + + if (nameMemberInformation != null) + { + var result = new List {nameMemberInformation}; + return result; + } + + foreach (var embeddedMembers in information.OrderedMembers.Where(memberInformation => + memberInformation.Computations.IsEmbeddedReference && memberInformation.StructureType != null)) + { + var embeddedMemberName = FindNameMember(embeddedMembers.StructureType); + + if (embeddedMemberName == null) continue; + + embeddedMemberName.Insert(0, embeddedMembers); + return embeddedMemberName; + } + + return null; + } + + public bool PostProcess(IDataRepository repository) + { + var assetDataTypes = + repository.GetAllStructs() + .AsEnumerable() + .Concat(repository.GetAllUnions()) + .Select(repository.GetInformationFor) + .Where(information => information.IsAsset); + + foreach (var assetInformation in assetDataTypes) + { + assetInformation.NameChain = FindNameMember(assetInformation); + } + + return true; + } + } +} \ No newline at end of file