Nameless Engine
|
#include <Serializable.h>
Public Member Functions | |
std::optional< Error > | serialize (std::filesystem::path pathToFile, bool bEnableBackup, const std::unordered_map< std::string, std::string > &customAttributes={}) |
std::variant< std::string, Error > | serialize (toml::value &tomlData, const std::string &sEntityId="", const std::unordered_map< std::string, std::string > &customAttributes={}, const std::optional< std::filesystem::path > &optionalPathToFile={}, bool bEnableBackup=false) |
std::variant< std::string, Error > | serialize (toml::value &tomlData, Serializable *pOriginalObject, std::string sEntityId="", const std::unordered_map< std::string, std::string > &customAttributes={}, const std::optional< std::filesystem::path > &optionalPathToFile={}, bool bEnableBackup=false) |
std::optional< std::pair< std::string, std::string > > | getPathDeserializedFromRelativeToRes () const |
Static Public Member Functions | |
static std::variant< std::pair< std::set< std::string >, toml::value >, Error > | getIdsFromFile (std::filesystem::path pathToFile) |
static std::optional< Error > | serializeMultiple (std::filesystem::path pathToFile, std::vector< SerializableObjectInformation > vObjects, bool bEnableBackup) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (const std::filesystem::path &pathToFile) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (const std::filesystem::path &pathToFile, std::unordered_map< std::string, std::string > &customAttributes) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (std::filesystem::path pathToFile, std::unordered_map< std::string, std::string > &customAttributes, const std::string &sEntityId) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (const std::filesystem::path &pathToFile, const std::string &sEntityId) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::same_as<SmartPointer, sgc::GcPtr<Serializable>> || std::same_as<SmartPointer, std::unique_ptr<Serializable>> | |
static std::variant< std::vector< DeserializedObjectInformation< SmartPointer > >, Error > | deserializeMultiple (std::filesystem::path pathToFile) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserialize (const toml::value &tomlData, std::unordered_map< std::string, std::string > &customAttributes, std::string sEntityId="", const std::optional< std::filesystem::path > &optionalPathToFile={}) |
Protected Member Functions | |
virtual void | onAfterDeserialized () |
Static Private Member Functions | |
static std::optional< Error > | resolvePathToToml (std::filesystem::path &pathToFile) |
static const rfk::Class * | getClassForGuid (const std::string &sGuid) |
static const rfk::Struct * | getClassForGuid (const rfk::Struct *pArchetypeToAnalyze, const std::string &sGuid) |
template<typename SmartPointer , typename InnerType = typename SmartPointer::element_type> requires std::derived_from<InnerType, Serializable> && (std::same_as<SmartPointer, sgc::GcPtr<InnerType>> || std::same_as<SmartPointer, std::unique_ptr<InnerType>>) | |
static std::variant< SmartPointer, Error > | deserializeFromSection (const toml::value &tomlData, std::unordered_map< std::string, std::string > &customAttributes, const std::string &sSectionName, const std::string &sTypeGuid, const std::string &sEntityId, const std::optional< std::filesystem::path > &optionalPathToFile) |
Private Attributes | |
std::optional< std::pair< std::string, std::string > > | pathDeserializedFromRelativeToRes |
Static Private Attributes | |
static const auto | sSubEntityFieldNameKey = ".field_name" |
static const auto | sPathRelativeToResKey = ".path_relative_to_res" |
static const auto | sNothingToSerializeKey = ".none" |
static constexpr std::string_view | sCustomAttributePrefix = ".." |
Friends | |
class | SerializableObjectFieldSerializer |
Adds support for serialization/deserialization for your reflected type.
Inherit your class/struct from this class to add functions which will serialize the type and reflected fields (even inherited) that are marked with special properties.
|
static |
Deserializes an object and all reflected fields (including inherited) from a file. Specify the type of an object (that is located in the file) as the T template parameter, which can be entity's actual type or entity's parent (up to Serializable).
sgc::GcPtr
or std::unique_ptr
as a smart pointer for deserialized object.pathToFile | File to read reflected data from. The ".toml" extension will be added automatically if not specified in the path. |
|
static |
Deserializes an object and all reflected fields (including inherited) from a file. Specify the type of an object (that is located in the file) as the T template parameter, which can be entity's actual type or entity's parent (up to Serializable).
pathToFile | File to read reflected data from. The ".toml" extension will be added automatically if not specified in the path. |
sEntityId | Unique ID of this object. When serializing multiple objects into one toml value provide different IDs for each object so they could be differentiated. |
|
static |
Deserializes an object and all reflected fields (including inherited) from a file. Specify the type of an object (that is located in the file) as the T template parameter, which can be entity's actual type or entity's parent (up to Serializable).
pathToFile | File to read reflected data from. The ".toml" extension will be added automatically if not specified in the path. |
customAttributes | Pairs of values that were associated with this object. |
|
static |
Deserializes an object and all reflected fields (including inherited) from a toml value. Specify the type of an object (that is located in the file) as the T template parameter, which can be entity's actual type or entity's parent (up to Serializable).
tomlData | Toml value to retrieve an object from. |
customAttributes | Pairs of values that were associated with this object. |
sEntityId | Unique ID of this object. When serializing multiple objects into one toml value provide different IDs for each object so they could be differentiated. |
optionalPathToFile | Optional. Path to the file that this TOML data is deserialized from. Used for fields marked as Serialize(AsExternal) |
|
static |
Deserializes an object and all reflected fields (including inherited) from a file. Specify the type of an object (that is located in the file) as the T template parameter, which can be entity's actual type or entity's parent (up to Serializable).
pathToFile | File to read reflected data from. The ".toml" extension will be added automatically if not specified in the path. |
customAttributes | Pairs of values that were associated with this object. |
sEntityId | Unique ID of this object. When serializing multiple objects into one toml value provide different IDs for each object so they could be differentiated. |
|
staticprivate |
Deserializes an object and all reflected fields (including inherited) from a file. Specify the type of an object (that is located in the file) as the T template parameter, which can be entity's actual type or entity's parent (up to Serializable).
tomlData | Toml value to retrieve an object from. |
customAttributes | Pairs of values that were associated with this object. |
sSectionName | Name of the TOML section to deserialize. |
sTypeGuid | GUID of the type to deserialize (taken from section name). |
sEntityId | Entity ID chain string. |
optionalPathToFile | Optional. Path to the file that this TOML data is deserialized from. Used for fields marked as Serialize(AsExternal) |
|
static |
Deserializes multiple objects and their reflected fields (including inherited) from a file.
pathToFile | File to read reflected data from. The ".toml" extension will be added automatically if not specified in the path. |
|
staticprivate |
Looks for all children of the specified archetype to find a type that has the specified GUID.
pArchetypeToAnalyze | Type which children to analyze. |
sGuid | GUID to look for. |
|
staticprivate |
Returns archetype for the specified GUID.
sGuid | GUID to look for. |
|
static |
Analyzes the file for serialized objects, gathers and returns unique IDs of those objects.
pathToFile | File to read serialized data from. The ".toml" extension will be added automatically if not specified in the path. |
std::optional< std::pair< std::string, std::string > > ne::Serializable::getPathDeserializedFromRelativeToRes | ( | ) | const |
If this object was deserialized from a file that is located in the res
directory of this project, returns a pair of values:
res
directory,This path will never point to a backup file and will always point to the original file (even if the backup file was used in deserialization).
Example: say this object is deserialized from the file located at .../res/game/test.toml
, this value will be equal to the following pair: {game/test.toml
, some.id
}.
res
directory.
|
inlineprotectedvirtual |
Called after the object was successfully deserialized. Used to execute post-deserialization logic.
Reimplemented in ne::DirectionalLightNode, ne::PointLightNode, ne::SpotlightNode, ne::MeshNode, ne::SpatialNode, ne::Material, and ne::RenderSettings.
|
staticprivate |
Adds ".toml" extension to the path (if needed) and copies a backup file to the specified path if the specified path does not exist but there is a backup file.
pathToFile | Path to toml file (may point to non-existing path or don't have ".toml" extension). |
std::optional< Error > ne::Serializable::serialize | ( | std::filesystem::path | pathToFile, |
bool | bEnableBackup, | ||
const std::unordered_map< std::string, std::string > & | customAttributes = {} |
||
) |
Serializes the object and all reflected fields (including inherited) that are marked with ne::Serialize
property into a file. Serialized object can later be deserialized using deserialize.
pathToFile | File to write reflected data to. The ".toml" extension will be added automatically if not specified in the path. If the specified file already exists it will be overwritten. If the directories of the specified file do not exist they will be recursively created. |
bEnableBackup | If 'true' will also use a backup (copy) file. deserialize can use backup file if the original file does not exist. Generally you want to use a backup file if you are saving important information, such as player progress, other cases such as player game settings and etc. usually do not need a backup but you can use it if you want. |
customAttributes | Optional. Custom pairs of values that will be saved as this object's additional information and could be later retrieved in deserialize. |
ne::Serialize
property like so: Note that not all reflected fields can be serialized, only specific types can be serialized. Const fields, pointer fields, lvalue references, rvalue references and C-arrays will always be ignored and will not be serialized (no error returned). Supported for serialization types are:bool
int
unsigned int
long long
unsigned long long
float
double
std::string
T
(where T
is any type that derives from Serializable)io/serializers
directory for available field serializers (you don't need to use them directly, they will be automatically picked inside of this function).std::variant< std::string, Error > ne::Serializable::serialize | ( | toml::value & | tomlData, |
const std::string & | sEntityId = "" , |
||
const std::unordered_map< std::string, std::string > & | customAttributes = {} , |
||
const std::optional< std::filesystem::path > & | optionalPathToFile = {} , |
||
bool | bEnableBackup = false |
||
) |
Serializes the object and all reflected fields (including inherited) into a toml value.
tomlData | Toml value to append this object to. |
sEntityId | Unique ID of this object. When serializing multiple objects into one toml value provide different IDs for each object so they could be differentiated. Don't use dots in the entity ID, dots are used in recursion when this function is called from this function to process reflected field (sub entity). |
customAttributes | Optional. Custom pairs of values that will be saved as this object's additional information and could be later retrieved in deserialize. |
optionalPathToFile | Optional. Path to the file that this TOML data will be serialized. Used for fields marked as Serialize(AsExternal) . |
bEnableBackup | Optional. If this TOML data will be serialized to file whether the backup file is needed or not. Used for fields marked as Serialize(AsExternal) . |
std::variant< std::string, Error > ne::Serializable::serialize | ( | toml::value & | tomlData, |
Serializable * | pOriginalObject, | ||
std::string | sEntityId = "" , |
||
const std::unordered_map< std::string, std::string > & | customAttributes = {} , |
||
const std::optional< std::filesystem::path > & | optionalPathToFile = {} , |
||
bool | bEnableBackup = false |
||
) |
Serializes the object and all reflected fields (including inherited) into a toml value.
tomlData | Toml value to append this object to. |
pOriginalObject | Optional. Original object of the same type as the object being serialized, this object is a deserialized version of the object being serialized, used to compare serializable fields' values and only serialize changed values. |
sEntityId | Unique ID of this object. When serializing multiple objects into one toml value provide different IDs for each object so they could be differentiated. Don't use dots in the entity ID, dots are used in recursion when this function is called from this function to process reflected field (sub entity). |
customAttributes | Optional. Custom pairs of values that will be saved as this object's additional information and could be later retrieved in deserialize. |
optionalPathToFile | Optional. Path to the file that this TOML data will be serialized. Used for fields marked as Serialize(AsExternal) . |
bEnableBackup | Optional. If this TOML data will be serialized to file whether the backup file is needed or not. Used for fields marked as Serialize(AsExternal) . |
|
static |
Serializes multiple objects, their reflected fields (including inherited) and provided custom attributes (if any) into a file.
pathToFile | File to write reflected data to. The ".toml" extension will be added automatically if not specified in the path. If the specified file already exists it will be overwritten. |
vObjects | Array of objects to serialize, their unique IDs (so they could be differentiated in the file) and custom attributes (if any). Don't use dots in the entity IDs, dots are used internally. |
bEnableBackup | If 'true' will also use a backup (copy) file. deserialize can use backup file if the original file does not exist. Generally you want to use a backup file if you are saving important information, such as player progress, other cases such as player game settings and etc. usually do not need a backup but you can use it if you want. |
|
private |
If this object was deserialized from a file that is located in the res
directory of this project, this field will contain a pair of values:
res
directory,This path will never point to a backup file and will always point to the original file (even if the backup file was used in deserialization).
Example: say this object is deserialized from the file located at .../res/game/test.toml
, this value will be equal to game/test.toml
.
|
staticconstexprprivate |
Text that we add to custom (user-specified) attributes in TOML files.
|
inlinestaticprivate |
Name of the key which we use when there is nothing to serialize.
|
inlinestaticprivate |
Name of the key which we use when we serialize an object that was previously deserialized from the res
directory.
|
inlinestaticprivate |
Name of the key in which to store name of the field a section represents.