-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMeta.cpp
More file actions
165 lines (155 loc) · 3.82 KB
/
Copy pathMeta.cpp
File metadata and controls
165 lines (155 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "Meta.h"
#include "Core/SObject.h"
#include "Core/FileSystem.h"
#include "Core/Logger.h"
#include <cassert>
namespace sh::editor
{
Meta::Meta() :
uuid(core::UUID::Generate())
{
}
SH_EDITOR_API auto Meta::Load(const std::filesystem::path& path) -> bool
{
if (!std::filesystem::exists(path))
return false;
this->path = path;
auto metaText = core::FileSystem::LoadText(path);
if (!metaText.has_value())
{
SH_ERROR_FORMAT("Can't read {}", path.u8string());
return false;
}
json = core::Json::parse(metaText.value());
if (!json.contains("uuid"))
{
SH_ERROR_FORMAT("Not found UUID from {}", path.u8string());
json.clear();
return false;
}
if (!json.contains("typeHash"))
{
SH_ERROR_FORMAT("Not found typeHash from {}", path.u8string());
json.clear();
return false;
}
if (!json.contains("name"))
{
SH_ERROR_FORMAT("Not found name from {}", path.u8string());
json.clear();
return false;
}
if (json.contains("metaHash"))
{
hash = json["metaHash"];
if (json.contains("obj"))
{
const core::Json objJson = json["obj"];
std::hash<std::string> hasher{};
const std::size_t objHash = hasher(objJson.dump());
bChanged = hash != objHash;
}
}
uuid = core::UUID{ json["uuid"].get_ref<const std::string&>() };
typeHash = json["typeHash"];
name = json["name"];
return true;
}
SH_EDITOR_API auto Meta::DeserializeSObject(core::SObject& obj) const -> bool
{
assert(IsLoad());
if (json.contains("uuid"))
obj.SetUUID(core::UUID{ json["uuid"].get_ref<const std::string&>() });
if (json.contains("name"))
obj.SetName(json["name"].get_ref<const std::string&>());
if (json.contains("obj"))
obj.Deserialize(json["obj"]);
return true;
}
SH_EDITOR_API void Meta::SaveWithObj(const core::SObject& obj, const std::filesystem::path& path, bool bCalcHash)
{
core::Json metaJson{};
metaJson["uuid"] = obj.GetUUID().ToString();
metaJson["typeHash"] = obj.GetType().type.hash;
metaJson["name"] = obj.GetName().ToString();
metaJson["version"] = 1;
if (bCalcHash)
{
std::hash<std::string> hasher{};
metaJson["metaHash"] = hasher(obj.Serialize().dump());
}
else
metaJson["metaHash"] = hash;
metaJson["obj"] = obj.Serialize();
std::ofstream os{ path };
if (os.is_open())
{
os << std::setw(4) << metaJson;
os.close();
}
else
{
SH_ERROR_FORMAT("Can't create meta: {}", path.u8string());
}
}
SH_EDITOR_API void Meta::Save(const core::SObject& obj, const std::filesystem::path& path, bool bCalcHash)
{
core::Json metaJson{};
metaJson["uuid"] = obj.GetUUID().ToString();
metaJson["typeHash"] = obj.GetType().type.hash;
metaJson["name"] = obj.GetName().ToString();
metaJson["version"] = VERSION;
std::ofstream os{ path };
if (os.is_open())
{
os << std::setw(4) << metaJson;
os.close();
}
else
{
SH_ERROR_FORMAT("Can't create meta: {}", path.u8string());
}
}
SH_EDITOR_API auto Meta::IsLoad() const -> bool
{
return !json.empty();
}
SH_EDITOR_API auto Meta::HasObjData() const -> bool
{
if (!IsLoad())
return false;
return json.contains("obj");
}
SH_EDITOR_API auto Meta::GetUUID() const -> const core::UUID&
{
return uuid;
}
SH_EDITOR_API auto Meta::GetTypeHash() const -> std::size_t
{
return typeHash;
}
SH_EDITOR_API auto Meta::GetName() const -> const std::string&
{
return name;
}
SH_EDITOR_API auto Meta::GetObjJson() const -> const core::Json*
{
if (!HasObjData())
return nullptr;
return &json["obj"];
}
SH_EDITOR_API void Meta::Clear()
{
return json.clear();
}
SH_EDITOR_API auto Meta::IsChanged() const -> bool
{
return bChanged;
}
SH_EDITOR_API auto Meta::CreateMetaDirectory(const std::filesystem::path& filePath) -> std::filesystem::path
{
std::filesystem::path metaPath = filePath;
metaPath += ".meta";
return metaPath;
}
}//namespace