Showing the changes to libraries/script-engine/src/AssetScriptingInterface.cpp (1 of 2 files). Show all
Unified · Split
| @@ -42,12 +42,12 @@ void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) { · 41 unmodified lines | ||
| 42 | 42 | QByteArray dataByteArray = data.toUtf8(); |
| 43 | 43 | auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray); |
| 44 | 44 | |
| 45 | − Promise deferred = makePromise(__FUNCTION__) | |
| 46 | − ->ready([this, handler](QString error, QVariantMap result) { | |
| 47 | − auto url = result.value("url").toString(); | |
| 48 | − auto hash = result.value("hash").toString(); | |
| 49 | − jsCallback(handler, url, hash); | |
| 50 | − }); | |
| 45 | + Promise deferred = makePromise(__FUNCTION__); | |
| 46 | + deferred->ready([this, handler](QString error, QVariantMap result) { | |
| 47 | + auto url = result.value("url").toString(); | |
| 48 | + auto hash = result.value("hash").toString(); | |
| 49 | + jsCallback(handler, url, hash); | |
| 50 | + }); | |
| 51 | 51 | |
| 52 | 52 | connect(upload, &AssetUpload::finished, upload, [this, deferred](AssetUpload* upload, const QString& hash) { |
| 53 | 53 | // we are now on the "Resource Manager" thread (and "hash" being a *reference* makes it unsafe to use directly) |
| @@ -64,10 +64,10 @@ void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) { · 10 unmodified lines | ||
| 64 | 64 | void AssetScriptingInterface::setMapping(QString path, QString hash, QScriptValue callback) { |
| 65 | 65 | auto handler = makeScopedHandlerObject(thisObject(), callback); |
| 66 | 66 | auto setMappingRequest = assetClient()->createSetMappingRequest(path, hash); |
| 67 | − Promise deferred = makePromise(__FUNCTION__) | |
| 68 | − ->ready([=](QString error, QVariantMap result) { | |
| 69 | − jsCallback(handler, error, result); | |
| 70 | − }); | |
| 67 | + Promise deferred = makePromise(__FUNCTION__); | |
| 68 | + deferred->ready([=](QString error, QVariantMap result) { | |
| 69 | + jsCallback(handler, error, result); | |
| 70 | + }); | |
| 71 | 71 | |
| 72 | 72 | connect(setMappingRequest, &SetMappingRequest::finished, setMappingRequest, [this, deferred](SetMappingRequest* request) { |
| 73 | 73 | Q_ASSERT(QThread::currentThread() == request->thread()); |
| @@ -96,11 +96,11 @@ void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callb · 22 unmodified lines | ||
| 96 | 96 | auto assetClient = DependencyManager::get<AssetClient>(); |
| 97 | 97 | auto assetRequest = assetClient->createRequest(hash); |
| 98 | 98 | |
| 99 | − Promise deferred = makePromise(__FUNCTION__) | |
| 100 | − ->ready([=](QString error, QVariantMap result) { | |
| 101 | − // FIXME: to remain backwards-compatible the signature here is "callback(data, n/a)" | |
| 102 | − jsCallback(handler, result.value("data").toString(), { { "errorMessage", error } }); | |
| 103 | − }); | |
| 99 | + Promise deferred = makePromise(__FUNCTION__); | |
| 100 | + deferred->ready([=](QString error, QVariantMap result) { | |
| 101 | + // FIXME: to remain backwards-compatible the signature here is "callback(data, n/a)" | |
| 102 | + jsCallback(handler, result.value("data").toString(), { { "errorMessage", error } }); | |
| 103 | + }); | |
| 104 | 104 | |
| 105 | 105 | connect(assetRequest, &AssetRequest::finished, assetRequest, [this, deferred](AssetRequest* request) { |
| 106 | 106 | Q_ASSERT(QThread::currentThread() == request->thread()); |
| @@ -127,10 +127,10 @@ void AssetScriptingInterface::setBakingEnabled(QString path, bool enabled, QScri · 20 unmodified lines | ||
| 127 | 127 | auto handler = makeScopedHandlerObject(thisObject(), callback); |
| 128 | 128 | auto setBakingEnabledRequest = DependencyManager::get<AssetClient>()->createSetBakingEnabledRequest({ path }, enabled); |
| 129 | 129 | |
| 130 | − Promise deferred = makePromise(__FUNCTION__) | |
| 131 | − ->ready([=](QString error, QVariantMap result) { | |
| 132 | − jsCallback(handler, error, result); | |
| 133 | − }); | |
| 130 | + Promise deferred = makePromise(__FUNCTION__); | |
| 131 | + deferred->ready([=](QString error, QVariantMap result) { | |
| 132 | + jsCallback(handler, error, result); | |
| 133 | + }); | |
| 134 | 134 | |
| 135 | 135 | connect(setBakingEnabledRequest, &SetBakingEnabledRequest::finished, setBakingEnabledRequest, [this, deferred](SetBakingEnabledRequest* request) { |
| 136 | 136 | Q_ASSERT(QThread::currentThread() == request->thread()); |
| @@ -160,14 +160,13 @@ void AssetScriptingInterface::getMapping(QString asset, QScriptValue callback) { · 23 unmodified lines | ||
| 160 | 160 | JS_ASSERT(AssetUtils::isValidFilePath(path), "invalid ATP file path: " + asset + "(path:"+path+")"); |
| 161 | 161 | JS_ASSERT(callback.isFunction(), "expected second parameter to be a callback function"); |
| 162 | 162 | qDebug() << ">>getMapping//getAssetInfo" << path; |
| 163 | − getAssetInfo(path)->ready([this, handler](QString error, QVariantMap result) { | |
| 163 | + Promise promise = getAssetInfo(path); | |
| 164 | + promise->ready([this, handler](QString error, QVariantMap result) { | |
| 164 | 165 | qDebug() << "//getMapping//getAssetInfo" << error << result.keys(); |
| 165 | 166 | jsCallback(handler, error, result.value("hash").toString()); |
| 166 | 167 | }); |
| 167 | 168 | } |
| 168 | 169 | |
| 169 | −/////////////////////////// new APIS //////////////////////////////////// | |
| 170 | − | |
| 171 | 170 | bool AssetScriptingInterface::jsAssert(bool condition, const QString& error) { |
| 172 | 171 | if (condition) { |
| 173 | 172 | return true; |
| @@ -187,10 +186,6 @@ void AssetScriptingInterface::jsCallback(const QScriptValue& handler, · 13 unmodified lines | ||
| 187 | 186 | JS_ASSERT(handler.isObject() && handler.property("callback").isFunction(), |
| 188 | 187 | QString("jsCallback -- .callback is not a function (%1)") |
| 189 | 188 | .arg(handler.property("callback").toVariant().typeName())); |
| 190 | −#if 1 || DEGUG_JSCALLBACK | |
| 191 | − QScriptValue debug = result; | |
| 192 | − debug.setProperty("toString", handler.engine()->evaluate("1,function() { return JSON.stringify(this, 0, 2); }")); | |
| 193 | −#endif | |
| 194 | 189 | ::callScopedHandlerObject(handler, errorValue, result); |
| 195 | 190 | } |
| 196 | 191 | |
| @@ -228,8 +223,8 @@ void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, · 31 unmodified lines | ||
| 228 | 223 | JS_ASSERT(RESPONSE_TYPES.contains(responseType), |
| 229 | 224 | QString("Invalid responseType: '%1' (expected: %2)").arg(responseType).arg(RESPONSE_TYPES.join(" | "))); |
| 230 | 225 | |
| 231 | − Promise resolved = makePromise("resolved"), | |
| 232 | − loaded = makePromise("loaded"); | |
| 226 | + Promise resolved = makePromise("resolved"); | |
| 227 | + Promise loaded = makePromise("loaded"); | |
| 233 | 228 | |
| 234 | 229 | loaded->ready([=](QString error, QVariantMap result) { |
| 235 | 230 | qDebug() << "//loaded" << error; |
| @@ -242,9 +237,9 @@ void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, · 6 unmodified lines | ||
| 242 | 237 | if (!error.isEmpty() || !AssetUtils::isValidHash(hash)) { |
| 243 | 238 | loaded->reject(error.isEmpty() ? "internal hash error: " + hash : error, result); |
| 244 | 239 | } else { |
| 245 | − loadAsset(hash, decompress, responseType) | |
| 246 | − ->mixin(result) | |
| 247 | − ->ready([this, loaded, hash](QString error, QVariantMap result) { | |
| 240 | + Promise promise = loadAsset(hash, decompress, responseType); | |
| 241 | + promise->mixin(result); | |
| 242 | + promise->ready([this, loaded, hash](QString error, QVariantMap result) { | |
| 248 | 243 | qDebug() << "//getAssetInfo/loadAsset" << error << hash; |
| 249 | 244 | loaded->resolve(NoError, result); |
| 250 | 245 | }); |
| @@ -254,11 +249,11 @@ void AssetScriptingInterface::getAsset(QScriptValue options, QScriptValue scope, · 3 unmodified lines | ||
| 254 | 249 | if (AssetUtils::isValidHash(asset)) { |
| 255 | 250 | resolved->resolve(NoError, { { "hash", asset } }); |
| 256 | 251 | } else { |
| 257 | − getAssetInfo(asset) | |
| 258 | − ->ready([this, resolved](QString error, QVariantMap result) { | |
| 259 | − qDebug() << "//getAssetInfo" << error << result.value("hash") << result.value("path"); | |
| 252 | + Promise promise = getAssetInfo(asset); | |
| 253 | + promise->ready([this, resolved](QString error, QVariantMap result) { | |
| 254 | + qDebug() << "//getAssetInfo" << error << result.value("hash") << result.value("path"); | |
| 260 | 255 | resolved->resolve(error, result); |
| 261 | − }); | |
| 256 | + }); | |
| 262 | 257 | } |
| 263 | 258 | } |
| 264 | 259 | |
| @@ -286,34 +281,40 @@ void AssetScriptingInterface::decompressData(QScriptValue options, QScriptValue · 21 unmodified lines | ||
| 286 | 281 | if (responseType.isEmpty()) { |
| 287 | 282 | responseType = "text"; |
| 288 | 283 | } |
| 289 | − decompressBytes(dataByteArray) | |
| 290 | − ->ready([=](QString error, QVariantMap result) { | |
| 291 | − if (responseType == "arraybuffer") { | |
| 284 | + Promise promise = decompressBytes(dataByteArray); | |
| 285 | + promise->ready([=](QString error, QVariantMap result) { | |
| 286 | + if (responseType == "arraybuffer") { | |
| 287 | + jsCallback(handler, error, result); | |
| 288 | + } else { | |
| 289 | + Promise promise = convertBytes(result.value("data").toByteArray(), responseType); | |
| 290 | + promise->mixin(result); | |
| 291 | + promise->ready([=](QString error, QVariantMap result) { | |
| 292 | 292 | jsCallback(handler, error, result); |
| 293 | − } else { | |
| 294 | − convertBytes(result.value("data").toByteArray(), responseType) | |
| 295 | − ->mixin(result) | |
| 296 | − ->ready([=](QString error, QVariantMap result) { | |
| 297 | − jsCallback(handler, error, result); | |
| 298 | − }); | |
| 299 | − } | |
| 293 | + }); | |
| 294 | + } | |
| 300 | 295 | }); |
| 301 | 296 | } |
| 302 | 297 | |
| 298 | +namespace { | |
| 299 | + const int32_t DEFAULT_GZIP_COMPRESSION_LEVEL = -1; | |
| 300 | + const int32_t MAX_GZIP_COMPRESSION_LEVEL = 9; | |
| 301 | +} | |
| 302 | + | |
| 303 | 303 | void AssetScriptingInterface::compressData(QScriptValue options, QScriptValue scope, QScriptValue callback) { |
| 304 | + | |
| 304 | 305 | auto data = options.property("data"); |
| 305 | 306 | QByteArray dataByteArray = data.isString() ? |
| 306 | 307 | data.toString().toUtf8() : |
| 307 | 308 | qscriptvalue_cast<QByteArray>(data); |
| 308 | 309 | auto handler = makeScopedHandlerObject(scope, callback); |
| 309 | 310 | auto level = options.property("level").toInt32(); |
| 310 | − if (level < -1 || level > 9) { | |
| 311 | − level = -1; | |
| 311 | + if (level < DEFAULT_GZIP_COMPRESSION_LEVEL || level > MAX_GZIP_COMPRESSION_LEVEL) { | |
| 312 | + level = DEFAULT_GZIP_COMPRESSION_LEVEL; | |
| 312 | 313 | } |
| 313 | − compressBytes(dataByteArray, level) | |
| 314 | − ->ready([=](QString error, QVariantMap result) { | |
| 315 | − jsCallback(handler, error, result); | |
| 316 | − }); | |
| 314 | + Promise promise = compressBytes(dataByteArray, level); | |
| 315 | + promise->ready([=](QString error, QVariantMap result) { | |
| 316 | + jsCallback(handler, error, result); | |
| 317 | + }); | |
| 317 | 318 | } |
| 318 | 319 | |
| 319 | 320 | void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, QScriptValue callback) { |
| @@ -327,7 +328,6 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 7 unmodified lines | ||
| 327 | 328 | QByteArray dataByteArray = data.isString() ? |
| 328 | 329 | data.toString().toUtf8() : |
| 329 | 330 | qscriptvalue_cast<QByteArray>(data); |
| 330 | − //auto rawByteLength = dataByteArray.size(); | |
| 331 | 331 | |
| 332 | 332 | JS_ASSERT(path.isEmpty() || AssetUtils::isValidFilePath(path), |
| 333 | 333 | QString("expected valid ATP file path '%1' ('%2')").arg(rawPath).arg(path)); |
| @@ -338,18 +338,17 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 4 unmodified lines | ||
| 338 | 338 | .arg(data.toVariant().typeName()) |
| 339 | 339 | .arg(dataByteArray.size())); |
| 340 | 340 | |
| 341 | − | |
| 342 | 341 | // [compressed] => uploaded to server => [mapped to path] |
| 343 | − Promise prepared = makePromise("putAsset::prepared"), | |
| 344 | − uploaded = makePromise("putAsset::uploaded"), | |
| 345 | − finished = makePromise("putAsset::finished"); | |
| 342 | + Promise prepared = makePromise("putAsset::prepared"); | |
| 343 | + Promise uploaded = makePromise("putAsset::uploaded"); | |
| 344 | + Promise finished = makePromise("putAsset::finished"); | |
| 346 | 345 | |
| 347 | 346 | if (compress) { |
| 348 | 347 | qDebug() << "putAsset::compressBytes..."; |
| 349 | − compressBytes(dataByteArray, -1) | |
| 350 | − ->finally([=](QString error, QVariantMap result) { | |
| 351 | − qDebug() << "//putAsset::compressedBytes" << error << result.keys(); | |
| 352 | − prepared->handle(error, result); | |
| 348 | + Promise promise = compressBytes(dataByteArray, DEFAULT_GZIP_COMPRESSION_LEVEL); | |
| 349 | + promise->finally([=](QString error, QVariantMap result) { | |
| 350 | + qDebug() << "//putAsset::compressedBytes" << error << result.keys(); | |
| 351 | + prepared->handle(error, result); | |
| 353 | 352 | }); |
| 354 | 353 | } else { |
| 355 | 354 | prepared->resolve(NoError, {{ "data", dataByteArray }}); |
| @@ -357,12 +356,12 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 1 unmodified line | ||
| 357 | 356 | |
| 358 | 357 | prepared->ready([=](QString error, QVariantMap result) { |
| 359 | 358 | qDebug() << "//putAsset::prepared" << error << result.value("data").toByteArray().size() << result.keys(); |
| 360 | − uploadBytes(result.value("data").toByteArray()) | |
| 361 | − ->mixin(result) | |
| 362 | − ->ready([=](QString error, QVariantMap result) { | |
| 363 | − qDebug() << "===========//putAsset::prepared/uploadBytes" << error << result.keys(); | |
| 364 | − uploaded->handle(error, result); | |
| 365 | − }); | |
| 359 | + Promise promise = uploadBytes(result.value("data").toByteArray()); | |
| 360 | + promise->mixin(result); | |
| 361 | + promise->ready([=](QString error, QVariantMap result) { | |
| 362 | + qDebug() << "===========//putAsset::prepared/uploadBytes" << error << result.keys(); | |
| 363 | + uploaded->handle(error, result); | |
| 364 | + }); | |
| 366 | 365 | }); |
| 367 | 366 | |
| 368 | 367 | uploaded->ready([=](QString error, QVariantMap result) { |
| @@ -374,10 +373,10 @@ void AssetScriptingInterface::putAsset(QScriptValue options, QScriptValue scope, · 5 unmodified lines | ||
| 374 | 373 | finished->reject("path mapping requested, but did not receive valid hash", result); |
| 375 | 374 | } else { |
| 376 | 375 | qDebug() << "symlinkAsset" << hash << path << QThread::currentThread(); |
| 377 | − symlinkAsset(hash, path) | |
| 378 | − ->mixin(result) | |
| 379 | − ->ready([=](QString error, QVariantMap result) { | |
| 380 | − finished->handle(error, result); | |
| 376 | + Promise promise = symlinkAsset(hash, path); | |
| 377 | + promise->mixin(result); | |
| 378 | + promise->ready([=](QString error, QVariantMap result) { | |
| 379 | + finished->handle(error, result); | |
| 381 | 380 | qDebug() << "//symlinkAsset" << hash << path << result.keys(); |
| 382 | 381 | }); |
| 383 | 382 | } |