diff --git a/libraries/networking/src/ResourceManager.cpp b/libraries/networking/src/ResourceManager.cpp index 8af33c5463..774664f2c8 100644 --- a/libraries/networking/src/ResourceManager.cpp +++ b/libraries/networking/src/ResourceManager.cpp @@ -17,21 +17,31 @@ #include -ResourceRequest* ResourceManager::createResourceRequest(QObject* parent, const QUrl& url) { +QUrl ResourceManager::normalizeURL(const QUrl& url) { auto scheme = url.scheme(); + if (!(scheme == URL_SCHEME_FILE || + scheme == URL_SCHEME_HTTP || scheme == URL_SCHEME_HTTPS || scheme == URL_SCHEME_FTP || + scheme == URL_SCHEME_ATP)) { + + // check the degenerative file case: on windows we can often have urls of the form c:/filename + // this checks for and works around that case. + QUrl urlWithFileScheme{ URL_SCHEME_FILE + ":///" + url.toString() }; + if (!urlWithFileScheme.toLocalFile().isEmpty()) { + return urlWithFileScheme; + } + } + return url; +} + +ResourceRequest* ResourceManager::createResourceRequest(QObject* parent, const QUrl& url) { + auto normalizedURL = normalizeURL(url); + auto scheme = normalizedURL.scheme(); if (scheme == URL_SCHEME_FILE) { return new FileResourceRequest(parent, url); } else if (scheme == URL_SCHEME_HTTP || scheme == URL_SCHEME_HTTPS || scheme == URL_SCHEME_FTP) { return new HTTPResourceRequest(parent, url); } else if (scheme == URL_SCHEME_ATP) { return new AssetResourceRequest(parent, url); - } else { - // check the degenerative file case: on windows we can often have urls of the form c:/filename - // this checks for and works around that case. - QUrl urlWithFileScheme { URL_SCHEME_FILE + ":///" + url.toString() }; - if (!urlWithFileScheme.toLocalFile().isEmpty()) { - return new FileResourceRequest(parent, urlWithFileScheme); - } } qDebug() << "Unknown scheme (" << scheme << ") for URL: " << url.url(); diff --git a/libraries/networking/src/ResourceManager.h b/libraries/networking/src/ResourceManager.h index 3748036c8e..40b67b1cd1 100644 --- a/libraries/networking/src/ResourceManager.h +++ b/libraries/networking/src/ResourceManager.h @@ -24,6 +24,7 @@ const QString URL_SCHEME_ATP = "atp"; class ResourceManager { public: + static QUrl normalizeURL(const QUrl& url); static ResourceRequest* createResourceRequest(QObject* parent, const QUrl& url); }; diff --git a/libraries/script-engine/src/ScriptCache.cpp b/libraries/script-engine/src/ScriptCache.cpp index 9e04cd4ec3..38837ec4b9 100644 --- a/libraries/script-engine/src/ScriptCache.cpp +++ b/libraries/script-engine/src/ScriptCache.cpp @@ -26,7 +26,8 @@ ScriptCache::ScriptCache(QObject* parent) { // nothing to do here... } -QString ScriptCache::getScript(const QUrl& url, ScriptUser* scriptUser, bool& isPending, bool reload) { +QString ScriptCache::getScript(const QUrl& unnormalizedURL, ScriptUser* scriptUser, bool& isPending, bool reload) { + QUrl url = ResourceManager::normalizeURL(unnormalizedURL); QString scriptContents; if (_scriptCache.contains(url) && !reload) { qCDebug(scriptengine) << "Found script in cache:" << url.toString(); @@ -50,7 +51,8 @@ QString ScriptCache::getScript(const QUrl& url, ScriptUser* scriptUser, bool& is return scriptContents; } -void ScriptCache::deleteScript(const QUrl& url) { +void ScriptCache::deleteScript(const QUrl& unnormalizedURL) { + QUrl url = ResourceManager::normalizeURL(unnormalizedURL); if (_scriptCache.contains(url)) { qCDebug(scriptengine) << "Delete script from cache:" << url.toString(); _scriptCache.remove(url); diff --git a/libraries/script-engine/src/ScriptCache.h b/libraries/script-engine/src/ScriptCache.h index 25a36c04d8..89db5596ba 100644 --- a/libraries/script-engine/src/ScriptCache.h +++ b/libraries/script-engine/src/ScriptCache.h @@ -26,8 +26,8 @@ class ScriptCache : public QObject, public Dependency { SINGLETON_DEPENDENCY public: - QString getScript(const QUrl& url, ScriptUser* scriptUser, bool& isPending, bool redownload = false); - void deleteScript(const QUrl& url); + QString getScript(const QUrl& unnormalizedURL, ScriptUser* scriptUser, bool& isPending, bool redownload = false); + void deleteScript(const QUrl& unnormalizedURL); void addScriptToBadScriptList(const QUrl& url) { _badScripts.insert(url); } bool isInBadScriptList(const QUrl& url) { return _badScripts.contains(url); } diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index d9adb1f6d2..5c4623f7cf 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -275,8 +275,6 @@ bool ScriptEngine::setScriptContents(const QString& scriptContents, const QStrin // FIXME - remove the file/url scheme code here and let the script cache handle things void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) { - qDebug() << "ScriptEngine::loadURL(" << scriptURL << ", " << reload << ");"; - if (_isRunning) { return; } @@ -286,39 +284,9 @@ void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) { QUrl url(scriptURL); - // if the scheme length is one or lower, maybe they typed in a file, let's try - const int WINDOWS_DRIVE_LETTER_SIZE = 1; - if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { - url = QUrl::fromLocalFile(_fileNameString); - } - - // ok, let's see if it's valid... and if so, load it - if (url.isValid()) { - if (url.scheme() == "file") { - _fileNameString = url.toLocalFile(); - QFile scriptFile(_fileNameString); - if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { - qCDebug(scriptengine) << "ScriptEngine loading file:" << _fileNameString; - QTextStream in(&scriptFile); - _scriptContents = in.readAll(); - if (_wantSignals) { - emit scriptLoaded(_fileNameString); - } - } else { - qCDebug(scriptengine) << "ERROR Loading file:" << _fileNameString << "line:" << __LINE__; - if (_wantSignals) { - emit errorLoadingScript(_fileNameString); - } - } - } else { - bool isPending; - auto scriptCache = DependencyManager::get(); - qDebug() << "calling scriptCache->getScript(" << url << ", " << reload << ");"; - scriptCache->getScript(url, this, isPending, reload); - qDebug() << " scriptCache->getScript(" << url << ", " << reload << ") ... isPending:" << isPending; - - } - } + bool isPending; + auto scriptCache = DependencyManager::get(); + scriptCache->getScript(url, this, isPending, reload); } void ScriptEngine::scriptContentsAvailable(const QUrl& url, const QString& scriptContents) {