From 7dacfeb52b2616421a566badaacce25fe3357a82 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Thu, 9 Feb 2023 11:28:55 -0500 Subject: [PATCH] Add script load warning --- include/config.h | 4 ++++ include/scripting.h | 1 + src/config.cpp | 12 ++++++++++++ src/scriptapi/scripting.cpp | 20 +++++++++++++++++++- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/include/config.h b/include/config.h index af649e8f..27beb112 100644 --- a/include/config.h +++ b/include/config.h @@ -61,6 +61,7 @@ public: this->theme = "default"; this->textEditorOpenFolder = ""; this->textEditorGotoLine = ""; + this->warnScriptLoad = true; } void setRecentProject(QString project); void setReopenOnLaunch(bool enabled); @@ -78,6 +79,7 @@ public: void setShowGrid(bool enabled); void setMonitorFiles(bool monitor); void setTilesetCheckerboardFill(bool checkerboard); + void setWarnScriptLoad(bool enabled); void setTheme(QString theme); void setTextEditorOpenFolder(const QString &command); void setTextEditorGotoLine(const QString &command); @@ -97,6 +99,7 @@ public: bool getShowGrid(); bool getMonitorFiles(); bool getTilesetCheckerboardFill(); + bool getWarnScriptLoad(); QString getTheme(); QString getTextEditorOpenFolder(); QString getTextEditorGotoLine(); @@ -132,6 +135,7 @@ private: bool showGrid; bool monitorFiles; bool tilesetCheckerboardFill; + bool warnScriptLoad; QString theme; QString textEditorOpenFolder; QString textEditorGotoLine; diff --git a/include/scripting.h b/include/scripting.h index 873e24c2..28354d9b 100644 --- a/include/scripting.h +++ b/include/scripting.h @@ -61,6 +61,7 @@ public: static QJSValue dialogInput(QJSValue input, bool selectedOk); private: + MainWindow *mainWindow; QJSEngine *engine; QStringList filepaths; QList modules; diff --git a/src/config.cpp b/src/config.cpp index b1f2f03f..780e0389 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -240,6 +240,8 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) { this->monitorFiles = getConfigBool(key, value); } else if (key == "tileset_checkerboard_fill") { this->tilesetCheckerboardFill = getConfigBool(key, value); + } else if (key == "warn_script_load") { + this->warnScriptLoad = getConfigBool(key, value); } else if (key == "theme") { this->theme = value; } else if (key == "text_editor_open_directory") { @@ -275,6 +277,7 @@ QMap PorymapConfig::getKeyValueMap() { map.insert("show_grid", this->showGrid ? "1" : "0"); map.insert("monitor_files", this->monitorFiles ? "1" : "0"); map.insert("tileset_checkerboard_fill", this->tilesetCheckerboardFill ? "1" : "0"); + map.insert("warn_script_load", this->warnScriptLoad ? "1" : "0"); map.insert("theme", this->theme); map.insert("text_editor_open_directory", this->textEditorOpenFolder); map.insert("text_editor_goto_line", this->textEditorGotoLine); @@ -329,6 +332,11 @@ void PorymapConfig::setTilesetCheckerboardFill(bool checkerboard) { this->save(); } +void PorymapConfig::setWarnScriptLoad(bool enabled) { + this->warnScriptLoad = enabled; + this->save(); +} + void PorymapConfig::setMainGeometry(QByteArray mainWindowGeometry_, QByteArray mainWindowState_, QByteArray mapSplitterState_, QByteArray mainSplitterState_) { this->mainWindowGeometry = mainWindowGeometry_; @@ -486,6 +494,10 @@ bool PorymapConfig::getTilesetCheckerboardFill() { return this->tilesetCheckerboardFill; } +bool PorymapConfig::getWarnScriptLoad() { + return this->warnScriptLoad; +} + QString PorymapConfig::getTheme() { return this->theme; } diff --git a/src/scriptapi/scripting.cpp b/src/scriptapi/scripting.cpp index 97e0eaf9..a534c2e5 100644 --- a/src/scriptapi/scripting.cpp +++ b/src/scriptapi/scripting.cpp @@ -33,6 +33,7 @@ void Scripting::init(MainWindow *mainWindow) { } Scripting::Scripting(MainWindow *mainWindow) { + this->mainWindow = mainWindow; this->engine = new QJSEngine(mainWindow); this->engine->installExtensions(QJSEngine::ConsoleExtension); for (QString script : userConfig.getCustomScripts()) { @@ -48,7 +49,24 @@ void Scripting::loadModules(QStringList moduleFiles) { if (module.isError()) { QString relativePath = QDir::cleanPath(userConfig.getProjectDir() + QDir::separator() + filepath); module = this->engine->importModule(relativePath); - if (tryErrorJS(module)) continue; + if (tryErrorJS(module)) { + if (porymapConfig.getWarnScriptLoad()) { + QMessageBox messageBox(this->mainWindow); + messageBox.setText("Failed to load script"); + messageBox.setInformativeText(QString("An error occurred while loading custom script file '%1'").arg(filepath)); + messageBox.setDetailedText(getMostRecentError()); + messageBox.setIcon(QMessageBox::Warning); + messageBox.addButton(QMessageBox::Ok); + messageBox.setDefaultButton(QMessageBox::Ok); + QCheckBox * checkbox = new QCheckBox("Don't show this warning again"); + messageBox.setCheckBox(checkbox); + QObject::connect(checkbox, &QCheckBox::stateChanged, [](int state) { + porymapConfig.setWarnScriptLoad(static_cast(state) != Qt::CheckState::Checked); + }); + messageBox.exec(); + } + continue; + } } logInfo(QString("Successfully loaded custom script file '%1'").arg(filepath));