2020-04-29 18:02:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2018 Maschell
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
2024-11-27 20:44:36 +01:00
|
|
|
|
2020-04-29 18:02:36 +02:00
|
|
|
#include "PluginDataFactory.h"
|
2023-08-16 10:08:44 +02:00
|
|
|
#include "NotificationsUtils.h"
|
2024-08-04 16:42:24 +02:00
|
|
|
#include "PluginLoadWrapper.h"
|
2022-05-14 14:00:20 +02:00
|
|
|
#include "fs/FSUtils.h"
|
|
|
|
#include "utils/StringTools.h"
|
|
|
|
#include "utils/logger.h"
|
2024-11-27 20:44:36 +01:00
|
|
|
|
2022-02-04 16:25:44 +01:00
|
|
|
#include <memory>
|
2024-11-27 20:44:36 +01:00
|
|
|
#include <set>
|
|
|
|
#include <sys/dirent.h>
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2024-08-04 16:42:24 +02:00
|
|
|
std::vector<PluginLoadWrapper> PluginDataFactory::loadDir(std::string_view path) {
|
|
|
|
std::vector<PluginLoadWrapper> result;
|
2024-11-27 20:44:36 +01:00
|
|
|
dirent *dp;
|
2022-01-27 12:47:33 +01:00
|
|
|
DIR *dfd;
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
if (path.empty()) {
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to load Plugins from dir: Path was empty");
|
2020-04-29 18:02:36 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-04 15:32:45 +01:00
|
|
|
if ((dfd = opendir(path.data())) == nullptr) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Couldn't open dir %s", path.data());
|
2020-04-29 18:02:36 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-12-26 14:17:50 +01:00
|
|
|
while ((dp = readdir(dfd)) != nullptr) {
|
2022-09-24 12:59:00 +02:00
|
|
|
if (dp->d_type == DT_DIR) {
|
2020-04-29 18:02:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
2022-09-24 12:59:00 +02:00
|
|
|
if (std::string_view(dp->d_name).starts_with('.') || std::string_view(dp->d_name).starts_with('_') || !std::string_view(dp->d_name).ends_with(".wps")) {
|
2023-11-04 15:32:45 +01:00
|
|
|
DEBUG_FUNCTION_LINE_WARN("Skip file %s/%s", path.data(), dp->d_name);
|
2020-04-29 18:02:36 +02:00
|
|
|
continue;
|
2022-09-24 12:59:00 +02:00
|
|
|
}
|
|
|
|
|
2023-11-04 15:32:45 +01:00
|
|
|
auto full_file_path = string_format("%s/%s", path.data(), dp->d_name);
|
2022-09-24 12:59:00 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Loading plugin: %s", full_file_path.c_str());
|
2024-08-04 16:42:24 +02:00
|
|
|
|
2024-11-27 20:44:36 +01:00
|
|
|
if (auto pluginData = load(full_file_path)) {
|
2024-08-04 16:42:24 +02:00
|
|
|
// TODO: This is only for testing. Remove me!c
|
|
|
|
bool shouldBeLoadedAndLinked = false;
|
|
|
|
if (full_file_path.ends_with("AromaBasePlugin.wps") ||
|
|
|
|
full_file_path.ends_with("drc_region_free.wps") ||
|
|
|
|
full_file_path.ends_with("regionfree.wps") ||
|
|
|
|
full_file_path.ends_with("ftpiiu.wps") ||
|
2024-08-04 18:02:20 +02:00
|
|
|
full_file_path.ends_with("wiiload.wps") ||
|
|
|
|
full_file_path.ends_with("homebrew_on_menu.wps")) {
|
2024-08-04 16:42:24 +02:00
|
|
|
shouldBeLoadedAndLinked = true;
|
|
|
|
}
|
|
|
|
result.emplace_back(std::move(pluginData), shouldBeLoadedAndLinked);
|
2020-04-29 18:02:36 +02:00
|
|
|
} else {
|
2023-08-16 10:08:44 +02:00
|
|
|
auto errMsg = string_format("Failed to load plugin: %s", full_file_path.c_str());
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("%s", errMsg.c_str());
|
|
|
|
DisplayErrorNotificationMessage(errMsg, 15.0f);
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
|
|
|
|
closedir(dfd);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-11-27 20:44:36 +01:00
|
|
|
std::unique_ptr<PluginData> PluginDataFactory::load(const std::string_view path) {
|
2023-11-04 15:32:45 +01:00
|
|
|
std::vector<uint8_t> buffer;
|
2024-11-27 20:44:36 +01:00
|
|
|
if (FSUtils::LoadFileToMem(path, buffer) < 0) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to load %s into memory", path.data());
|
2023-11-04 15:32:45 +01:00
|
|
|
return nullptr;
|
2020-12-28 14:46:30 +01:00
|
|
|
}
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2021-02-19 19:41:04 +01:00
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Loaded file!");
|
2024-11-27 20:44:36 +01:00
|
|
|
return load(std::move(buffer), path);
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2023-11-04 15:32:45 +01:00
|
|
|
std::unique_ptr<PluginData> PluginDataFactory::load(std::vector<uint8_t> &&buffer, std::string_view source) {
|
2020-05-03 12:30:15 +02:00
|
|
|
if (buffer.empty()) {
|
2023-11-04 15:32:45 +01:00
|
|
|
return nullptr;
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2023-11-04 15:32:45 +01:00
|
|
|
return make_unique_nothrow<PluginData>(std::move(buffer), source);
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|