2020-05-17 20:26:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2019,2020 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 <cstring>
|
2020-06-27 12:06:00 +02:00
|
|
|
|
2020-05-17 20:26:31 +02:00
|
|
|
#include "wups_backend/PluginUtils.h"
|
|
|
|
#include "imports.h"
|
|
|
|
|
2021-01-10 13:15:10 +01:00
|
|
|
#define ERROR_NONE 0
|
|
|
|
#define ERROR_INVALID_SIZE 0xFFFFFFFF
|
|
|
|
#define ERROR_INVALID_ARG 0xFFFFFFFE
|
|
|
|
#define ERROR_FAILED_ALLOC 0xFFFFFFFD
|
|
|
|
#define ERROR_FILE_NOT_FOUND 0xFFFFFFFC
|
|
|
|
|
2020-05-17 20:26:31 +02:00
|
|
|
std::optional<PluginMetaInformation> PluginUtils::getMetaInformationForBuffer(char *buffer, size_t size) {
|
|
|
|
plugin_information info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
if (WUPSGetPluginMetaInformationByBuffer(&info, buffer, size)) {
|
|
|
|
// DEBUG_FUNCTION_LINE("Failed to load meta infos for buffer %08X with size %08X\n", buffer, size);
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
PluginMetaInformation metaInfo(info.name,
|
|
|
|
info.author,
|
|
|
|
info.version,
|
|
|
|
info.license,
|
|
|
|
info.buildTimestamp,
|
|
|
|
info.description,
|
|
|
|
info.size);
|
|
|
|
|
|
|
|
return metaInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<PluginMetaInformation> PluginUtils::getMetaInformationForPath(const std::string &path) {
|
|
|
|
plugin_information info;
|
|
|
|
memset(&info, 0, sizeof(info));
|
2021-01-10 13:15:10 +01:00
|
|
|
if (WUPSGetPluginMetaInformationByPath(&info, path.c_str()) != ERROR_NONE) {
|
2020-05-17 20:26:31 +02:00
|
|
|
// DEBUG_FUNCTION_LINE("Failed to load meta infos for %s\n", path.c_str());
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
PluginMetaInformation metaInfo(info.name,
|
|
|
|
info.author,
|
|
|
|
info.version,
|
|
|
|
info.license,
|
|
|
|
info.buildTimestamp,
|
|
|
|
info.description,
|
|
|
|
info.size);
|
|
|
|
return metaInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<PluginContainer> PluginUtils::getPluginForPath(const std::string &path) {
|
|
|
|
auto metaInfoOpt = PluginUtils::getMetaInformationForPath(path);
|
|
|
|
if (!metaInfoOpt) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin_data_handle dataHandle;
|
2021-01-10 13:15:10 +01:00
|
|
|
if (WUPSLoadPluginAsDataByPath(&dataHandle, path.c_str()) != ERROR_NONE) {
|
2020-05-17 20:26:31 +02:00
|
|
|
// DEBUG_FUNCTION_LINE("Failed to load data");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-01-10 13:15:10 +01:00
|
|
|
return PluginContainer(PluginData(dataHandle), metaInfoOpt.value(), ERROR_NONE);
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<PluginContainer> PluginUtils::getPluginForBuffer(char *buffer, size_t size) {
|
|
|
|
auto metaInfoOpt = PluginUtils::getMetaInformationForBuffer(buffer, size);
|
|
|
|
if (!metaInfoOpt) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin_data_handle dataHandle;
|
2021-01-10 13:15:10 +01:00
|
|
|
if (WUPSLoadPluginAsDataByBuffer(&dataHandle, buffer, size) != ERROR_NONE) {
|
2020-05-17 20:26:31 +02:00
|
|
|
// DEBUG_FUNCTION_LINE("Failed to load data");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
return PluginContainer(PluginData(dataHandle), metaInfoOpt.value(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<PluginContainer> PluginUtils::getLoadedPlugins(uint32_t maxSize) {
|
|
|
|
std::vector<PluginContainer> result;
|
2020-06-27 12:06:00 +02:00
|
|
|
plugin_container_handle *handles = (plugin_container_handle *) malloc(maxSize * sizeof(plugin_container_handle));
|
|
|
|
if (!handles) {
|
|
|
|
return result;
|
|
|
|
}
|
2020-05-17 20:26:31 +02:00
|
|
|
uint32_t realSize = 0;
|
|
|
|
|
2021-01-10 13:15:10 +01:00
|
|
|
if (WUPSGetLoadedPlugins(handles, maxSize, &realSize) != ERROR_NONE) {
|
2020-06-27 12:06:00 +02:00
|
|
|
free(handles);
|
2020-05-17 20:26:31 +02:00
|
|
|
// DEBUG_FUNCTION_LINE("Failed");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (realSize == 0) {
|
2020-06-27 12:06:00 +02:00
|
|
|
free(handles);
|
2020-05-17 20:26:31 +02:00
|
|
|
// DEBUG_FUNCTION_LINE("realsize is 0");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-10 13:15:42 +01:00
|
|
|
plugin_data_handle *dataHandles = (plugin_data_handle *) malloc(realSize * sizeof(plugin_data_handle));
|
2020-06-27 12:06:00 +02:00
|
|
|
if(!dataHandles){
|
|
|
|
free(handles);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-17 20:26:31 +02:00
|
|
|
if (WUPSGetPluginDataForContainerHandles(handles, dataHandles, realSize) != 0) {
|
2020-06-27 12:06:00 +02:00
|
|
|
free(handles);
|
|
|
|
free(dataHandles);
|
2020-05-17 20:26:31 +02:00
|
|
|
// DEBUG_FUNCTION_LINE("Failed to get plugin data");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-27 12:06:00 +02:00
|
|
|
plugin_information* information = (plugin_information *) malloc(realSize * sizeof(plugin_information));
|
|
|
|
if(information == NULL){
|
|
|
|
free(handles);
|
|
|
|
free(dataHandles);
|
|
|
|
return result;
|
|
|
|
}
|
2021-01-10 13:15:10 +01:00
|
|
|
if (WUPSGetMetaInformation(handles, information, realSize) != ERROR_NONE) {
|
2020-06-27 12:06:00 +02:00
|
|
|
free(handles);
|
|
|
|
free(dataHandles);
|
|
|
|
free(information);
|
2020-05-17 20:26:31 +02:00
|
|
|
// DEBUG_FUNCTION_LINE("Failed to get meta information for handles");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < realSize; i++) {
|
|
|
|
PluginMetaInformation metaInfo(information[i].name,
|
|
|
|
information[i].author,
|
|
|
|
information[i].version,
|
|
|
|
information[i].license,
|
|
|
|
information[i].buildTimestamp,
|
|
|
|
information[i].description,
|
|
|
|
information[i].size);
|
|
|
|
PluginData pluginData((uint32_t) dataHandles[i]);
|
|
|
|
result.push_back(PluginContainer(pluginData, metaInfo, handles[i]));
|
|
|
|
}
|
|
|
|
|
2020-06-27 12:06:00 +02:00
|
|
|
free(handles);
|
|
|
|
free(dataHandles);
|
|
|
|
free(information);
|
|
|
|
|
2020-05-17 20:26:31 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginUtils::destroyPluginContainer(PluginContainer &plugin) {
|
|
|
|
std::vector<PluginContainer> list;
|
|
|
|
list.push_back(plugin);
|
|
|
|
return destroyPluginContainer(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginUtils::destroyPluginContainer(std::vector<PluginContainer> &plugins) {
|
|
|
|
uint32_t containerSize = plugins.size();
|
|
|
|
uint32_t dataSize = containerSize;
|
|
|
|
plugin_container_handle container_handles[containerSize];
|
|
|
|
plugin_data_handle data_handles[dataSize];
|
|
|
|
|
|
|
|
uint32_t cntC = 0;
|
|
|
|
uint32_t cntD = 0;
|
|
|
|
for (auto &plugin : plugins) {
|
|
|
|
if (plugin.getHandle() != 0) {
|
|
|
|
container_handles[cntC] = plugin.getHandle();
|
|
|
|
cntC++;
|
|
|
|
} else {
|
|
|
|
containerSize--;
|
|
|
|
}
|
|
|
|
if (plugin.pluginData.getHandle() != 0) {
|
|
|
|
data_handles[cntD] = plugin.pluginData.getHandle();
|
|
|
|
cntD++;
|
|
|
|
} else {
|
|
|
|
dataSize--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WUPSDeletePluginContainer(container_handles, containerSize);
|
|
|
|
WUPSDeletePluginData(data_handles, dataSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t PluginUtils::LoadAndLinkOnRestart(std::vector<PluginContainer> &plugins) {
|
|
|
|
uint32_t dataSize = plugins.size();
|
|
|
|
plugin_data_handle handles[dataSize];
|
|
|
|
int i = 0;
|
|
|
|
for (auto &plugin:plugins) {
|
|
|
|
plugin_data_handle handle = plugin.getPluginData().getHandle();
|
|
|
|
if (handle == 0) {
|
|
|
|
dataSize--;
|
|
|
|
} else {
|
|
|
|
handles[i] = handle;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = WUPSLoadAndLinkByDataHandle(handles, dataSize);
|
|
|
|
// DEBUG_FUNCTION_LINE("%d", res);
|
|
|
|
return res;
|
|
|
|
}
|