ZoneCodeGenerator: Add postprocessor to inspect whether a structure is a leaf (No complex tasks for loading/writing required, like pointers, strings, scriptstrings)

This commit is contained in:
Jan 2019-11-14 14:53:12 +01:00
parent ef8a040db4
commit f777c049c6
3 changed files with 61 additions and 0 deletions

View File

@ -30,6 +30,8 @@ namespace ZoneCodeGenerator.Domain.Information
public bool ArrayPointerReferenceExists { get; set; } public bool ArrayPointerReferenceExists { get; set; }
public bool ArrayReferenceExists { 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 bool HasNameMember => Type.Members.Any(variable => variable.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase));
public StructureInformation(DataTypeWithMembers type) public StructureInformation(DataTypeWithMembers type)

View File

@ -15,6 +15,7 @@ namespace ZoneCodeGenerator.Parsing.CommandFile
{ {
new PostProcessorDefaultBlock(), new PostProcessorDefaultBlock(),
new PostProcessorUsages(), new PostProcessorUsages(),
new PostProcessorLeafs()
}; };
public static bool ReadFile(string path, CUISession session, bool verbose = false) public static bool ReadFile(string path, CUISession session, bool verbose = false)

View File

@ -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<ReferenceTypePointer>().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<DataTypeWithMembers>()
.Concat(repository.GetAllUnions()))
{
var information = repository.GetInformationFor(dataTypeWithMembers);
information.IsLeaf = IsLeaf(information);
}
return true;
}
}
}