From 9d44e51a75c034a7cacec7ece0ed2f1d785ce385 Mon Sep 17 00:00:00 2001 From: Maschell Date: Tue, 26 Dec 2023 20:30:28 +0100 Subject: [PATCH] WUPS 0.8.0 --- Dockerfile | 2 +- README.md | 2 +- src/main.cpp | 98 ++++++++++++++++++++++++---------------------------- 3 files changed, 47 insertions(+), 55 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5b0521e..f75b679 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ FROM ghcr.io/wiiu-env/devkitppc:20231112 +COPY --from=ghcr.io/wiiu-env/wiiupluginsystem:0.8.0-dev-20231221-ca17105 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libwupsbackend:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/librpxloader:20230621 /artifacts $DEVKITPRO -COPY --from=ghcr.io/wiiu-env/wiiupluginsystem:20230719 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libnotifications:20230621 /artifacts $DEVKITPRO WORKDIR project diff --git a/README.md b/README.md index 5fdfe1d..6798e6d 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Building via `make` only logs errors (via OSReport). To enable logging via the [ `make DEBUG=1` Enables information and error logging via [LoggingModule](https://github.com/wiiu-env/LoggingModule). `make DEBUG=VERBOSE` Enables verbose information and error logging via [LoggingModule](https://github.com/wiiu-env/LoggingModule). -If the [LoggingModule](https://github.com/wiiu-env/LoggingModule) is not present, it'll fallback to UDP (Port 4405) and [CafeOS](https://github.com/wiiu-env/USBSerialLoggingModule) logging. +If the [LoggingModule](https://github.com/wiiu-env/LoggingModule) is not present, it'll fall back to UDP (Port 4405) and [CafeOS](https://github.com/wiiu-env/USBSerialLoggingModule) logging. ## Building using the Dockerfile diff --git a/src/main.cpp b/src/main.cpp index 0ae37c0..9f926a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,11 @@ WUPS_USE_WUT_DEVOPTAB(); WUPS_USE_STORAGE("wiiload"); // Unqiue id for the storage api #define WIILOAD_ENABLED_STRING "enabled" -TcpReceiver *thread = nullptr; +std::unique_ptr tcpReceiverThread = nullptr; + + +static WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle); +static void ConfigMenuClosedCallback(); INITIALIZE_PLUGIN() { RPXLoaderStatus error; @@ -31,79 +35,72 @@ INITIALIZE_PLUGIN() { NotificationModuleStatus res; if ((res = NotificationModule_InitLibrary()) != NOTIFICATION_MODULE_RESULT_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to init NotificationModule"); + DEBUG_FUNCTION_LINE_ERR("Failed to init NotificationModule: %s", NotificationModule_GetStatusStr(res)); gNotificationModuleLoaded = false; } else { NotificationModule_SetDefaultValue(NOTIFICATION_MODULE_NOTIFICATION_TYPE_ERROR, NOTIFICATION_MODULE_DEFAULT_OPTION_DURATION_BEFORE_FADE_OUT, 10.0f); gNotificationModuleLoaded = true; } - // Open storage to read values - WUPSStorageError storageRes = WUPS_OpenStorage(); - if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to open storage %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes); - } else { - if ((storageRes = WUPS_GetBool(nullptr, WIILOAD_ENABLED_STRING, &gWiiloadServerEnabled)) == WUPS_STORAGE_ERROR_NOT_FOUND) { - // Add the value to the storage if it's missing. - if (WUPS_StoreBool(nullptr, WIILOAD_ENABLED_STRING, gWiiloadServerEnabled) != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to store bool"); - } - } else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to get bool %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes); - } - - // Close storage - if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); - } + WUPSConfigAPIOptionsV1 configOptions = {.name = "Wiiload Plugin"}; + if (WUPSConfigAPI_Init(configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to init config api"); } - thread = nullptr; + if (WUPSStorageAPI::GetOrStoreDefault(WIILOAD_ENABLED_STRING, gWiiloadServerEnabled, true) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", WIILOAD_ENABLED_STRING); + } + + if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to save storage"); + } } void gServerEnabledChanged(ConfigItemBoolean *item, bool newValue) { + if (std::string_view(WIILOAD_ENABLED_STRING) != item->identifier) { + DEBUG_FUNCTION_LINE_WARN("Unexpected identifier in bool callback: %s", item->identifier); + return; + } DEBUG_FUNCTION_LINE_VERBOSE("New value in gWiiloadServerEnabled: %d", newValue); gWiiloadServerEnabled = newValue; - if (thread) { - delete thread; - thread = nullptr; - } + + tcpReceiverThread.reset(); + if (gWiiloadServerEnabled) { DEBUG_FUNCTION_LINE("Starting server!"); - thread = new (std::nothrow) TcpReceiver(4299); - if (thread == nullptr) { + tcpReceiverThread = std::make_unique(4299); + + if (tcpReceiverThread == nullptr) { DEBUG_FUNCTION_LINE_ERR("Failed to create wiiload thread"); } } else { DEBUG_FUNCTION_LINE("Wiiload server has been stopped!"); } // If the value has changed, we store it in the storage. - auto res = WUPS_StoreInt(nullptr, item->configId, gWiiloadServerEnabled); + auto res = WUPSStorageAPI::Store(item->identifier, gWiiloadServerEnabled); if (res != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to store gWiiloadServerEnabled: %s (%d)", WUPS_GetStorageStatusStr(res), res); + DEBUG_FUNCTION_LINE_ERR("Failed to store gWiiloadServerEnabled: %s", WUPSStorageAPI_GetStatusStr(res)); } } -WUPS_GET_CONFIG() { - // We open the storage, so we can persist the configuration the user did. - if (WUPS_OpenStorage() != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to open storage"); - return 0; +static WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle) { + try { + WUPSConfigCategory root = WUPSConfigCategory(rootHandle); + + root.add(WUPSConfigItemBoolean::Create(WIILOAD_ENABLED_STRING, "Enable Wiiload", + true, gWiiloadServerEnabled, + &gServerEnabledChanged)); + + } catch (std::exception &e) { + OSReport("Exception T_T : %s\n", e.what()); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; } - - WUPSConfigHandle config; - WUPSConfig_CreateHandled(&config, "Wiiload"); - - WUPSConfigCategoryHandle setting; - WUPSConfig_AddCategoryByNameHandled(config, "Settings", &setting); - WUPSConfigItemBoolean_AddToCategoryHandled(config, setting, WIILOAD_ENABLED_STRING, "Enable Wiiload", gWiiloadServerEnabled, &gServerEnabledChanged); - - return config; + return WUPSCONFIG_API_CALLBACK_RESULT_SUCCESS; } -WUPS_CONFIG_CLOSED() { +static void ConfigMenuClosedCallback() { // Save all changes - if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) { + if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) { DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); } } @@ -113,8 +110,8 @@ ON_APPLICATION_START() { initLogging(); if (gWiiloadServerEnabled) { DEBUG_FUNCTION_LINE("Start wiiload thread"); - thread = new (std::nothrow) TcpReceiver(4299); - if (thread == nullptr) { + tcpReceiverThread = std::make_unique(4299); + if (tcpReceiverThread == nullptr) { DEBUG_FUNCTION_LINE_ERR("Failed to create wiiload thread"); } } else { @@ -123,11 +120,6 @@ ON_APPLICATION_START() { } ON_APPLICATION_ENDS() { - DEBUG_FUNCTION_LINE("Stop wiiload thread!"); - if (thread != nullptr) { - delete thread; - thread = nullptr; - } - DEBUG_FUNCTION_LINE_VERBOSE("Done!"); + tcpReceiverThread.reset(); deinitLogging(); } \ No newline at end of file