ZoneCodeGenerator: Change code templates to support loading of simple assets like rawfile

This commit is contained in:
Jan 2019-11-14 14:59:48 +01:00
parent f80d661c1b
commit dda9cc0700
2 changed files with 209 additions and 90 deletions

View File

@ -1,2 +1,44 @@
test() ::= << delimiters "$", "$"
>>
Capital(name) ::= "$name; format=\"cap\"$"
Lower(name) ::= "$name; format=\"lower\"$"
TypeDeclaration(typeDecl) ::= "$if(typeDecl.IsConst)$const $endif$$typeDecl.Type.FullName$"
TypeVarName(structure) ::= "var$structure.Type.Name$"
TypePtrVarName(structure) ::= "var$structure.Type.Name$Ptr"
PrintOperandStatic(op) ::= <%
$op.Value$
%>
PrintOperandDynamic(op) ::= <%
$TypeVarName(op.Structure)$->$first(op.ReferencedMemberChain):{member | $member.Member.Name$}$$rest(op.ReferencedMemberChain):{member | .$member.Member.Name$}$
%>
PrintOperation(operation) ::= <%
$if(operation.Operand1NeedsParenthesis)$
($PrintEvaluation(operation.Operand1)$)
$else$
$PrintEvaluation(operation.Operand1)$
$endif$
$operation.OperationType.Syntax$
$if(operation.Operand2NeedsParenthesis)$
($PrintEvaluation(operation.Operand2)$)
$else$
$PrintEvaluation(operation.Operand2)$
$endif$
%>
PrintEvaluation(eval) ::= <%
$if(eval.OperationType)$
$PrintOperation(eval)$
$elseif(eval.IsStatic)$
$PrintOperandStatic(eval)$
$else$
$PrintOperandDynamic(eval)$
$endif$
%>

View File

@ -2,19 +2,25 @@ delimiters "$", "$"
import "Common.stg" import "Common.stg"
Capital(name) ::= "$name; format=\"cap\"$"
Lower(name) ::= "$name; format=\"lower\"$"
LoaderClassName(asset) ::= "Loader_$asset.Type.Name$" LoaderClassName(asset) ::= "Loader_$asset.Type.Name$"
HeaderConstructor(context) ::= "$LoaderClassName(context.Asset)$(IZoneScriptStringProvider* scriptStringProvider, Zone* zone, IZoneInputStream* stream);" HeaderConstructor(context) ::= "$LoaderClassName(context.Asset)$(IZoneScriptStringProvider* scriptStringProvider, Zone* zone, IZoneInputStream* stream);"
HeaderSinglePtrLoadMethodDeclaration(structure) ::= "void LoadPtr_$structure.Type.Name$($structure.Type.FullName$** pPtr);" HeaderSinglePtrLoadMethodDeclaration(structure) ::= "void LoadPtr_$structure.Type.Name$(bool atStreamStart);"
HeaderArrayPtrLoadMethodDeclaration(structure) ::= "void LoadArray_$structure.Type.Name$($structure.Type.FullName$** pArray, size_t count, bool atStreamStart);" HeaderArrayPtrLoadMethodDeclaration(structure) ::= "void LoadArray_$structure.Type.Name$(bool atStreamStart, size_t count);"
HeaderLoadMethodDeclaration(structure) ::= "void Load_$structure.Type.Name$($structure.Type.FullName$* p$structure.Type.Name$, bool atStreamStart);" 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$* p$asset.Type.Name$);"
HeaderAssetLoadMethodDeclaration(asset) ::= "void LoadAsset_$asset.Type.Name$($asset.Type.FullName$** pPtr);" HeaderAssetLoadMethodDeclaration(asset) ::= "void LoadAsset_$asset.Type.Name$($asset.Type.FullName$** pAsset);"
HeaderMainLoadMethodDeclaration(asset) ::= "void Load($asset.Type.FullName$** pAsset);"
VariableDeclaration(type) ::= <<
$type.FullName$* var$type.Name$;
>>
PointerVariableDeclaration(type) ::= <<
$type.FullName$** var$type.Name$Ptr;
>>
HeaderDeclaration(structure) ::= <% HeaderDeclaration(structure) ::= <%
$if(structure.SinglePointerReferenceExists)$ $if(structure.SinglePointerReferenceExists)$
@ -22,12 +28,12 @@ $HeaderSinglePtrLoadMethodDeclaration(structure)$
$\n$ $\n$
$endif$ $endif$
$if(structure.ArrayPointerReferenceExists)$ $if(structure.ArrayPointerReferenceExists && !structure.IsLeaf)$
$HeaderArrayPtrLoadMethodDeclaration(structure)$ $HeaderArrayPtrLoadMethodDeclaration(structure)$
$\n$ $\n$
$endif$ $endif$
$if(structure.NonEmbeddedReferenceExists)$ $if(structure.NonEmbeddedReferenceExists && !structure.IsLeaf)$
$HeaderLoadMethodDeclaration(structure)$ $HeaderLoadMethodDeclaration(structure)$
$\n$ $\n$
$endif$ $endif$
@ -53,134 +59,198 @@ namespace $context.Game$
{ {
class $LoaderClassName(context.Asset)$ final : public AssetLoader class $LoaderClassName(context.Asset)$ final : public AssetLoader
{ {
$context.Structures:{structure | $if(!structure.IsAsset)$$HeaderDeclaration(structure)$$endif$}$ $VariableDeclaration(context.Asset.Type)$
$HeaderLoadMethodDeclaration(context.Asset)$ $PointerVariableDeclaration(context.Asset.Type)$
$HeaderAssetLoadMethodDeclaration(context.Asset)$ $context.MemberTypes:{type | $if(!type.IsAnonymous)$$VariableDeclaration(type)$
$endif$}$
$context.Structures:{structure | $if(!structure.IsAsset && structure.SinglePointerReferenceExists)$$PointerVariableDeclaration(structure.Type)$
$endif$}$
$context.Structures:{structure | $if(!structure.IsAsset)$$HeaderDeclaration(structure)$$endif$}$
$HeaderLoadMethodDeclaration(context.Asset)$
$HeaderSinglePtrLoadMethodDeclaration(context.Asset)$
$HeaderAssetLoadMethodDeclaration(context.Asset)$
public: public:
$HeaderConstructor(context)$ $HeaderConstructor(context)$
$HeaderMainLoadMethodDeclaration(context.Asset)$
$HeaderSinglePtrLoadMethodDeclaration(context.Asset)$ $HeaderGetNameMethodDeclaration(context.Asset)$
$HeaderGetNameMethodDeclaration(context.Asset)$
}; };
} }
>> >>
IncludeHeaderOfOtherAsset(asset) ::= << LoadMember(context, member) ::= <<
#include "../$Lower(asset.Type.Name)$/$Lower(asset.Type.Name)$_load_db.h" Loading member $member.Member.Name$
>>
LoadString(context, structure, member) ::= <<
varXString = &$TypeVarName(structure)$->$member.Member.Name$;
LoadXString(false);
>>
LoadArrayPointer(context, structure, member) ::= <<
if ($TypeVarName(structure)$->$member.Member.Name$)
{
$TypeVarName(structure)$->$member.Member.Name$ = m_stream->Alloc<$TypeDeclaration(member.Member.VariableType)$>(alignof($TypeDeclaration(member.Member.VariableType)$));
$if(member.StructureType && !member.StructureType.IsLeaf)$
$TypeVarName(member.StructureType)$ = $TypeVarName(structure)$->$member.Member.Name$;
LoadArray_$member.Member.VariableType.Type.Name$(true, $PrintEvaluation(member.Computations.ArrayPointerCountEvaluation)$);
$else$
m_stream->Load<$TypeDeclaration(member.Member.VariableType)$>($TypeVarName(structure)$->$member.Member.Name$, $PrintEvaluation(member.Computations.ArrayPointerCountEvaluation)$);
$endif$
}
>>
LoadMemberIfNeedsTreatment(context, structure, member) ::= <<
$if(member.IsString)$
$LoadString(context, structure, member)$
$elseif(member.IsScriptString)$
// Load scriptstring for $member.Member.Name$
$elseif(member.Computations.IsArrayPointerReference && member.Computations.IsSinglePointer)$
$LoadArrayPointer(context, structure, member)$
$endif$
>> >>
LoadMethod(structure, context) ::= << LoadMethod(structure, context) ::= <<
void $LoaderClassName(context.Asset)$::Load_$structure.Type.Name$($structure.Type.FullName$* p$structure.Type.Name$, const bool atStreamStart) void $LoaderClassName(context.Asset)$::Load_$structure.Type.Name$(const bool atStreamStart)
{ {
assert(p$structure.Type.Name$ != nullptr); assert($TypeVarName(structure)$ != nullptr);
if(atStreamStart) if(atStreamStart)
m_stream->Load<$structure.Type.FullName$>(); m_stream->Load<$structure.Type.FullName$>($TypeVarName(structure)$);
$if(structure.Block.IsTemp)$ $if(structure.Block.IsTemp)$
m_stream->PushBlock($context.DefaultNormalBlock.Name$); m_stream->PushBlock($context.DefaultNormalBlock.Name$);
$endif$ $endif$
// Load content here $structure.OrderedMembers:{member | $LoadMemberIfNeedsTreatment(context, structure, member)$}$
$if(structure.Block.IsTemp)$ $if(structure.Block.IsTemp)$
m_stream->PopBlock(); m_stream->PopBlock();
$endif$ $endif$
} }
>> >>
LoadSinglePtrMethod(structure, context) ::= << LoadSinglePtrMethod(structure, context) ::= <<
void $LoaderClassName(context.Asset)$::LoadPtr_$structure.Type.Name$($structure.Type.FullName$** pPtr) void $LoaderClassName(context.Asset)$::LoadPtr_$structure.Type.Name$(const bool atStreamStart)
{ {
assert(pPtr != nullptr); assert($TypePtrVarName(structure)$ != nullptr);
if(atStreamStart)
m_stream->Load<$structure.Type.FullName$*>($TypePtrVarName(structure)$);
m_stream->PushBlock($structure.Block.Name$); m_stream->PushBlock($structure.Block.Name$);
if(*pPtr != nullptr) if(*$TypePtrVarName(structure)$ != nullptr)
{ {
$if(structure.Block.IsTemp)$ $if(structure.Block.IsTemp)$
if(*pPtr == PTR_FOLLOWING || *pPtr == PTR_INSERT) if(*$TypePtrVarName(structure)$ == PTR_FOLLOWING || *$TypePtrVarName(structure)$ == PTR_INSERT)
$else$ $else$
if(*pPtr == PTR_FOLLOWING) if(*$TypePtrVarName(structure)$ == PTR_FOLLOWING)
$endif$ $endif$
{ {
$if(structure.Block.IsTemp)$ $if(structure.Block.IsTemp)$
$structure.Type.FullName$** toInsert = nullptr; $structure.Type.FullName$** toInsert = nullptr;
if(*pPtr == PTR_INSERT) if(*$TypePtrVarName(structure)$ == PTR_INSERT)
toInsert = m_stream->InsertPointer<$structure.Type.FullName$>(); toInsert = m_stream->InsertPointer<$structure.Type.FullName$>();
$endif$ $endif$
$if(structure.HasNonDefaultAlign)$ *$TypePtrVarName(structure)$ = m_stream->Alloc<$structure.Type.FullName$>(alignof($structure.Type.FullName$));
*pPtr = m_stream->Alloc<$structure.Type.FullName$>($structure.FastFileAlign$);
$else$
*pPtr = m_stream->Alloc<$structure.Type.FullName$>();
$endif$
Load_$structure.Type.Name$(*pPtr, true);
$if(structure.IsAsset)$
LoadAsset_$structure.Type.Name$(pPtr);
$endif$
$if(structure.Block.IsTemp)$
if(toInsert != nullptr) $if(!structure.IsLeaf)$
*toInsert = *pPtr; $TypeVarName(structure)$ = *$TypePtrVarName(structure)$;
$endif$ Load_$structure.Type.Name$(true);
} $endif$
else
{ $if(structure.IsAsset)$
$if(structure.Block.IsTemp)$ LoadAsset_$structure.Type.Name$($TypePtrVarName(structure)$);
*pPtr = m_stream->ConvertOffsetToAlias(*pPtr); $endif$
$else$ $if(structure.Block.IsTemp)$
*pPtr = m_stream->ConvertOffsetToPointer(*pPtr);
$endif$ if(toInsert != nullptr)
} *toInsert = *$TypePtrVarName(structure)$;
} $endif$
}
else
{
$if(structure.Block.IsTemp)$
*$TypePtrVarName(structure)$ = m_stream->ConvertOffsetToAlias(*$TypePtrVarName(structure)$);
$else$
*$TypePtrVarName(structure)$ = m_stream->ConvertOffsetToPointer(*$TypePtrVarName(structure)$);
$endif$
}
}
m_stream->PopBlock(); m_stream->PopBlock();
} }
>> >>
LoadArrayPtrMethod(structure, context) ::= << LoadArrayPtrMethod(structure, context) ::= <<
void $LoaderClassName(context.Asset)$::LoadArray_$structure.Type.Name$($structure.Type.FullName$** pArray, const size_t count, const bool atStreamStart) void $LoaderClassName(context.Asset)$::LoadArray_$structure.Type.Name$(const bool atStreamStart, const size_t count)
{ {
assert(pArray != nullptr); assert($TypeVarName(structure)$ != nullptr);
if(atStreamStart) if(atStreamStart)
m_stream->Load<$structure.Type.FullName$>(count); m_stream->Load<$structure.Type.FullName$>($TypeVarName(structure)$, count);
for(size_t index = 0; index < count; index++) for(size_t index = 0; index < count; index++)
{ {
Load_$structure.Type.Name$(&(*pArray)[index], false); Load_$structure.Type.Name$(false);
} $TypeVarName(structure)$++;
}
} }
>> >>
SourceDefinition(structure, context) ::= << SourceDefinition(structure, context) ::= <<
$if(structure.NonEmbeddedReferenceExists)$
$LoadMethod(structure, context)$
$endif$
$if(structure.SinglePointerReferenceExists)$ $if(structure.SinglePointerReferenceExists)$
$LoadSinglePtrMethod(structure, context)$ $LoadSinglePtrMethod(structure, context)$
$endif$ $endif$
$if(structure.ArrayPointerReferenceExists)$ $if(structure.ArrayPointerReferenceExists && !structure.IsLeaf)$
$LoadArrayPtrMethod(structure, context)$ $LoadArrayPtrMethod(structure, context)$
$endif$
$if(structure.NonEmbeddedReferenceExists && !structure.IsLeaf)$
$LoadMethod(structure, context)$
$endif$ $endif$
>> >>
VariableInitialization(type) ::= <<
var$type.Name$ = nullptr;
>>
PointerVariableInitialization(type) ::= <<
var$type.Name$Ptr = nullptr;
>>
ConstructorMethod(context) ::= << ConstructorMethod(context) ::= <<
$LoaderClassName(context.Asset)$::$LoaderClassName(context.Asset)$(IZoneScriptStringProvider* scriptStringProvider, Zone* zone, IZoneInputStream* stream) $LoaderClassName(context.Asset)$::$LoaderClassName(context.Asset)$(IZoneScriptStringProvider* scriptStringProvider, Zone* zone, IZoneInputStream* stream)
: AssetLoader($context.Asset.AssetEnumEntry.Name$, scriptStringProvider, zone, stream){} : AssetLoader($context.Asset.AssetEnumEntry.Name$, scriptStringProvider, zone, stream)
{
$VariableInitialization(context.Asset.Type)$
$PointerVariableInitialization(context.Asset.Type)$
$context.MemberTypes:{type | $if(!type.IsAnonymous)$$VariableInitialization(type)$
$endif$}$
$context.Structures:{structure | $if(!structure.IsAsset && structure.SinglePointerReferenceExists)$$PointerVariableInitialization(structure.Type)$
$endif$}$
}
>> >>
LoadAssetMethod(structure, context) ::= << LoadAssetMethod(structure, context) ::= <<
void $LoaderClassName(context.Asset)$::LoadAsset_$structure.Type.Name$($structure.Type.FullName$** pPtr) void $LoaderClassName(context.Asset)$::LoadAsset_$structure.Type.Name$($structure.Type.FullName$** pAsset)
{ {
assert(pPtr != nullptr); assert(pAsset != nullptr);
*pPtr = static_cast<$structure.Type.FullName$*>(LinkAsset(GetAssetName(*pPtr), *pPtr)); *pAsset = static_cast<$structure.Type.FullName$*>(LinkAsset(GetAssetName(*pAsset), *pAsset));
}
>>
MainLoadMethod(context) ::= <<
void $LoaderClassName(context.Asset)$::Load($context.Asset.Type.FullName$** pAsset)
{
assert(pAsset != nullptr);
$TypePtrVarName(context.Asset)$ = pAsset;
LoadPtr_$context.Asset.Type.Name$(false);
} }
>> >>
@ -190,11 +260,16 @@ std::string $LoaderClassName(context.Asset)$::GetAssetName($context.Asset.Type.F
$if(context.Asset.HasNameMember)$ $if(context.Asset.HasNameMember)$
return p$context.Asset.Type.Name$->name; return p$context.Asset.Type.Name$->name;
$else$ $else$
return "$context.Asset.Type.Name$"; return "$context.Asset.Type.Name$";
$endif$ $endif$
} }
>> >>
IncludeHeaderOfOtherAsset(asset) ::= <<
#include "../$Lower(asset.Type.Name)$/$Lower(asset.Type.Name)$_load_db.h"
>>
// ======================= // =======================
// Source file entry point // Source file entry point
// ======================= // =======================
@ -222,5 +297,7 @@ $LoadAssetMethod(context.Asset, context)$
$LoadSinglePtrMethod(context.Asset, context)$ $LoadSinglePtrMethod(context.Asset, context)$
$MainLoadMethod(context)$
$GetNameMethod(context)$ $GetNameMethod(context)$
>> >>