mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 08:21:32 +02:00
normalize how ScriptCache::getScript() works relative to ResourceManager
This commit is contained in:
parent
f85cb2c888
commit
8395fb6eff
5 changed files with 28 additions and 47 deletions
|
@ -17,21 +17,31 @@
|
||||||
|
|
||||||
#include <SharedUtil.h>
|
#include <SharedUtil.h>
|
||||||
|
|
||||||
ResourceRequest* ResourceManager::createResourceRequest(QObject* parent, const QUrl& url) {
|
QUrl ResourceManager::normalizeURL(const QUrl& url) {
|
||||||
auto scheme = url.scheme();
|
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) {
|
if (scheme == URL_SCHEME_FILE) {
|
||||||
return new FileResourceRequest(parent, url);
|
return new FileResourceRequest(parent, url);
|
||||||
} else if (scheme == URL_SCHEME_HTTP || scheme == URL_SCHEME_HTTPS || scheme == URL_SCHEME_FTP) {
|
} else if (scheme == URL_SCHEME_HTTP || scheme == URL_SCHEME_HTTPS || scheme == URL_SCHEME_FTP) {
|
||||||
return new HTTPResourceRequest(parent, url);
|
return new HTTPResourceRequest(parent, url);
|
||||||
} else if (scheme == URL_SCHEME_ATP) {
|
} else if (scheme == URL_SCHEME_ATP) {
|
||||||
return new AssetResourceRequest(parent, url);
|
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();
|
qDebug() << "Unknown scheme (" << scheme << ") for URL: " << url.url();
|
||||||
|
|
|
@ -24,6 +24,7 @@ const QString URL_SCHEME_ATP = "atp";
|
||||||
|
|
||||||
class ResourceManager {
|
class ResourceManager {
|
||||||
public:
|
public:
|
||||||
|
static QUrl normalizeURL(const QUrl& url);
|
||||||
static ResourceRequest* createResourceRequest(QObject* parent, const QUrl& url);
|
static ResourceRequest* createResourceRequest(QObject* parent, const QUrl& url);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,8 @@ ScriptCache::ScriptCache(QObject* parent) {
|
||||||
// nothing to do here...
|
// 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;
|
QString scriptContents;
|
||||||
if (_scriptCache.contains(url) && !reload) {
|
if (_scriptCache.contains(url) && !reload) {
|
||||||
qCDebug(scriptengine) << "Found script in cache:" << url.toString();
|
qCDebug(scriptengine) << "Found script in cache:" << url.toString();
|
||||||
|
@ -50,7 +51,8 @@ QString ScriptCache::getScript(const QUrl& url, ScriptUser* scriptUser, bool& is
|
||||||
return scriptContents;
|
return scriptContents;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptCache::deleteScript(const QUrl& url) {
|
void ScriptCache::deleteScript(const QUrl& unnormalizedURL) {
|
||||||
|
QUrl url = ResourceManager::normalizeURL(unnormalizedURL);
|
||||||
if (_scriptCache.contains(url)) {
|
if (_scriptCache.contains(url)) {
|
||||||
qCDebug(scriptengine) << "Delete script from cache:" << url.toString();
|
qCDebug(scriptengine) << "Delete script from cache:" << url.toString();
|
||||||
_scriptCache.remove(url);
|
_scriptCache.remove(url);
|
||||||
|
|
|
@ -26,8 +26,8 @@ class ScriptCache : public QObject, public Dependency {
|
||||||
SINGLETON_DEPENDENCY
|
SINGLETON_DEPENDENCY
|
||||||
|
|
||||||
public:
|
public:
|
||||||
QString getScript(const QUrl& url, ScriptUser* scriptUser, bool& isPending, bool redownload = false);
|
QString getScript(const QUrl& unnormalizedURL, ScriptUser* scriptUser, bool& isPending, bool redownload = false);
|
||||||
void deleteScript(const QUrl& url);
|
void deleteScript(const QUrl& unnormalizedURL);
|
||||||
void addScriptToBadScriptList(const QUrl& url) { _badScripts.insert(url); }
|
void addScriptToBadScriptList(const QUrl& url) { _badScripts.insert(url); }
|
||||||
bool isInBadScriptList(const QUrl& url) { return _badScripts.contains(url); }
|
bool isInBadScriptList(const QUrl& url) { return _badScripts.contains(url); }
|
||||||
|
|
||||||
|
|
|
@ -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
|
// FIXME - remove the file/url scheme code here and let the script cache handle things
|
||||||
void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) {
|
void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) {
|
||||||
qDebug() << "ScriptEngine::loadURL(" << scriptURL << ", " << reload << ");";
|
|
||||||
|
|
||||||
if (_isRunning) {
|
if (_isRunning) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -286,39 +284,9 @@ void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) {
|
||||||
|
|
||||||
QUrl url(scriptURL);
|
QUrl url(scriptURL);
|
||||||
|
|
||||||
// if the scheme length is one or lower, maybe they typed in a file, let's try
|
bool isPending;
|
||||||
const int WINDOWS_DRIVE_LETTER_SIZE = 1;
|
auto scriptCache = DependencyManager::get<ScriptCache>();
|
||||||
if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) {
|
scriptCache->getScript(url, this, isPending, reload);
|
||||||
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<ScriptCache>();
|
|
||||||
qDebug() << "calling scriptCache->getScript(" << url << ", " << reload << ");";
|
|
||||||
scriptCache->getScript(url, this, isPending, reload);
|
|
||||||
qDebug() << " scriptCache->getScript(" << url << ", " << reload << ") ... isPending:" << isPending;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptEngine::scriptContentsAvailable(const QUrl& url, const QString& scriptContents) {
|
void ScriptEngine::scriptContentsAvailable(const QUrl& url, const QString& scriptContents) {
|
||||||
|
|
Loading…
Reference in a new issue