2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-22 15:27:53 +00:00
|
|
|
//
|
2024-07-10 16:04:40 +00:00
|
|
|
// ES-DE Frontend
|
2020-06-22 15:27:53 +00:00
|
|
|
// Scripting.cpp
|
|
|
|
//
|
2021-12-05 10:25:11 +00:00
|
|
|
// 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
|
2021-12-05 10:25:11 +00:00
|
|
|
// argument "eventName" will be executed with arg1, arg2, arg3 and arg4 as arguments.
|
2020-06-22 15:27:53 +00:00
|
|
|
//
|
2023-12-15 17:33:02 +00:00
|
|
|
// The scripts are searched for in <application data>/scripts/<eventName>
|
2021-12-05 10:25:11 +00:00
|
|
|
// For example, if the event is called "game-start", all scripts inside the directory
|
2023-12-15 17:33:02 +00:00
|
|
|
// <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"
|
2020-07-09 17:24:20 +00:00
|
|
|
|
2018-01-30 00:49:08 +00:00
|
|
|
#include "Log.h"
|
2020-07-09 17:24:20 +00:00
|
|
|
#include "Settings.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
2022-01-10 17:56:04 +00:00
|
|
|
#include "utils/PlatformUtil.h"
|
2022-09-07 18:01:04 +00:00
|
|
|
#include "utils/StringUtil.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2018-01-30 00:49:08 +00:00
|
|
|
|
|
|
|
namespace Scripting
|
|
|
|
{
|
2021-12-05 10:25:11 +00:00
|
|
|
void fireEvent(const std::string& eventName,
|
|
|
|
const std::string& arg1,
|
|
|
|
const std::string& arg2,
|
|
|
|
const std::string& arg3,
|
|
|
|
const std::string& arg4)
|
2021-07-07 18:31:46 +00:00
|
|
|
{
|
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
|
2021-12-05 10:25:11 +00:00
|
|
|
<< "\" \"" << arg3 << "\" \"" << arg4 << "\"";
|
2018-01-30 00:49:08 +00:00
|
|
|
|
|
|
|
std::list<std::string> scriptDirList;
|
2023-12-19 16:35:58 +00:00
|
|
|
std::string scriptDir;
|
2018-01-30 00:49:08 +00:00
|
|
|
|
2023-12-15 17:33:02 +00:00
|
|
|
// Check in application data directory.
|
2023-12-19 16:35:58 +00:00
|
|
|
scriptDir = Utils::FileSystem::getAppDataDirectory() + "/scripts/" + eventName;
|
|
|
|
if (Utils::FileSystem::exists(scriptDir))
|
|
|
|
scriptDirList.push_back(scriptDir);
|
2018-01-30 00:49:08 +00:00
|
|
|
|
2021-11-17 16:48:49 +00:00
|
|
|
for (auto dirIt = scriptDirList.cbegin(); dirIt != scriptDirList.cend(); ++dirIt) {
|
2022-09-07 18:01:04 +00:00
|
|
|
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
|
2021-11-17 16:48:49 +00:00
|
|
|
for (auto it = scripts.cbegin(); it != scripts.cend(); ++it) {
|
2020-12-31 12:10:28 +00:00
|
|
|
std::string arg1Quotation;
|
|
|
|
std::string arg2Quotation;
|
2021-12-05 10:25:11 +00:00
|
|
|
std::string arg3Quotation;
|
|
|
|
std::string arg4Quotation;
|
2020-12-31 12:10:28 +00:00
|
|
|
// Add quotation marks around the arguments as long as these are not already
|
|
|
|
// present (i.e. for arguments with spaces in them).
|
2021-12-05 10:25:11 +00:00
|
|
|
if (!arg1.empty() && arg1.front() != '\"')
|
2020-12-31 12:10:28 +00:00
|
|
|
arg1Quotation = "\"";
|
2021-12-05 10:25:11 +00:00
|
|
|
if (!arg2.empty() && arg2.front() != '\"')
|
2020-12-31 12:10:28 +00:00
|
|
|
arg2Quotation = "\"";
|
2021-12-05 10:25:11 +00:00
|
|
|
if (!arg3.empty() && arg3.front() != '\"')
|
|
|
|
arg3Quotation = "\"";
|
|
|
|
if (!arg4.empty() && arg4.front() != '\"')
|
|
|
|
arg4Quotation = "\"";
|
2021-07-09 17:58:21 +00:00
|
|
|
std::string script;
|
|
|
|
script.append(*it)
|
|
|
|
.append(" ")
|
|
|
|
.append(arg1Quotation)
|
|
|
|
.append(arg1)
|
|
|
|
.append(arg1Quotation)
|
|
|
|
.append(" ")
|
|
|
|
.append(arg2Quotation)
|
|
|
|
.append(arg2)
|
2021-12-05 10:25:11 +00:00
|
|
|
.append(arg2Quotation)
|
|
|
|
.append(" ")
|
|
|
|
.append(arg3Quotation)
|
|
|
|
.append(arg3)
|
|
|
|
.append(arg3Quotation)
|
|
|
|
.append(" ")
|
|
|
|
.append(arg4Quotation)
|
|
|
|
.append(arg4)
|
|
|
|
.append(arg4Quotation);
|
2020-08-05 12:49:54 +00:00
|
|
|
LOG(LogDebug) << "Executing: " << script;
|
2023-12-23 20:14:59 +00:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
Utils::Platform::runSystemCommand("sh " + script);
|
|
|
|
#else
|
2022-01-10 17:43:17 +00:00
|
|
|
Utils::Platform::runSystemCommand(script);
|
2023-12-23 20:14:59 +00:00
|
|
|
#endif
|
2018-01-30 00:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
}
|
|
|
|
} // namespace Scripting
|