Add script load warning

This commit is contained in:
GriffinR 2023-02-09 11:28:55 -05:00
parent e710567400
commit 7dacfeb52b
4 changed files with 36 additions and 1 deletions

View file

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

View file

@ -61,6 +61,7 @@ public:
static QJSValue dialogInput(QJSValue input, bool selectedOk);
private:
MainWindow *mainWindow;
QJSEngine *engine;
QStringList filepaths;
QList<QJSValue> modules;

View file

@ -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<QString, QString> 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;
}

View file

@ -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<Qt::CheckState>(state) != Qt::CheckState::Checked);
});
messageBox.exec();
}
continue;
}
}
logInfo(QString("Successfully loaded custom script file '%1'").arg(filepath));