Add QJSEngine compatible AssetMappingScriptInterface

This commit is contained in:
Ryan Huffman 2016-03-08 16:53:04 -08:00
parent 2e9ee4f8a7
commit fd5a5dd8f4
3 changed files with 177 additions and 0 deletions

View file

@ -1267,6 +1267,7 @@ void Application::initializeUi() {
rootContext->setContextProperty("Quat", new Quat());
rootContext->setContextProperty("Vec3", new Vec3());
rootContext->setContextProperty("Uuid", new ScriptUUID());
rootContext->setContextProperty("Assets", new AssetMappingsScriptingInterface(engine));
rootContext->setContextProperty("AvatarList", DependencyManager::get<AvatarManager>().data());

View file

@ -581,3 +581,145 @@ void AssetClient::handleNodeKilled(SharedNodePointer node) {
_mappingCache.clear();
}
void AssetScriptingInterface::uploadData(QString data, QScriptValue callback) {
QByteArray dataByteArray = data.toUtf8();
auto upload = DependencyManager::get<AssetClient>()->createUpload(dataByteArray);
if (!upload) {
qCWarning(asset_client) << "Error uploading file to asset server";
return;
}
QObject::connect(upload, &AssetUpload::finished, this, [this, callback](AssetUpload* upload, const QString& hash) mutable {
if (callback.isFunction()) {
QString url = "atp://" + hash;
QScriptValueList args { url };
callback.call(_engine->currentContext()->thisObject(), args);
}
});
upload->start();
}
AssetScriptingInterface::AssetScriptingInterface(QScriptEngine* engine) :
_engine(engine)
{
}
void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callback) {
const QString ATP_SCHEME { "atp://" };
if (!urlString.startsWith(ATP_SCHEME)) {
return;
}
// Make request to atp
auto path = urlString.right(urlString.length() - ATP_SCHEME.length());
auto parts = path.split(".", QString::SkipEmptyParts);
auto hash = parts.length() > 0 ? parts[0] : "";
if (hash.length() != SHA256_HASH_HEX_LENGTH) {
return;
}
auto assetClient = DependencyManager::get<AssetClient>();
auto assetRequest = assetClient->createRequest(hash);
if (!assetRequest) {
return;
}
_pendingRequests << assetRequest;
connect(assetRequest, &AssetRequest::finished, this, [this, callback](AssetRequest* request) mutable {
Q_ASSERT(request->getState() == AssetRequest::Finished);
if (request->getError() == AssetRequest::Error::NoError) {
if (callback.isFunction()) {
QString data = QString::fromUtf8(request->getData());
QScriptValueList args { data };
callback.call(_engine->currentContext()->thisObject(), args);
}
}
request->deleteLater();
_pendingRequests.remove(request);
});
assetRequest->start();
}
AssetMappingsScriptingInterface::AssetMappingsScriptingInterface(QJSEngine* engine) :
_engine(engine)
{
}
void AssetMappingsScriptingInterface::setMapping(QString path, QString hash, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createSetMappingRequest(path, hash);
connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::getMapping(QString path, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetMappingRequest(path);
connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()), request->getHash() };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createDeleteMappingsRequest(paths);
connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetAllMappingsRequest();
connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings();
auto map = callback.engine()->newObject();
for (auto& kv : mappings ) {
map.setProperty(kv.first, kv.second);
}
QJSValueList args { uint8_t(request->getError()), map };
callback.call(args);
request->deleteLater();
});
request->start();
}

View file

@ -12,6 +12,7 @@
#ifndef hifi_AssetClient_h
#define hifi_AssetClient_h
#include <QtQml/QJSEngine>
#include <QString>
#include <map>
@ -106,4 +107,37 @@ private:
friend class RenameMappingRequest;
};
class AssetScriptingInterface : public QObject {
Q_OBJECT
public:
AssetScriptingInterface(QScriptEngine* engine);
Q_INVOKABLE void uploadData(QString data, QScriptValue callback);
Q_INVOKABLE void downloadData(QString url, QScriptValue downloadComplete);
// Q_INVOKABLE void setMapping(QString path, QString hash, QScriptValue callback);
// Q_INVOKABLE void getMapping(QString path, QScriptValue callback);
// Q_INVOKABLE void deleteMappings(QStringList paths, QScriptValue callback);
// Q_INVOKABLE void getAllMappings(QScriptValue callback);
protected:
QSet<AssetRequest*> _pendingRequests;
QScriptEngine* _engine;
};
class AssetMappingsScriptingInterface : public QObject {
Q_OBJECT
public:
AssetMappingsScriptingInterface(QJSEngine* engine);
Q_INVOKABLE void setMapping(QString path, QString hash, QJSValue callback);
Q_INVOKABLE void getMapping(QString path, QJSValue callback);
Q_INVOKABLE void deleteMappings(QStringList paths, QJSValue callback);
Q_INVOKABLE void getAllMappings(QJSValue callback);
protected:
QSet<AssetRequest*> _pendingRequests;
QJSEngine* _engine;
};
#endif