History

short circuit the mapping operations for invalid path/hash

Stephen Birarda committed 10 years ago

Commit 99718e91 parent b0c326f8 files · +252 −148Browse files

Showing the changes to libraries/script-engine/src/AssetMappingsScriptingInterface.cpp (1 of 8 files). Show all

Unified · Split

libraries/script-engine/src/AssetMappingsScriptingInterface.cpp+161−121
@@ -29,87 +29,117 @@ void AssetMappingsScriptingInterface::setMapping(QString path, QString hash, QJS · 28 unmodified lines
29 auto assetClient = DependencyManager::get<AssetClient>();29 auto assetClient = DependencyManager::get<AssetClient>();
30 auto request = assetClient->createSetMappingRequest(path, hash);30 auto request = assetClient->createSetMappingRequest(path, hash);
31 31
32 connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {32 if (request) {
33 QJSValueList args { uint8_t(request->getError()) };33 connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {
34 QJSValueList args { uint8_t(request->getError()) };
34 35
35 callback.call(args);36 callback.call(args);
36 37
37 request->deleteLater();38 request->deleteLater();
38
39 });
40 39
41 request->start();40 });
41
42 request->start();
43 } else {
44 // not connected to an Asset Server, return network error
45 QJSValueList args { uint8_t(MappingRequest::NetworkError) };
46 callback.call(args);
47 }
42}48}
43 49
44void AssetMappingsScriptingInterface::getMapping(QString path, QJSValue callback) {50void AssetMappingsScriptingInterface::getMapping(QString path, QJSValue callback) {
45 auto assetClient = DependencyManager::get<AssetClient>();51 auto assetClient = DependencyManager::get<AssetClient>();
46 auto request = assetClient->createGetMappingRequest(path);52 auto request = assetClient->createGetMappingRequest(path);
47 53
48 connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {54 if (request) {
49 QJSValueList args { uint8_t(request->getError()), request->getHash() };55 connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {
56 QJSValueList args { uint8_t(request->getError()), request->getHash() };
50 57
51 callback.call(args);58 callback.call(args);
52 59
53 request->deleteLater();60 request->deleteLater();
54
55 });
56 61
57 request->start();62 });
63
64 request->start();
65 } else {
66 // not connected to an Asset Server, return network error
67 QJSValueList args { uint8_t(MappingRequest::NetworkError) };
68 callback.call(args);
69 }
58}70}
59 71
60void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) {72void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) {
61 auto assetClient = DependencyManager::get<AssetClient>();73 auto assetClient = DependencyManager::get<AssetClient>();
62 auto request = assetClient->createDeleteMappingsRequest(paths);74 auto request = assetClient->createDeleteMappingsRequest(paths);
63 75
64 connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {76 if (request) {
65 QJSValueList args { uint8_t(request->getError()) };77 connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {
78 QJSValueList args { uint8_t(request->getError()) };
66 79
67 callback.call(args);80 callback.call(args);
68 81
69 request->deleteLater();82 request->deleteLater();
70
71 });
72 83
73 request->start();84 });
85
86 request->start();
87 } else {
88 // not connected to an Asset Server, return network error
89 QJSValueList args { uint8_t(MappingRequest::NetworkError) };
90 callback.call(args);
91 }
74}92}
75 93
76void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) {94void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) {
77 auto assetClient = DependencyManager::get<AssetClient>();95 auto assetClient = DependencyManager::get<AssetClient>();
78 auto request = assetClient->createGetAllMappingsRequest();96 auto request = assetClient->createGetAllMappingsRequest();
79 97
80 connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {98 if (request) {
81 auto mappings = request->getMappings();99 connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {
82 auto map = callback.engine()->newObject();100 auto mappings = request->getMappings();
101 auto map = callback.engine()->newObject();
83 102
84 for (auto& kv : mappings ) {103 for (auto& kv : mappings ) {
85 map.setProperty(kv.first, kv.second);104 map.setProperty(kv.first, kv.second);
86 }105 }
87
88 QJSValueList args { uint8_t(request->getError()), map };
89 106
90 callback.call(args);107 QJSValueList args { uint8_t(request->getError()), map };
91 108
92 request->deleteLater();109 callback.call(args);
93
94 });
95 110
96 request->start();111 request->deleteLater();
112
113 });
114
115 request->start();
116 } else {
117 // not connected to an Asset Server, return network error
118 QJSValueList args { uint8_t(MappingRequest::NetworkError) };
119 callback.call(args);
120 }
97}121}
98 122
99void AssetMappingsScriptingInterface::renameMapping(QString oldPath, QString newPath, QJSValue callback) {123void AssetMappingsScriptingInterface::renameMapping(QString oldPath, QString newPath, QJSValue callback) {
100 auto assetClient = DependencyManager::get<AssetClient>();124 auto assetClient = DependencyManager::get<AssetClient>();
101 auto request = assetClient->createRenameMappingRequest(oldPath, newPath);125 auto request = assetClient->createRenameMappingRequest(oldPath, newPath);
102 126
103 connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable {127 if (request) {
104 QJSValueList args { uint8_t(request->getError()) };128 connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable {
129 QJSValueList args { uint8_t(request->getError()) };
130
131 callback.call(args);
132
133 request->deleteLater();
134
135 });
105 136
137 request->start();
138 } else {
139 // not connected to an Asset Server, return network error
140 QJSValueList args { uint8_t(MappingRequest::NetworkError) };
106 callback.call(args);141 callback.call(args);
107 142 }
108 request->deleteLater();
109
110 });
111
112 request->start();
113}143}
114 144
115 145
@@ -134,92 +164,102 @@ void AssetMappingModel::refresh() { · 18 unmodified lines
134 auto assetClient = DependencyManager::get<AssetClient>();164 auto assetClient = DependencyManager::get<AssetClient>();
135 auto request = assetClient->createGetAllMappingsRequest();165 auto request = assetClient->createGetAllMappingsRequest();
136 166
137 connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable {167 if (request) {
138 auto mappings = request->getMappings();168 connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable {
139 auto existingPaths = _pathToItemMap.keys();169 if (request->getError() == MappingRequest::NoError) {
140 for (auto& mapping : mappings) {170 auto mappings = request->getMappings();
141 auto& path = mapping.first;171 auto existingPaths = _pathToItemMap.keys();
142 auto parts = path.split("/");172 for (auto& mapping : mappings) {
143 auto length = parts.length();173 auto& path = mapping.first;
144 174 auto parts = path.split("/");
145 existingPaths.removeOne(mapping.first);175 auto length = parts.length();
146 176
147 QString fullPath = "/";177 existingPaths.removeOne(mapping.first);
148 178
149 QStandardItem* lastItem = nullptr;179 QString fullPath = "/";
150 180
151 // start index at 1 to avoid empty string from leading slash181 QStandardItem* lastItem = nullptr;
152 for (int i = 1; i < length; ++i) {182
153 fullPath += (i == 1 ? "" : "/") + parts[i];183 // start index at 1 to avoid empty string from leading slash
154 184 for (int i = 1; i < length; ++i) {
155 auto it = _pathToItemMap.find(fullPath);185 fullPath += (i == 1 ? "" : "/") + parts[i];
156 if (it == _pathToItemMap.end()) {186
157 qDebug() << "prefix not found: " << fullPath;187 auto it = _pathToItemMap.find(fullPath);
158 auto item = new QStandardItem(parts[i]);188 if (it == _pathToItemMap.end()) {
159 bool isFolder = i < length - 1;189 qDebug() << "prefix not found: " << fullPath;
160 item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole);190 auto item = new QStandardItem(parts[i]);
161 item->setData(isFolder, Qt::UserRole + 1);191 bool isFolder = i < length - 1;
162 item->setData(parts[i], Qt::UserRole + 2);192 item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole);
163 item->setData("atp:" + fullPath, Qt::UserRole + 3);193 item->setData(isFolder, Qt::UserRole + 1);
164 if (lastItem) {194 item->setData(parts[i], Qt::UserRole + 2);
165 lastItem->setChild(lastItem->rowCount(), 0, item);195 item->setData("atp:" + fullPath, Qt::UserRole + 3);
166 } else {196 if (lastItem) {
167 appendRow(item);197 lastItem->setChild(lastItem->rowCount(), 0, item);
198 } else {
199 appendRow(item);
200 }
201
202 lastItem = item;
203 _pathToItemMap[fullPath] = lastItem;
204 }
205 else {
206 lastItem = it.value();
207 }
168 }208 }
169 209
170 lastItem = item;210 Q_ASSERT(fullPath == path);
171 _pathToItemMap[fullPath] = lastItem;
172 }211 }
173 else {
174 lastItem = it.value();
175 }
176 }
177
178 Q_ASSERT(fullPath == path);
179 }
180
181 // Remove folders from list
182 auto it = existingPaths.begin();
183 while (it != existingPaths.end()) {
184 auto item = _pathToItemMap[*it];
185 if (item->data(Qt::UserRole + 1).toBool()) {
186 it = existingPaths.erase(it);
187 } else {
188 ++it;
189 }
190 }
191 212
192 for (auto& path : existingPaths) {213 // Remove folders from list
193 Q_ASSERT(_pathToItemMap.contains(path));214 auto it = existingPaths.begin();
194 qDebug() << "removing existing: " << path;215 while (it != existingPaths.end()) {
195 216 auto item = _pathToItemMap[*it];
196 auto item = _pathToItemMap[path];217 if (item->data(Qt::UserRole + 1).toBool()) {
197 218 it = existingPaths.erase(it);
198 while (item) {
199 // During each iteration, delete item
200 QStandardItem* nextItem = nullptr;
201
202 auto parent = item->parent();
203 if (parent) {
204 parent->removeRow(item->row());
205 if (parent->rowCount() > 0) {
206 // The parent still contains children, set the nextItem to null so we stop processing
207 nextItem = nullptr;
208 } else {219 } else {
209 nextItem = parent;220 ++it;
210 }221 }
211 } else {
212 removeRow(item->row());
213 }222 }
214 223
215 _pathToItemMap.remove(path);224 for (auto& path : existingPaths) {
216 //delete item;225 Q_ASSERT(_pathToItemMap.contains(path));
217 226 qDebug() << "removing existing: " << path;
218 item = nextItem;227
228 auto item = _pathToItemMap[path];
229
230 while (item) {
231 // During each iteration, delete item
232 QStandardItem* nextItem = nullptr;
233
234 auto parent = item->parent();
235 if (parent) {
236 parent->removeRow(item->row());
237 if (parent->rowCount() > 0) {
238 // The parent still contains children, set the nextItem to null so we stop processing
239 nextItem = nullptr;
240 } else {
241 nextItem = parent;
242 }
243 } else {
244 removeRow(item->row());
245 }
246
247 _pathToItemMap.remove(path);
248 //delete item;
249
250 item = nextItem;
251 }
252 //removeitem->index();
253 }
254 } else {
255 emit errorGettingMappings(uint8_t(request->getError()));
219 }256 }
220 //removeitem->index();257 });
221 }258
222 });259 request->start();
223 260 } else {
224 request->start();261 qDebug() << "NO CONNECTED ASSET SERVER";
262 // not connected to an Asset Server, emit network error
263 emit errorGettingMappings(uint8_t(MappingRequest::NetworkError));
264 }
225}265}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t