normalize how ScriptCache::getScript() works relative to ResourceManager

This commit is contained in:
Brad Hefta-Gaub 2015-09-11 21:24:39 -07:00
parent f85cb2c888
commit 8395fb6eff
5 changed files with 28 additions and 47 deletions

View file

@ -17,21 +17,31 @@
#include <SharedUtil.h>
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();

View file

@ -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);
};

View file

@ -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);

View file

@ -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); }

View file

@ -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<ScriptCache>();
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>();
scriptCache->getScript(url, this, isPending, reload);
}
void ScriptEngine::scriptContentsAvailable(const QUrl& url, const QString& scriptContents) {