Skip to content

Commit

Permalink
gh-80: add structured tree serizalization & remove global yaml usage
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Sep 14, 2024
1 parent 4d0d071 commit 02e3c45
Show file tree
Hide file tree
Showing 58 changed files with 901 additions and 880 deletions.
13 changes: 5 additions & 8 deletions engine/plugins/runtime/asset/default_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "default_asset_loader.hpp"

#include "io/yaml.hpp"
#include "profiler/profiler.hpp"

namespace wmoge {
Expand Down Expand Up @@ -56,17 +57,13 @@ namespace wmoge {
return StatusCode::FailedInstantiate;
}

auto asset_tree = yaml_parse_file(path_on_disk);
if (asset_tree.empty()) {
WG_LOG_ERROR("failed to read parse file " << path_on_disk);
return StatusCode::FailedParse;
};
IoContext context;
IoYamlTree asset_tree;
WG_CHECKED(asset_tree.parse_file(path_on_disk));

asset->set_name(name);

IoContext context;

if (!asset->read_from_yaml(context, asset_tree.crootref())) {
if (!asset->read_from_tree(context, asset_tree)) {
WG_LOG_ERROR("failed to load asset from file " << path_on_disk);
return StatusCode::FailedRead;
}
Expand Down
10 changes: 6 additions & 4 deletions engine/plugins/runtime/asset/shader_asset_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "grc/shader.hpp"
#include "grc/shader_file.hpp"
#include "grc/shader_manager.hpp"
#include "io/yaml.hpp"
#include "profiler/profiler.hpp"
#include "system/ioc_container.hpp"

Expand All @@ -55,10 +56,11 @@ namespace wmoge {
}

ShaderFile shader_file;
if (!yaml_read_file(path_on_disk, shader_file)) {
WG_LOG_ERROR("failed to read parse shader file " << path_on_disk);
return StatusCode::FailedParse;
}

IoContext context;
IoYamlTree tree;
WG_CHECKED(tree.parse_file(path_on_disk));
WG_TREE_READ(context, tree, shader_file);

auto* shader_manager = IocContainer::iresolve_v<ShaderManager>();

Expand Down
1 change: 0 additions & 1 deletion engine/runtime/asset/_rtti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace wmoge {
void rtti_asset() {
rtti_type<AssetImportData>();
rtti_type<AssetMetaFile>();
rtti_type<AssetMeta>();
rtti_type<AssetLoader>();
rtti_type<AssetUnloader>();
rtti_type<Asset>();
Expand Down
8 changes: 4 additions & 4 deletions engine/runtime/asset/asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@

namespace wmoge {

Status yaml_read(IoContext& context, YamlConstNodeRef node, AssetId& id) {
WG_YAML_READ(context, node, id.m_name);
Status tree_read(IoContext& context, IoPropertyTree& tree, AssetId& id) {
WG_TREE_READ(context, tree, id.m_name);
return WG_OK;
}
Status yaml_write(IoContext& context, YamlNodeRef node, const AssetId& id) {
WG_YAML_WRITE(context, node, id.m_name);
Status tree_write(IoContext& context, IoPropertyTree& tree, const AssetId& id) {
WG_TREE_WRITE(context, tree, id.m_name);
return WG_OK;
}
Status stream_read(IoContext& context, IoStream& stream, AssetId& id) {
Expand Down
4 changes: 2 additions & 2 deletions engine/runtime/asset/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ namespace wmoge {
[[nodiscard]] bool is_empty() const { return m_name.empty(); }
[[nodiscard]] std::size_t hash() const { return m_name.hash(); }

friend Status yaml_read(IoContext& context, YamlConstNodeRef node, AssetId& id);
friend Status yaml_write(IoContext& context, YamlNodeRef node, const AssetId& id);
friend Status tree_read(IoContext& context, IoPropertyTree& tree, AssetId& id);
friend Status tree_write(IoContext& context, IoPropertyTree& tree, const AssetId& id);
friend Status stream_read(IoContext& context, IoStream& stream, AssetId& id);
friend Status stream_write(IoContext& context, IoStream& stream, const AssetId& id);

Expand Down
10 changes: 0 additions & 10 deletions engine/runtime/asset/asset_meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ namespace wmoge {
* @brief Meta information of a particular asset
*/
struct AssetMeta {
WG_RTTI_STRUCT(AssetMeta);

UUID uuid = UUID();
class RttiClass* rtti = nullptr;
class AssetPak* pak = nullptr;
Expand All @@ -82,12 +80,4 @@ namespace wmoge {
Ref<AssetImportData> import_data;
};

WG_RTTI_STRUCT_BEGIN(AssetMeta) {
WG_RTTI_META_DATA();
WG_RTTI_FIELD(uuid, {RttiOptional});
WG_RTTI_FIELD(deps, {RttiOptional});
WG_RTTI_FIELD(import_data, {RttiOptional});
}
WG_RTTI_END;

}// namespace wmoge
2 changes: 1 addition & 1 deletion engine/runtime/asset/asset_pak.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "core/data.hpp"
#include "core/status.hpp"
#include "core/string_id.hpp"
#include "io/yaml.hpp"
#include "io/property_tree.hpp"

#include <filesystem>
#include <optional>
Expand Down
9 changes: 5 additions & 4 deletions engine/runtime/asset/asset_pak_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "asset/asset_manager.hpp"
#include "core/string_utils.hpp"
#include "io/property_tree.hpp"
#include "io/yaml.hpp"
#include "platform/file_system.hpp"
#include "profiler/profiler.hpp"
Expand Down Expand Up @@ -56,10 +57,10 @@ namespace wmoge {

AssetMetaFile asset_file;

if (!yaml_read_file(meta_file_path, asset_file)) {
WG_LOG_ERROR("failed to parse .asset file " << meta_file_path);
return StatusCode::FailedRead;
}
IoContext context;
IoYamlTree tree;
WG_CHECKED(tree.parse_file(meta_file_path));
WG_TREE_READ(context, tree, asset_file);

auto loader = IocContainer::iresolve_v<AssetManager>()->find_loader(asset_file.loader);
auto rtti = IocContainer::iresolve_v<RttiTypeStorage>()->find_class(asset_file.rtti);
Expand Down
18 changes: 9 additions & 9 deletions engine/runtime/asset/asset_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

#include "asset/asset.hpp"
#include "asset/asset_manager.hpp"
#include "io/property_tree.hpp"
#include "io/stream.hpp"
#include "io/yaml.hpp"

#include <cassert>
#include <optional>
Expand Down Expand Up @@ -66,9 +66,9 @@ namespace wmoge {
};

template<typename T>
Status yaml_read(IoContext& context, YamlConstNodeRef node, AssetRef<T>& ref) {
Status tree_read(IoContext& context, IoPropertyTree& tree, AssetRef<T>& ref) {
AssetId id;
WG_YAML_READ(context, node, id);
WG_TREE_READ(context, node, id);
Ref<T> ptr = context.get_asset_manager()->find(id).cast<T>();
if (!ptr) {
return StatusCode::NoAsset;
Expand All @@ -78,12 +78,12 @@ namespace wmoge {
}

template<typename T>
Status yaml_write(IoContext& context, YamlNodeRef node, const AssetRef<T>& ref) {
Status tree_write(IoContext& context, IoPropertyTree& tree, const AssetRef<T>& ref) {
assert(ref);
if (!ref) {
return StatusCode::NoAsset;
}
WG_YAML_WRITE(context, node, ref->get_id());
WG_TREE_WRITE(context, node, ref->get_id());
return WG_OK;
}

Expand All @@ -110,17 +110,17 @@ namespace wmoge {
}

template<typename T>
Status yaml_read(IoContext& context, YamlConstNodeRef node, AssetRefWeak<T>& ref) {
Status tree_read(IoContext& context, IoPropertyTree& tree, AssetRefWeak<T>& ref) {
AssetId id;
WG_YAML_READ(context, node, id);
WG_TREE_READ(context, node, id);
ref = AssetRefWeak<T>(id);
return WG_OK;
}

template<typename T>
Status yaml_write(IoContext& context, YamlNodeRef node, const AssetRefWeak<T>& ref) {
Status tree_write(IoContext& context, IoPropertyTree& tree, const AssetRefWeak<T>& ref) {
AssetId id = ref;
WG_YAML_WRITE(context, node, id);
WG_TREE_WRITE(context, node, id);
return WG_OK;
}

Expand Down
41 changes: 22 additions & 19 deletions engine/runtime/core/buffered_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#pragma once

#include "io/property_tree.hpp"
#include "io/stream.hpp"
#include "io/yaml.hpp"

#include <svector.hpp>
#include <vector>
Expand All @@ -45,15 +45,6 @@ namespace wmoge {
template<typename T, std::size_t MinCapacity = 4>
using buffered_vector = ankerl::svector<T, MinCapacity>;

template<typename T, std::size_t MinCapacity>
Status stream_write(IoContext& context, IoStream& stream, const buffered_vector<T, MinCapacity>& vector) {
WG_ARCHIVE_WRITE(context, stream, vector.size());
for (const auto& entry : vector) {
WG_ARCHIVE_WRITE(context, stream, entry);
}
return WG_OK;
}

template<typename T, std::size_t MinCapacity>
Status stream_read(IoContext& context, IoStream& stream, buffered_vector<T, MinCapacity>& vector) {
assert(vector.empty());
Expand All @@ -67,26 +58,38 @@ namespace wmoge {
}

template<typename T, std::size_t MinCapacity>
Status yaml_write(IoContext& context, YamlNodeRef node, const buffered_vector<T, MinCapacity>& vector) {
WG_YAML_SEQ(node);
for (const T& value : vector) {
YamlNodeRef child = node.append_child();
WG_YAML_WRITE(context, child, value);
Status stream_write(IoContext& context, IoStream& stream, const buffered_vector<T, MinCapacity>& vector) {
WG_ARCHIVE_WRITE(context, stream, vector.size());
for (const auto& entry : vector) {
WG_ARCHIVE_WRITE(context, stream, entry);
}
return WG_OK;
}

template<typename T, std::size_t MinCapacity>
Status yaml_read(IoContext& context, YamlConstNodeRef node, buffered_vector<T, MinCapacity>& vector) {
Status tree_read(IoContext& context, IoPropertyTree& tree, buffered_vector<T, MinCapacity>& vector) {
assert(vector.empty());
vector.resize(node.num_children());
vector.resize(tree.node_num_children());
std::size_t element_id = 0;
for (auto child = node.first_child(); child.valid(); child = child.next_sibling()) {
WG_YAML_READ(context, child, vector[element_id]);
tree.node_find_first_child();
for (; tree.node_is_valid(); tree.node_next_sibling()) {
WG_TREE_READ(context, tree, vector[element_id]);
element_id += 1;
}
return WG_OK;
}

template<typename T, std::size_t MinCapacity>
Status tree_write(IoContext& context, IoPropertyTree& tree, const buffered_vector<T, MinCapacity>& vector) {
WG_TREE_SEQ(tree, vector.size());
for (const T& value : vector) {
WG_CHECKED(tree.node_append_child());
WG_TREE_WRITE(context, tree, value);
tree.node_pop();
}
return WG_OK;
}

#endif

}// namespace wmoge
10 changes: 5 additions & 5 deletions engine/runtime/core/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ namespace wmoge {
return stream.nread(static_cast<int>(size), data->buffer());
}

Status yaml_write(IoContext& context, YamlNodeRef node, const Ref<Data>& data) {
Status tree_write(IoContext& context, IoPropertyTree& tree, const Ref<Data>& data) {
if (!data) {
node << "";
tree.node_write_value("");
return WG_OK;
}

std::string encoded;
if (Base64::encode(data, encoded)) {
return yaml_write(context, node, encoded);
return tree_write(context, tree, encoded);
}
return StatusCode::FailedWrite;
}
Status yaml_read(IoContext& context, YamlConstNodeRef node, Ref<Data>& data) {
Status tree_read(IoContext& context, IoPropertyTree& tree, Ref<Data>& data) {
std::string encoded;
if (yaml_read(context, node, encoded)) {
if (tree_read(context, tree, encoded)) {
return Base64::decode(encoded, data);
}
return StatusCode::FailedRead;
Expand Down
6 changes: 3 additions & 3 deletions engine/runtime/core/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#include "core/ref.hpp"
#include "core/sha256.hpp"
#include "core/string_utils.hpp"
#include "io/property_tree.hpp"
#include "io/stream.hpp"
#include "io/yaml.hpp"
#include "math/math_utils.hpp"

#include <cstddef>
Expand Down Expand Up @@ -76,8 +76,8 @@ namespace wmoge {
friend Status stream_write(IoContext& context, IoStream& stream, const Ref<Data>& data);
friend Status stream_read(IoContext& context, IoStream& stream, Ref<Data>& data);

friend Status yaml_write(IoContext& context, YamlNodeRef node, const Ref<Data>& data);
friend Status yaml_read(IoContext& context, YamlConstNodeRef node, Ref<Data>& data);
friend Status tree_write(IoContext& context, IoPropertyTree& tree, const Ref<Data>& data);
friend Status tree_read(IoContext& context, IoPropertyTree& tree, Ref<Data>& data);

private:
std::size_t m_size = 0;
Expand Down
8 changes: 4 additions & 4 deletions engine/runtime/core/date_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ namespace wmoge {
return t;
}

Status yaml_read(IoContext& context, YamlConstNodeRef node, DateTime& value) {
Status tree_read(IoContext& context, IoPropertyTree& tree, DateTime& value) {
std::string s;
WG_YAML_READ(context, node, s);
WG_TREE_READ(context, tree, s);
value = DateTime(s);
return WG_OK;
}
Status yaml_write(IoContext& context, YamlNodeRef node, const DateTime& value) {
Status tree_write(IoContext& context, IoPropertyTree& tree, const DateTime& value) {
const std::string s = value.to_string();
WG_YAML_WRITE(context, node, s);
WG_TREE_WRITE(context, tree, s);
return WG_OK;
}
Status stream_read(IoContext& context, IoStream& stream, DateTime& value) {
Expand Down
4 changes: 2 additions & 2 deletions engine/runtime/core/date_time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ namespace wmoge {

static DateTime now();

friend Status yaml_read(IoContext& context, YamlConstNodeRef node, DateTime& value);
friend Status yaml_write(IoContext& context, YamlNodeRef node, const DateTime& value);
friend Status tree_read(IoContext& context, IoPropertyTree& tree, DateTime& value);
friend Status tree_write(IoContext& context, IoPropertyTree& tree, const DateTime& value);
friend Status stream_read(IoContext& context, IoStream& stream, DateTime& value);
friend Status stream_write(IoContext& context, IoStream& stream, const DateTime& value);

Expand Down
Loading

0 comments on commit 02e3c45

Please sign in to comment.