From f777c049c67ee3d1ca8886362cfe688b14135a26 Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 14 Nov 2019 14:53:12 +0100 Subject: [PATCH] ZoneCodeGenerator: Add postprocessor to inspect whether a structure is a leaf (No complex tasks for loading/writing required, like pointers, strings, scriptstrings) --- .../Information/StructureInformation.cs | 2 + .../Parsing/CommandFile/CommandFileReader.cs | 1 + .../PostProcessor/PostProcessorLeafs.cs | 58 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/ZoneCodeGenerator/Parsing/CommandFile/PostProcessor/PostProcessorLeafs.cs diff --git a/src/ZoneCodeGenerator/Domain/Information/StructureInformation.cs b/src/ZoneCodeGenerator/Domain/Information/StructureInformation.cs index 532df051..2b917bcb 100644 --- a/src/ZoneCodeGenerator/Domain/Information/StructureInformation.cs +++ b/src/ZoneCodeGenerator/Domain/Information/StructureInformation.cs @@ -30,6 +30,8 @@ namespace ZoneCodeGenerator.Domain.Information public bool ArrayPointerReferenceExists { get; set; } public bool ArrayReferenceExists { get; set; } + public bool IsLeaf { get; set; } + public bool HasNameMember => Type.Members.Any(variable => variable.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase)); public StructureInformation(DataTypeWithMembers type) diff --git a/src/ZoneCodeGenerator/Parsing/CommandFile/CommandFileReader.cs b/src/ZoneCodeGenerator/Parsing/CommandFile/CommandFileReader.cs index 506552c0..089376a8 100644 --- a/src/ZoneCodeGenerator/Parsing/CommandFile/CommandFileReader.cs +++ b/src/ZoneCodeGenerator/Parsing/CommandFile/CommandFileReader.cs @@ -15,6 +15,7 @@ namespace ZoneCodeGenerator.Parsing.CommandFile { new PostProcessorDefaultBlock(), new PostProcessorUsages(), + new PostProcessorLeafs() }; public static bool ReadFile(string path, CUISession session, bool verbose = false) diff --git a/src/ZoneCodeGenerator/Parsing/CommandFile/PostProcessor/PostProcessorLeafs.cs b/src/ZoneCodeGenerator/Parsing/CommandFile/PostProcessor/PostProcessorLeafs.cs new file mode 100644 index 00000000..0ef37ab1 --- /dev/null +++ b/src/ZoneCodeGenerator/Parsing/CommandFile/PostProcessor/PostProcessorLeafs.cs @@ -0,0 +1,58 @@ +using System.Linq; +using ZoneCodeGenerator.Domain; +using ZoneCodeGenerator.Domain.Information; +using ZoneCodeGenerator.Persistence; + +namespace ZoneCodeGenerator.Parsing.CommandFile.PostProcessor +{ + class PostProcessorLeafs : IDataPostProcessor + { + private static bool IsLeaf(StructureInformation structureInformation) + { + foreach (var member in structureInformation.OrderedMembers) + { + // If there is a condition to this member and it always evaluates to false: Skip this member + if (member.Condition != null && member.Condition.IsStatic && member.Condition.EvaluateNumeric() == 0) + continue; + + // Any ScriptStrings or Strings need to be processed. + if (member.IsScriptString + || member.IsString) + return false; + + // If there are any Pointer members that are not always count 0 it needs to be processed. + var hasNoPointerMembers = member.Member.VariableType.References.OfType().All(pointer => + { + if (!pointer.Count.IsStatic) + return false; + + return pointer.Count.EvaluateNumeric() == 0; + }); + + if (!hasNoPointerMembers) + return false; + + if (member.StructureType != null + && member.StructureType != structureInformation + && !IsLeaf(member.StructureType)) + return false; + } + + return true; + } + + public bool PostProcess(IDataRepository repository) + { + foreach (var dataTypeWithMembers in repository.GetAllStructs() + .AsEnumerable() + .Concat(repository.GetAllUnions())) + { + var information = repository.GetInformationFor(dataTypeWithMembers); + + information.IsLeaf = IsLeaf(information); + } + + return true; + } + } +} \ No newline at end of file