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
2929 auto assetClient = DependencyManager::get<AssetClient>();
3030 auto request = assetClient->createSetMappingRequest(path, hash);
3131
32− connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {
33− QJSValueList args { uint8_t(request->getError()) };
32+ if (request) {
33+ connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {
34+ QJSValueList args { uint8_t(request->getError()) };
3435
35− callback.call(args);
36+ callback.call(args);
3637
37− request->deleteLater();
38−
39− });
38+ request->deleteLater();
4039
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+ }
4248 }
4349
4450 void AssetMappingsScriptingInterface::getMapping(QString path, QJSValue callback) {
4551 auto assetClient = DependencyManager::get<AssetClient>();
4652 auto request = assetClient->createGetMappingRequest(path);
4753
48− connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {
49− QJSValueList args { uint8_t(request->getError()), request->getHash() };
54+ if (request) {
55+ connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {
56+ QJSValueList args { uint8_t(request->getError()), request->getHash() };
5057
51− callback.call(args);
58+ callback.call(args);
5259
53− request->deleteLater();
54−
55− });
60+ request->deleteLater();
5661
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+ }
5870 }
5971
6072 void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) {
6173 auto assetClient = DependencyManager::get<AssetClient>();
6274 auto request = assetClient->createDeleteMappingsRequest(paths);
6375
64− connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {
65− QJSValueList args { uint8_t(request->getError()) };
76+ if (request) {
77+ connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {
78+ QJSValueList args { uint8_t(request->getError()) };
6679
67− callback.call(args);
80+ callback.call(args);
6881
69− request->deleteLater();
70−
71− });
82+ request->deleteLater();
7283
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+ }
7492 }
7593
7694 void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) {
7795 auto assetClient = DependencyManager::get<AssetClient>();
7896 auto request = assetClient->createGetAllMappingsRequest();
7997
80− connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {
81− auto mappings = request->getMappings();
82− auto map = callback.engine()->newObject();
98+ if (request) {
99+ connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {
100+ auto mappings = request->getMappings();
101+ auto map = callback.engine()->newObject();
83102
84− for (auto& kv : mappings ) {
85− map.setProperty(kv.first, kv.second);
86− }
87−
88− QJSValueList args { uint8_t(request->getError()), map };
103+ for (auto& kv : mappings ) {
104+ map.setProperty(kv.first, kv.second);
105+ }
89106
90− callback.call(args);
107+ QJSValueList args { uint8_t(request->getError()), map };
91108
92− request->deleteLater();
93−
94− });
109+ callback.call(args);
95110
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+ }
97121 }
98122
99123 void AssetMappingsScriptingInterface::renameMapping(QString oldPath, QString newPath, QJSValue callback) {
100124 auto assetClient = DependencyManager::get<AssetClient>();
101125 auto request = assetClient->createRenameMappingRequest(oldPath, newPath);
102126
103− connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable {
104− QJSValueList args { uint8_t(request->getError()) };
127+ if (request) {
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+ });
105136
137+ request->start();
138+ } else {
139+ // not connected to an Asset Server, return network error
140+ QJSValueList args { uint8_t(MappingRequest::NetworkError) };
106141 callback.call(args);
107−
108− request->deleteLater();
109−
110− });
111−
112− request->start();
142+ }
113143 }
114144
115145
@@ -134,92 +164,102 @@ void AssetMappingModel::refresh() { · 18 unmodified lines
134164 auto assetClient = DependencyManager::get<AssetClient>();
135165 auto request = assetClient->createGetAllMappingsRequest();
136166
137− connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable {
138− auto mappings = request->getMappings();
139− auto existingPaths = _pathToItemMap.keys();
140− for (auto& mapping : mappings) {
141− auto& path = mapping.first;
142− auto parts = path.split("/");
143− auto length = parts.length();
144−
145− existingPaths.removeOne(mapping.first);
146−
147− QString fullPath = "/";
148−
149− QStandardItem* lastItem = nullptr;
150−
151− // start index at 1 to avoid empty string from leading slash
152− for (int i = 1; i < length; ++i) {
153− fullPath += (i == 1 ? "" : "/") + parts[i];
154−
155− auto it = _pathToItemMap.find(fullPath);
156− if (it == _pathToItemMap.end()) {
157− qDebug() << "prefix not found: " << fullPath;
158− auto item = new QStandardItem(parts[i]);
159− bool isFolder = i < length - 1;
160− item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole);
161− item->setData(isFolder, Qt::UserRole + 1);
162− item->setData(parts[i], Qt::UserRole + 2);
163− item->setData("atp:" + fullPath, Qt::UserRole + 3);
164− if (lastItem) {
165− lastItem->setChild(lastItem->rowCount(), 0, item);
166− } else {
167− appendRow(item);
167+ if (request) {
168+ connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable {
169+ if (request->getError() == MappingRequest::NoError) {
170+ auto mappings = request->getMappings();
171+ auto existingPaths = _pathToItemMap.keys();
172+ for (auto& mapping : mappings) {
173+ auto& path = mapping.first;
174+ auto parts = path.split("/");
175+ auto length = parts.length();
176+
177+ existingPaths.removeOne(mapping.first);
178+
179+ QString fullPath = "/";
180+
181+ QStandardItem* lastItem = nullptr;
182+
183+ // start index at 1 to avoid empty string from leading slash
184+ for (int i = 1; i < length; ++i) {
185+ fullPath += (i == 1 ? "" : "/") + parts[i];
186+
187+ auto it = _pathToItemMap.find(fullPath);
188+ if (it == _pathToItemMap.end()) {
189+ qDebug() << "prefix not found: " << fullPath;
190+ auto item = new QStandardItem(parts[i]);
191+ bool isFolder = i < length - 1;
192+ item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole);
193+ item->setData(isFolder, Qt::UserRole + 1);
194+ item->setData(parts[i], Qt::UserRole + 2);
195+ item->setData("atp:" + fullPath, Qt::UserRole + 3);
196+ if (lastItem) {
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+ }
168208 }
169209
170− lastItem = item;
171− _pathToItemMap[fullPath] = lastItem;
210+ Q_ASSERT(fullPath == path);
172211 }
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− }
191212
192− for (auto& path : existingPaths) {
193− Q_ASSERT(_pathToItemMap.contains(path));
194− qDebug() << "removing existing: " << path;
195−
196− auto item = _pathToItemMap[path];
197−
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;
213+ // Remove folders from list
214+ auto it = existingPaths.begin();
215+ while (it != existingPaths.end()) {
216+ auto item = _pathToItemMap[*it];
217+ if (item->data(Qt::UserRole + 1).toBool()) {
218+ it = existingPaths.erase(it);
208219 } else {
209− nextItem = parent;
220+ ++it;
210221 }
211− } else {
212− removeRow(item->row());
213222 }
214223
215− _pathToItemMap.remove(path);
216− //delete item;
217−
218− item = nextItem;
224+ for (auto& path : existingPaths) {
225+ Q_ASSERT(_pathToItemMap.contains(path));
226+ qDebug() << "removing existing: " << path;
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()));
219256 }
220− //removeitem->index();
221− }
222− });
223−
224− request->start();
257+ });
258+
259+ request->start();
260+ } else {
261+ qDebug() << "NO CONNECTED ASSET SERVER";
262+ // not connected to an Asset Server, emit network error
263+ emit errorGettingMappings(uint8_t(MappingRequest::NetworkError));
264+ }
225265 }

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