Make individual reload buttons reload scripts

This commit is contained in:
David Rowe 2015-06-15 17:10:00 -07:00
parent 2a00586e21
commit 67206332e6
8 changed files with 53 additions and 30 deletions

View file

@ -4062,6 +4062,7 @@ void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scri
connect(scriptEngine, SIGNAL(finished(const QString&)), this, SLOT(scriptFinished(const QString&))); connect(scriptEngine, SIGNAL(finished(const QString&)), this, SLOT(scriptFinished(const QString&)));
connect(scriptEngine, SIGNAL(loadScript(const QString&, bool)), this, SLOT(loadScript(const QString&, bool))); connect(scriptEngine, SIGNAL(loadScript(const QString&, bool)), this, SLOT(loadScript(const QString&, bool)));
connect(scriptEngine, SIGNAL(reloadScript(const QString&, bool)), this, SLOT(reloadScript(const QString&, bool)));
scriptEngine->registerGlobalObject("Overlays", &_overlays); scriptEngine->registerGlobalObject("Overlays", &_overlays);
qScriptRegisterMetaType(scriptEngine, OverlayPropertyResultToScriptValue, OverlayPropertyResultFromScriptValue); qScriptRegisterMetaType(scriptEngine, OverlayPropertyResultToScriptValue, OverlayPropertyResultFromScriptValue);
@ -4277,7 +4278,7 @@ bool Application::askToLoadScript(const QString& scriptFilenameOrURL) {
} }
ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUserLoaded, ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUserLoaded,
bool loadScriptFromEditor, bool activateMainWindow) { bool loadScriptFromEditor, bool activateMainWindow, bool reload) {
if (isAboutToQuit()) { if (isAboutToQuit()) {
return NULL; return NULL;
@ -4306,7 +4307,7 @@ ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUser
connect(scriptEngine, &ScriptEngine::errorLoadingScript, this, &Application::handleScriptLoadError); connect(scriptEngine, &ScriptEngine::errorLoadingScript, this, &Application::handleScriptLoadError);
// get the script engine object to load the script at the designated script URL // get the script engine object to load the script at the designated script URL
scriptEngine->loadURL(scriptUrl); scriptEngine->loadURL(scriptUrl, reload);
} }
// restore the main window's active state // restore the main window's active state
@ -4317,17 +4318,8 @@ ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUser
return scriptEngine; return scriptEngine;
} }
void Application::reloadScript(const QString& scriptFilename) { void Application::reloadScript(const QString& scriptName, bool isUserLoaded) {
DependencyManager::get<ScriptCache>()->deleteScript(scriptFilename); loadScript(scriptName, isUserLoaded, false, false, true);
ScriptEngine* scriptEngine = _scriptEnginesHash.value(scriptFilename);
connect(scriptEngine, SIGNAL(finished(const QString&)), SLOT(loadScript(const QString&)));
scriptEngine->stop();
// HACK: ATM scripts cannot set/get their animation priorities, so we clear priorities
// whenever a script stops in case it happened to have been setting joint rotations.
// TODO: expose animation priorities and provide a layered animation control system.
_myAvatar->clearScriptableSettings();
} }
void Application::handleScriptEngineLoaded(const QString& scriptFilename) { void Application::handleScriptEngineLoaded(const QString& scriptFilename) {
@ -4375,7 +4367,7 @@ void Application::stopAllScripts(bool restart) {
continue; continue;
} }
if (restart && it.value()->isUserLoaded()) { if (restart && it.value()->isUserLoaded()) {
connect(it.value(), SIGNAL(finished(const QString&)), SLOT(loadScript(const QString&))); connect(it.value(), SIGNAL(finished(const QString&)), SLOT(reloadScript(const QString&)));
} }
it.value()->stop(); it.value()->stop();
qCDebug(interfaceapp) << "stopping script..." << it.key(); qCDebug(interfaceapp) << "stopping script..." << it.key();
@ -4387,10 +4379,16 @@ void Application::stopAllScripts(bool restart) {
_myAvatar->clearScriptableSettings(); _myAvatar->clearScriptableSettings();
} }
void Application::stopScript(const QString &scriptName) { void Application::stopScript(const QString &scriptName, bool restart) {
const QString& scriptURLString = QUrl(scriptName).toString(); const QString& scriptURLString = QUrl(scriptName).toString();
if (_scriptEnginesHash.contains(scriptURLString)) { if (_scriptEnginesHash.contains(scriptURLString)) {
_scriptEnginesHash.value(scriptURLString)->stop(); ScriptEngine* scriptEngine = _scriptEnginesHash[scriptURLString];
if (restart) {
auto scriptCache = DependencyManager::get<ScriptCache>();
scriptCache->deleteScript(scriptName);
connect(scriptEngine, SIGNAL(finished(const QString&)), SLOT(reloadScript(const QString&)));
}
scriptEngine->stop();
qCDebug(interfaceapp) << "stopping script..." << scriptName; qCDebug(interfaceapp) << "stopping script..." << scriptName;
// HACK: ATM scripts cannot set/get their animation priorities, so we clear priorities // HACK: ATM scripts cannot set/get their animation priorities, so we clear priorities
// whenever a script stops in case it happened to have been setting joint rotations. // whenever a script stops in case it happened to have been setting joint rotations.
@ -4406,6 +4404,10 @@ void Application::reloadAllScripts() {
stopAllScripts(true); stopAllScripts(true);
} }
void Application::reloadOneScript(const QString& scriptName) {
stopScript(scriptName, true);
}
void Application::loadDefaultScripts() { void Application::loadDefaultScripts() {
if (!_scriptEnginesHash.contains(DEFAULT_SCRIPTS_JS_URL)) { if (!_scriptEnginesHash.contains(DEFAULT_SCRIPTS_JS_URL)) {
loadScript(DEFAULT_SCRIPTS_JS_URL); loadScript(DEFAULT_SCRIPTS_JS_URL);

View file

@ -398,16 +398,19 @@ public slots:
bool acceptSnapshot(const QString& urlString); bool acceptSnapshot(const QString& urlString);
bool askToSetAvatarUrl(const QString& url); bool askToSetAvatarUrl(const QString& url);
bool askToLoadScript(const QString& scriptFilenameOrURL); bool askToLoadScript(const QString& scriptFilenameOrURL);
ScriptEngine* loadScript(const QString& scriptFilename = QString(), bool isUserLoaded = true, ScriptEngine* loadScript(const QString& scriptFilename = QString(), bool isUserLoaded = true,
bool loadScriptFromEditor = false, bool activateMainWindow = false); bool loadScriptFromEditor = false, bool activateMainWindow = false, bool reload = false);
void reloadScript(const QString& scriptFilename); void reloadScript(const QString& scriptName, bool isUserLoaded = true);
void scriptFinished(const QString& scriptName); void scriptFinished(const QString& scriptName);
void stopAllScripts(bool restart = false); void stopAllScripts(bool restart = false);
void stopScript(const QString& scriptName); void stopScript(const QString& scriptName, bool restart = false);
void reloadAllScripts(); void reloadAllScripts();
void reloadOneScript(const QString& scriptName);
void loadDefaultScripts(); void loadDefaultScripts();
void toggleRunningScriptsWidget(); void toggleRunningScriptsWidget();
void saveScripts(); void saveScripts();
void showFriendsWindow(); void showFriendsWindow();
void friendsWindowClosed(); void friendsWindowClosed();

View file

@ -65,7 +65,7 @@ RunningScriptsWidget::RunningScriptsWidget(QWidget* parent) :
Application::getInstance(), &Application::loadDialog); Application::getInstance(), &Application::loadDialog);
connect(ui->loadScriptFromURLButton, &QPushButton::clicked, connect(ui->loadScriptFromURLButton, &QPushButton::clicked,
Application::getInstance(), &Application::loadScriptURLDialog); Application::getInstance(), &Application::loadScriptURLDialog);
connect(&_reloadSignalMapper, SIGNAL(mapped(QString)), Application::getInstance(), SLOT(reloadScript(const QString&))); connect(&_reloadSignalMapper, SIGNAL(mapped(QString)), Application::getInstance(), SLOT(reloadOneScript(const QString&)));
connect(&_stopSignalMapper, SIGNAL(mapped(QString)), Application::getInstance(), SLOT(stopScript(const QString&))); connect(&_stopSignalMapper, SIGNAL(mapped(QString)), Application::getInstance(), SLOT(stopScript(const QString&)));
UIUtil::scaleWidgetFontSizes(this); UIUtil::scaleWidgetFontSizes(this);

View file

@ -36,7 +36,7 @@ public:
const ScriptsModel* getScriptsModel() { return &_scriptsModel; } const ScriptsModel* getScriptsModel() { return &_scriptsModel; }
signals: signals:
void stopScriptName(const QString& name); void stopScriptName(const QString& name, bool restart = false);
protected: protected:
virtual bool eventFilter(QObject* sender, QEvent* event); virtual bool eventFilter(QObject* sender, QEvent* event);

View file

@ -16,16 +16,20 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QObject> #include <QObject>
#include <assert.h>
#include <NetworkAccessManager.h> #include <NetworkAccessManager.h>
#include <SharedUtil.h> #include <SharedUtil.h>
#include "ScriptEngineLogging.h"
#include "ScriptCache.h" #include "ScriptCache.h"
#include "ScriptEngineLogging.h"
ScriptCache::ScriptCache(QObject* parent) { ScriptCache::ScriptCache(QObject* parent) {
// nothing to do here... // nothing to do here...
} }
QString ScriptCache::getScript(const QUrl& url, ScriptUser* scriptUser, bool& isPending) { QString ScriptCache::getScript(const QUrl& url, ScriptUser* scriptUser, bool& isPending, bool redownload) {
assert(!_scriptCache.contains(url) || !redownload);
QString scriptContents; QString scriptContents;
if (_scriptCache.contains(url)) { if (_scriptCache.contains(url)) {
qCDebug(scriptengine) << "Found script in cache:" << url.toString(); qCDebug(scriptengine) << "Found script in cache:" << url.toString();
@ -43,8 +47,13 @@ QString ScriptCache::getScript(const QUrl& url, ScriptUser* scriptUser, bool& is
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
QNetworkRequest networkRequest = QNetworkRequest(url); QNetworkRequest networkRequest = QNetworkRequest(url);
networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT); networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
if (redownload) {
networkRequest.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork);
qCDebug(scriptengine) << "Redownloading script at:" << url.toString();
} else {
qCDebug(scriptengine) << "Downloading script at:" << url.toString();
}
qCDebug(scriptengine) << "Downloading script at:" << url.toString();
QNetworkReply* reply = networkAccessManager.get(networkRequest); QNetworkReply* reply = networkAccessManager.get(networkRequest);
connect(reply, &QNetworkReply::finished, this, &ScriptCache::scriptDownloaded); connect(reply, &QNetworkReply::finished, this, &ScriptCache::scriptDownloaded);
} }

View file

@ -26,7 +26,7 @@ class ScriptCache : public QObject, public Dependency {
SINGLETON_DEPENDENCY SINGLETON_DEPENDENCY
public: public:
QString getScript(const QUrl& url, ScriptUser* scriptUser, bool& isPending); QString getScript(const QUrl& url, ScriptUser* scriptUser, bool& isPending, bool redownload = false);
void deleteScript(const QUrl& url); void deleteScript(const QUrl& url);
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); }

View file

@ -94,6 +94,7 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam
_vec3Library(), _vec3Library(),
_uuidLibrary(), _uuidLibrary(),
_isUserLoaded(false), _isUserLoaded(false),
_isReloading(false),
_arrayBufferClass(new ArrayBufferClass(this)) _arrayBufferClass(new ArrayBufferClass(this))
{ {
_allScriptsMutex.lock(); _allScriptsMutex.lock();
@ -251,12 +252,13 @@ bool ScriptEngine::setScriptContents(const QString& scriptContents, const QStrin
return true; return true;
} }
void ScriptEngine::loadURL(const QUrl& scriptURL) { void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) {
if (_isRunning) { if (_isRunning) {
return; return;
} }
_fileNameString = scriptURL.toString(); _fileNameString = scriptURL.toString();
_isReloading = reload;
QUrl url(scriptURL); QUrl url(scriptURL);
@ -283,8 +285,7 @@ void ScriptEngine::loadURL(const QUrl& scriptURL) {
} else { } else {
bool isPending; bool isPending;
auto scriptCache = DependencyManager::get<ScriptCache>(); auto scriptCache = DependencyManager::get<ScriptCache>();
scriptCache->getScript(url, this, isPending); scriptCache->getScript(url, this, isPending, reload);
} }
} }
} }
@ -901,7 +902,13 @@ void ScriptEngine::load(const QString& loadFile) {
} }
QUrl url = resolvePath(loadFile); QUrl url = resolvePath(loadFile);
emit loadScript(url.toString(), false); if (_isReloading) {
auto scriptCache = DependencyManager::get<ScriptCache>();
scriptCache->deleteScript(url.toString());
emit reloadScript(url.toString(), false);
} else {
emit loadScript(url.toString(), false);
}
} }
void ScriptEngine::nodeKilled(SharedNodePointer node) { void ScriptEngine::nodeKilled(SharedNodePointer node) {

View file

@ -104,7 +104,7 @@ public:
Q_INVOKABLE void removeEventHandler(const EntityItemID& entityID, const QString& eventName, QScriptValue handler); Q_INVOKABLE void removeEventHandler(const EntityItemID& entityID, const QString& eventName, QScriptValue handler);
public slots: public slots:
void loadURL(const QUrl& scriptURL); void loadURL(const QUrl& scriptURL, bool reload);
void stop(); void stop();
QScriptValue evaluate(const QString& program, const QString& fileName = QString(), int lineNumber = 1); QScriptValue evaluate(const QString& program, const QString& fileName = QString(), int lineNumber = 1);
@ -132,6 +132,7 @@ signals:
void runningStateChanged(); void runningStateChanged();
void evaluationFinished(QScriptValue result, bool isException); void evaluationFinished(QScriptValue result, bool isException);
void loadScript(const QString& scriptName, bool isUserLoaded); void loadScript(const QString& scriptName, bool isUserLoaded);
void reloadScript(const QString& scriptName, bool isUserLoaded);
void doneRunning(); void doneRunning();
protected: protected:
@ -165,6 +166,7 @@ private:
Vec3 _vec3Library; Vec3 _vec3Library;
ScriptUUID _uuidLibrary; ScriptUUID _uuidLibrary;
bool _isUserLoaded; bool _isUserLoaded;
bool _isReloading;
ArrayBufferClass* _arrayBufferClass; ArrayBufferClass* _arrayBufferClass;