WiiUPluginLoaderBackend/source/plugin/PluginDataFactory.cpp

90 lines
3.1 KiB
C++
Raw Normal View History

/****************************************************************************
* 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/>.
****************************************************************************/
#include "PluginDataFactory.h"
#include "../fs/FSUtils.h"
2020-12-26 14:17:50 +01:00
#include "../utils/StringTools.h"
2022-02-04 16:25:44 +01:00
#include <dirent.h>
#include <memory>
#include <sys/stat.h>
2021-12-15 17:09:30 +01:00
std::vector<std::shared_ptr<PluginData>> PluginDataFactory::loadDir(const std::string &path, MEMHeapHandle heapHandle) {
std::vector<std::shared_ptr<PluginData>> result;
struct dirent *dp;
2022-01-27 12:47:33 +01:00
DIR *dfd;
2020-05-03 12:30:15 +02:00
if (path.empty()) {
2020-12-28 14:40:53 +01:00
DEBUG_FUNCTION_LINE("Path was empty");
return result;
}
2020-12-26 14:17:50 +01:00
if ((dfd = opendir(path.c_str())) == nullptr) {
2020-12-28 14:40:53 +01:00
DEBUG_FUNCTION_LINE("Couldn't open dir %s", path.c_str());
return result;
}
2020-12-26 14:17:50 +01:00
while ((dp = readdir(dfd)) != nullptr) {
2022-02-04 16:25:44 +01:00
struct stat stbuf {};
2020-05-03 12:30:15 +02:00
std::string full_file_path = StringTools::strfmt("%s/%s", path.c_str(), dp->d_name);
StringTools::RemoveDoubleSlashs(full_file_path);
2020-05-03 12:30:15 +02:00
if (stat(full_file_path.c_str(), &stbuf) == -1) {
2020-12-28 14:40:53 +01:00
DEBUG_FUNCTION_LINE("Unable to stat file: %s", full_file_path.c_str());
continue;
}
2020-05-03 12:30:15 +02:00
if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { // Skip directories
continue;
} else {
2021-02-19 19:41:04 +01:00
DEBUG_FUNCTION_LINE("Loading plugin: %s", full_file_path.c_str());
auto pluginData = load(full_file_path, heapHandle);
2020-05-03 12:30:15 +02:00
if (pluginData) {
result.push_back(pluginData.value());
}
}
}
2020-12-26 14:17:50 +01:00
if (dfd != nullptr) {
closedir(dfd);
}
return result;
}
2021-12-15 17:09:30 +01:00
std::optional<std::shared_ptr<PluginData>> PluginDataFactory::load(const std::string &filename, MEMHeapHandle heapHandle) {
uint8_t *buffer = nullptr;
uint32_t fsize = 0;
if (FSUtils::LoadFileToMem(filename.c_str(), &buffer, &fsize) < 0) {
DEBUG_FUNCTION_LINE("Failed to load file");
return {};
}
std::vector<uint8_t> result;
result.resize(fsize);
memcpy(&result[0], buffer, fsize);
free(buffer);
2021-02-19 19:41:04 +01:00
DEBUG_FUNCTION_LINE_VERBOSE("Loaded file!");
return load(result, heapHandle);
}
2021-12-15 17:09:30 +01:00
std::optional<std::shared_ptr<PluginData>> PluginDataFactory::load(std::vector<uint8_t> &buffer, MEMHeapHandle heapHandle) {
2020-05-03 12:30:15 +02:00
if (buffer.empty()) {
return std::nullopt;
}
2021-12-15 17:09:30 +01:00
return std::shared_ptr<PluginData>(new PluginData(buffer, heapHandle, eMemoryTypes::eMemTypeMEM2));
}