Unravel Engine C++ Reference
Loading...
Searching...
No Matches
project_info.cpp
Go to the documentation of this file.
1#include "project_info.hpp"
2
3#include <fstream>
7
8#include <logging/logging.h>
9
10namespace unravel
11{
12
13namespace
14{
15
16// `version::engine_version` is intentionally serialized as its canonical
17// "M.m.p[-commit[-sha]]" string. This keeps the on-disk format human-readable,
18// stable across engine refactors of the struct, and hand-editable by users.
19auto to_nvp_string(const version::engine_version& v) -> std::string
20{
21 return v.to_string();
22}
23
24void from_nvp_string(const std::string& s, version::engine_version& out)
25{
26 if(auto parsed = version::parse(s))
27 {
28 out = *parsed;
29 }
30 else
31 {
32 APPLOG_WARNING("Could not parse engine version '{}' from project file. "
33 "Using zero-initialized version.",
34 s);
35 out = {};
36 }
37}
38
39} // namespace
40
42{
43 entt::meta_factory<project_info>{}
44 .type("project_info"_hs)
46 entt::attribute{"name", "project_info"},
47 entt::attribute{"category", "PROJECT"},
48 entt::attribute{"pretty_name", "Project Info"},
49 })
50 .data<&project_info::project_guid>("project_guid"_hs)
52 entt::attribute{"name", "project_guid"},
53 entt::attribute{"pretty_name", "Project GUID"},
54 entt::attribute{"tooltip", "Stable unique id generated when this project was created."},
55 entt::attribute{"readonly", true},
56 });
57}
58
60{
61 try_save(ar, ser20::make_nvp("project_guid", obj.project_guid));
62
63 // Serialized as strings: see rationale at the top of this file.
64 const std::string created = to_nvp_string(obj.engine_version_created);
65 const std::string opened = to_nvp_string(obj.engine_version_opened);
66 try_save(ar, ser20::make_nvp("engine_version_created", created));
67 try_save(ar, ser20::make_nvp("engine_version_opened", opened));
68}
71
73{
74 try_load(ar, ser20::make_nvp("project_guid", obj.project_guid));
75
76 std::string created;
77 if(try_load(ar, ser20::make_nvp("engine_version_created", created)))
78 {
79 from_nvp_string(created, obj.engine_version_created);
80 }
81 std::string opened;
82 if(try_load(ar, ser20::make_nvp("engine_version_opened", opened)))
83 {
84 from_nvp_string(opened, obj.engine_version_opened);
85 }
86}
89
90void save_to_file(const std::string& absolute_path, const project_info& obj)
91{
92 std::ofstream stream(absolute_path);
93 if(stream.good())
94 {
95 try
96 {
97 auto ar = ser20::create_oarchive_associative(stream);
98 try_save(ar, ser20::make_nvp("project", obj));
99 }
100 catch(const std::exception& e)
101 {
102 APPLOG_WARNING("Failed to save project info {}: {}", absolute_path, e.what());
103 }
104 }
105}
106
107auto load_from_file(const std::string& absolute_path, project_info& obj) -> bool
108{
109 std::ifstream stream(absolute_path);
110 if(stream.good())
111 {
112 try
113 {
114 auto ar = ser20::create_iarchive_associative(stream);
115 return try_load(ar, ser20::make_nvp("project", obj));
116 }
117 catch(const std::exception& e)
118 {
119 APPLOG_WARNING("Failed to load project info {}: {}", absolute_path, e.what());
120 }
121 }
122
123 return false;
124}
125
126} // namespace unravel
#define APPLOG_WARNING(...)
Definition logging.h:19
attributes::value_type attribute
Definition reflection.h:19
std::map< std::string, meta_any > attributes
Definition reflection.h:18
auto create_oarchive_associative(std::ostream &stream)
BinaryInputArchive iarchive_binary_t
auto create_iarchive_associative(std::istream &stream)
simd::JSONOutputArchive oarchive_associative_t
BinaryOutputArchive oarchive_binary_t
simd::JSONInputArchive iarchive_associative_t
void load_from_file(const std::string &absolute_path, animation_clip &obj)
void save_to_file(const std::string &absolute_path, const animation_clip &obj)
auto parse(std::string_view text) -> std::optional< engine_version >
Parses a version string. Returns nullopt on malformed input.
Definition version.cpp:129
#define REFLECT(cls)
Definition reflection.h:149
#define SAVE_INSTANTIATE(cls, Archive)
#define LOAD(cls)
auto try_save(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
#define LOAD_INSTANTIATE(cls, Archive)
#define SAVE(cls)
auto try_load(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool