From fd9872828317a67af620e07bca293515e04bcc17 Mon Sep 17 00:00:00 2001 From: ouczbs Date: Fri, 26 Apr 2024 19:22:14 +0800 Subject: [PATCH] update liquid template --- src/refl/ClassMeta.cs | 4 + src/refl/ClassMetaGen.cs | 43 ++++- src/refl/Properties/launchSettings.json | 2 +- src/refl/cpp/meta_vertex_gen.inl | 214 ++++++++++++++++++++++-- src/refl/cpp/uimeta_vertex_gen.inl | 54 +++--- src/refl/cpp/vertex.h | 116 ++++++++----- src/refl/cpp/vertex_gen.inl | 1 + src/refl/template/refl.liquid | 89 +++++----- 8 files changed, 390 insertions(+), 133 deletions(-) diff --git a/src/refl/ClassMeta.cs b/src/refl/ClassMeta.cs index dd461f7..759f250 100644 --- a/src/refl/ClassMeta.cs +++ b/src/refl/ClassMeta.cs @@ -53,6 +53,10 @@ namespace refl ClassList = new List(); ChildList = new List(); } + public bool HasMeta() + { + return ClassList.Count > 0 || ChildList.Count > 0; + } public static ClassMeta ParseCppClass(CppClass cppClass) { string parentName = "void"; diff --git a/src/refl/ClassMetaGen.cs b/src/refl/ClassMetaGen.cs index fec8fb1..953a927 100644 --- a/src/refl/ClassMetaGen.cs +++ b/src/refl/ClassMetaGen.cs @@ -23,6 +23,7 @@ namespace refl public string NameSpace { get; set; } = ""; public string ParentName { get; set; } = ""; public string Indent { get; set; } = ""; + public string Indent2 { get; set; } = ""; public string MetaName { get; set; } = ""; public List FieldList = new List(); @@ -30,6 +31,14 @@ namespace refl { Indent = indent; NameSpace = nameSpace; + if (string.IsNullOrEmpty(nameSpace)) + { + Indent2 = indent; + } + else + { + Indent2 = indent + "\t"; + } } } @@ -55,12 +64,15 @@ namespace refl { foreach (var name in ModuleMeta.NameSet) { - FileList.Add(name, new StringBuilder()); + var build = new StringBuilder(); + build.AppendLine("#pragma once"); + FileList.Add(name, build); } GenModuleMeta(module, null); string file_name = Path.GetFileName(target); string? dir = Path.GetDirectoryName(target); var output = new StringBuilder(); + output.AppendLine("#pragma once"); foreach (var pair in FileList) { string path = $"{pair.Key}_{file_name}".ToLower(); @@ -75,7 +87,15 @@ namespace refl } public static void GenModuleMeta(ModuleMeta module, string? prefix) { + if (!module.HasMeta()) + { + return; + } var gen = new ClassMetaGenData(prefix+"", module.NameSpace); + if (prefix != null) + { + GenNamespaceBegin(gen); + } foreach (var cls in module.ClassList) { GenClassMeta(cls, gen); @@ -84,13 +104,32 @@ namespace refl { if(prefix == null) { - GenModuleMeta(child, prefix + "\r"); + GenModuleMeta(child, ""); } else { GenModuleMeta(child, prefix + "\t"); } } + if (prefix != null) + { + GenNamespaceEnd(gen); + } + } + public static void GenNamespaceBegin(ClassMetaGenData gen) + { + foreach (var name in ModuleMeta.NameSet) + { + string line = $"{gen.Indent}namespace {gen.NameSpace} {{"; + FileList[name].AppendLine(line); + } + } + public static void GenNamespaceEnd(ClassMetaGenData gen) + { + foreach (var name in ModuleMeta.NameSet) + { + FileList[name].AppendLine($"{gen.Indent}}}"); + } } public static void GenClassMeta(ClassMeta cls, ClassMetaGenData gen) { diff --git a/src/refl/Properties/launchSettings.json b/src/refl/Properties/launchSettings.json index 678521e..28ebb8a 100644 --- a/src/refl/Properties/launchSettings.json +++ b/src/refl/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "refl": { "commandName": "Project", - "commandLineArgs": "build F:\\csharp\\CppAst.NET\\src\\refl\\cpp\\vertex.h -o F:\\csharp\\CppAst.NET\\src\\refl\\cpp\\vertex_gen.inl -t F:\\csharp\\CppAst.NET\\src\\refl\\template\\refl.liquid -v" + "commandLineArgs": "build F:\\csharp\\cppast\\src\\refl\\cpp\\vertex.h -o F:\\csharp\\cppast\\src\\refl\\cpp\\vertex_gen.inl -t F:\\csharp\\cppast\\src\\refl\\template\\refl.liquid -v" } } } \ No newline at end of file diff --git a/src/refl/cpp/meta_vertex_gen.inl b/src/refl/cpp/meta_vertex_gen.inl index fc3f74f..e5fd8ff 100644 --- a/src/refl/cpp/meta_vertex_gen.inl +++ b/src/refl/cpp/meta_vertex_gen.inl @@ -1,34 +1,220 @@ -struct vec3_Static_Meta { +#pragma once +struct TestMain1_Static_Meta { consteval static auto __StaticFields() { - return std::make_tuple(&vec3::x, &vec3::y, &vec3::z, &vec3::norm); + return std::make_tuple(&TestMain1::x, &TestMain1::y, &TestMain1::z); }; consteval static auto __MakeStaticFields() { return std::array{ - StaticMemberField(&vec3::x, FName("x"), { 0.f}), - StaticMemberField(&vec3::y, FName("y"), { 0.f}), - StaticMemberField(&vec3::z, FName("z"), { 0.f}), - StaticMethodField(&vec3::norm, FName("norm"), { {3,4} }), + refl::StaticMemberField(&TestMain1::x, FName("x"), {}), + refl::StaticMemberField(&TestMain1::y, FName("y"), {}), + refl::StaticMemberField(&TestMain1::z, FName("z"), {}), }; }; consteval static int Size() { - return fetch_meta_size(); + return refl::fetch_meta_size(); }; }; -struct vec3_Meta : public Meta { - using MyStatic = vec3_Static_Meta; - using MyUClass = UClass_Meta;; +struct TestMain1_Meta : public refl::Meta { + using MyStatic = TestMain1_Static_Meta; + using MyUClass = refl::UClass_Meta;; inline static char s_data[MyStatic::Size()]{}; static auto __MakeFields() { char* memory = &s_data[0]; return std::array{ - StaticMemberField(&vec3::x, FName("x"), { 0.f}), - StaticMemberField(&vec3::y, FName("y"), { 0.f}), - StaticMemberField(&vec3::z, FName("z"), { 0.f}), - StaticMethodField(&vec3::norm, FName("norm"), { {3,4} }), + MemberField(&TestMain1::x, FName("x"), memory, {}), + MemberField(&TestMain1::y, FName("y"), memory, {}), + MemberField(&TestMain1::z, FName("z"), memory, {}), }; }; }; +namespace engineapi { + struct Vector3_Static_Meta { + consteval static auto __StaticFields() { + return std::make_tuple(&Vector3::x, &Vector3::y, &Vector3::z); + }; + consteval static auto __MakeStaticFields() { + return std::array{ + refl::StaticMemberField(&Vector3::x, FName("x"), {}), + refl::StaticMemberField(&Vector3::y, FName("y"), {}), + refl::StaticMemberField(&Vector3::z, FName("z"), {}), + }; + }; + + consteval static int Size() { + return refl::fetch_meta_size(); + }; + }; + + struct Vector3_Meta : public refl::Meta { + using MyStatic = Vector3_Static_Meta; + using MyUClass = refl::UClass_Meta;; + inline static char s_data[MyStatic::Size()]{}; + static auto __MakeFields() { + char* memory = &s_data[0]; + return std::array{ + MemberField(&Vector3::x, FName("x"), memory, {}), + MemberField(&Vector3::y, FName("y"), memory, {}), + MemberField(&Vector3::z, FName("z"), memory, {}), + }; + }; + }; + struct Vector2_Static_Meta { + consteval static auto __StaticFields() { + return std::make_tuple(&Vector2::x, &Vector2::y); + }; + + consteval static auto __MakeStaticFields() { + return std::array{ + refl::StaticMemberField(&Vector2::x, FName("x"), { 1.0 }), + refl::StaticMemberField(&Vector2::y, FName("y"), { 2.0 }), + }; + }; + + consteval static int Size() { + return refl::fetch_meta_size(); + }; + }; + + struct Vector2_Meta : public refl::Meta { + using MyStatic = Vector2_Static_Meta; + using MyUClass = refl::UClass_Meta;; + inline static char s_data[MyStatic::Size()]{}; + static auto __MakeFields() { + char* memory = &s_data[0]; + return std::array{ + MemberField(&Vector2::x, FName("x"), memory, { 1.0 }), + MemberField(&Vector2::y, FName("y"), memory, { 2.0 }), + }; + }; + }; + struct PosVertex_Static_Meta { + consteval static auto __StaticFields() { + return std::make_tuple(&PosVertex::Position); + }; + + consteval static auto __MakeStaticFields() { + return std::array{ + refl::StaticMemberField(&PosVertex::Position, FName("Position"), {}), + }; + }; + + consteval static int Size() { + return refl::fetch_meta_size(); + }; + }; + + struct PosVertex_Meta : public refl::Meta { + using MyStatic = PosVertex_Static_Meta; + using MyUClass = refl::UClass_Meta;; + inline static char s_data[MyStatic::Size()]{}; + static auto __MakeFields() { + char* memory = &s_data[0]; + return std::array{ + MemberField(&PosVertex::Position, FName("Position"), memory, {}), + }; + }; + }; + struct TexVertex_Static_Meta { + consteval static auto __StaticFields() { + return std::make_tuple(&TexVertex::Position, &TexVertex::Normal, &TexVertex::TexCoords); + }; + + consteval static auto __MakeStaticFields() { + return std::array{ + refl::StaticMemberField(&TexVertex::Position, FName("Position"), {}), + refl::StaticMemberField(&TexVertex::Normal, FName("Normal"), {}), + refl::StaticMemberField(&TexVertex::TexCoords, FName("TexCoords"), {}), + }; + }; + + consteval static int Size() { + return refl::fetch_meta_size(); + }; + }; + + struct TexVertex_Meta : public refl::Meta { + using MyStatic = TexVertex_Static_Meta; + using MyUClass = refl::UClass_Meta;; + inline static char s_data[MyStatic::Size()]{}; + static auto __MakeFields() { + char* memory = &s_data[0]; + return std::array{ + MemberField(&TexVertex::Position, FName("Position"), memory, {}), + MemberField(&TexVertex::Normal, FName("Normal"), memory, {}), + MemberField(&TexVertex::TexCoords, FName("TexCoords"), memory, {}), + }; + }; + }; + struct BoneVertex_Static_Meta { + consteval static auto __StaticFields() { + return std::make_tuple(&BoneVertex::Position, &BoneVertex::TexCoords, &BoneVertex::Normal, &BoneVertex::Tangent, &BoneVertex::Weights, &BoneVertex::BoneIDs); + }; + + consteval static auto __MakeStaticFields() { + return std::array{ + refl::StaticMemberField(&BoneVertex::Position, FName("Position"), {}), + refl::StaticMemberField(&BoneVertex::TexCoords, FName("TexCoords"), {}), + refl::StaticMemberField(&BoneVertex::Normal, FName("Normal"), {}), + refl::StaticMemberField(&BoneVertex::Tangent, FName("Tangent"), {}), + refl::StaticMemberField(&BoneVertex::Weights, FName("Weights"), {}), + refl::StaticMemberField(&BoneVertex::BoneIDs, FName("BoneIDs"), {}), + }; + }; + + consteval static int Size() { + return refl::fetch_meta_size(); + }; + }; + + struct BoneVertex_Meta : public refl::Meta { + using MyStatic = BoneVertex_Static_Meta; + using MyUClass = refl::UClass_Meta;; + inline static char s_data[MyStatic::Size()]{}; + static auto __MakeFields() { + char* memory = &s_data[0]; + return std::array{ + MemberField(&BoneVertex::Position, FName("Position"), memory, {}), + MemberField(&BoneVertex::TexCoords, FName("TexCoords"), memory, {}), + MemberField(&BoneVertex::Normal, FName("Normal"), memory, {}), + MemberField(&BoneVertex::Tangent, FName("Tangent"), memory, {}), + MemberField(&BoneVertex::Weights, FName("Weights"), memory, {}), + MemberField(&BoneVertex::BoneIDs, FName("BoneIDs"), memory, {}), + }; + }; + }; + namespace hello { + struct TestHello_Static_Meta { + consteval static auto __StaticFields() { + return std::make_tuple(&TestHello::x, &TestHello::z); + }; + + consteval static auto __MakeStaticFields() { + return std::array{ + refl::StaticMemberField(&TestHello::x, FName("x"), {}), + refl::StaticMemberField(&TestHello::z, FName("z"), {}), + }; + }; + + consteval static int Size() { + return refl::fetch_meta_size(); + }; + }; + + struct TestHello_Meta : public refl::Meta { + using MyStatic = TestHello_Static_Meta; + using MyUClass = refl::UClass_Meta;; + inline static char s_data[MyStatic::Size()]{}; + static auto __MakeFields() { + char* memory = &s_data[0]; + return std::array{ + MemberField(&TestHello::x, FName("x"), memory, {}), + MemberField(&TestHello::z, FName("z"), memory, {}), + }; + }; + }; + } +} diff --git a/src/refl/cpp/uimeta_vertex_gen.inl b/src/refl/cpp/uimeta_vertex_gen.inl index f7591f6..8ff9741 100644 --- a/src/refl/cpp/uimeta_vertex_gen.inl +++ b/src/refl/cpp/uimeta_vertex_gen.inl @@ -1,28 +1,32 @@ -struct vec3_Static_UIMeta { - consteval static auto __StaticFields() { - return std::make_tuple(&vec3::x); - }; +#pragma once +namespace engineapi { + namespace hello { + struct TestHello_Static_UIMeta { + consteval static auto __StaticFields() { + return std::make_tuple(&TestHello::y); + }; - consteval static auto __MakeStaticFields() { - return std::array{ - StaticMemberField(&vec3::x, FName("x"), {1.f}), - }; - }; + consteval static auto __MakeStaticFields() { + return std::array{ + refl::StaticMemberField(&TestHello::y, FName("y"), {}), + }; + }; - consteval static int Size() { - return fetch_meta_size(); - }; -}; - -struct vec3_UIMeta : public Meta { - using MyStatic = vec3_Static_UIMeta; - using MyUClass = UClass_Meta;; - inline static char s_data[MyStatic::Size()]{}; - static auto __MakeFields() { - char* memory = &s_data[0]; - return std::array{ - StaticMemberField(&vec3::x, FName("x"), {1.f}), - }; - }; -}; + consteval static int Size() { + return refl::fetch_meta_size(); + }; + }; + struct TestHello_UIMeta : public refl::Meta { + using MyStatic = TestHello_Static_UIMeta; + using MyUClass = refl::UClass_Meta;; + inline static char s_data[MyStatic::Size()]{}; + static auto __MakeFields() { + char* memory = &s_data[0]; + return std::array{ + MemberField(&TestHello::y, FName("y"), memory, {}), + }; + }; + }; + } +} diff --git a/src/refl/cpp/vertex.h b/src/refl/cpp/vertex.h index d62237d..e33f1b2 100644 --- a/src/refl/cpp/vertex.h +++ b/src/refl/cpp/vertex.h @@ -1,45 +1,75 @@ +#pragma once #include -using namespace std; +#define MAX_NUM_BONES_PER_VERTEX 4 +struct TestMain1 { + __cppast(Meta = {}) + float x; + __cppast(Meta = {}) + float y; + __cppast(Meta = {}) + float z; +}; +namespace engineapi { + namespace hello { + struct TestHello{ + __cppast(Meta = {}) + float x; + __cppast(UIMeta = {}) + float y; + __cppast(Meta = {}) + float z; + }; + } + struct Vector3 { + __cppast(Meta = {}) + float x; + __cppast(Meta = {}) + float y; + __cppast(Meta = {}) + float z; + }; + struct Vector2 { + __cppast(Meta = { 1.0 }) + float x; + __cppast(Meta = { 2.0 }) + float y; + }; + struct Vertex { - struct vec3_parent { - virtual int norm(int x1, int& x2) { - x2 = x1 * x2; - return x2; - //cout << x2 << "vec3_parent::norm" << endl; - } - }; - struct vec3_Meta; - struct vec3 : public vec3_parent { - using MyMeta = vec3_Meta; - __cppast(Meta = { 0.f}, UIMeta = {1.f}) - float x = 1; - __cppast(Meta = { 0.f}) - float y = 2; - __cppast(Meta = { 0.f}) - float z = 3; - string name{ "hellohellohellohellohellohello" }; - __cppast(Meta = { {3,4} }) - int norm(int x1, int& x2)override { - int tmp = x1 * 2 + 1; - x1 = x2; - x2 = tmp; - return x2; - //cout << x2 << "vec3::norm" << endl; - } - virtual float norm1(int& x1) { - x1 = x1 * x * y * z; - //x = x1; - //y = x1 - 1; - //z = x1 - 10; - //cout << x1 << "::norm1" << endl; - return x1; - } - static void norm2(int x1 = 10) { - cout << x1 << "::norm2" << endl; - } - static void norm3(int x1 = 10) { - x1 = x1 * 10; - cout << x1 << "::norm3" << endl; - } - }; -#include "meta.vertex.inl" \ No newline at end of file + }; + struct PosVertex_Meta; + struct PosVertex : public Vertex { + using MyMeta = PosVertex_Meta; + __cppast(Meta = {}) + Vector3 Position = {}; + }; + struct TexVertex_Meta; + struct TexVertex : public Vertex { + using MyMeta = TexVertex_Meta; + __cppast(Meta = {}) + Vector3 Position = {}; + __cppast(Meta = {}) + Vector3 Normal = {}; + __cppast(Meta = {}) + Vector2 TexCoords = {}; + }; + struct BoneVertex_Meta; + struct BoneVertex : public Vertex + { + using MyMeta = BoneVertex_Meta; + __cppast(Meta = {}) + Vector3 Position = {}; + __cppast(Meta = {}) + Vector2 TexCoords = {}; + __cppast(Meta = {}) + Vector3 Normal = {}; + __cppast(Meta = {}) + Vector3 Tangent = {}; + // 骨骼蒙皮数据 + __cppast(Meta = {}) + float Weights[MAX_NUM_BONES_PER_VERTEX] = {}; + __cppast(Meta = {}) + uint32_t BoneIDs[MAX_NUM_BONES_PER_VERTEX] = {}; + }; +}; +#include "meta_vertex_gen.inl" \ No newline at end of file diff --git a/src/refl/cpp/vertex_gen.inl b/src/refl/cpp/vertex_gen.inl index 237cd3d..5c2c35f 100644 --- a/src/refl/cpp/vertex_gen.inl +++ b/src/refl/cpp/vertex_gen.inl @@ -1,2 +1,3 @@ +#pragma once #include "meta_vertex_gen.inl" #include "uimeta_vertex_gen.inl" diff --git a/src/refl/template/refl.liquid b/src/refl/template/refl.liquid index bf85356..13be47c 100644 --- a/src/refl/template/refl.liquid +++ b/src/refl/template/refl.liquid @@ -1,48 +1,41 @@ -{%- assign indent = Indent -%} -{%- if NameSpace != blank -%} - {{ indent }}namespace {{ NameSpace }} { - {% capture newIndent %}{{ indent }} {% endcapture %} - {%- assign indent = newIndent -%} -{%- endif -%} -{{ indent }}struct {{ Name }}_Static_{{MetaName}} { -{{ indent }} consteval static auto __StaticFields() { -{{ indent }} return std::make_tuple({% for field in FieldList %}&{{Name}}::{{field.Name}}{% unless forloop.last %}, {% endunless %}{% endfor %}); -{{ indent }} }; - -{{ indent }} consteval static auto __MakeStaticFields() { -{{ indent }} return std::array{ -{{ indent }} {%- for field in FieldList -%} -{{ indent }} {%- if field.IsMethod -%} -{{ indent }} StaticMethodField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), -{{ indent }} {%- else -%} -{{ indent }} StaticMemberField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), -{{ indent }} {%- endif -%} -{{ indent }} {%- endfor -%} -{{ indent }} }; -{{ indent }} }; - -{{ indent }} consteval static int Size() { -{{ indent }} return fetch_meta_size<{{ Name }}_Static_{{MetaName}}>(); -{{ indent }} }; -{{ indent }}}; - -{{ indent }}struct {{ Name }}_{{MetaName}} : public Meta { -{{ indent }} using MyStatic = {{ Name }}_Static_{{MetaName}}; -{{ indent }} using MyUClass = UClass_Meta<{{ Name }}, {{ ParentName }}>;; -{{ indent }} inline static char s_data[MyStatic::Size()]{}; -{{ indent }} static auto __MakeFields() { -{{ indent }} char* memory = &s_data[0]; -{{ indent }} return std::array{ -{{ indent }} {%- for field in FieldList -%} -{{ indent }} {%- if field.IsMethod -%} -{{ indent }} StaticMethodField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), -{{ indent }} {%- else -%} -{{ indent }} StaticMemberField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), -{{ indent }} {%- endif -%} -{{ indent }} {%- endfor -%} -{{ indent }} }; -{{ indent }} }; -{{ indent }}}; -{% if NameSpace != blank %} -{{ Indent }}} -{% endif %} +{%- assign NewLine = newline_to_br -%} +{{ Indent2 }}struct {{ Name }}_Static_{{MetaName}} { +{{ Indent2 }} consteval static auto __StaticFields() { +{{ Indent2 }} return std::make_tuple({% for field in FieldList %}&{{Name}}::{{field.Name}}{% unless forloop.last %}, {% endunless %}{% endfor %}); +{{ Indent2 }} }; +{{ NewLine }} +{{ Indent2 }} consteval static auto __MakeStaticFields() { +{{ Indent2 }} return std::array{ +{{ NewLine }} {%- for field in FieldList -%} +{{ NewLine }} {%- if field.IsMethod -%} +{{ Indent2 }} refl::StaticMethodField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), +{{ NewLine }} {%- else -%} +{{ Indent2 }} refl::StaticMemberField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"){% if field.Meta %}, {{field.Meta}}{% endif %}), +{{ NewLine }} {%- endif -%} +{{ NewLine }} {%- endfor -%} +{{ Indent2 }} }; +{{ Indent2 }} }; +{{ NewLine }} +{{ Indent2 }} consteval static int Size() { +{{ Indent2 }} return refl::fetch_meta_size<{{ Name }}_Static_{{MetaName}}>(); +{{ Indent2 }} }; +{{ Indent2 }}}; +{{ NewLine }} +{{ Indent2 }}struct {{ Name }}_{{MetaName}} : public refl::Meta { +{{ Indent2 }} using MyStatic = {{ Name }}_Static_{{MetaName}}; +{{ Indent2 }} using MyUClass = refl::UClass_Meta<{{ Name }}, {{ ParentName }}>;; +{{ Indent2 }} inline static char s_data[MyStatic::Size()]{}; +{{ Indent2 }} static auto __MakeFields() { +{{ Indent2 }} char* memory = &s_data[0]; +{{ Indent2 }} return std::array{ +{{ NewLine }} {%- for field in FieldList -%} +{{ NewLine }} {%- if field.IsMethod -%} +{{ Indent2 }} MethodField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"), memory{% if field.Meta %}, {{field.Meta}}{% endif %}), +{{ NewLine }} {%- else -%} +{{ Indent2 }} MemberField(&{{Name}}::{{field.Name}}, FName("{{field.Name}}"), memory{% if field.Meta %}, {{field.Meta}}{% endif %}), +{{ NewLine }} {%- endif -%} +{{ NewLine }} {%- endfor -%} +{{ Indent2 }} }; +{{ Indent2 }} }; +{{ Indent2 }}}; +{{ NewLine }} \ No newline at end of file