Add a `--script` option to load scripts automatically on startup

This commit is contained in:
Bastien Orivel 2024-01-19 17:45:37 +01:00 committed by Vicki Pfau
parent 0815443aeb
commit 7cacae1262
2 changed files with 32 additions and 1 deletions

View File

@ -19,6 +19,9 @@
static const mOption s_frontendOptions[] = {
{ "ecard", true, '\0' },
{ "mb", true, '\0' },
#ifdef ENABLE_SCRIPTING
{ "script", true, '\0' },
#endif
{ 0 }
};
@ -153,7 +156,12 @@ ConfigController::ConfigController(QObject* parent)
m_subparsers[1].usage = "Frontend options:\n"
" --ecard FILE Scan an e-Reader card in the first loaded game\n"
" Can be passed multiple times for multiple cards\n"
" --mb FILE Boot a multiboot image with FILE inserted into the ROM slot";
" --mb FILE Boot a multiboot image with FILE inserted into the ROM slot"
#ifdef ENABLE_SCRIPTING
"\n --script FILE Script file to load on start"
#endif
;
m_subparsers[1].parse = nullptr;
m_subparsers[1].parseLong = [](struct mSubParser* parser, const char* option, const char* arg) {
ConfigController* self = static_cast<ConfigController*>(parser->opts);
@ -171,6 +179,17 @@ ConfigController::ConfigController(QObject* parent)
self->m_argvOptions[optionName] = QString::fromUtf8(arg);
return true;
}
#ifdef ENABLE_SCRIPTING
if (optionName == QLatin1String("script")) {
QStringList scripts;
if (self->m_argvOptions.contains(optionName)) {
scripts = self->m_argvOptions[optionName].toStringList();
}
scripts.append(QString::fromUtf8(arg));
self->m_argvOptions[optionName] = scripts;
return true;
}
#endif
return false;
};
m_subparsers[1].apply = nullptr;

View File

@ -2184,6 +2184,18 @@ void Window::setController(CoreController* controller, const QString& fname) {
m_controller->setPaused(true);
m_pendingPause = false;
}
#ifdef ENABLE_SCRIPTING
if (!m_scripting) {
QStringList scripts = m_config->getArgvOption("script").toStringList();
if (!scripts.isEmpty()) {
scriptingOpen();
for (const auto& scriptPath : scripts) {
m_scripting->loadFile(scriptPath);
}
}
}
#endif
}
void Window::attachDisplay() {