WiiUPluginSystem/libraries/libwups/storage.cpp

159 lines
7.6 KiB
C++
Raw Permalink Normal View History

#include <cstring>
2023-10-01 13:00:55 +02:00
#include <wups/storage.h>
2023-10-01 13:00:27 +02:00
struct wups_internal_functions_t {
2023-12-14 17:20:38 +01:00
WUPSStorage_SaveFunction save_function_ptr = nullptr;
WUPSStorage_ForceReloadFunction force_reload_function_ptr = nullptr;
WUPSStorage_WipeStorageFunction wipe_storage_function_ptr = nullptr;
WUPSStorage_DeleteItemFunction delete_item_function_ptr = nullptr;
WUPSStorage_CreateSubItemFunction create_sub_item_function_ptr = nullptr;
WUPSStorage_GetSubItemFunction get_sub_item_function_ptr = nullptr;
WUPSStorage_StoreItemFunction store_item_function_ptr = nullptr;
WUPSStorage_GetItemFunction get_item_function_ptr = nullptr;
WUPSStorage_GetItemSizeFunction get_item_size_function_ptr = nullptr;
wups_storage_root_item __storageroot_item = nullptr;
2023-10-01 13:00:27 +02:00
};
static wups_internal_functions_t __internal_functions __attribute__((section(".data"))) = {};
2023-12-14 17:20:38 +01:00
WUPSStorageError WUPSStorageAPI_InitInternal(wups_loader_init_storage_args_t args) {
2023-10-01 13:00:55 +02:00
if (args.version > WUPS_STORAGE_CUR_API_VERSION) {
2023-10-01 13:00:27 +02:00
__internal_functions = {};
2023-12-14 17:20:38 +01:00
return WUPS_STORAGE_ERROR_INTERNAL_INVALID_VERSION;
2023-10-01 13:00:27 +02:00
}
2023-12-14 17:20:38 +01:00
__internal_functions.__storageroot_item = args.root_item;
__internal_functions.save_function_ptr = args.save_function_ptr;
__internal_functions.force_reload_function_ptr = args.force_reload_function_ptr;
__internal_functions.wipe_storage_function_ptr = args.wipe_storage_function_ptr;
2023-10-01 13:00:27 +02:00
__internal_functions.delete_item_function_ptr = args.delete_item_function_ptr;
__internal_functions.create_sub_item_function_ptr = args.create_sub_item_function_ptr;
__internal_functions.get_sub_item_function_ptr = args.get_sub_item_function_ptr;
__internal_functions.store_item_function_ptr = args.store_item_function_ptr;
__internal_functions.get_item_function_ptr = args.get_item_function_ptr;
__internal_functions.get_item_size_function_ptr = args.get_item_size_function_ptr;
return WUPS_STORAGE_ERROR_SUCCESS;
}
2023-12-14 17:20:38 +01:00
const char *WUPSStorageAPI_GetStatusStr(WUPSStorageError status) {
2022-08-26 19:27:00 +02:00
switch (status) {
case WUPS_STORAGE_ERROR_SUCCESS:
return "WUPS_STORAGE_ERROR_SUCCESS";
case WUPS_STORAGE_ERROR_INVALID_ARGS:
return "WUPS_STORAGE_ERROR_INVALID_ARGS";
case WUPS_STORAGE_ERROR_MALLOC_FAILED:
return "WUPS_STORAGE_ERROR_MALLOC_FAILED";
2023-12-14 17:20:38 +01:00
case WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE:
return "WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE";
2023-10-01 13:00:27 +02:00
case WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL:
return "WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL";
case WUPS_STORAGE_ERROR_ALREADY_EXISTS:
return "WUPS_STORAGE_ERROR_ALREADY_EXISTS";
2023-12-14 17:20:38 +01:00
case WUPS_STORAGE_ERROR_IO_ERROR:
return "WUPS_STORAGE_ERROR_IO_ERROR";
case WUPS_STORAGE_ERROR_NOT_FOUND:
return "WUPS_STORAGE_ERROR_NOT_FOUND";
case WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED:
return "WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED";
case WUPS_STORAGE_ERROR_INTERNAL_INVALID_VERSION:
return "WUPS_STORAGE_ERROR_INTERNAL_INVALID_VERSION";
2023-10-01 13:00:27 +02:00
case WUPS_STORAGE_ERROR_UNKNOWN_ERROR:
return "WUPS_STORAGE_ERROR_UNKNOWN_ERROR";
2022-08-26 19:27:00 +02:00
}
return "WUPS_STORAGE_ERROR_UNKNOWN";
}
2023-12-14 17:20:38 +01:00
WUPSStorageError WUPSStorageAPI_SaveStorage(bool force) {
if (__internal_functions.save_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-12-14 17:20:38 +01:00
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-12-14 17:20:38 +01:00
return __internal_functions.save_function_ptr(__internal_functions.__storageroot_item, force);
}
2023-12-14 17:20:38 +01:00
WUPSStorageError WUPSStorageAPI_ForceReloadStorage() {
if (__internal_functions.force_reload_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-12-14 17:20:38 +01:00
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-12-14 17:20:38 +01:00
return __internal_functions.force_reload_function_ptr(__internal_functions.__storageroot_item);
2023-10-01 13:00:27 +02:00
}
2023-12-14 17:20:38 +01:00
WUPSStorageError WUPSStorageAPI_WipeStorage() {
if (__internal_functions.wipe_storage_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.wipe_storage_function_ptr(__internal_functions.__storageroot_item);
}
WUPSStorageError WUPSStorageAPI_DeleteItem(wups_storage_item parent, const char *key) {
2023-10-01 13:00:27 +02:00
if (__internal_functions.delete_item_function_ptr == nullptr) {
2023-12-14 17:20:38 +01:00
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-10-01 13:00:27 +02:00
return __internal_functions.delete_item_function_ptr(__internal_functions.__storageroot_item, parent, key);
}
2023-12-14 17:20:38 +01:00
WUPSStorageError WUPSStorageAPI_CreateSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem) {
2023-10-01 13:00:27 +02:00
if (__internal_functions.create_sub_item_function_ptr == nullptr) {
2023-12-14 17:20:38 +01:00
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-10-01 13:00:27 +02:00
return __internal_functions.create_sub_item_function_ptr(__internal_functions.__storageroot_item, parent, key, outItem);
}
2023-12-14 17:20:38 +01:00
WUPSStorageError WUPSStorageAPI_GetSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem) {
2023-10-01 13:00:27 +02:00
if (__internal_functions.get_sub_item_function_ptr == nullptr) {
2023-12-14 17:20:38 +01:00
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-10-01 13:00:27 +02:00
return __internal_functions.get_sub_item_function_ptr(__internal_functions.__storageroot_item, parent, key, outItem);
}
2023-12-14 17:20:38 +01:00
WUPSStorageError WUPSStorageAPI_StoreItem(wups_storage_item parent, const char *key, WUPSStorageItemType type, void *data, uint32_t size) {
2023-10-01 13:00:27 +02:00
if (__internal_functions.store_item_function_ptr == nullptr) {
2023-12-14 17:20:38 +01:00
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-12-14 17:20:38 +01:00
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-12-14 17:20:38 +01:00
return __internal_functions.store_item_function_ptr(__internal_functions.__storageroot_item, parent, key, type, data, size);
2023-10-01 13:00:27 +02:00
}
2023-12-14 17:20:38 +01:00
WUPSStorageError WUPSStorageAPI_GetItem(wups_storage_item parent, const char *key, WUPSStorageItemType type, void *data, uint32_t maxSize, uint32_t *outSize) {
2023-10-01 13:00:27 +02:00
if (data == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
2023-10-01 13:00:27 +02:00
if (__internal_functions.get_item_function_ptr == nullptr) {
2023-12-14 17:20:38 +01:00
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-12-14 17:20:38 +01:00
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
2023-12-14 17:20:38 +01:00
return __internal_functions.get_item_function_ptr(__internal_functions.__storageroot_item, parent, key, type, data, maxSize, outSize);
}
WUPSStorageError WUPSStorageAPI_GetItemSize(wups_storage_item parent, const char *key, WUPSStorageItemType itemType, uint32_t *outSize) {
2023-10-01 13:00:27 +02:00
if (outSize == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
2023-10-01 13:00:27 +02:00
if (__internal_functions.get_item_size_function_ptr == nullptr) {
2023-12-14 17:20:38 +01:00
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.get_item_size_function_ptr(__internal_functions.__storageroot_item, parent, key, itemType, outSize);
}