Merge pull request #6749 from jherico/resource_override

Allow a script to set a resource override.
This commit is contained in:
Brad Hefta-Gaub 2016-01-04 09:17:38 -08:00
commit 5b177404d5
6 changed files with 68 additions and 5 deletions

View file

@ -30,6 +30,7 @@
#include <SharedUtil.h>
#include <ShutdownEventListener.h>
#include <SoundCache.h>
#include <ResourceScriptingInterface.h>
#include "AssignmentFactory.h"
#include "AssignmentActionFactory.h"
@ -61,6 +62,7 @@ AssignmentClient::AssignmentClient(Assignment::Type requestAssignmentType, QStri
DependencyManager::registerInheritance<EntityActionFactoryInterface, AssignmentActionFactory>();
auto actionFactory = DependencyManager::set<AssignmentActionFactory>();
DependencyManager::set<ResourceScriptingInterface>();
// setup a thread for the NodeList and its PacketReceiver
QThread* nodeThread = new QThread(this);

View file

@ -52,6 +52,7 @@
#include <gl/Config.h>
#include <gl/QOpenGLContextWrapper.h>
#include <ResourceScriptingInterface.h>
#include <AccountManager.h>
#include <AddressManager.h>
#include <ApplicationVersion.h>
@ -342,6 +343,8 @@ bool setupEssentials(int& argc, char** argv) {
DependencyManager::set<RecordingScriptingInterface>();
DependencyManager::set<WindowScriptingInterface>();
DependencyManager::set<HMDScriptingInterface>();
DependencyManager::set<ResourceScriptingInterface>();
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
DependencyManager::set<SpeechRecognizer>();

View file

@ -23,16 +23,27 @@ QMutex ResourceManager::_prefixMapLock;
void ResourceManager::setUrlPrefixOverride(const QString& prefix, const QString& replacement) {
QMutexLocker locker(&_prefixMapLock);
_prefixMap[prefix] = replacement;
if (replacement.isEmpty()) {
_prefixMap.erase(prefix);
} else {
_prefixMap[prefix] = replacement;
}
}
QString ResourceManager::normalizeURL(const QString& urlString) {
QString result = urlString;
QMutexLocker locker(&_prefixMapLock);
foreach(const auto& entry, _prefixMap) {
PrefixMap copy;
{
QMutexLocker locker(&_prefixMapLock);
copy = _prefixMap;
}
foreach(const auto& entry, copy) {
const auto& prefix = entry.first;
const auto& replacement = entry.second;
if (result.startsWith(prefix)) {
qDebug() << "Replacing " << prefix << " with " << replacement;
result.replace(0, prefix.size(), replacement);
}
}

View file

@ -0,0 +1,15 @@
//
// Created by Bradley Austin Davis on 2015/12/29
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "ResourceScriptingInterface.h"
#include "ResourceManager.h"
void ResourceScriptingInterface::overrideUrlPrefix(const QString& prefix, const QString& replacement) {
ResourceManager::setUrlPrefixOverride(prefix, replacement);
}

View file

@ -0,0 +1,31 @@
//
// AssetClient.h
// libraries/networking/src
//
// Created by Ryan Huffman on 2015/07/21
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_networking_ResourceScriptingInterface_h
#define hifi_networking_ResourceScriptingInterface_h
#include <QtCore/QObject>
#include <DependencyManager.h>
class ResourceScriptingInterface : public QObject, public Dependency {
Q_OBJECT
public:
Q_INVOKABLE void overrideUrlPrefix(const QString& prefix, const QString& replacement);
Q_INVOKABLE void restoreUrlPrefix(const QString& prefix) {
overrideUrlPrefix(prefix, "");
}
};
#endif

View file

@ -26,6 +26,7 @@
#include <EntityScriptingInterface.h>
#include <MessagesClient.h>
#include <NetworkAccessManager.h>
#include <ResourceScriptingInterface.h>
#include <NodeList.h>
#include <udt/PacketHeaders.h>
#include <UUID.h>
@ -391,7 +392,7 @@ void ScriptEngine::init() {
registerGlobalObject("Recording", recordingInterface.data());
registerGlobalObject("Assets", &_assetScriptingInterface);
registerGlobalObject("Resources", DependencyManager::get<ResourceScriptingInterface>().data());
}
void ScriptEngine::registerValue(const QString& valueName, QScriptValue value) {
@ -1296,4 +1297,4 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS
entityScript.property(methodName).call(entityScript, args);
}
}
}
}