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/>.
|
|
|
|
****************************************************************************/
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
2020-12-28 14:46:30 +01:00
|
|
|
#include <memory>
|
2020-04-29 18:02:36 +02:00
|
|
|
#include "PluginDataFactory.h"
|
2020-12-26 14:17:50 +01:00
|
|
|
#include "../utils/logger.h"
|
|
|
|
#include "../utils/StringTools.h"
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
std::vector<PluginData> PluginDataFactory::loadDir(const std::string &path, MEMHeapHandle heapHandle) {
|
2020-04-29 18:02:36 +02:00
|
|
|
std::vector<PluginData> result;
|
|
|
|
struct dirent *dp;
|
2020-12-26 14:17:50 +01:00
|
|
|
DIR *dfd = nullptr;
|
2020-04-29 18:02:36 +02:00
|
|
|
|
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");
|
2020-04-29 18:02:36 +02:00
|
|
|
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());
|
2020-04-29 18:02:36 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-12-26 14:17:50 +01:00
|
|
|
while ((dp = readdir(dfd)) != nullptr) {
|
|
|
|
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);
|
2020-04-29 18:02:36 +02:00
|
|
|
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());
|
2020-04-29 18:02:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { // Skip directories
|
2020-04-29 18:02:36 +02:00
|
|
|
continue;
|
|
|
|
} else {
|
2020-12-28 14:40:53 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Found file: %s", full_file_path.c_str());
|
2020-04-29 18:02:36 +02:00
|
|
|
auto pluginData = load(full_file_path, heapHandle);
|
2020-05-03 12:30:15 +02:00
|
|
|
if (pluginData) {
|
2020-04-29 18:02:36 +02:00
|
|
|
result.push_back(pluginData.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-26 14:17:50 +01:00
|
|
|
if (dfd != nullptr) {
|
2020-04-29 18:02:36 +02:00
|
|
|
closedir(dfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
std::optional<PluginData> PluginDataFactory::load(const std::string &filename, MEMHeapHandle heapHandle) {
|
2020-12-28 14:46:30 +01:00
|
|
|
// Not going to explicitly check these.
|
|
|
|
// The use of gcount() below will compensate for a failure here.
|
|
|
|
std::ifstream is(filename, std::ios::binary);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-12-28 14:46:30 +01:00
|
|
|
is.seekg(0, std::ios::end);
|
|
|
|
std::streampos length = is.tellg();
|
|
|
|
is.seekg(0, std::ios::beg);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-12-28 14:46:30 +01:00
|
|
|
// reading into a 0x40 aligned buffer increases reading speed.
|
|
|
|
char *data = (char *) memalign(0x40, length);
|
|
|
|
if (!data) {
|
2021-01-01 01:59:09 +01:00
|
|
|
is.close();
|
2020-12-28 14:46:30 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Failed to alloc memory for holding the plugin");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
is.read(data, length);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-12-28 14:46:30 +01:00
|
|
|
std::vector<uint8_t> result;
|
|
|
|
result.resize(length);
|
|
|
|
memcpy(&result[0], data, length);
|
|
|
|
free(data);
|
2021-01-01 01:59:09 +01:00
|
|
|
is.close();
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-12-28 14:46:30 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Loaded file!");
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-12-28 14:46:30 +01:00
|
|
|
return load(result, heapHandle);
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
std::optional<PluginData> PluginDataFactory::load(std::vector<uint8_t> &buffer, MEMHeapHandle heapHandle) {
|
|
|
|
if (buffer.empty()) {
|
2020-04-29 18:02:36 +02:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-01-01 01:59:36 +01:00
|
|
|
PluginData pluginData(buffer, heapHandle, eMemoryTypes::eMemTypeMEM2);
|
2020-04-29 18:02:36 +02:00
|
|
|
return pluginData;
|
|
|
|
}
|