mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-07-01 09:07:55 -05:00
chore: update ZoneCodeGenerator code style
This commit is contained in:
@ -3,21 +3,24 @@
|
||||
#include "ICodeTemplate.h"
|
||||
#include "ZoneCodeGeneratorArguments.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
class CodeGenerator
|
||||
{
|
||||
const ZoneCodeGeneratorArguments* m_args;
|
||||
public:
|
||||
explicit CodeGenerator(const ZoneCodeGeneratorArguments* args);
|
||||
|
||||
std::unordered_map<std::string, std::unique_ptr<ICodeTemplate>> m_template_mapping;
|
||||
bool GenerateCode(IDataRepository* repository);
|
||||
|
||||
private:
|
||||
void SetupTemplates();
|
||||
|
||||
bool GenerateCodeForTemplate(RenderingContext* context, ICodeTemplate* codeTemplate) const;
|
||||
static bool GetAssetWithName(IDataRepository* repository, const std::string& name, StructureInformation*& asset);
|
||||
|
||||
public:
|
||||
explicit CodeGenerator(const ZoneCodeGeneratorArguments* args);
|
||||
const ZoneCodeGeneratorArguments* m_args;
|
||||
|
||||
bool GenerateCode(IDataRepository* repository);
|
||||
std::unordered_map<std::string, std::unique_ptr<ICodeTemplate>> m_template_mapping;
|
||||
};
|
||||
|
@ -10,6 +10,8 @@
|
||||
class RenderingUsedType
|
||||
{
|
||||
public:
|
||||
RenderingUsedType(const DataDefinition* type, StructureInformation* info);
|
||||
|
||||
bool m_members_loaded;
|
||||
const DataDefinition* m_type;
|
||||
StructureInformation* m_info;
|
||||
@ -20,25 +22,13 @@ public:
|
||||
bool m_array_reference_exists;
|
||||
bool m_pointer_array_reference_exists;
|
||||
bool m_pointer_array_reference_is_reusable;
|
||||
|
||||
RenderingUsedType(const DataDefinition* type, StructureInformation* info);
|
||||
};
|
||||
|
||||
class RenderingContext
|
||||
{
|
||||
std::unordered_map<const DataDefinition*, std::unique_ptr<RenderingUsedType>> m_used_types_lookup;
|
||||
|
||||
RenderingContext(std::string game, std::vector<const FastFileBlock*> fastFileBlocks);
|
||||
|
||||
RenderingUsedType* AddUsedType(std::unique_ptr<RenderingUsedType> usedType);
|
||||
RenderingUsedType* GetBaseType(const IDataRepository* repository, MemberComputations* computations, RenderingUsedType* usedType);
|
||||
void AddMembersToContext(const IDataRepository* repository, StructureInformation* info);
|
||||
void ScanUsedTypeIfNeeded(const IDataRepository* repository, MemberComputations* computations, RenderingUsedType* usedType);
|
||||
void MakeAsset(const IDataRepository* repository, StructureInformation* asset);
|
||||
void CreateUsedTypeCollections();
|
||||
bool UsedTypeHasActions(const RenderingUsedType* usedType) const;
|
||||
|
||||
public:
|
||||
static std::unique_ptr<RenderingContext> BuildContext(const IDataRepository* repository, StructureInformation* asset);
|
||||
|
||||
std::string m_game;
|
||||
std::vector<const FastFileBlock*> m_blocks;
|
||||
|
||||
@ -52,5 +42,16 @@ public:
|
||||
const FastFileBlock* m_default_normal_block;
|
||||
const FastFileBlock* m_default_temp_block;
|
||||
|
||||
static std::unique_ptr<RenderingContext> BuildContext(const IDataRepository* repository, StructureInformation* asset);
|
||||
private:
|
||||
RenderingContext(std::string game, std::vector<const FastFileBlock*> fastFileBlocks);
|
||||
|
||||
RenderingUsedType* AddUsedType(std::unique_ptr<RenderingUsedType> usedType);
|
||||
RenderingUsedType* GetBaseType(const IDataRepository* repository, MemberComputations* computations, RenderingUsedType* usedType);
|
||||
void AddMembersToContext(const IDataRepository* repository, StructureInformation* info);
|
||||
void ScanUsedTypeIfNeeded(const IDataRepository* repository, MemberComputations* computations, RenderingUsedType* usedType);
|
||||
void MakeAsset(const IDataRepository* repository, StructureInformation* asset);
|
||||
void CreateUsedTypeCollections();
|
||||
bool UsedTypeHasActions(const RenderingUsedType* usedType) const;
|
||||
|
||||
std::unordered_map<const DataDefinition*, std::unique_ptr<RenderingUsedType>> m_used_types_lookup;
|
||||
};
|
||||
|
@ -6,69 +6,75 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
class AssetStructTestsTemplate::Internal final : BaseTemplate
|
||||
namespace
|
||||
{
|
||||
void TestMethod(StructureInformation* structure)
|
||||
{
|
||||
LINE("TEST_CASE(\"" << m_env.m_game << "::" << m_env.m_asset->m_definition->GetFullName() << ": Tests for " << structure->m_definition->GetFullName()
|
||||
<< "\", \"[assetstruct]\")")
|
||||
LINE("{")
|
||||
m_intendation++;
|
||||
static constexpr int TAG_SOURCE = 1;
|
||||
|
||||
for (const auto& member : structure->m_ordered_members)
|
||||
class Template final : BaseTemplate
|
||||
{
|
||||
public:
|
||||
Template(std::ostream& stream, RenderingContext* context)
|
||||
: BaseTemplate(stream, context)
|
||||
{
|
||||
if (!member->m_member->m_name.empty() && !member->m_member->m_type_declaration->m_has_custom_bit_size)
|
||||
}
|
||||
|
||||
void Source()
|
||||
{
|
||||
LINE("// ====================================================================")
|
||||
LINE("// This file has been generated by ZoneCodeGenerator.")
|
||||
LINE("// Do not modify.")
|
||||
LINE("// Any changes will be discarded when regenerating.")
|
||||
LINE("// ====================================================================")
|
||||
LINE("")
|
||||
LINE("#include <catch2/catch_test_macros.hpp>")
|
||||
LINE("#include <catch2/generators/catch_generators.hpp>")
|
||||
LINE("#include <cstddef>")
|
||||
LINE("#include \"Game/" << m_env.m_game << "/" << m_env.m_game << ".h\"")
|
||||
LINE("")
|
||||
LINE("using namespace " << m_env.m_game << ";")
|
||||
LINE("")
|
||||
LINE("namespace game::" << m_env.m_game << "::xassets::asset_" << Lower(m_env.m_asset->m_definition->m_name))
|
||||
LINE("{")
|
||||
m_intendation++;
|
||||
|
||||
TestMethod(m_env.m_asset);
|
||||
for (auto* structure : m_env.m_used_structures)
|
||||
{
|
||||
LINE("REQUIRE(offsetof(" << structure->m_definition->GetFullName() << ", " << member->m_member->m_name << ") == " << member->m_member->m_offset
|
||||
<< ");")
|
||||
StructureComputations computations(structure->m_info);
|
||||
if (!structure->m_info->m_definition->m_anonymous && !computations.IsAsset())
|
||||
TestMethod(structure->m_info);
|
||||
}
|
||||
|
||||
m_intendation--;
|
||||
LINE("}")
|
||||
}
|
||||
|
||||
LINE("")
|
||||
|
||||
LINE("REQUIRE(" << structure->m_definition->GetSize() << "u == sizeof(" << structure->m_definition->GetFullName() << "));")
|
||||
LINE("REQUIRE(" << structure->m_definition->GetAlignment() << "u == alignof(" << structure->m_definition->GetFullName() << "));")
|
||||
m_intendation--;
|
||||
LINE("}")
|
||||
}
|
||||
|
||||
public:
|
||||
Internal(std::ostream& stream, RenderingContext* context)
|
||||
: BaseTemplate(stream, context)
|
||||
{
|
||||
}
|
||||
|
||||
void Source()
|
||||
{
|
||||
LINE("// ====================================================================")
|
||||
LINE("// This file has been generated by ZoneCodeGenerator.")
|
||||
LINE("// Do not modify.")
|
||||
LINE("// Any changes will be discarded when regenerating.")
|
||||
LINE("// ====================================================================")
|
||||
LINE("")
|
||||
LINE("#include <catch2/catch_test_macros.hpp>")
|
||||
LINE("#include <catch2/generators/catch_generators.hpp>")
|
||||
LINE("#include <cstddef>")
|
||||
LINE("#include \"Game/" << m_env.m_game << "/" << m_env.m_game << ".h\"")
|
||||
LINE("")
|
||||
LINE("using namespace " << m_env.m_game << ";")
|
||||
LINE("")
|
||||
LINE("namespace game::" << m_env.m_game << "::xassets::asset_" << Lower(m_env.m_asset->m_definition->m_name))
|
||||
LINE("{")
|
||||
m_intendation++;
|
||||
|
||||
TestMethod(m_env.m_asset);
|
||||
for (auto* structure : m_env.m_used_structures)
|
||||
private:
|
||||
void TestMethod(StructureInformation* structure)
|
||||
{
|
||||
StructureComputations computations(structure->m_info);
|
||||
if (!structure->m_info->m_definition->m_anonymous && !computations.IsAsset())
|
||||
TestMethod(structure->m_info);
|
||||
}
|
||||
LINE("TEST_CASE(\"" << m_env.m_game << "::" << m_env.m_asset->m_definition->GetFullName() << ": Tests for "
|
||||
<< structure->m_definition->GetFullName() << "\", \"[assetstruct]\")")
|
||||
LINE("{")
|
||||
m_intendation++;
|
||||
|
||||
m_intendation--;
|
||||
LINE("}")
|
||||
}
|
||||
};
|
||||
for (const auto& member : structure->m_ordered_members)
|
||||
{
|
||||
if (!member->m_member->m_name.empty() && !member->m_member->m_type_declaration->m_has_custom_bit_size)
|
||||
{
|
||||
LINE("REQUIRE(offsetof(" << structure->m_definition->GetFullName() << ", " << member->m_member->m_name
|
||||
<< ") == " << member->m_member->m_offset << ");")
|
||||
}
|
||||
}
|
||||
|
||||
LINE("")
|
||||
|
||||
LINE("REQUIRE(" << structure->m_definition->GetSize() << "u == sizeof(" << structure->m_definition->GetFullName() << "));")
|
||||
LINE("REQUIRE(" << structure->m_definition->GetAlignment() << "u == alignof(" << structure->m_definition->GetFullName() << "));")
|
||||
m_intendation--;
|
||||
LINE("}")
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::vector<CodeTemplateFile> AssetStructTestsTemplate::GetFilesToRender(RenderingContext* context)
|
||||
{
|
||||
@ -89,10 +95,10 @@ std::vector<CodeTemplateFile> AssetStructTestsTemplate::GetFilesToRender(Renderi
|
||||
|
||||
void AssetStructTestsTemplate::RenderFile(std::ostream& stream, const int fileTag, RenderingContext* context)
|
||||
{
|
||||
Internal internal(stream, context);
|
||||
Template t(stream, context);
|
||||
|
||||
if (fileTag == TAG_SOURCE)
|
||||
internal.Source();
|
||||
t.Source();
|
||||
else
|
||||
std::cout << "Invalid tag in AssetStructTestsTemplate\n";
|
||||
}
|
||||
|
@ -3,10 +3,6 @@
|
||||
|
||||
class AssetStructTestsTemplate final : public ICodeTemplate
|
||||
{
|
||||
static constexpr int TAG_SOURCE = 1;
|
||||
|
||||
class Internal;
|
||||
|
||||
public:
|
||||
std::vector<CodeTemplateFile> GetFilesToRender(RenderingContext* context) override;
|
||||
void RenderFile(std::ostream& stream, int fileTag, RenderingContext* context) override;
|
||||
|
@ -15,27 +15,10 @@ class BaseTemplate
|
||||
protected:
|
||||
static constexpr const char* INTENDATION = " ";
|
||||
|
||||
std::ostream& m_out;
|
||||
RenderingContext& m_env;
|
||||
unsigned m_intendation;
|
||||
|
||||
BaseTemplate(std::ostream& stream, RenderingContext* context);
|
||||
|
||||
void DoIntendation() const;
|
||||
|
||||
private:
|
||||
static void MakeSafeTypeNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeTypeVarNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeTypeWrittenVarNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeTypePtrVarNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeTypeWrittenPtrVarNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeArrayIndicesInternal(const DeclarationModifierComputations& modifierComputations, std::ostringstream& str);
|
||||
static void MakeOperandStatic(const OperandStatic* op, std::ostringstream& str);
|
||||
static void MakeOperandDynamic(const OperandDynamic* op, std::ostringstream& str);
|
||||
static void MakeOperation(const Operation* operation, std::ostringstream& str);
|
||||
static void MakeEvaluationInternal(const IEvaluation* evaluation, std::ostringstream& str);
|
||||
|
||||
protected:
|
||||
static std::string Upper(std::string str);
|
||||
static std::string Lower(std::string str);
|
||||
static std::string MakeTypeVarName(const DataDefinition* def);
|
||||
@ -55,6 +38,22 @@ protected:
|
||||
static std::string MakeCustomActionCall(CustomAction* action);
|
||||
static std::string MakeArrayCount(const ArrayDeclarationModifier* arrayModifier);
|
||||
static std::string MakeEvaluation(const IEvaluation* evaluation);
|
||||
|
||||
std::ostream& m_out;
|
||||
RenderingContext& m_env;
|
||||
unsigned m_intendation;
|
||||
|
||||
private:
|
||||
static void MakeSafeTypeNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeTypeVarNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeTypeWrittenVarNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeTypePtrVarNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeTypeWrittenPtrVarNameInternal(const DataDefinition* def, std::ostringstream& str);
|
||||
static void MakeArrayIndicesInternal(const DeclarationModifierComputations& modifierComputations, std::ostringstream& str);
|
||||
static void MakeOperandStatic(const OperandStatic* op, std::ostringstream& str);
|
||||
static void MakeOperandDynamic(const OperandDynamic* op, std::ostringstream& str);
|
||||
static void MakeOperation(const Operation* operation, std::ostringstream& str);
|
||||
static void MakeEvaluationInternal(const IEvaluation* evaluation, std::ostringstream& str);
|
||||
};
|
||||
|
||||
#define LINE(x) \
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -3,11 +3,6 @@
|
||||
|
||||
class ZoneLoadTemplate final : public ICodeTemplate
|
||||
{
|
||||
static constexpr int TAG_HEADER = 1;
|
||||
static constexpr int TAG_SOURCE = 2;
|
||||
|
||||
class Internal;
|
||||
|
||||
public:
|
||||
std::vector<CodeTemplateFile> GetFilesToRender(RenderingContext* context) override;
|
||||
void RenderFile(std::ostream& stream, int fileTag, RenderingContext* context) override;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -3,11 +3,6 @@
|
||||
|
||||
class ZoneMarkTemplate final : public ICodeTemplate
|
||||
{
|
||||
static constexpr int TAG_HEADER = 1;
|
||||
static constexpr int TAG_SOURCE = 2;
|
||||
|
||||
class Internal;
|
||||
|
||||
public:
|
||||
std::vector<CodeTemplateFile> GetFilesToRender(RenderingContext* context) override;
|
||||
void RenderFile(std::ostream& stream, int fileTag, RenderingContext* context) override;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -3,11 +3,6 @@
|
||||
|
||||
class ZoneWriteTemplate final : public ICodeTemplate
|
||||
{
|
||||
static constexpr int TAG_HEADER = 1;
|
||||
static constexpr int TAG_SOURCE = 2;
|
||||
|
||||
class Internal;
|
||||
|
||||
public:
|
||||
std::vector<CodeTemplateFile> GetFilesToRender(RenderingContext* context) override;
|
||||
void RenderFile(std::ostream& stream, int fileTag, RenderingContext* context) override;
|
||||
|
Reference in New Issue
Block a user