New config API

This commit is contained in:
Maschell
2023-10-01 13:00:55 +02:00
parent d84fd56329
commit ba3cd165dd
18 changed files with 490 additions and 162 deletions

View File

@@ -27,8 +27,17 @@
#include "wups/common.h"
#include "wups/config.h"
#include "wups/config_imports.h"
#include "wups/config_api.h"
#include "wups/function_patching.h"
#include "wups/hooks.h"
#include "wups/meta.h"
#include "wups/storage.h"
#ifdef DEBUG
#include <coreinit/debug.h>
#endif
#ifdef DEBUG
#define WUPS_DEBUG_REPORT(fmt, ...) OSReport(fmt, ##__VA_ARGS__)
#else
#define WUPS_DEBUG_REPORT(fmt, ...)
#endif

View File

@@ -38,6 +38,26 @@
#define WUPS_CONFIG_BUTTON_MINUS (1 << 15)
typedef int32_t WUPSConfigButtons;
typedef enum WUPSConfigAPIStatus {
WUPSCONFIG_API_RESULT_SUCCESS = 0,
WUPSCONFIG_API_RESULT_INVALID_ARGUMENT = -0x01,
WUPSCONFIG_API_RESULT_OUT_OF_MEMORY = -0x03,
WUPSCONFIG_API_RESULT_NOT_FOUND = -0x06,
WUPSCONFIG_API_RESULT_INVALID_PLUGIN_IDENTIFIER = -0x70,
WUPSCONFIG_API_RESULT_MISSING_CALLBACK = -0x71,
WUPSCONFIG_API_RESULT_MODULE_NOT_FOUND = -0x80,
WUPSCONFIG_API_RESULT_MODULE_MISSING_EXPORT = -0x81,
WUPSCONFIG_API_RESULT_UNSUPPORTED_VERSION = -0x82,
WUPSCONFIG_API_RESULT_UNSUPPORTED_COMMAND = -0x83,
WUPSCONFIG_API_RESULT_LIB_UNINITIALIZED = -0x84,
WUPSCONFIG_API_RESULT_UNKNOWN_ERROR = -0x100,
} WUPSConfigAPIStatus;
typedef enum WUPSConfigAPICallbackStatus {
WUPSCONFIG_API_CALLBACK_RESULT_SUCCESS = 0,
WUPSCONFIG_API_CALLBACK_RESULT_ERROR = -1,
} WUPSConfigAPICallbackStatus;
typedef struct {
int32_t (*getCurrentValueDisplay)(void *context, char *out_buf, int32_t out_size);
@@ -54,8 +74,101 @@ typedef struct {
void (*onButtonPressed)(void *context, WUPSConfigButtons button);
void (*onDelete)(void *context);
} WUPSConfigCallbacks_t;
} WUPSConfigAPIItemCallbacksV1;
typedef uint32_t WUPSConfigItemHandle;
typedef uint32_t WUPSConfigHandle;
typedef uint32_t WUPSConfigCategoryHandle;
#define WUPS_API_ITEM_OPTION_VERSION_V1 1
typedef struct WUPSConfigAPIItemOptionsV1 {
const char *displayName;
void *context;
WUPSConfigAPIItemCallbacksV1 callbacks;
} WUPSConfigAPIItemOptionsV1;
typedef struct WUPSConfigAPICreateItemOptions {
uint32_t version;
union {
WUPSConfigAPIItemOptionsV1 v1;
} data;
} WUPSConfigAPICreateItemOptions;
typedef uint32_t WUPSConfigAPIVersion;
typedef struct WUPSConfigItemHandle {
void *handle;
#ifdef __cplusplus
WUPSConfigItemHandle() {
handle = nullptr;
}
explicit WUPSConfigItemHandle(void *handle) : handle(handle) {}
bool operator==(const WUPSConfigItemHandle other) const {
return handle == other.handle;
}
bool operator==(const void *other) const {
return handle == other;
}
#endif
} WUPSConfigItemHandle;
typedef struct WUPSConfigHandle {
void *handle;
#ifdef __cplusplus
WUPSConfigHandle() {
handle = nullptr;
}
explicit WUPSConfigHandle(void *handle) : handle(handle) {}
bool operator==(const WUPSConfigHandle other) const {
return handle == other.handle;
}
bool operator==(const void *other) const {
return handle == other;
}
#endif
} WUPSConfigHandle;
typedef struct WUPSConfigCategoryHandle {
void *handle;
#ifdef __cplusplus
WUPSConfigCategoryHandle() {
handle = nullptr;
}
explicit WUPSConfigCategoryHandle(void *handle) : handle(handle) {}
bool operator==(const WUPSConfigCategoryHandle other) const {
return handle == other.handle;
}
bool operator==(const void *other) const {
return handle == other;
}
#endif
} WUPSConfigCategoryHandle;
#define WUPS_API_CATEGORY_OPTION_VERSION_V1 1
typedef struct WUPSConfigAPICreateCategoryOptionsV1 {
const char *name;
} WUPSConfigAPICreateCategoryOptionsV1;
typedef struct WUPSConfigAPICreateCategoryOptions {
uint32_t version;
union {
WUPSConfigAPICreateCategoryOptionsV1 v1;
} data;
} WUPSConfigAPICreateCategoryOptions;
#define WUPS_API_CONFIG_API_OPTION_VERSION_V1 1
typedef struct WUPSConfigAPIOptionsV1 {
const char *name;
} WUPSConfigAPIOptionsV1;
typedef struct WUPSConfigAPIOptions {
uint32_t version;
union {
WUPSConfigAPIOptionsV1 v1;
} data;
} WUPSConfigAPIOptions;
#define WUPS_CONFIG_API_VERSION_ERROR 0xFFFFFFFF
typedef struct wups_loader_init_config_args_t {
uint32_t arg_version;
uint32_t plugin_identifier;
} wups_loader_init_config_args_t;

View File

@@ -20,22 +20,6 @@ bool WUPSConfigItemBoolean_AddToCategory(WUPSConfigCategoryHandle cat, const cha
bool WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, bool defaultValue, BooleanValueChangedCallback callback, const char *trueValue,
const char *falseValue);
#define WUPSConfigItemBoolean_AddToCategoryHandled(__config__, __cat__, __configIs__, __displayName__, __defaultValue__, __callback__) \
do { \
if (!WUPSConfigItemBoolean_AddToCategory(__cat__, __configIs__, __displayName__, __defaultValue__, __callback__)) { \
WUPSConfig_Destroy(__config__); \
return 0; \
} \
} while (0)
#define WUPSConfigItemBoolean_AddToCategoryHandledEx(__config__, __cat__, __configID__, __displayName__, __defaultValue__, __callback__, __trueValue__, __falseValue__) \
do { \
if (!WUPSConfigItemBoolean_AddToCategoryEx(__cat__, __configID__, __displayName__, __defaultValue__, __callback__, __trueValue__, __falseValue__)) { \
WUPSConfig_Destroy(__config__); \
return 0; \
} \
} while (0)
#ifdef __cplusplus
}
#endif

View File

@@ -20,14 +20,6 @@ bool WUPSConfigItemIntegerRange_AddToCategory(WUPSConfigCategoryHandle cat, cons
int32_t defaultValue, int32_t minValue, int32_t maxValue,
IntegerRangeValueChangedCallback callback);
#define WUPSConfigItemIntegerRange_AddToCategoryHandled(__config__, __cat__, __configId__, __displayName__, __defaultValue__, __minValue__, __maxValue__, __callback__) \
do { \
if (!WUPSConfigItemIntegerRange_AddToCategory(__cat__, __configId__, __displayName__, __defaultValue__, __minValue__, __maxValue__, __callback__)) { \
WUPSConfig_Destroy(__config__); \
return 0; \
} \
} while (0)
#ifdef __cplusplus
}
#endif

View File

@@ -1,4 +1,6 @@
#include <wups.h>
#include <stdbool.h>
#include <stdint.h>
#include <wups/config.h>
#ifdef __cplusplus
extern "C" {
@@ -24,14 +26,6 @@ typedef void (*MultipleValuesChangedCallback)(ConfigItemMultipleValues *, uint32
bool WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, int defaultValueIndex, ConfigItemMultipleValuesPair *possibleValues,
int pairCount, MultipleValuesChangedCallback callback);
#define WUPSConfigItemMultipleValues_AddToCategoryHandled(__config__, __cat__, __configID__, __displayName__, __defaultValueIndex__, __possibleValues__, __pairCount__, __callback__) \
do { \
if (!WUPSConfigItemMultipleValues_AddToCategory(__cat__, __configID__, __displayName__, __defaultValueIndex__, __possibleValues__, __pairCount__, __callback__)) { \
WUPSConfig_Destroy(__config__); \
return 0; \
} \
} while (0)
#ifdef __cplusplus
}
#endif

View File

@@ -10,14 +10,6 @@ typedef struct ConfigItemStub {
bool WUPSConfigItemStub_AddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName);
#define WUPSConfigItemStub_AddToCategoryHandled(__config__, __cat__, __configID__, __displayName__) \
do { \
if (!WUPSConfigItemStub_AddToCategory(__cat__, __configID__, __displayName__)) { \
WUPSConfig_Destroy(__config__); \
return 0; \
} \
} while (0)
#ifdef __cplusplus
}
#endif

52
include/wups/config_api.h Normal file
View File

@@ -0,0 +1,52 @@
#pragma once
#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef WUPSConfigAPICallbackStatus (*WUPSConfigAPI_MenuOpenedCallback)(WUPSConfigCategoryHandle root);
typedef void (*WUPSConfigAPI_MenuClosedCallback)();
WUPSConfigAPIStatus WUPSConfigAPI_InitEx(uint32_t pluginIdentifier, WUPSConfigAPIOptions, WUPSConfigAPI_MenuOpenedCallback, WUPSConfigAPI_MenuClosedCallback);
WUPSConfigAPIStatus WUPSConfigAPI_Init(WUPSConfigAPIOptionsV1 optionsV1, WUPSConfigAPI_MenuOpenedCallback openedCallback, WUPSConfigAPI_MenuClosedCallback closedCallback);
WUPSConfigAPIStatus WUPSConfigAPI_GetVersion(WUPSConfigAPIVersion *outVariable);
WUPSConfigAPIStatus WUPSConfigAPI_Category_CreateEx(WUPSConfigAPICreateCategoryOptions options, WUPSConfigCategoryHandle *out);
static inline WUPSConfigAPIStatus WUPSConfigAPI_Category_Create(WUPSConfigAPICreateCategoryOptionsV1 options, WUPSConfigCategoryHandle *out) {
WUPSConfigAPICreateCategoryOptions optionsWrapper = {
.version = WUPS_API_CATEGORY_OPTION_VERSION_V1,
.data = {.v1 = options},
};
return WUPSConfigAPI_Category_CreateEx(optionsWrapper, out);
}
WUPSConfigAPIStatus WUPSConfigAPI_Category_Destroy(WUPSConfigCategoryHandle handle);
WUPSConfigAPIStatus WUPSConfigAPI_Category_AddCategory(WUPSConfigCategoryHandle parentHandle, WUPSConfigCategoryHandle categoryHandle);
WUPSConfigAPIStatus WUPSConfigAPI_Category_AddItem(WUPSConfigCategoryHandle parentHandle, WUPSConfigItemHandle itemHandle);
WUPSConfigAPIStatus WUPSConfigAPI_Item_CreateEx(WUPSConfigAPICreateItemOptions options, WUPSConfigItemHandle *out);
static inline WUPSConfigAPIStatus WUPSConfigAPI_Item_Create(WUPSConfigAPIItemOptionsV1 options, WUPSConfigItemHandle *out) {
WUPSConfigAPICreateItemOptions itemOptions = {
.version = WUPS_API_ITEM_OPTION_VERSION_V1,
.data = {.v1 = options},
};
return WUPSConfigAPI_Item_CreateEx(itemOptions, out);
}
WUPSConfigAPIStatus WUPSConfigAPI_Item_Destroy(WUPSConfigItemHandle handle);
const char *WUPSConfigAPI_GetStatusStr(WUPSConfigAPIStatus status);
#ifdef __cplusplus
}
#endif

View File

@@ -1,63 +0,0 @@
#pragma once
#include "config.h"
#include "stdint.h"
#ifdef __cplusplus
extern "C" {
#endif
int32_t WUPSConfigItem_Create(WUPSConfigItemHandle *out, const char *configID, const char *displayName, WUPSConfigCallbacks_t callbacks, void *context);
int32_t WUPSConfigItem_Destroy(WUPSConfigItemHandle handle);
int32_t WUPSConfig_Destroy(WUPSConfigHandle handle);
int32_t WUPSConfigItem_SetDisplayName(WUPSConfigItemHandle handle, const char *displayName);
int32_t WUPSConfigItem_GetDisplayName(WUPSConfigItemHandle handle, char *out_buf, int32_t out_len);
int32_t WUPSConfigItem_SetConfigID(WUPSConfigItemHandle handle, const char *configID);
int32_t WUPSConfigItem_GetConfigID(WUPSConfigItemHandle handle, char *out_buf, int32_t out);
int32_t WUPSConfig_Create(WUPSConfigHandle *out, const char *name);
int32_t WUPSConfigCategory_Destroy(WUPSConfigCategoryHandle handle);
int32_t WUPSConfig_GetName(WUPSConfigHandle handle, char *out_buf, int32_t out_len);
int32_t WUPSConfig_AddCategoryByName(WUPSConfigHandle handle, const char *categoryName, WUPSConfigCategoryHandle *out);
int32_t WUPSConfig_AddCategory(WUPSConfigHandle handle, WUPSConfigCategoryHandle category);
/*
int32_t WUPSConfig_GetCategoryCount(WUPSConfigHandle handle, int32_t *category_count);
int32_t WUPSConfig_GetCategories(WUPSConfigHandle handle, WUPSConfigCategoryHandle *categories_out, int32_t categories_out_size);
*/
int32_t WUPSConfigCategory_Create(WUPSConfigCategoryHandle *out, const char *name);
int32_t WUPSConfigCategory_GetName(WUPSConfigCategoryHandle handle, char *out_buf, int32_t out_len);
int32_t WUPSConfigCategory_AddItem(WUPSConfigCategoryHandle handle, WUPSConfigItemHandle item_Handle);
#define WUPSConfig_AddCategoryByNameHandled(__config__, __categoryName__, __out__) \
do { \
if (WUPSConfig_AddCategoryByName(__config__, __categoryName__, __out__) < 0) { \
WUPSConfig_Destroy(__config__); \
return 0; \
} \
} while (0)
#define WUPSConfig_CreateHandled(__config__, __configName__) \
do { \
if (WUPSConfig_Create(__config__, __configName__) < 0) { \
return 0; \
} \
} while (0)
#ifdef __cplusplus
}
#endif

View File

@@ -44,20 +44,21 @@ typedef enum wups_loader_hook_type_t {
WUPS_LOADER_HOOK_INIT_WRAPPER, /* Calls __init */
WUPS_LOADER_HOOK_FINI_WRAPPER, /* Calls __fini */
WUPS_LOADER_HOOK_GET_CONFIG,
WUPS_LOADER_HOOK_CONFIG_CLOSED,
WUPS_LOADER_HOOK_GET_CONFIG_DEPRECATED, /* Deprecated implementation */
WUPS_LOADER_HOOK_CONFIG_CLOSED_DEPRECATED, /* Deprecated implementation */
WUPS_LOADER_HOOK_INIT_STORAGE_DEPRECATED, /* Deprecated implementation */
WUPS_LOADER_HOOK_INIT_PLUGIN, /* Called when exiting the plugin loader */
WUPS_LOADER_HOOK_DEINIT_PLUGIN, /* Called when re-entering the plugin loader */
WUPS_LOADER_HOOK_APPLICATION_STARTS, /* Called when an application gets started */
WUPS_LOADER_HOOK_RELEASE_FOREGROUND, /* Called when an foreground is going to be released */
WUPS_LOADER_HOOK_ACQUIRED_FOREGROUND, /* Called when an foreground is acquired */
WUPS_LOADER_HOOK_RELEASE_FOREGROUND, /* Called when a foreground is going to be released */
WUPS_LOADER_HOOK_ACQUIRED_FOREGROUND, /* Called when a foreground is acquired */
WUPS_LOADER_HOOK_APPLICATION_REQUESTS_EXIT, /* Called when an application wants to exit */
WUPS_LOADER_HOOK_APPLICATION_ENDS, /* Called when an application ends */
WUPS_LOADER_HOOK_INIT_STORAGE, /* Only for internal usage */
WUPS_LOADER_HOOK_INIT_CONFIG, /* Only for internal usage */
} wups_loader_hook_type_t;
typedef struct wups_loader_hook_t {
@@ -100,15 +101,11 @@ typedef struct wups_loader_hook_t {
WUPS_HOOK_EX(WUPS_LOADER_HOOK_APPLICATION_ENDS, on_app_ending); \
void on_app_ending(void)
#define WUPS_GET_CONFIG() \
WUPSConfigHandle on_get_wups_config(void); \
WUPS_HOOK_EX(WUPS_LOADER_HOOK_GET_CONFIG, on_get_wups_config); \
WUPSConfigHandle on_get_wups_config(void)
#define WUPS_GET_CONFIG(x) \
static_assert(false, "Please use \"WUPSConfigAPI_Init\" inside \"INITIALIZE_PLUGIN\" to provide a callback instead");
#define WUPS_CONFIG_CLOSED() \
void on_wups_config_closed(void); \
WUPS_HOOK_EX(WUPS_LOADER_HOOK_CONFIG_CLOSED, on_wups_config_closed); \
void on_wups_config_closed(void)
#define WUPS_CONFIG_CLOSED() \
static_assert(false, "Please use \"WUPSConfigAPI_Init\" inside \"INITIALIZE_PLUGIN\" to provide a callback instead");
#define WUPS_USE_STORAGE(x) \
WUPS_META(storage_id, x); \
@@ -118,12 +115,21 @@ typedef struct wups_loader_hook_t {
WUPS_InitStorage(args); \
}
#ifdef __cplusplus
#define __EXTERN_C_MACRO extern "C"
#else
#define __EXTERN_C_MACRO
#endif
#define WUPS_INIT_CONFIG_FUNCTIONS() \
__EXTERN_C_MACRO WUPSConfigAPIStatus WUPSConfigAPI_InitLibrary_Internal(wups_loader_init_config_args_t args); \
void wups_init_config_functions(wups_loader_init_config_args_t); \
WUPS_HOOK_EX(WUPS_LOADER_HOOK_INIT_CONFIG, wups_init_config_functions); \
void wups_init_config_functions(wups_loader_init_config_args_t args) { \
WUPSConfigAPI_InitLibrary_Internal(args); \
}
#define WUPS_USE_WUT_MALLOC() \
__EXTERN_C_MACRO void __init_wut_malloc(); \
void on_init_wut_malloc() { \

View File

@@ -48,6 +48,7 @@ extern "C" {
WUPS_USE_WUT_STDCPP(); \
WUPS___INIT_WRAPPER(); \
WUPS___FINI_WRAPPER(); \
WUPS_INIT_CONFIG_FUNCTIONS(); \
WUPS_META(buildtimestamp, __DATE__ " " __TIME__); \
extern const char wups_meta_plugin_name[] WUPS_SECTION("meta"); \
const char wups_meta_plugin_name[] = __plugin_name; \