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