2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-06-28 07:11:52 +00:00

ZoneCodeGenerator: Remove PostProcessor for asset names and instead add a statement for command files due to different member names for names

This commit is contained in:
Jan
2019-11-21 13:35:38 +01:00
parent 9b9256d2cb
commit c547520ae8
7 changed files with 188 additions and 64 deletions

View File

@ -16,7 +16,7 @@ HeaderSinglePtrLoadMethodDeclaration(structure) ::= "void LoadPtr_$structure.Typ
HeaderArrayPtrLoadMethodDeclaration(structure) ::= "void LoadArray_$structure.Type.Name$(bool atStreamStart, size_t count);"
HeaderLoadMethodDeclaration(structure) ::= "void Load_$structure.Type.Name$(bool atStreamStart);"
HeaderGetNameMethodDeclaration(asset) ::= "static std::string GetAssetName($asset.Type.FullName$* p$asset.Type.Name$);"
HeaderGetNameMethodDeclaration(asset) ::= "static std::string GetAssetName($asset.Type.FullName$* pAsset);"
HeaderAssetLoadMethodDeclaration(asset) ::= "void LoadAsset_$asset.Type.Name$($asset.Type.FullName$** pAsset);"
HeaderMainLoadMethodDeclaration(asset) ::= "void Load($asset.Type.FullName$** pAsset);"
@ -246,10 +246,10 @@ 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$)
std::string $LoaderClassName(context.Asset)$::GetAssetName($context.Asset.Type.FullName$* pAsset)
{
$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$}$;
return pAsset->$first(context.Asset.NameChain):{member | $member.Member.Name$}$$rest(context.Asset.NameChain):{member | .$member.Member.Name$}$;
$else$
return "$context.Asset.Type.Name$";
$endif$

View File

@ -13,7 +13,6 @@ namespace ZoneCodeGenerator.Parsing.CommandFile
{
private static readonly IDataPostProcessor[] postProcessors =
{
new PostProcessorAssetName(),
new PostProcessorDefaultBlock(),
new PostProcessorUsages(),
new PostProcessorLeafs()

View File

@ -20,6 +20,7 @@ namespace ZoneCodeGenerator.Parsing.CommandFile.Impl
new TestCondition(),
new TestCount(),
new TestGame(),
new TestName(),
new TestReorder(),
new TestReusable(),
new TestScriptString(),

View File

@ -1,56 +0,0 @@
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<MemberInformation> FindNameMember(StructureInformation information)
{
var nameMemberInformation =
information.OrderedMembers.FirstOrDefault(memberInformation =>
memberInformation.Member.Name.Equals(NameMemberName));
if (nameMemberInformation != null)
{
var result = new List<MemberInformation> {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<DataTypeWithMembers>()
.Concat(repository.GetAllUnions())
.Select(repository.GetInformationFor)
.Where(information => information.IsAsset);
foreach (var assetInformation in assetDataTypes)
{
assetInformation.NameChain = FindNameMember(assetInformation);
}
return true;
}
}
}

View File

@ -0,0 +1,67 @@
using System;
using System.Linq;
using ZoneCodeGenerator.Domain.Information;
using ZoneCodeGenerator.Parsing.Matching;
using ZoneCodeGenerator.Parsing.Matching.Matchers;
using ZoneCodeGenerator.Parsing.Testing;
namespace ZoneCodeGenerator.Parsing.CommandFile.Tests
{
class TestName : AbstractTokenTest<ICommandParserState>
{
private const string MemberTypeNameToken = "name";
private static readonly TokenMatcher[] matchers = {
new MatcherLiteral("set"),
new MatcherLiteral("name"),
new MatcherTypename().WithName(MemberTypeNameToken),
new MatcherLiteral(";")
};
public TestName() : base(matchers)
{
}
protected override void ProcessMatch(ICommandParserState state)
{
var typeName = NextMatch(MemberTypeNameToken);
var typeNameParts = typeName.Split(new[] { "::" }, StringSplitOptions.None);
StructureInformation structure;
if (state.DataTypeInUse != null &&
state.GetMembersFromParts(typeNameParts, state.DataTypeInUse, out var memberList))
{
structure = state.DataTypeInUse;
}
else if (state.GetTypenameAndMembersFromParts(typeNameParts, out structure, out memberList))
{
// Do nothing
}
else
{
throw new TestFailedException($"Could not find type '{typeName}'.");
}
if (memberList == null || !memberList.Any())
{
throw new TestFailedException("Need to specify a member when trying to set to a structure name.");
}
for (var i = 0; i < memberList.Count - 1; i++)
{
if (!memberList[i].Computations.IsEmbeddedReference)
{
throw new TestFailedException("Can only add embedded members to name chain.");
}
}
if (!memberList[memberList.Count - 1].IsString)
{
throw new TestFailedException("Final name member must be a string.");
}
structure.NameChain = memberList;
}
}
}