2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-22 15:27:53 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-22 15:27:53 +00:00
|
|
|
// Scripting.cpp
|
|
|
|
//
|
|
|
|
// Executes custom scripts for various events in EmulationStation.
|
|
|
|
// By calling fireEvent() the scripts inside the directory corresponding to the
|
|
|
|
// argument 'eventName' will be executed with arg1 and arg2 as the script arguments.
|
|
|
|
//
|
2020-12-31 12:10:28 +00:00
|
|
|
// The scripts are searched for in ~/.emulationstation/scripts/<eventName>
|
2020-06-22 15:27:53 +00:00
|
|
|
// For example, if the event is called 'game-start', all scripts inside the directory
|
2020-12-31 12:10:28 +00:00
|
|
|
// ~/.emulationstation/scripts/game-start/ will be executed.
|
2020-06-22 15:27:53 +00:00
|
|
|
//
|
|
|
|
|
2018-01-30 00:49:08 +00:00
|
|
|
#include "Scripting.h"
|
2020-07-09 17:24:20 +00:00
|
|
|
|
2018-01-30 00:49:08 +00:00
|
|
|
#include "Log.h"
|
2020-06-21 10:26:21 +00:00
|
|
|
#include "Platform.h"
|
2020-07-09 17:24:20 +00:00
|
|
|
#include "Settings.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
2018-01-30 00:49:08 +00:00
|
|
|
|
|
|
|
namespace Scripting
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
void fireEvent(const std::string& eventName, const std::string& arg1, const std::string& arg2)
|
|
|
|
{
|
2020-07-09 17:24:20 +00:00
|
|
|
if (!Settings::getInstance()->getBool("CustomEventScripts"))
|
|
|
|
return;
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
LOG(LogDebug) << "Scripting::fireEvent(): " << eventName << " \"" << arg1 << "\" \"" << arg2
|
|
|
|
<< "\"";
|
2018-01-30 00:49:08 +00:00
|
|
|
|
|
|
|
std::list<std::string> scriptDirList;
|
2020-12-31 12:10:28 +00:00
|
|
|
std::string scriptDir;
|
2018-01-30 00:49:08 +00:00
|
|
|
|
2020-06-22 15:27:53 +00:00
|
|
|
// Check in homepath.
|
2020-12-31 12:10:28 +00:00
|
|
|
scriptDir = Utils::FileSystem::getHomePath() + "/.emulationstation/scripts/" + eventName;
|
|
|
|
if (Utils::FileSystem::exists(scriptDir))
|
|
|
|
scriptDirList.push_back(scriptDir);
|
2018-01-30 00:49:08 +00:00
|
|
|
|
2020-07-13 18:58:25 +00:00
|
|
|
for (std::list<std::string>::const_iterator dirIt = scriptDirList.cbegin();
|
2021-07-07 18:31:46 +00:00
|
|
|
dirIt != scriptDirList.cend(); dirIt++) {
|
2018-01-30 00:49:08 +00:00
|
|
|
std::list<std::string> scripts = Utils::FileSystem::getDirContent(*dirIt);
|
2021-07-07 18:31:46 +00:00
|
|
|
for (std::list<std::string>::const_iterator it = scripts.cbegin(); // Line break.
|
|
|
|
it != scripts.cend(); it++) {
|
2020-12-31 12:10:28 +00:00
|
|
|
std::string arg1Quotation;
|
|
|
|
std::string arg2Quotation;
|
|
|
|
// Add quotation marks around the arguments as long as these are not already
|
|
|
|
// present (i.e. for arguments with spaces in them).
|
|
|
|
if (arg1.front() != '\"')
|
|
|
|
arg1Quotation = "\"";
|
|
|
|
if (arg2.front() != '\"')
|
|
|
|
arg2Quotation = "\"";
|
|
|
|
std::string script = *it + " " + arg1Quotation + arg1 + arg1Quotation + " " +
|
2021-07-07 18:31:46 +00:00
|
|
|
arg2Quotation + arg2 + arg2Quotation;
|
2020-08-05 12:49:54 +00:00
|
|
|
LOG(LogDebug) << "Executing: " << script;
|
2018-01-30 00:49:08 +00:00
|
|
|
runSystemCommand(script);
|
|
|
|
}
|
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
}
|
|
|
|
} // namespace Scripting
|