2020-05-17 20:26:31 +02:00
|
|
|
/****************************************************************************
|
2022-05-14 12:06:16 +02:00
|
|
|
* Copyright (C) 2019-2022 Maschell
|
2020-05-17 20:26:31 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-13 16:12:10 +01:00
|
|
|
#include "wups_backend/PluginUtils.h"
|
2022-05-14 12:06:16 +02:00
|
|
|
#include "logger.h"
|
|
|
|
#include "utils.h"
|
2024-03-23 08:12:10 +01:00
|
|
|
#include "wups_backend/api.h"
|
2022-05-14 12:06:16 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
2020-05-17 20:26:31 +02:00
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
namespace WUPSBackend {
|
2023-02-17 12:03:27 +01:00
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
const char *GetStatusStr(PluginBackendApiErrorType status) {
|
|
|
|
return WUPSBackend_GetStatusStr(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace PluginUtils {
|
|
|
|
static std::optional<PluginMetaInformation> getMetaInformation(const wups_backend_plugin_information &info, PluginBackendApiErrorType &err) {
|
2024-03-23 08:11:02 +01:00
|
|
|
if (info.plugin_information_version != WUPS_BACKEND_PLUGIN_INFORMATION_VERSION) {
|
2024-03-23 19:15:29 +01:00
|
|
|
err = PLUGIN_BACKEND_API_ERROR_UNSUPPORTED_VERSION;
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Version mismatch");
|
|
|
|
return {};
|
|
|
|
}
|
2024-03-23 19:15:29 +01:00
|
|
|
return PluginMetaInformation(info.name,
|
|
|
|
info.author,
|
|
|
|
info.version,
|
|
|
|
info.license,
|
|
|
|
info.buildTimestamp,
|
|
|
|
info.description,
|
|
|
|
info.storageId,
|
|
|
|
info.size);
|
2022-05-14 12:06:16 +02:00
|
|
|
}
|
2021-10-01 17:25:19 +02:00
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
std::optional<PluginMetaInformation> getMetaInformationForBuffer(char *buffer, size_t size, PluginBackendApiErrorType &err) {
|
|
|
|
wups_backend_plugin_information info = {};
|
|
|
|
if ((err = WUPSBackend_GetPluginMetaInformationByBuffer(&info, buffer, size)) != PLUGIN_BACKEND_API_ERROR_NONE) {
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to load meta infos for buffer %08X with size %08X", buffer, size);
|
|
|
|
return {};
|
|
|
|
}
|
2024-03-23 19:15:29 +01:00
|
|
|
return getMetaInformation(info, err);
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
std::optional<PluginMetaInformation> getMetaInformationForPath(const std::string &path, PluginBackendApiErrorType &err) {
|
2024-03-23 08:11:02 +01:00
|
|
|
wups_backend_plugin_information info = {};
|
2024-03-23 19:15:29 +01:00
|
|
|
if ((err = WUPSBackend_GetPluginMetaInformationByPath(&info, path.c_str())) != PLUGIN_BACKEND_API_ERROR_NONE) {
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to load meta infos for %s", path.c_str());
|
|
|
|
return {};
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
2024-03-23 19:15:29 +01:00
|
|
|
return getMetaInformation(info, err);
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
std::optional<PluginContainer> getPluginForPath(const std::string &path, PluginBackendApiErrorType &err) {
|
|
|
|
auto metaInfoOpt = getMetaInformationForPath(path, err);
|
2020-05-17 20:26:31 +02:00
|
|
|
if (!metaInfoOpt) {
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get MetaInformation for path %s", path.c_str());
|
|
|
|
return {};
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
|
|
|
|
2024-03-23 08:11:02 +01:00
|
|
|
wups_backend_plugin_data_handle dataHandle;
|
2024-03-23 19:15:29 +01:00
|
|
|
if ((err = WUPSBackend_LoadPluginAsDataByPath(&dataHandle, path.c_str())) != PLUGIN_BACKEND_API_ERROR_NONE) {
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("WUPSLoadPluginAsDataByPath failed for path %s", path.c_str());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
return PluginContainer(PluginData(dataHandle), std::move(metaInfoOpt.value()));
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
std::optional<PluginContainer> getPluginForBuffer(char *buffer, size_t size, PluginBackendApiErrorType &err) {
|
|
|
|
auto metaInfoOpt = getMetaInformationForBuffer(buffer, size, err);
|
2020-05-17 20:26:31 +02:00
|
|
|
if (!metaInfoOpt) {
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get MetaInformation for buffer %08X (%d bytes)", buffer, size);
|
|
|
|
return {};
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
|
|
|
|
2024-03-23 08:11:02 +01:00
|
|
|
wups_backend_plugin_data_handle dataHandle;
|
2024-03-23 19:15:29 +01:00
|
|
|
if ((err = WUPSBackend_LoadPluginAsDataByBuffer(&dataHandle, buffer, size)) != PLUGIN_BACKEND_API_ERROR_NONE) {
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("WUPSLoadPluginAsDataByBuffer failed for buffer %08X (%d bytes)", buffer, size);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
return PluginContainer(PluginData(dataHandle), std::move(metaInfoOpt.value()));
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
std::vector<PluginContainer> getLoadedPlugins(PluginBackendApiErrorType &err) {
|
|
|
|
std::vector<PluginContainer> result;
|
2020-05-17 20:26:31 +02:00
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
PluginBackendApiErrorType err2;
|
|
|
|
uint32_t maxSize;
|
|
|
|
if ((err2 = WUPSBackend_GetNumberOfLoadedPlugins(&maxSize)) != PLUGIN_BACKEND_API_ERROR_NONE) {
|
|
|
|
DEBUG_FUNCTION_LINE_WARN("Failed to get number of loaded plugins: %s", WUPSBackend_GetStatusStr(err2));
|
|
|
|
maxSize = 120;
|
|
|
|
}
|
2022-05-14 12:06:16 +02:00
|
|
|
|
2024-03-23 08:11:02 +01:00
|
|
|
auto handles = make_unique_nothrow<wups_backend_plugin_container_handle[]>(maxSize);
|
2022-05-14 12:06:16 +02:00
|
|
|
if (!handles) {
|
2024-03-23 19:15:29 +01:00
|
|
|
err = PLUGIN_BACKEND_API_ERROR_FAILED_ALLOC;
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Not enough memory");
|
2020-06-27 12:06:00 +02:00
|
|
|
return result;
|
|
|
|
}
|
2020-05-17 20:26:31 +02:00
|
|
|
uint32_t realSize = 0;
|
|
|
|
|
2021-04-07 23:36:30 +02:00
|
|
|
for (uint32_t i = 0; i < maxSize; i++) {
|
|
|
|
handles[i] = 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:25:19 +02:00
|
|
|
uint32_t plugin_information_version = 0;
|
2024-03-23 19:15:29 +01:00
|
|
|
if ((err = WUPSBackend_GetLoadedPlugins(handles.get(), maxSize, &realSize, &plugin_information_version)) != PLUGIN_BACKEND_API_ERROR_NONE) {
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("WUPSGetLoadedPlugins: Failed");
|
2020-05-17 20:26:31 +02:00
|
|
|
return result;
|
|
|
|
}
|
2024-03-23 08:11:02 +01:00
|
|
|
if (realSize == 0 || plugin_information_version != WUPS_BACKEND_PLUGIN_INFORMATION_VERSION) {
|
2024-03-23 19:15:29 +01:00
|
|
|
err = PLUGIN_BACKEND_API_ERROR_UNSUPPORTED_VERSION;
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("realSize is 0 or version mismatch");
|
2020-05-17 20:26:31 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-23 08:11:02 +01:00
|
|
|
auto dataHandles = make_unique_nothrow<wups_backend_plugin_data_handle[]>(realSize);
|
2021-04-07 23:36:30 +02:00
|
|
|
if (!dataHandles) {
|
2024-03-23 19:15:29 +01:00
|
|
|
err = PLUGIN_BACKEND_API_ERROR_FAILED_ALLOC;
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Not enough memory");
|
2020-06-27 12:06:00 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
if ((err = WUPSBackend_GetPluginDataForContainerHandles(handles.get(), dataHandles.get(), realSize)) != PLUGIN_BACKEND_API_ERROR_NONE) {
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get plugin data");
|
2020-05-17 20:26:31 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-23 08:11:02 +01:00
|
|
|
auto information = make_unique_nothrow<wups_backend_plugin_information[]>(realSize);
|
2021-04-07 23:36:30 +02:00
|
|
|
if (!information) {
|
2024-03-23 19:15:29 +01:00
|
|
|
err = PLUGIN_BACKEND_API_ERROR_FAILED_ALLOC;
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Not enough memory");
|
2020-06-27 12:06:00 +02:00
|
|
|
return result;
|
|
|
|
}
|
2024-03-23 19:15:29 +01:00
|
|
|
if ((err = WUPSBackend_GetMetaInformation(handles.get(), information.get(), realSize)) != PLUGIN_BACKEND_API_ERROR_NONE) {
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to get meta information for handles");
|
2020-05-17 20:26:31 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < realSize; i++) {
|
2024-03-23 08:11:02 +01:00
|
|
|
if (information[i].plugin_information_version != WUPS_BACKEND_PLUGIN_INFORMATION_VERSION) {
|
2024-03-23 19:15:29 +01:00
|
|
|
err = PLUGIN_BACKEND_API_ERROR_UNSUPPORTED_VERSION;
|
2022-05-14 12:06:16 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Skip, wrong struct version.");
|
2024-03-23 19:15:29 +01:00
|
|
|
return {};
|
2021-10-01 17:25:19 +02:00
|
|
|
}
|
2024-03-23 19:15:29 +01:00
|
|
|
result.emplace_back(PluginData(dataHandles[i]), PluginMetaInformation(information[i].name,
|
|
|
|
information[i].author,
|
|
|
|
information[i].version,
|
|
|
|
information[i].license,
|
|
|
|
information[i].buildTimestamp,
|
|
|
|
information[i].description,
|
|
|
|
information[i].storageId,
|
|
|
|
information[i].size));
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
2022-05-14 12:06:16 +02:00
|
|
|
|
|
|
|
return result;
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
PluginBackendApiErrorType LoadAndLinkOnRestart(const std::vector<PluginContainer> &plugins) {
|
2020-05-17 20:26:31 +02:00
|
|
|
uint32_t dataSize = plugins.size();
|
2024-03-23 19:15:29 +01:00
|
|
|
auto handles = make_unique_nothrow<wups_backend_plugin_data_handle[]>(dataSize);
|
|
|
|
if (!handles) {
|
|
|
|
return PLUGIN_BACKEND_API_ERROR_FAILED_ALLOC;
|
|
|
|
}
|
2020-05-17 20:26:31 +02:00
|
|
|
int i = 0;
|
2024-03-23 19:15:29 +01:00
|
|
|
for (const auto &plugin : plugins) {
|
|
|
|
const auto &handle = plugin.getPluginData().getHandle();
|
2020-05-17 20:26:31 +02:00
|
|
|
if (handle == 0) {
|
2024-03-23 19:15:29 +01:00
|
|
|
return PLUGIN_BACKEND_API_INVALID_HANDLE;
|
2020-05-17 20:26:31 +02:00
|
|
|
} else {
|
|
|
|
handles[i] = handle;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:15:29 +01:00
|
|
|
return WUPSBackend_LoadAndLinkByDataHandle(handles.get(), dataSize);
|
2020-05-17 20:26:31 +02:00
|
|
|
}
|
2024-03-23 19:15:29 +01:00
|
|
|
} // namespace PluginUtils
|
|
|
|
} // namespace WUPSBackend
|