Low level support for URL overrides when loading content

This commit is contained in:
Brad Davis 2015-10-08 14:03:59 -07:00
parent 700c11563c
commit e5fc2e5525
4 changed files with 40 additions and 5 deletions

View file

@ -704,7 +704,9 @@ void EntityTreeRenderer::checkAndCallPreload(const EntityItemID& entityID, const
if (_tree && !_shuttingDown) {
EntityItemPointer entity = getTree()->findEntityByEntityItemID(entityID);
if (entity && !entity->getScript().isEmpty()) {
_entitiesScriptEngine->loadEntityScript(entityID, entity->getScript(), reload);
QString scriptUrl = entity->getScript();
scriptUrl = ResourceManager::normalizeURL(scriptUrl);
_entitiesScriptEngine->loadEntityScript(entityID, scriptUrl, reload);
}
}
}

View file

@ -17,7 +17,37 @@
#include <SharedUtil.h>
QUrl ResourceManager::normalizeURL(const QUrl& url) {
using PrefixMap = std::map<QString, QString>;
static PrefixMap PREFIX_MAP;
static QMutex PREFIX_MAP_LOCK;
void ResourceManager::setUrlPrefixOverride(const QString& prefix, const QString& replacement) {
QMutexLocker locker(&PREFIX_MAP_LOCK);
PREFIX_MAP[prefix] = replacement;
}
QString ResourceManager::normalizeURL(const QString& urlString) {
QString result = urlString;
QMutexLocker locker(&PREFIX_MAP_LOCK);
bool modified{ false };
foreach(const auto& entry, PREFIX_MAP) {
const auto& prefix = entry.first;
const auto& replacement = entry.second;
if (result.startsWith(prefix)) {
qDebug() << prefix;
result.replace(0, prefix.size(), replacement);
modified = true;
}
}
if (modified) {
qDebug() << result;
}
return result;
}
QUrl ResourceManager::normalizeURL(const QUrl& originalUrl) {
QUrl url = QUrl(normalizeURL(originalUrl.toString()));
auto scheme = url.scheme();
if (!(scheme == URL_SCHEME_FILE ||
scheme == URL_SCHEME_HTTP || scheme == URL_SCHEME_HTTPS || scheme == URL_SCHEME_FTP ||
@ -37,11 +67,11 @@ ResourceRequest* ResourceManager::createResourceRequest(QObject* parent, const Q
auto normalizedURL = normalizeURL(url);
auto scheme = normalizedURL.scheme();
if (scheme == URL_SCHEME_FILE) {
return new FileResourceRequest(parent, url);
return new FileResourceRequest(parent, normalizedURL);
} else if (scheme == URL_SCHEME_HTTP || scheme == URL_SCHEME_HTTPS || scheme == URL_SCHEME_FTP) {
return new HTTPResourceRequest(parent, url);
return new HTTPResourceRequest(parent, normalizedURL);
} else if (scheme == URL_SCHEME_ATP) {
return new AssetResourceRequest(parent, url);
return new AssetResourceRequest(parent, normalizedURL);
}
qDebug() << "Unknown scheme (" << scheme << ") for URL: " << url.url();

View file

@ -24,6 +24,8 @@ const QString URL_SCHEME_ATP = "atp";
class ResourceManager {
public:
static void setUrlPrefixOverride(const QString& prefix, const QString& replacement);
static QString normalizeURL(const QString& urlString);
static QUrl normalizeURL(const QUrl& url);
static ResourceRequest* createResourceRequest(QObject* parent, const QUrl& url);
};

View file

@ -88,6 +88,7 @@ void Procedural::parse(const QJsonObject& proceduralData) {
// Get the path to the shader
{
QString shaderUrl = proceduralData[URL_KEY].toString();
shaderUrl = ResourceManager::normalizeURL(shaderUrl);
_shaderUrl = QUrl(shaderUrl);
if (!_shaderUrl.isValid()) {
qWarning() << "Invalid shader URL: " << shaderUrl;