ES-DE/es-core/src/Scripting.cpp

101 lines
3.8 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: MIT
2020-06-22 15:27:53 +00:00
//
// ES-DE Frontend
2020-06-22 15:27:53 +00:00
// Scripting.cpp
//
// Executes custom scripts for various events.
2020-06-22 15:27:53 +00:00
// By calling fireEvent() the scripts inside the directory corresponding to the
// argument "eventName" will be executed with arg1, arg2, arg3 and arg4 as arguments.
2020-06-22 15:27:53 +00:00
//
// The scripts are searched for in <application data>/scripts/<eventName>
// For example, if the event is called "game-start", all scripts inside the directory
// <application data>/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"
2018-01-30 00:49:08 +00:00
#include "Log.h"
#include "Settings.h"
#include "utils/FileSystemUtil.h"
2022-01-10 17:56:04 +00:00
#include "utils/PlatformUtil.h"
#include "utils/StringUtil.h"
#include <algorithm>
2018-01-30 00:49:08 +00:00
namespace Scripting
{
void fireEvent(const std::string& eventName,
const std::string& arg1,
const std::string& arg2,
const std::string& arg3,
const std::string& arg4)
{
if (!Settings::getInstance()->getBool("CustomEventScripts"))
return;
LOG(LogDebug) << "Scripting::fireEvent(): " << eventName << " \"" << arg1 << "\" \"" << arg2
<< "\" \"" << arg3 << "\" \"" << arg4 << "\"";
2018-01-30 00:49:08 +00:00
std::list<std::string> scriptDirList;
std::string scriptDir;
2018-01-30 00:49:08 +00:00
// Check in application data directory.
scriptDir = Utils::FileSystem::getAppDataDirectory() + "/scripts/" + eventName;
if (Utils::FileSystem::exists(scriptDir))
scriptDirList.push_back(scriptDir);
2018-01-30 00:49:08 +00:00
for (auto dirIt = scriptDirList.cbegin(); dirIt != scriptDirList.cend(); ++dirIt) {
std::list<std::string> scripts {Utils::FileSystem::getDirContent(*dirIt)};
// Sort the scripts in case-sensitive order on Unix/Linux and in case-insensitive order
// on macOS and Windows.
#if defined(__unix__)
scripts.sort([](std::string a, std::string b) { return a.compare(b) < 0; });
#else
scripts.sort([](std::string a, std::string b) {
return Utils::String::toUpper(a).compare(Utils::String::toUpper(b)) < 0;
});
#endif
for (auto it = scripts.cbegin(); it != scripts.cend(); ++it) {
std::string arg1Quotation;
std::string arg2Quotation;
std::string arg3Quotation;
std::string arg4Quotation;
// Add quotation marks around the arguments as long as these are not already
// present (i.e. for arguments with spaces in them).
if (!arg1.empty() && arg1.front() != '\"')
arg1Quotation = "\"";
if (!arg2.empty() && arg2.front() != '\"')
arg2Quotation = "\"";
if (!arg3.empty() && arg3.front() != '\"')
arg3Quotation = "\"";
if (!arg4.empty() && arg4.front() != '\"')
arg4Quotation = "\"";
std::string script;
script.append(*it)
.append(" ")
.append(arg1Quotation)
.append(arg1)
.append(arg1Quotation)
.append(" ")
.append(arg2Quotation)
.append(arg2)
.append(arg2Quotation)
.append(" ")
.append(arg3Quotation)
.append(arg3)
.append(arg3Quotation)
.append(" ")
.append(arg4Quotation)
.append(arg4)
.append(arg4Quotation);
LOG(LogDebug) << "Executing: " << script;
#if defined(__ANDROID__)
Utils::Platform::runSystemCommand("sh " + script);
#else
Utils::Platform::runSystemCommand(script);
#endif
2018-01-30 00:49:08 +00:00
}
}
}
} // namespace Scripting