From 0a5cb27b272b497d5d15027162171bb077576c99 Mon Sep 17 00:00:00 2001 From: ouczbs Date: Wed, 24 Jul 2024 14:42:14 +0800 Subject: [PATCH] support yyjson --- .../engine/core/include/archive/json.h | 53 +++++++++++ .../engine/core/include/archive/json/serde.h | 60 ++++++++++-- .../core/include/archive/json/serde.inl | 26 +++++ .../core/include/archive/json/serialize.h | 95 ++++--------------- .../core/include/archive/json/serialize.inl | 65 +++++++++++++ .../engine/core/include/archive/register.inl | 11 +++ .../modules/engine/zlib/include/meta/result.h | 46 +++++++++ .../engine/zlib/include/meta/result.inl | 54 +++++++++++ .../engine/zlib/include/pmr/frame_allocator.h | 4 +- engine/modules/engine/zlib/include/pmr/name.h | 4 +- .../modules/engine/zlib/include/pmr/name.inl | 2 +- .../engine/zlib/include/refl/detail/any.h | 26 ++--- .../engine/zlib/include/refl/detail/name.h | 2 + .../engine/zlib/include/refl/detail/name.inl | 8 +- .../engine/zlib/include/refl/detail/uclass.h | 4 +- .../zlib/include/refl/detail/uclass.inl | 31 +++--- engine/modules/engine/zlib/xmake.lua | 2 +- engine/tools/make_plugin/src/main.cpp | 39 ++++++-- game/zworld/editor/main.cpp | 2 +- 19 files changed, 399 insertions(+), 135 deletions(-) create mode 100644 engine/modules/engine/core/include/archive/json.h create mode 100644 engine/modules/engine/core/include/archive/json/serde.inl create mode 100644 engine/modules/engine/core/include/archive/json/serialize.inl create mode 100644 engine/modules/engine/core/include/archive/register.inl create mode 100644 engine/modules/engine/zlib/include/meta/result.h create mode 100644 engine/modules/engine/zlib/include/meta/result.inl diff --git a/engine/modules/engine/core/include/archive/json.h b/engine/modules/engine/core/include/archive/json.h new file mode 100644 index 0000000..8f611e5 --- /dev/null +++ b/engine/modules/engine/core/include/archive/json.h @@ -0,0 +1,53 @@ +#pragma once +#include "meta/result.h" +#include "json/serialize.inl" +#include "json/serde.inl" +namespace api { + using meta::result; + enum class SerializeError : char + { + EMPTY, + ERROR, + }; + template + inline bool JsonDeserialize(string_view text, T* obj) { + yyjson_alc alc = JsonAllocatorAdapter(); + yyjson_doc* doc = yyjson_read_opts((char*)text.data(), text.size(), YYJSON_READ_INSITU, &alc, nullptr); + yyjson_val* root = yyjson_doc_get_root(doc); + if constexpr (has_json_specialization_v) { + return JsonSerde::Read(root, obj); + } + else { + return JsonArchive::Deserialize(root, obj); + } + } + template + inline result JsonDeserialize(string_view text, Args&& ...args) { + if (text.empty()) { + return SerializeError::EMPTY; + } + T* obj = new(FramePool)T(std::forward(args)...); + bool bsuccess = JsonDeserialize(text, obj); + using ResultType = result; + return bsuccess ? ResultType{ *obj } : ResultType{ SerializeError::ERROR }; + } + template + inline string_view JsonSerialize(const T& t) { + yyjson_alc alc = JsonAllocatorAdapter(); + yyjson_mut_doc* doc = yyjson_mut_doc_new(&alc); + yyjson_mut_val* root = JsonSerde::Write(doc, Any{t}); + yyjson_mut_doc_set_root(doc, root); + size_t len; + const char* json_str = yyjson_mut_write_opts(doc, 0, &alc, &len, NULL); + return json_str ? string_view(json_str, len) : string_view(""); + } + inline string_view JsonSerialize(Any any) { + yyjson_alc alc = JsonAllocatorAdapter(); + yyjson_mut_doc* doc = yyjson_mut_doc_new(&alc); + yyjson_mut_val* root = JsonArchive::Serialize(doc, any); + yyjson_mut_doc_set_root(doc, root); + size_t len; + const char* json_str = yyjson_mut_write_opts(doc, 0, &alc, &len, NULL); + return json_str ? string_view(json_str, len) : string_view(""); + } +} \ No newline at end of file diff --git a/engine/modules/engine/core/include/archive/json/serde.h b/engine/modules/engine/core/include/archive/json/serde.h index f900ba3..a3f5012 100644 --- a/engine/modules/engine/core/include/archive/json/serde.h +++ b/engine/modules/engine/core/include/archive/json/serde.h @@ -1,14 +1,58 @@ #pragma once -#include "serialize.h" +#include "refl/pch.h" +#include +#include namespace api { - struct JsonSerialize { - - }; + namespace archive { + template + concept is_string_v = requires(T t) { + { static_cast(t) } -> std::convertible_to; + }; + } + using std::string_view; + using refl::Any; + using refl::UClass; + using refl::type_name; + using refl::TypeInfo; template struct JsonSerde { - inline static bool read(yyjson_val* val, T& v) - { - return r.read(v); - } + inline static bool Read(yyjson_val* val, Any any) { + if constexpr (std::is_same_v) { + *any.CastTo() = (T)yyjson_get_bool(val); + } + else if constexpr (std::is_integral_v) { + *any.CastTo() = (T)yyjson_get_uint(val); + } + else if constexpr (std::is_floating_point_v) { + *any.CastTo() = (T)yyjson_get_real(val); + } + else if constexpr (archive::is_string_v) { + *any.CastTo() = yyjson_get_str(val); + } + else { + throw std::runtime_error(std::format("unknown json read type {}", type_name().View())); + return false; + } + return true; + } + inline static yyjson_mut_val* Write(yyjson_mut_doc* doc, Any any) { + if constexpr (std::is_same_v) { + return yyjson_mut_bool(doc, *any.CastTo()); + } + else if constexpr (std::is_integral_v) { + return yyjson_mut_uint(doc, *any.CastTo()); + } + else if constexpr (std::is_floating_point_v) { + return yyjson_mut_real(doc, *any.CastTo()); + } + else if constexpr (archive::is_string_v) { + auto c = (any.CastTo())->data(); + return yyjson_mut_str(doc, ""); + } + else { + throw std::runtime_error(std::format("unknown json write type {}", type_name().View())); + return {}; + } + } }; } \ No newline at end of file diff --git a/engine/modules/engine/core/include/archive/json/serde.inl b/engine/modules/engine/core/include/archive/json/serde.inl new file mode 100644 index 0000000..6eccbe4 --- /dev/null +++ b/engine/modules/engine/core/include/archive/json/serde.inl @@ -0,0 +1,26 @@ +#include "serde.h" +namespace api { + namespace detail { + // 辅助结构体,用于检查特化版本 + template + struct has_json_specialization : std::false_type {}; + // 特化辅助结构体,匹配特化版本 + template + struct has_json_specialization::Read) >> : std::true_type {}; + } + template + concept has_json_specialization_v = detail::has_json_specialization::value; + +#ifdef API_DEBUG + template + inline bool JsonRead(yyjson_val* node, const T& t) { + return JsonSerde::Read(node, t); + } + template + inline yyjson_mut_val* JsonWrite(yyjson_mut_doc* doc, const T& t) { + return JsonSerde::Write(doc, t); + } +#else +#define JsonRead(node, t) JsonSerde::Read(node, t) +#endif +} \ No newline at end of file diff --git a/engine/modules/engine/core/include/archive/json/serialize.h b/engine/modules/engine/core/include/archive/json/serialize.h index 57c9638..a993f6c 100644 --- a/engine/modules/engine/core/include/archive/json/serialize.h +++ b/engine/modules/engine/core/include/archive/json/serialize.h @@ -1,85 +1,24 @@ #pragma once -#include "zlog.h" #include "yyjson.h" -#include -#include +#include "serde.h" namespace api { - using std::string_view; - struct JsonReader { - struct Level { - enum EType : uint32_t { - EObjectIndex = -1, - EArrayIndex = 0, - }; - yyjson_val* val = nullptr; - uint32_t index = EObjectIndex; - constexpr Level(yyjson_val* val, EType type = EObjectIndex) noexcept - : val(val),index(type){} - constexpr bool is_object()const { - return index == EObjectIndex; - } - constexpr bool is_array()const { - return index != EObjectIndex; - } - }; - yyjson_doc* doc; - std::vector stack; - ~JsonReader() { - if (doc) { - yyjson_doc_free(doc); - } - } - JsonReader(string_view json) { - yyjson_read_err err = {}; - doc = yyjson_read_opts((char*)json.data(), json.size(), 0, nullptr, &err); - if (doc == nullptr) { - zlog::error("Failed to parse JSON: {}, error: {}", json.data(), err.msg); - } - } - yyjson_val* find(string_view name) { - auto back = stack.back(); - if (back.is_object()) { - return yyjson_obj_get(back.val, name.data()); - } - return yyjson_arr_get(back.val, back.index++); - } - bool start_object(string_view name = "") { - yyjson_val* root = stack.empty() ? yyjson_doc_get_root(doc) : find(name); - stack.emplace_back(root, Level::EObjectIndex); - } - void end_object() { - stack.pop_back(); - } - bool start_array(string_view name = "") { - yyjson_val* root = stack.empty() ? yyjson_doc_get_root(doc) : find(name); - stack.emplace_back(root, Level::EArrayIndex); - } - void end_array() { - stack.pop_back(); - } + struct JsonVTable { + bool(*Read)(yyjson_val*, Any) = nullptr; + yyjson_mut_val*(*Write)(yyjson_mut_doc*, Any) = nullptr; template - bool read(T& v, const string_view& name = "") { - template - concept is_string_v = requires(T t) { - { static_cast(t) } -> std::convertible_to; - }; - yyjson_val* parent = find(name); - if constexpr (std::is_same_v) { - v = (T)yyjson_get_bool(parent); - } - else if constexpr (std::is_integral_v) { - v = (T)yyjson_get_uint(parent); - } - else if constexpr (std::is_floating_point_v) { - v = (T)yyjson_get_real(parent); - } - else if constexpr (is_string_v){ - v = yyjson_get_str(parent); - } - else { - return false; - } - return true; + static JsonVTable Make() { + return { &JsonSerde::Read, &JsonSerde::Write }; } }; + using JsonFuncTable = std::pmr::unordered_map; + struct JsonArchive { + private: + static JsonFuncTable BuildFuncTable(); + inline static JsonFuncTable FuncTable = BuildFuncTable(); + public: + template + static void Register(); + static yyjson_mut_val* Serialize(yyjson_mut_doc* doc, Any any); + static bool Deserialize(yyjson_val* res, Any any); + }; } \ No newline at end of file diff --git a/engine/modules/engine/core/include/archive/json/serialize.inl b/engine/modules/engine/core/include/archive/json/serialize.inl new file mode 100644 index 0000000..9d3a12a --- /dev/null +++ b/engine/modules/engine/core/include/archive/json/serialize.inl @@ -0,0 +1,65 @@ +#include "serialize.h" +#include "serde.h" +namespace api { + // 定义 yyjson_alc 适配器类 + yyjson_alc JsonAllocatorAdapter(std::pmr::memory_resource* mr = &FramePool) { + // 初始化 yyjson_alc 结构体 + yyjson_alc alc; + alc.malloc = [](void* ctx, size_t size) -> void* { + auto* adapter = static_cast(ctx); + return adapter->allocate(size); + }; + alc.realloc = [](void* ctx, void* ptr, size_t old_size, size_t size) -> void* { + auto* adapter = static_cast(ctx); + // std::pmr::memory_resource 不支持直接重新分配 + // 因此,先分配新的内存,再拷贝数据 + void* new_ptr = adapter->allocate(size); + if (ptr) { + std::memcpy(new_ptr, ptr, std::min(old_size, size)); + adapter->deallocate(ptr, old_size); + } + return new_ptr; + }; + alc.free = [](void* ctx, void* ptr) { + auto* adapter = static_cast(ctx); + adapter->deallocate(ptr, 0); + }; + alc.ctx = mr; + return alc; + } + inline JsonFuncTable JsonArchive::BuildFuncTable() + { + JsonFuncTable funcTable{ &MemPool }; +#define RegisterAny(T) funcTable.emplace(&TypeInfo::StaticClass,JsonVTable::Make()) +#include "../register.inl" +#undef RegisterAny + return funcTable; + } + template + inline void JsonArchive::Register() + { + FuncTable.emplace(&TypeInfo::StaticClass, JsonVTable::Make()); + } + inline yyjson_mut_val* JsonArchive::Serialize(yyjson_mut_doc* doc, Any any) + { + if (!any) { + return {}; + } + auto it = FuncTable.find(any.cls); + if (it != FuncTable.end()) { + return it->second.Write(doc, any); + } + return {}; + } + inline bool JsonArchive::Deserialize(yyjson_val* res, Any any) + { + if (!any) { + return false; + } + auto it = FuncTable.find(any.cls); + if (it != FuncTable.end()) { + return it->second.Read(res, any); + } + return false; + } +} \ No newline at end of file diff --git a/engine/modules/engine/core/include/archive/register.inl b/engine/modules/engine/core/include/archive/register.inl new file mode 100644 index 0000000..d332339 --- /dev/null +++ b/engine/modules/engine/core/include/archive/register.inl @@ -0,0 +1,11 @@ +#ifdef RegisterAny +RegisterAny(string_view); +RegisterAny(int); +RegisterAny(unsigned int); +RegisterAny(short); +RegisterAny(unsigned short); +RegisterAny(char); +RegisterAny(unsigned char); +RegisterAny(float); +RegisterAny(double); +#endif // RegisterAny \ No newline at end of file diff --git a/engine/modules/engine/zlib/include/meta/result.h b/engine/modules/engine/zlib/include/meta/result.h new file mode 100644 index 0000000..08bf78e --- /dev/null +++ b/engine/modules/engine/zlib/include/meta/result.h @@ -0,0 +1,46 @@ +#pragma once +#include +#include + +namespace meta +{ + // a struct stolen from rust + // lets us define a result, or return an error otherwise + template + class result + : protected std::variant + { + protected: + using Base = std::variant; + private: + static constexpr auto Success = 0; + static constexpr auto Failure = 1; + static_assert(std::is_default_constructible_v, "Error must be default constructible"); + public: + using value_type = Result; + using Base::Base; + using Base::operator=; + + // accessors + Result& value(); + template>> + Result value_or(U&& result) const; + Error& error(); + const Result& value() const; + const Error& error() const; + + // monadic operators + template auto map(Fn&& visitor) const; + template auto and_then(Fn&& visitor) const; + template auto and_then(Fn&& visitor, ErrFn&& err_visitor) const; + template auto or_else(ErrFunc&& err_visitor) const; + + // operator overloads + explicit operator bool() const; + Result& operator*(); + Result* operator->(); + const Result& operator*() const; + const Result* operator->() const; + }; +} +#include "result.inl" \ No newline at end of file diff --git a/engine/modules/engine/zlib/include/meta/result.inl b/engine/modules/engine/zlib/include/meta/result.inl new file mode 100644 index 0000000..24e7a56 --- /dev/null +++ b/engine/modules/engine/zlib/include/meta/result.inl @@ -0,0 +1,54 @@ +namespace meta +{ + template + inline Result& result::value() + { + return std::get(*this); + } + template + template + Result result::value_or(U&& result) const + { + return bool(*this) ? **this : static_cast(std::forward(result)); + } + template + inline Error& result::error() + { + return std::get(*this); + } + template + inline const Result& result::value() const + { + return std::get(*this); + } + template + inline const Error& result::error() const + { + return std::get(*this); + } + template + inline result::operator bool() const + { + return Base::index() == Success; + } + template + inline Result& result::operator*() + { + return value(); + } + template + inline Result* result::operator->() + { + return &operator*(); + } + template + const Result& result::operator*() const + { + return value(); + } + template + const Result* result::operator->() const + { + return &operator*(); + } +} \ No newline at end of file diff --git a/engine/modules/engine/zlib/include/pmr/frame_allocator.h b/engine/modules/engine/zlib/include/pmr/frame_allocator.h index c03e005..8d799b3 100644 --- a/engine/modules/engine/zlib/include/pmr/frame_allocator.h +++ b/engine/modules/engine/zlib/include/pmr/frame_allocator.h @@ -44,4 +44,6 @@ inline void* operator new(size_t size, pmr::FrameAllocatorPool& pool, size_t ali size = (size + alignment - 1) & ~(alignment - 1); return pool.allocate(size, alignment); } -#include "frame_allocator.inl" \ No newline at end of file +#include "frame_allocator.inl" +inline static pmr::FrameAllocatorPool MemPool{}; +inline static pmr::FrameAllocatorPool FramePool{}; \ No newline at end of file diff --git a/engine/modules/engine/zlib/include/pmr/name.h b/engine/modules/engine/zlib/include/pmr/name.h index 0a5b4b9..1eb180a 100644 --- a/engine/modules/engine/zlib/include/pmr/name.h +++ b/engine/modules/engine/zlib/include/pmr/name.h @@ -31,9 +31,9 @@ namespace pmr }; struct Name { size_t hash; -#ifdef Z_DEBUG +#ifdef API_DEBUG std::string_view value; -#endif // Z_DEBUG +#endif // API_DEBUG public: Name():hash(NameID::InvalidValue()) {}; Name(const char* str)noexcept; diff --git a/engine/modules/engine/zlib/include/pmr/name.inl b/engine/modules/engine/zlib/include/pmr/name.inl index 200debc..22f9a41 100644 --- a/engine/modules/engine/zlib/include/pmr/name.inl +++ b/engine/modules/engine/zlib/include/pmr/name.inl @@ -40,7 +40,7 @@ namespace pmr { { return NameTable::Find(hash); } -#ifdef Z_DEBUG +#ifdef API_DEBUG #define MAKE_NAME_PAIR(hash, str) value = NameTable::MakePair(hash, str) #define NAME_TO_STRING value #else diff --git a/engine/modules/engine/zlib/include/refl/detail/any.h b/engine/modules/engine/zlib/include/refl/detail/any.h index 19bcf1d..2960111 100644 --- a/engine/modules/engine/zlib/include/refl/detail/any.h +++ b/engine/modules/engine/zlib/include/refl/detail/any.h @@ -2,27 +2,20 @@ #include "type.h" namespace refl { class UClass; + struct Any; + template + concept is_not_any_v = !std::is_same_v, Any>; struct Any { public: const void* ptr; const UClass* cls; public: - constexpr Any() : ptr(nullptr), cls(nullptr) {} - constexpr Any(const void* ptr, const UClass* cls) : ptr(ptr), cls(cls) {} - template - constexpr Any(T&& v) : ptr(&v), cls(TypeInfo>::StaticClass) { - if constexpr (std::is_same_v, Any>) { - ptr = v.ptr; - cls = v.cls; - } - } - template - constexpr Any(T* v) : ptr(v), cls(&TypeInfo>::StaticClass) { - if constexpr (std::is_same_v, Any>) { - ptr = v->ptr; - cls = v->cls; - } - } + constexpr Any() noexcept: ptr(nullptr), cls(nullptr) {} + constexpr Any(const void* ptr, const UClass* cls) noexcept : ptr(ptr), cls(cls) {} + template + constexpr Any(T&& v) noexcept : ptr(&v), cls(&TypeInfo>::StaticClass) {} + template + constexpr Any(T* v) noexcept : ptr(v), cls(&TypeInfo>::StaticClass) {} template//参数 T* => T* constexpr inline T CastTo() const { if constexpr (std::is_pointer_v) { @@ -37,6 +30,7 @@ namespace refl { } } public: + operator bool()const { return cls && ptr; } bool Check(const UClass* parent) const; }; } \ No newline at end of file diff --git a/engine/modules/engine/zlib/include/refl/detail/name.h b/engine/modules/engine/zlib/include/refl/detail/name.h index 6c1c778..8e5fa4f 100644 --- a/engine/modules/engine/zlib/include/refl/detail/name.h +++ b/engine/modules/engine/zlib/include/refl/detail/name.h @@ -1,4 +1,6 @@ #pragma once +#include +#include namespace refl { template struct TStr { diff --git a/engine/modules/engine/zlib/include/refl/detail/name.inl b/engine/modules/engine/zlib/include/refl/detail/name.inl index fafd2ee..e78046f 100644 --- a/engine/modules/engine/zlib/include/refl/detail/name.inl +++ b/engine/modules/engine/zlib/include/refl/detail/name.inl @@ -79,13 +79,17 @@ namespace refl { } template constexpr auto type_name() noexcept { - if constexpr (std::is_arithmetic_v) { + if constexpr (std::is_same_v) { + return TStr{ "void" }; + } + else if constexpr (std::is_arithmetic_v) { constexpr auto prefix = detail::num_prefix_name(); constexpr auto bit = detail::num_name<8 * sizeof(T)>(); return detail::concat(prefix, bit); } else { - static_assert("not support"); + //static_assert("not support"); + return TStr{ "unknown" }; } } } \ No newline at end of file diff --git a/engine/modules/engine/zlib/include/refl/detail/uclass.h b/engine/modules/engine/zlib/include/refl/detail/uclass.h index d7e897b..f1c22f2 100644 --- a/engine/modules/engine/zlib/include/refl/detail/uclass.h +++ b/engine/modules/engine/zlib/include/refl/detail/uclass.h @@ -44,6 +44,8 @@ namespace refl { uint32_t flag{0}; const UClass* parent; vtable_uclass* vtable{nullptr}; + UClass(const UClass*) = delete; + UClass& operator=(const UClass*) = delete; public: UClass(std::string_view name, uint32_t size, const UClass* parent = nullptr) :name(name), size(size), parent(parent) {} @@ -59,7 +61,7 @@ namespace refl { } template bool IsChildOf(bool bthis = false) const { - return IsChildOf(TypeInfo::StaticClass, bthis); + return IsChildOf(&TypeInfo::StaticClass, bthis); } public: template diff --git a/engine/modules/engine/zlib/include/refl/detail/uclass.inl b/engine/modules/engine/zlib/include/refl/detail/uclass.inl index 53bd87c..9907b62 100644 --- a/engine/modules/engine/zlib/include/refl/detail/uclass.inl +++ b/engine/modules/engine/zlib/include/refl/detail/uclass.inl @@ -1,44 +1,41 @@ #include "uclass.h" #include "name.h" namespace refl{ - namespace detail { - inline static pmr::FrameAllocatorPool MemPool{}; - } template concept is_metas_v = false;//requires(const Name & name) { MetaImpl::MyMetas::GetMeta(name); }; template class UClass_Auto : public UClass { - using UClass::UClass; - using MyUClass = UClass_Auto; public: - static MyUClass* BuildClass() { - MyUClass* cls = new(detail::MemPool)MyUClass(type_name().View(), sizeof(T)); + UClass_Auto() : UClass(type_name().View(), sizeof(T)) { if constexpr (std::is_pointer_v) { using RT = std::remove_pointer_t; - cls->flag |= CLASS_POINTER_FLAG; + flag |= CLASS_POINTER_FLAG; if constexpr (!std::is_same_v) { - cls->parent = TypeInfo::StaticClass; + parent = &TypeInfo::StaticClass; } } else if constexpr (is_array_v) { using RT = is_array_t; - cls->flag = CLASS_ARRAY_FLAG; + flag = CLASS_ARRAY_FLAG; if constexpr (std::is_pointer_v) { - cls->flag |= CLASS_POINTER_FLAG; + flag |= CLASS_POINTER_FLAG; } - cls->parent = TypeInfo::StaticClass; + parent = &TypeInfo::StaticClass; } else { - cls->vtable = new(detail::MemPool)vtable_uclass(); - cls->vtable->Construct = &UClass::Construct; - cls->vtable->Destruct = &UClass::Destruct; + vtable = new(MemPool)vtable_uclass(); + vtable->Construct = &UClass::Construct; + vtable->Destruct = &UClass::Destruct; } - return cls; } }; + template<> + struct TypeInfoImpl { + inline static UClass StaticClass{ type_name().View(), 0 }; + }; template struct TypeInfoImpl { using MyUClass = UClass_Auto; - inline static MyUClass* StaticClass = MyUClass::BuildClass(); + inline static MyUClass StaticClass{}; }; } \ No newline at end of file diff --git a/engine/modules/engine/zlib/xmake.lua b/engine/modules/engine/zlib/xmake.lua index 8306ddd..28e61e5 100644 --- a/engine/modules/engine/zlib/xmake.lua +++ b/engine/modules/engine/zlib/xmake.lua @@ -2,6 +2,6 @@ header_component("zlib","engine") set_basename("myzlib") add_headerfiles("include/**.h", "include/**.inl") if is_mode("debug") then - add_defines("Z_DEBUG", {public = true}) + add_defines("API_DEBUG", {public = true}) end set_pcheader("include/refl/pch.h") \ No newline at end of file diff --git a/engine/tools/make_plugin/src/main.cpp b/engine/tools/make_plugin/src/main.cpp index 1514124..d42e072 100644 --- a/engine/tools/make_plugin/src/main.cpp +++ b/engine/tools/make_plugin/src/main.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "yyjson.h" +#include "archive/json.h" namespace pmr { using std::pmr::monotonic_buffer_resource; using std::pmr::vector; @@ -134,13 +134,38 @@ void genLua(const char* file_path, const pmr::vector& mdList) { oss << '}'; writeFile(file_path, oss.str()); } +namespace api { + struct Guid { + int a; + float b; + }; + template<> + struct JsonSerde { + inline static bool Read(yyjson_val* node, Any any) { + Guid& v = *any.CastTo(); + JsonRead(yyjson_obj_get(node, "a"), v.a); + JsonRead(yyjson_obj_get(node, "b"), v.b); + return true; + } + inline static yyjson_mut_val* Write(yyjson_mut_doc* doc, Any any) { + Guid& v = *any.CastTo(); + auto obj = yyjson_mut_obj(doc); + yyjson_mut_obj_add_val(doc, obj, "a", JsonWrite(doc, v.a)); + yyjson_mut_obj_add_val(doc, obj, "b", JsonWrite(doc, v.b)); + return obj; + } + }; +} void genPlugin(const char* file_path, const pmr::vector& mdList) { - yyjson_read_err err = {}; - std::pmr::string json{"{a=1}"}; - yyjson_doc* _document = yyjson_read_opts( - (char*)json.data(), json.size(), - 0, nullptr, &err); - auto obj = yyjson_doc_get_root((yyjson_doc*)_document); + api::Guid guid; + guid.a = 1243; + std::string_view data = api::JsonSerialize(guid); + auto res = api::JsonDeserialize(data); + if (res) { + api::Guid g2 = res.value(); + int a = g2.a; + int b = g2.b; + } } int main() { const char* file_path = R"(F:\engine\zengine\engine\modules\render\vulkan\include\vulkan\module.h)"; diff --git a/game/zworld/editor/main.cpp b/game/zworld/editor/main.cpp index 7f69d63..a9b509c 100644 --- a/game/zworld/editor/main.cpp +++ b/game/zworld/editor/main.cpp @@ -17,7 +17,7 @@ int main() { constexpr TStr str3 = detail::concat(str1, str2); constexpr auto r1 = value_name<8 * sizeof(int)>(); constexpr int v = 12; - auto cls = refl::TypeInfo::StaticClass; + auto cls = &refl::TypeInfo::StaticClass; //auto str4 = concat(r1, str2); auto t1 = refl::type_name(); auto v1 = refl::type_name().View();