Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new jsonbridge which uses the new serializers #862

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
url = https://github.com/Dav1dde/glad.git
[submodule "engine/lib/implot"]
path = engine/lib/implot
url = git@github.com:epezent/implot.git
url = https://github.com/epezent/implot.git
45 changes: 26 additions & 19 deletions engine/include/cubos/engine/assets/bridges/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#pragma once

#include <cubos/core/data/old/json_deserializer.hpp>
#include <cubos/core/data/old/json_serializer.hpp>
#include <cubos/core/data/des/json.hpp>
#include <cubos/core/data/ser/json.hpp>
#include <cubos/core/log.hpp>
#include <cubos/core/reflection/reflect.hpp>

Expand All @@ -26,28 +26,37 @@ namespace cubos::engine
{
public:
/// @brief Constructs a bridge.
///
/// If the @p indentation level is set to -1, all whitespace is removed.
///
/// @param indentation Indentation level to use when saving the JSON file.
JSONBridge(int indentation = 4)
JSONBridge()
: FileBridge(core::reflection::reflect<T>())
, mIndentation{indentation}
{
}

protected:
bool loadFromFile(Assets& assets, const AnyAsset& handle, core::memory::Stream& stream) override
{
// Dump the file stream into a string and initialize a JSON deserializer with it.
std::string json{};
stream.readUntil(json, nullptr);
core::data::old::JSONDeserializer deserializer{json};
std::string jsonStr;
stream.readUntil(jsonStr, nullptr);

// Parse JSON from the string.
nlohmann::json json{};
try
{
json = nlohmann::json::parse(jsonStr);
}
catch (nlohmann::json::parse_error& e)
{
CUBOS_ERROR("{}", e.what());
return false;
}

// Feed the parsed JSON to a JSON deserializer.
core::data::JSONDeserializer deserializer{};
deserializer.feed(json);

// Deserialize the asset and store it in the asset manager.
T data{};
deserializer.read(data);
if (deserializer.failed())
if (deserializer.read(data))
{
CUBOS_ERROR("Could not deserialize asset from JSON file");
return false;
Expand All @@ -60,21 +69,19 @@ namespace cubos::engine
bool saveToFile(const Assets& assets, const AnyAsset& handle, core::memory::Stream& stream) override
{
// Initialize a JSON serializer with the file stream.
core::data::old::JSONSerializer serializer{stream, mIndentation};
core::data::JSONSerializer serializer{};

// Read the asset from the asset manager and serialize it to the file stream.
auto data = assets.read<T>(handle);
serializer.write(*data, nullptr); // JSON serializer does not use names.
if (serializer.failed())
if (serializer.write(*data))
{
CUBOS_ERROR("Could not serialize asset to JSON file");
return false;
}

auto jsonStr = serializer.output().dump();
stream.print(jsonStr);
return true;
}

private:
int mIndentation;
};
} // namespace cubos::engine
80 changes: 80 additions & 0 deletions engine/include/cubos/engine/assets/bridges/old/json.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/// @file
/// @brief Class @ref cubos::engine::JSONBridge.
/// @ingroup assets-plugin

#pragma once

#include <cubos/core/data/old/json_deserializer.hpp>
#include <cubos/core/data/old/json_serializer.hpp>
#include <cubos/core/log.hpp>
#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/assets/bridges/file.hpp>

namespace cubos::engine::old
{
/// @brief Bridge for loading and saving assets which are serialized to and from a JSON file.
///
/// This bridge automatically serializes and deserializes assets of type @p T to and from a
/// JSON file. Thus, @p T must be serializable and deserializable. No additional context is
/// given to the serializer or deserializer.
///
/// @tparam T Type of asset to load and save. Must be default constructible.
/// @ingroup assets-plugin
template <typename T>
class JSONBridge : public FileBridge
{
public:
/// @brief Constructs a bridge.
///
/// If the @p indentation level is set to -1, all whitespace is removed.
///
/// @param indentation Indentation level to use when saving the JSON file.
JSONBridge(int indentation = 4)
: FileBridge(core::reflection::reflect<T>())
, mIndentation{indentation}
{
}

protected:
bool loadFromFile(Assets& assets, const AnyAsset& handle, core::memory::Stream& stream) override
{
// Dump the file stream into a string and initialize a JSON deserializer with it.
std::string json{};
stream.readUntil(json, nullptr);
core::data::old::JSONDeserializer deserializer{json};

// Deserialize the asset and store it in the asset manager.
T data{};
deserializer.read(data);
if (deserializer.failed())
{
CUBOS_ERROR("Could not deserialize asset from JSON file");
return false;
}

assets.store(handle, std::move(data));
return true;
}

bool saveToFile(const Assets& assets, const AnyAsset& handle, core::memory::Stream& stream) override
{
// Initialize a JSON serializer with the file stream.
core::data::old::JSONSerializer serializer{stream, mIndentation};

// Read the asset from the asset manager and serialize it to the file stream.
auto data = assets.read<T>(handle);
serializer.write(*data, nullptr); // JSON serializer does not use names.
if (serializer.failed())
{
CUBOS_ERROR("Could not serialize asset to JSON file");
return false;
}

return true;
}

private:
int mIndentation;
};
} // namespace cubos::engine::old
6 changes: 3 additions & 3 deletions engine/samples/assets/json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/type.hpp>

#include <cubos/engine/assets/bridges/json.hpp>
#include <cubos/engine/assets/bridges/old/json.hpp>
#include <cubos/engine/assets/plugin.hpp>
#include <cubos/engine/settings/settings.hpp>

Expand Down Expand Up @@ -60,7 +60,7 @@ static void configSystem(Settings& settings)
/// [Register bridge]
static void bridgeSystem(Assets& assets)
{
assets.registerBridge(".strings", std::make_unique<JSONBridge<Strings>>());
assets.registerBridge(".strings", std::make_unique<old::JSONBridge<Strings>>());
}
/// [Register bridge]

Expand Down Expand Up @@ -89,4 +89,4 @@ int main()

cubos.startupSystem(configSystem).tagged("cubos.settings");
cubos.run();
}
}
4 changes: 2 additions & 2 deletions engine/samples/assets/saving/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/type.hpp>

#include <cubos/engine/assets/bridges/json.hpp>
#include <cubos/engine/assets/bridges/old/json.hpp>
#include <cubos/engine/assets/plugin.hpp>
#include <cubos/engine/settings/settings.hpp>

Expand Down Expand Up @@ -58,7 +58,7 @@ static void configSystem(Settings& settings)

static void bridgeSystem(Assets& assets)
{
assets.registerBridge(".int", std::make_unique<JSONBridge<IntegerAsset>>());
assets.registerBridge(".int", std::make_unique<old::JSONBridge<IntegerAsset>>());
}

/// [Create a new asset]
Expand Down
Loading