Deprecate WUPS_LOADER_HOOK_INIT_STORAGE hook and implemenation

This commit is contained in:
Maschell 2023-10-07 22:33:38 +02:00
parent 553d887526
commit fb2ea68627
5 changed files with 213 additions and 165 deletions

View File

@ -1,6 +1,6 @@
#include "hooks.h"
#include "plugin/PluginContainer.h"
#include "utils/StorageUtils.h"
#include "utils/StorageUtilsDeprecated.h"
#include "utils/logger.h"
static const char **hook_names = (const char *[]){
@ -71,14 +71,19 @@ void CallHook(const PluginContainer &plugin, wups_loader_hook_type_t hook_type)
// clang-format on
break;
case WUPS_LOADER_HOOK_INIT_STORAGE: {
wups_loader_init_storage_args_t args;
args.open_storage_ptr = &StorageUtils::OpenStorage;
args.close_storage_ptr = &StorageUtils::CloseStorage;
args.plugin_id = plugin.getMetaInformation().getStorageId().c_str();
// clang-format off
((void(*)(wups_loader_init_storage_args_t))((uint32_t *) func_ptr))(args);
// clang-format on
break;
if (plugin.getMetaInformation().getWUPSVersion() < WUPSVersion(0, 7, 2)) {
WUPSStorageDeprecated::wups_loader_init_storage_args_t args;
args.open_storage_ptr = &WUPSStorageDeprecated::StorageUtils::OpenStorage;
args.close_storage_ptr = &WUPSStorageDeprecated::StorageUtils::CloseStorage;
args.plugin_id = plugin.getMetaInformation().getStorageId().c_str();
// clang-format off
((void(*)(WUPSStorageDeprecated::wups_loader_init_storage_args_t))((uint32_t *) func_ptr))(args);
// clang-format on
break;
} else {
DEBUG_FUNCTION_LINE_ERR("WUPS_LOADER_HOOK_INIT_STORAGE hook for WUPSVersion 0.7.2 or higher not implemented");
break;
}
}
default: {
DEBUG_FUNCTION_LINE_ERR("######################################");

View File

@ -1,146 +0,0 @@
#include "StorageUtils.h"
#include "NotificationsUtils.h"
#include "StringTools.h"
#include "fs/CFile.hpp"
#include "fs/FSUtils.h"
#include "utils.h"
#include "utils/json.hpp"
#include "utils/logger.h"
#include <notifications/notifications.h>
static void processJson(wups_storage_item_t *items, nlohmann::json json) {
if (items == nullptr) {
return;
}
items->data = (wups_storage_item_t *) malloc(json.size() * sizeof(wups_storage_item_t));
items->data_size = json.size();
uint32_t index = 0;
for (auto it = json.begin(); it != json.end(); ++it) {
wups_storage_item_t *item = &((wups_storage_item_t *) items->data)[index];
item->type = WUPS_STORAGE_TYPE_INVALID;
item->deleted = false;
item->data = nullptr;
item->key = nullptr;
item->key = (char *) malloc(it.key().size() + 1);
strcpy(item->key, it.key().c_str());
if (it.value().is_string()) {
item->type = WUPS_STORAGE_TYPE_STRING;
uint32_t size = it.value().get<std::string>().size() + 1;
item->data = malloc(size);
item->data_size = size;
strcpy((char *) item->data, it.value().get<std::string>().c_str());
} else if (it.value().is_number_integer()) {
item->type = WUPS_STORAGE_TYPE_INT;
item->data = malloc(sizeof(int32_t));
item->data_size = sizeof(int32_t);
*(int32_t *) item->data = it.value().get<int32_t>();
} else if (it.value().is_object()) {
if (it.value().size() > 0) {
item->type = WUPS_STORAGE_TYPE_ITEM;
processJson(item, it.value());
}
} else {
DEBUG_FUNCTION_LINE_ERR("Unknown type %s for value %s", it.value().type_name(), it.key().c_str());
}
index++;
}
}
WUPSStorageError StorageUtils::OpenStorage(const char *plugin_id, wups_storage_item_t *items) {
if (!plugin_id || !items) {
return WUPS_STORAGE_ERROR_INVALID_BACKEND_PARAMS;
}
std::string filePath = getPluginPath() + "/config/" + plugin_id + ".json";
nlohmann::json j;
CFile file(filePath, CFile::ReadOnly);
if (file.isOpen() && file.size() > 0) {
auto *json_data = (uint8_t *) memalign(0x40, ROUNDUP(file.size() + 1, 0x40));
json_data[file.size()] = '\0';
file.read(json_data, file.size());
file.close();
j = nlohmann::json::parse(json_data, nullptr, false);
free(json_data);
if (j == nlohmann::detail::value_t::discarded || j.empty() || !j.is_object()) {
std::string errorMessage = string_format("Corrupted plugin storage detected: \"%s\". You have to reconfigure the plugin.", plugin_id);
DEBUG_FUNCTION_LINE_ERR("%s", errorMessage.c_str());
remove(filePath.c_str());
DisplayErrorNotificationMessage(errorMessage, 10.0f);
return WUPS_STORAGE_ERROR_SUCCESS;
}
} else { // empty or no config exists yet
return WUPS_STORAGE_ERROR_SUCCESS;
}
processJson(items, j["storageitems"]);
return WUPS_STORAGE_ERROR_SUCCESS;
}
static nlohmann::json processItems(wups_storage_item_t *items) {
nlohmann::json json;
if (!items) {
return json;
}
for (uint32_t i = 0; i < items->data_size; i++) {
wups_storage_item_t *item = &((wups_storage_item_t *) items->data)[i];
if (item->deleted || item->type == WUPS_STORAGE_TYPE_INVALID || !item->data || !item->key) {
continue;
}
if (item->type == WUPS_STORAGE_TYPE_STRING) {
json[item->key] = (const char *) item->data;
} else if (item->type == WUPS_STORAGE_TYPE_INT) {
json[item->key] = *(int32_t *) item->data;
} else if (item->type == WUPS_STORAGE_TYPE_ITEM) {
json[item->key] = processItems(item);
} else {
DEBUG_FUNCTION_LINE_ERR("Saving type %d not implemented", item->type);
}
}
return json;
}
WUPSStorageError StorageUtils::CloseStorage(const char *plugin_id, wups_storage_item_t *items) {
if (!plugin_id || !items) {
return WUPS_STORAGE_ERROR_INVALID_BACKEND_PARAMS;
}
std::string folderPath = getPluginPath() + "/config/";
std::string filePath = folderPath + plugin_id + ".json";
FSUtils::CreateSubfolder(folderPath);
CFile file(filePath, CFile::WriteOnly);
if (!file.isOpen()) {
DEBUG_FUNCTION_LINE_ERR("Cannot create file %s", filePath.c_str());
return WUPS_STORAGE_ERROR_IO;
};
nlohmann::json j;
j["storageitems"] = processItems(items);
std::string jsonString = j.dump(4, ' ', false, nlohmann::json::error_handler_t::ignore);
auto writeResult = file.write((const uint8_t *) jsonString.c_str(), jsonString.size());
file.close();
if (writeResult != (int32_t) jsonString.size()) {
return WUPS_STORAGE_ERROR_IO;
}
return WUPS_STORAGE_ERROR_SUCCESS;
}

View File

@ -1,10 +0,0 @@
#pragma once
#include <wups/storage.h>
class StorageUtils {
public:
static WUPSStorageError OpenStorage(const char *plugin_id, wups_storage_item_t *items);
static WUPSStorageError CloseStorage(const char *plugin_id, wups_storage_item_t *items);
};

View File

@ -0,0 +1,148 @@
#include "StorageUtilsDeprecated.h"
#include "NotificationsUtils.h"
#include "StringTools.h"
#include "fs/CFile.hpp"
#include "fs/FSUtils.h"
#include "utils.h"
#include "utils/json.hpp"
#include "utils/logger.h"
namespace WUPSStorageDeprecated {
static void processJson(wups_storage_item_t *items, nlohmann::json json) {
if (items == nullptr) {
return;
}
items->data = (wups_storage_item_t *) malloc(json.size() * sizeof(wups_storage_item_t));
items->data_size = json.size();
uint32_t index = 0;
for (auto it = json.begin(); it != json.end(); ++it) {
wups_storage_item_t *item = &((wups_storage_item_t *) items->data)[index];
item->type = WUPS_STORAGE_TYPE_INVALID;
item->deleted = false;
item->data = nullptr;
item->key = nullptr;
item->key = (char *) malloc(it.key().size() + 1);
strcpy(item->key, it.key().c_str());
if (it.value().is_string()) {
item->type = WUPS_STORAGE_TYPE_STRING;
uint32_t size = it.value().get<std::string>().size() + 1;
item->data = malloc(size);
item->data_size = size;
strcpy((char *) item->data, it.value().get<std::string>().c_str());
} else if (it.value().is_number_integer()) {
item->type = WUPS_STORAGE_TYPE_INT;
item->data = malloc(sizeof(int32_t));
item->data_size = sizeof(int32_t);
*(int32_t *) item->data = it.value().get<int32_t>();
} else if (it.value().is_object()) {
if (it.value().size() > 0) {
item->type = WUPS_STORAGE_TYPE_ITEM;
processJson(item, it.value());
}
} else {
DEBUG_FUNCTION_LINE_ERR("Unknown type %s for value %s", it.value().type_name(), it.key().c_str());
}
index++;
}
}
WUPSStorageError StorageUtils::OpenStorage(const char *plugin_id, wups_storage_item_t *items) {
if (!plugin_id || !items) {
return WUPS_STORAGE_ERROR_INVALID_BACKEND_PARAMS;
}
std::string filePath = getPluginPath() + "/config/" + plugin_id + ".json";
nlohmann::json j;
CFile file(filePath, CFile::ReadOnly);
if (file.isOpen() && file.size() > 0) {
auto *json_data = (uint8_t *) memalign(0x40, ROUNDUP(file.size() + 1, 0x40));
json_data[file.size()] = '\0';
file.read(json_data, file.size());
file.close();
j = nlohmann::json::parse(json_data, nullptr, false);
free(json_data);
if (j == nlohmann::detail::value_t::discarded || j.empty() || !j.is_object()) {
std::string errorMessage = string_format("Corrupted plugin storage detected: \"%s\". You have to reconfigure the plugin.", plugin_id);
DEBUG_FUNCTION_LINE_ERR("%s", errorMessage.c_str());
remove(filePath.c_str());
DisplayErrorNotificationMessage(errorMessage, 10.0f);
return WUPS_STORAGE_ERROR_SUCCESS;
}
} else { // empty or no config exists yet
return WUPS_STORAGE_ERROR_SUCCESS;
}
processJson(items, j["storageitems"]);
return WUPS_STORAGE_ERROR_SUCCESS;
}
static nlohmann::json processItems(wups_storage_item_t *items) {
nlohmann::json json;
if (!items) {
return json;
}
for (uint32_t i = 0; i < items->data_size; i++) {
wups_storage_item_t *item = &((wups_storage_item_t *) items->data)[i];
if (item->deleted || item->type == WUPS_STORAGE_TYPE_INVALID || !item->data || !item->key) {
continue;
}
if (item->type == WUPS_STORAGE_TYPE_STRING) {
json[item->key] = (const char *) item->data;
} else if (item->type == WUPS_STORAGE_TYPE_INT) {
json[item->key] = *(int32_t *) item->data;
} else if (item->type == WUPS_STORAGE_TYPE_ITEM) {
json[item->key] = processItems(item);
} else {
DEBUG_FUNCTION_LINE_ERR("Saving type %d not implemented", item->type);
}
}
return json;
}
WUPSStorageError StorageUtils::CloseStorage(const char *plugin_id, wups_storage_item_t *items) {
if (!plugin_id || !items) {
return WUPS_STORAGE_ERROR_INVALID_BACKEND_PARAMS;
}
std::string folderPath = getPluginPath() + "/config/";
std::string filePath = folderPath + plugin_id + ".json";
FSUtils::CreateSubfolder(folderPath);
CFile file(filePath, CFile::WriteOnly);
if (!file.isOpen()) {
DEBUG_FUNCTION_LINE_ERR("Cannot create file %s", filePath.c_str());
return WUPS_STORAGE_ERROR_IO;
}
nlohmann::json j;
j["storageitems"] = processItems(items);
std::string jsonString = j.dump(4, ' ', false, nlohmann::json::error_handler_t::ignore);
auto writeResult = file.write((const uint8_t *) jsonString.c_str(), jsonString.size());
file.close();
if (writeResult != (int32_t) jsonString.size()) {
return WUPS_STORAGE_ERROR_IO;
}
return WUPS_STORAGE_ERROR_SUCCESS;
}
} // namespace WUPSStorageDeprecated

View File

@ -0,0 +1,51 @@
#pragma once
#include <cstdint>
namespace WUPSStorageDeprecated {
typedef enum wups_storage_type_t_ {
WUPS_STORAGE_TYPE_INVALID,
WUPS_STORAGE_TYPE_STRING,
WUPS_STORAGE_TYPE_INT,
WUPS_STORAGE_TYPE_ITEM,
} wups_storage_type_t;
typedef enum {
WUPS_STORAGE_ERROR_SUCCESS = 0,
WUPS_STORAGE_ERROR_NOT_OPENED = -1,
WUPS_STORAGE_ERROR_ALREADY_OPENED = -2,
WUPS_STORAGE_ERROR_INVALID_ARGS = -3,
WUPS_STORAGE_ERROR_NOT_FOUND = -4,
WUPS_STORAGE_ERROR_NOT_INITIALIZED = -5,
WUPS_STORAGE_ERROR_INVALID_BACKEND_PARAMS = -6,
WUPS_STORAGE_ERROR_INVALID_JSON = -7,
WUPS_STORAGE_ERROR_IO = -8,
WUPS_STORAGE_ERROR_B64_DECODE_FAILED = -9,
WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL = -10,
WUPS_STORAGE_ERROR_MALLOC_FAILED = -11,
WUPS_STORAGE_ERROR_NOT_ACTIVE_CATEGORY = -13,
} WUPSStorageError;
typedef struct wups_storage_item_t_ {
char *key;
void *data;
uint32_t data_size;
uint32_t deleted;
wups_storage_type_t type;
} wups_storage_item_t;
typedef WUPSStorageError (*OpenStorageFunction)(const char *plugin_id, wups_storage_item_t *items);
typedef WUPSStorageError (*CloseStorageFunction)(const char *plugin_id, wups_storage_item_t *items);
typedef struct wups_loader_init_storage_args_t_ {
OpenStorageFunction open_storage_ptr;
CloseStorageFunction close_storage_ptr;
const char *plugin_id;
} wups_loader_init_storage_args_t;
class StorageUtils {
public:
static WUPSStorageError OpenStorage(const char *plugin_id, wups_storage_item_t *items);
static WUPSStorageError CloseStorage(const char *plugin_id, wups_storage_item_t *items);
};
} // namespace WUPSStorageDeprecated