Skip to content

Commit 0b199de

Browse files
committed
Editor: Added support for custom config validator
It's a function in the configure.js file that can be used to validate the selected directory that it's still valid and not become damaged.
1 parent 1d3d221 commit 0b199de

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

Editor/data_configs/selection_dialog/config_manager.cpp

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ void ConfigManager::on_buttonBox_accepted()
560560
on_configList_itemDoubleClicked(ui->configList->selectedItems().first());
561561
}
562562

563-
bool ConfigManager::isConfigured()
563+
bool ConfigManager::isConfigured(PGE_JsEngine *js)
564564
{
565565
QString settingsFile = DataConfig::buildLocalConfigPath(m_currentConfigPath);
566566
if(!QFile::exists(settingsFile))
@@ -576,9 +576,20 @@ bool ConfigManager::isConfigured()
576576
QString path = settings.value("application-path", QString()).toString();
577577
settings.endGroup();
578578

579+
PGE_JsEngine loc_js;
580+
bool js_valid = js != nullptr;
581+
if(!js)
582+
{
583+
js = &loc_js;
584+
js_valid = configure_loadScript(js);
585+
}
586+
579587
// When directory got moved or deleted, config pack should be marked as not configured because got broken
580588
ret &= !path.isEmpty() && QFileInfo(path).isDir() && QDir(path).exists();
581589

590+
if(js_valid && js->hasFunction("isValidIntegration"))
591+
ret &= js->call<bool>("isValidIntegration", nullptr, path);
592+
582593
return ret;
583594
}
584595

@@ -715,18 +726,8 @@ bool ConfigManager::runConfigureTool()
715726
if(ConfStatus::configIsIntegrational)
716727
{
717728
PGE_JsEngine js;
718-
// QString cpDirName = QDir(m_currentConfigPath).dirName();
719-
QString cpSetupFile = DataConfig::buildLocalConfigPath(m_currentConfigPath);
720-
721-
js.bindProxy(new PGE_JS_Common(parentW), "PGE");
722-
js.bindProxy(new PGE_JS_File(m_currentConfigPath, cpSetupFile, parentW), "FileIO");
723-
js.bindProxy(new PGE_JS_INI(parentW), "INI");
724-
js.bindProxy(new PGE_JS_System(parentW), "System");
725729

726-
bool successfulLoaded = false;
727-
js.loadFileByExpcetedResult<void>(ConfStatus::configConfigureTool, &successfulLoaded);
728-
729-
if(successfulLoaded)
730+
if(configure_loadScript(&js))
730731
{
731732
setEnabled(false);
732733
if(!js.call<bool>("onConfigure", nullptr))
@@ -735,7 +736,7 @@ bool ConfigManager::runConfigureTool()
735736
return false;
736737
}
737738
setEnabled(true);
738-
if(!isConfigured())
739+
if(!isConfigured(&js))
739740
return false;
740741
return true;
741742
}
@@ -761,3 +762,24 @@ bool ConfigManager::runConfigureTool()
761762
return false;
762763
}
763764
}
765+
766+
bool ConfigManager::configure_loadScript(PGE_JsEngine *js)
767+
{
768+
QWidget *parentW = qobject_cast<QWidget *>(parent());
769+
if(!parentW || isVisible())
770+
parentW = this;
771+
772+
bool successfulLoaded = false;
773+
774+
// QString cpDirName = QDir(m_currentConfigPath).dirName();
775+
QString cpSetupFile = DataConfig::buildLocalConfigPath(m_currentConfigPath);
776+
777+
js->bindProxy(new PGE_JS_Common(parentW), "PGE");
778+
js->bindProxy(new PGE_JS_File(m_currentConfigPath, cpSetupFile, parentW), "FileIO");
779+
js->bindProxy(new PGE_JS_INI(parentW), "INI");
780+
js->bindProxy(new PGE_JS_System(parentW), "System");
781+
782+
js->loadFileByExpcetedResult<void>(ConfStatus::configConfigureTool, &successfulLoaded);
783+
784+
return successfulLoaded;
785+
}

Editor/data_configs/selection_dialog/config_manager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace Ui {
2929
class ConfigManager;
3030
}
3131

32+
class PGE_JsEngine;
33+
3234
class ConfigManager : public QDialog
3335
{
3436
Q_OBJECT
@@ -70,13 +72,16 @@ class ConfigManager : public QDialog
7072
* @brief Checks is current config pack configured (or non-configurable)
7173
* @return true if config pack is non-configurable or already configured
7274
*/
73-
bool isConfigured();
75+
bool isConfigured(PGE_JsEngine *js = nullptr);
7476
/**
7577
* @brief Starts configure tool if available
7678
* @return true on success configuring, false if no config tool found, rejected or script was been errored
7779
*/
7880
bool runConfigureTool();
7981

82+
private:
83+
bool configure_loadScript(PGE_JsEngine *js);
84+
8085
private slots:
8186

8287
void on_configList_itemDoubleClicked(QListWidgetItem *item);

Editor/js_engine/pge_jsengine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ class PGE_JsEngine : public QObject
6969
m_jsengine.globalObject().setProperty(regName, m_jsengine.newQObject(obj));
7070
}
7171

72+
bool hasFunction(const QString &functionName, bool *ok = nullptr)
73+
{
74+
QJSValue function = m_jsengine.evaluate(functionName);
75+
return checkForErrors(function, ok);
76+
}
77+
7278
template<typename RetVal, typename... Args>
7379
RetVal call(const QString &functionName, bool *ok, Args &&... args)
7480
{

0 commit comments

Comments
 (0)