Skip to content

Commit

Permalink
docs(engine): remove saving code from assets.json
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 25, 2023
1 parent a7773f6 commit 001326e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 100 deletions.
6 changes: 6 additions & 0 deletions engine/samples/assets/json/assets/sample.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"strings": [
"Hello",
"World"
]
}
3 changes: 3 additions & 0 deletions engine/samples/assets/json/assets/sample.strings.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "6f42ae5a-59d1-5df3-8720-83b8df6dd536"
}
80 changes: 20 additions & 60 deletions engine/samples/assets/json/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/// @file
/// @brief A sample demonstrating how to save and load JSON assets.
///
/// This sample demonstrates how we can use the JSONBridge to easily save and load serializable
/// assets as JSON files. If we had a serializable asset and we wanted to store it as binary, we
/// could simply replace the JSONBridge with a BinaryBridge and the sample would work the same way.
///
/// In this sample, we will be creating an asset of type MyAsset, saving it to a JSON file, and
/// then loading it back from the file.

#include <vector>

#include <cubos/core/data/fs/file_system.hpp>
Expand All @@ -27,95 +17,65 @@ using namespace cubos::engine;

/// [Asset type]
/// A simple serializable type which we will be saving and loading.
struct MyAsset
struct Strings
{
int anInteger;
std::vector<std::string> someStrings;
std::vector<std::string> strings;
};
/// [Asset type]

/// [Serialization definition]
template <>
void cubos::core::data::serialize<MyAsset>(Serializer& ser, const MyAsset& obj, const char* name)
void cubos::core::data::serialize<Strings>(Serializer& ser, const Strings& obj, const char* name)
{
ser.beginObject(name);
ser.write(obj.anInteger, "anInteger");
ser.write(obj.someStrings, "someStrings");
ser.write(obj.strings, "strings");
ser.endObject();
}

template <>
void cubos::core::data::deserialize<MyAsset>(Deserializer& des, MyAsset& obj)
void cubos::core::data::deserialize<Strings>(Deserializer& des, Strings& obj)
{
des.beginObject();
des.read(obj.anInteger);
des.read(obj.someStrings);
des.read(obj.strings);
des.endObject();
}
/// [Serialization definition]

/// [Setting]
static void configSystem(Write<Settings> settings)
{
// If we want to save assets, we must set this to false.
settings->setBool("assets.io.readOnly", false);
/// [Setting]

settings->setString("assets.io.path", SAMPLE_ASSETS_FOLDER);
}

/// [Register bridge]
static void bridgeSystem(Write<Assets> assets)
{
assets->registerBridge(".my", std::make_unique<JSONBridge<MyAsset>>());
assets->registerBridge(".strings", std::make_unique<JSONBridge<Strings>>());
}
/// [Register bridge]

/// [Create a new asset]
static void saveAndLoadSystem(Write<Assets> assets)
{
// Create a new asset (with a random UUID).
auto handle = assets->create(MyAsset{
.anInteger = 42,
.someStrings = {"Hello", "World"},
});
/// [Create a new asset]

/// [Save the asset]
assets->writeMeta(handle)->set("path", "/assets/sample/asset.my");
assets->save(handle);
/// [Save the asset]

/// [Force reload]
assets->invalidate(handle);
/// [Force reload]
/// [Loading the asset]
static const Asset<Strings> SampleAsset = AnyAsset("6f42ae5a-59d1-5df3-8720-83b8df6dd536");

/// [Read the asset]
// Access the asset - will be loaded automatically.
auto read = assets->read(handle);
Stream::stdOut.printf("Integer: {}\n", read->anInteger);
for (const auto& str : read->someStrings)
static void loadSystem(Write<Assets> assets)
{
auto read = assets->read(SampleAsset);
for (const auto& str : read->strings)
{
Stream::stdOut.printf("String: {}\n", str);
}
/// [Read the asset]

// Wait for input before exiting.
Stream::stdOut.print("You can now check the contents of the file!\nPress enter to exit...");
Stream::stdIn.get();

// Cleanup the created asset.
FileSystem::destroy("/assets/sample");
}
/// [Loading the asset]

/// [Run]
int main()
{
Cubos cubos{};

/// [Configuration]
cubos.addPlugin(assetsPlugin);
cubos.startupSystem(configSystem).tagged("cubos.settings");
cubos.startupSystem(bridgeSystem).tagged("cubos.assets.bridge");
cubos.startupSystem(saveAndLoadSystem).tagged("cubos.assets");
cubos.startupSystem(loadSystem).tagged("cubos.assets");
/// [Configuration]

cubos.startupSystem(configSystem).tagged("cubos.settings");
cubos.run();
}
/// [Run]
50 changes: 10 additions & 40 deletions engine/samples/assets/json/page.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# Loading and Saving JSON Assets {#examples-engine-assets-json}
# Loading Serializable Assets {#examples-engine-assets-json}

@brief Loading and saving serializable assets from and to JSON.

This example demonstrates how you can load and save assets of a serializable
type from and to JSON files.

Before we go any further, if we want to save assets to the filesystem, we must
allow assets to be modified. This is done through the following setting:

@snippet assets/json/main.cpp Setting
@brief Loading serializable assets from JSON.

We'll use the following type as an example:

Expand All @@ -18,39 +10,17 @@ We can make it serializable by implementing the following specializations:

@snippet assets/json/main.cpp Serialization definition

Then, we must register a bridge for this type. Since we want to load and save
the data as JSON, we'll use @ref cubos::engine::JSONBridge "JSONBridge".
Then, we must register a bridge for this type. We provide @ref
cubos::engine::JSONBridge "JSONBridge" for easily loading and saving
serializable assets as JSON.

@snippet assets/json/main.cpp Register bridge

First, we'll create an asset of this type:

@snippet assets/json/main.cpp Create a new asset

Then, we'll assign it a path and save it. Its important that the path ends with
the correct extension, so that @ref cubos::engine::Assets "Assets" knows which
bridge to use when loading it.

@snippet assets/json/main.cpp Save the asset

With this, the files `sample/asset.my` and `sample/asset.my.meta` should have
appeared on the `assets/` directory. The `.meta` file contains the UUID of the
asset, which is used by the engine to identify it.

To test whether the asset can be loaded correctly from the saved data, we'll
force it to be reloaded, using @ref cubos::engine::Assets::invalidate
"Assets::invalidate".

@snippet assets/json/main.cpp Force reload

Now, when we try reading the asset data again, it will be loaded from the
previously saved file.

@snippet assets/json/main.cpp Read the asset

Finally, the engine is configured the following way:
With the bridge registered, we can just load it from its handle:

@snippet assets/json/main.cpp Run
@snippet assets/json/main.cpp Loading the asset

Try running the sample yourself to see the loaded data!
These sytems are configured the usual way, as explained in @ref
examples-engine-assets-bridge.

@snippet assets/json/main.cpp Configuration

0 comments on commit 001326e

Please sign in to comment.