-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathresourcesystem.cpp
More file actions
267 lines (226 loc) · 7.83 KB
/
resourcesystem.cpp
File metadata and controls
267 lines (226 loc) · 7.83 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "systems/resourcesystem.h"
#include <bson.h>
#include <json.h>
#include <log.h>
#include "file.h"
#include "resources/resource.h"
ResourceSystem::ResourceSystem() {
setName("ResourceSystem");
}
bool ResourceSystem::init() {
return true;
}
void ResourceSystem::update(World *) {
PROFILE_FUNCTION();
for(auto it = m_referenceCache.begin(); it != m_referenceCache.end();) {
processState(it->first);
++it;
}
while(!m_deleteList.empty()) {
Resource *res = m_deleteList.back();
m_deleteList.pop_back();
delete res;
}
}
int ResourceSystem::threadPolicy() const {
return Main;
}
void ResourceSystem::setResource(Resource *object, const string &uuid) {
PROFILE_FUNCTION();
m_resourceCache[uuid] = object;
m_referenceCache[object] = uuid;
}
bool ResourceSystem::isResourceExist(const string &path) {
PROFILE_FUNCTION();
auto it = m_indexMap.find(path);
return (it != m_indexMap.end());
}
Resource *ResourceSystem::loadResource(const string &path) {
PROFILE_FUNCTION();
if(!path.empty()) {
string uuid = path;
Resource *object = resource(uuid);
if(object) {
return object;
}
File *file = Engine::file();
_FILE *fp = file->fopen(uuid.c_str(), "r");
if(fp) {
ByteArray data;
size_t size = file->fsize(fp);
data.resize(size);
file->fread(&data[0], data.size(), 1, fp);
file->fclose(fp);
Variant var = Bson::load(data);
if(!var.isValid()) {
var = Json::load(string(data.begin(), data.end()));
}
if(var.isValid()) {
return static_cast<Resource *>(Engine::toObject(var, nullptr, uuid));
}
}
}
return nullptr;
}
void ResourceSystem::unloadResource(Resource *resource, bool force) {
PROFILE_FUNCTION();
if(resource) {
resource->switchState(Resource::Unloading);
if(force) {
processState(resource);
}
}
}
void ResourceSystem::reloadResource(Resource *resource, bool force) {
PROFILE_FUNCTION();
if(resource) {
if(force) {
resource->switchState(Resource::Loading);
processState(resource);
} else {
resource->switchState(Resource::ToBeUpdated);
}
}
}
void ResourceSystem::releaseAll() {
for(auto it = m_referenceCache.begin(); it != m_referenceCache.end();) {
if(it->first->isUnloadable()) {
unloadResource(it->first, true);
it->first->switchState(Resource::ToBeUpdated);
}
++it;
}
}
string ResourceSystem::reference(Resource *resource) {
PROFILE_FUNCTION();
auto it = m_referenceCache.find(resource);
if(it != m_referenceCache.end()) {
return it->second;
}
return string();
}
ResourceSystem::DictionaryMap &ResourceSystem::indices() const {
return m_indexMap;
}
void ResourceSystem::deleteFromCahe(Resource *resource) {
PROFILE_FUNCTION();
auto ref = m_referenceCache.find(resource);
if(ref != m_referenceCache.end()) {
auto res = m_resourceCache.find(ref->second);
if(res != m_resourceCache.end()) {
m_resourceCache.erase(res);
}
m_referenceCache.erase(ref);
}
for(auto it : m_deleteList) {
if(it == resource) {
m_deleteList.remove(it);
break;
}
}
}
void ResourceSystem::processState(Resource *resource) {
if(resource) {
switch(resource->state()) {
case Resource::Loading: {
string uuid = reference(resource);
if(!uuid.empty()) {
File *file = Engine::file();
_FILE *fp = file->fopen(uuid.c_str(), "r");
if(fp) {
ByteArray data;
data.resize(file->fsize(fp));
file->fread(&data[0], data.size(), 1, fp);
file->fclose(fp);
Variant var = Bson::load(data);
if(!var.isValid()) {
var = Json::load(string(data.begin(), data.end()));
}
ObjectList deleteObjects;
enumObjects(resource, deleteObjects);
VariantList objects = var.toList();
auto delIt = deleteObjects.begin();
bool first = true;
for(auto &obj : objects) {
VariantList fields = obj.toList();
auto it = std::next(fields.begin(), 1);
uint32_t id = it->toInt();
Object *object = resource;
if(!first) {
object = Engine::findObject(id, resource);
} else {
first = false;
}
if(object) {
it = std::next(fields.begin(), 4);
VariantMap &properties = *(reinterpret_cast<VariantMap *>((*it).data()));
for(const auto &prop : properties) {
Variant v = prop.second;
if(v.type() < MetaType::USERTYPE) {
object->setProperty(prop.first.c_str(), v);
}
}
object->loadUserData(fields.back().toMap());
delIt = deleteObjects.erase(delIt);
} else {
VariantList list;
list.push_back(obj);
Engine::toObject(list, resource, uuid);
}
}
deleteObjects.reverse();
for(auto toDel : deleteObjects) {
delete toDel;
}
resource->switchState(Resource::ToBeUpdated);
} else {
Log(Log::ERR) << "Unable to load resource: " << uuid.c_str();
resource->setState(Resource::Invalid);
}
}
} break;
case Resource::Suspend: { /// \todo Don't delete resource imidiately Cache pattern implementation required
resource->switchState(Resource::Unloading);
} break;
case Resource::ToBeDeleted: {
bool found = false;
for(auto it : m_deleteList) {
if(it == resource) {
found = true;
break;
}
}
if(!found) {
m_deleteList.push_back(resource);
}
} break;
default: break;
}
}
}
Resource *ResourceSystem::resource(string &path) const {
{
auto it = m_indexMap.find(path);
if(it != m_indexMap.end()) {
path = it->second.second;
}
}
{
auto it = m_resourceCache.find(path);
if(it != m_resourceCache.end() && it->second) {
return it->second;
}
}
return nullptr;
}
Object *ResourceSystem::instantiateObject(const MetaObject *meta, const string &name, Object *parent) {
Object *result = System::instantiateObject(meta, name, parent);
Resource *resource = dynamic_cast<Resource *>(result);
if(resource) {
if(!name.empty()) {
setResource(resource, name);
}
resource->switchState(Resource::ToBeUpdated);
}
return result;
}