From f0f849cd9371c99ef8e3b78e15c6dfb68487544d Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 24 Mar 2024 11:18:47 +0100 Subject: [PATCH] Move wups config code into a new file --- src/globals.cpp | 9 +++ src/{main.h => globals.h} | 9 +++ src/main.cpp | 121 ++++---------------------------------- src/modpackSelector.cpp | 21 +++---- src/utils/config.cpp | 92 +++++++++++++++++++++++++++++ src/utils/config.h | 3 + 6 files changed, 136 insertions(+), 119 deletions(-) create mode 100644 src/globals.cpp rename src/{main.h => globals.h} (59%) create mode 100644 src/utils/config.cpp create mode 100644 src/utils/config.h diff --git a/src/globals.cpp b/src/globals.cpp new file mode 100644 index 0000000..a1d6564 --- /dev/null +++ b/src/globals.cpp @@ -0,0 +1,9 @@ +#include "globals.h" + +bool gAutoApplySingleModpack = DEFAULT_AUTO_APPLY_SINGLE_MODPACK; +bool gSkipPrepareIfSingleModpack = DEFAULT_SKIP_PREPARE_IF_SINGLE_MODPACK; +bool gSDCafiineEnabled = DEFAULT_SDCAFIINE_ENABLED; + + +CRLayerHandle gContentLayerHandle = 0; +CRLayerHandle gAocLayerHandle = 0; \ No newline at end of file diff --git a/src/main.h b/src/globals.h similarity index 59% rename from src/main.h rename to src/globals.h index cc45b6e..4e15d50 100644 --- a/src/main.h +++ b/src/globals.h @@ -1,5 +1,6 @@ #pragma once #include "version.h" +#include #define VERSION "v0.1.3" #define VERSION_FULL VERSION VERSION_EXTRA @@ -8,5 +9,13 @@ #define SKIP_PREPARE_FOR_SINGLE_MODPACK_STRING "skipPrepareForSingleModpack" #define SDCAFIINE_ENABLED_STRING "sdCafiineEnabled" +#define DEFAULT_AUTO_APPLY_SINGLE_MODPACK false +#define DEFAULT_SKIP_PREPARE_IF_SINGLE_MODPACK false +#define DEFAULT_SDCAFIINE_ENABLED true + extern bool gAutoApplySingleModpack; extern bool gSkipPrepareIfSingleModpack; +extern bool gSDCafiineEnabled; + +extern CRLayerHandle gAocLayerHandle; +extern CRLayerHandle gContentLayerHandle; diff --git a/src/main.cpp b/src/main.cpp index b90d21f..477e1d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ -#include "main.h" +#include "globals.h" #include "modpackSelector.h" +#include "utils/config.h" #include "utils/logger.h" #include #include @@ -13,52 +14,19 @@ WUPS_PLUGIN_AUTHOR("Maschell"); WUPS_PLUGIN_LICENSE("GPL"); WUPS_USE_WUT_DEVOPTAB(); -WUPS_USE_STORAGE("sdcafiine"); // Unqiue id for the storage api - -CRLayerHandle sContentLayerHandle = 0; -CRLayerHandle sAocLayerHandle = 0; - -#define DEFAULT_AUTO_APPLY_SINGLE_MODPACK false -#define DEFAULT_SKIP_PREPARE_IF_SINGLE_MODPACK false -#define DEFAULT_SDCAFIINE_ENABLED true - -bool gAutoApplySingleModpack = DEFAULT_AUTO_APPLY_SINGLE_MODPACK; -bool gSkipPrepareIfSingleModpack = DEFAULT_SKIP_PREPARE_IF_SINGLE_MODPACK; -bool gSDCafiineEnabled = DEFAULT_SDCAFIINE_ENABLED; - -WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle); - -void ConfigMenuClosedCallback(); +WUPS_USE_STORAGE("sdcafiine"); // Unique id for the storage api INITIALIZE_PLUGIN() { - // But then use libcontentredirection instead. ContentRedirectionStatus error; if ((error = ContentRedirection_InitLibrary()) != CONTENT_REDIRECTION_RESULT_SUCCESS) { DEBUG_FUNCTION_LINE_ERR("Failed to init ContentRedirection. Error %s %d", ContentRedirection_GetStatusStr(error), error); OSFatal("Failed to init ContentRedirection."); } - if (WUPSStorageAPI::GetOrStoreDefault(AUTO_APPLY_SINGLE_MODPACK_STRING, gAutoApplySingleModpack, DEFAULT_AUTO_APPLY_SINGLE_MODPACK) != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", AUTO_APPLY_SINGLE_MODPACK_STRING); - } - if (WUPSStorageAPI::GetOrStoreDefault(SDCAFIINE_ENABLED_STRING, gSDCafiineEnabled, DEFAULT_SDCAFIINE_ENABLED) != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", SDCAFIINE_ENABLED_STRING); - } - if (WUPSStorageAPI::GetOrStoreDefault(SKIP_PREPARE_FOR_SINGLE_MODPACK_STRING, gSkipPrepareIfSingleModpack, DEFAULT_SKIP_PREPARE_IF_SINGLE_MODPACK) != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", gSkipPrepareIfSingleModpack); - } + InitStorageAndConfig(); - if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to save storage"); - } - - WUPSConfigAPIOptionsV1 configOptions = {.name = "SDCafiine"}; - if (WUPSConfigAPI_Init(configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback) != WUPSCONFIG_API_RESULT_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to init config api"); - } - - sContentLayerHandle = 0; - sAocLayerHandle = 0; + gContentLayerHandle = 0; + gAocLayerHandle = 0; } /* Entry point */ @@ -71,79 +39,14 @@ ON_APPLICATION_START() { } } -void autoApplySingleModpackChanged(ConfigItemBoolean *item, bool newValue) { - DEBUG_FUNCTION_LINE("New value in gAutoApplySingleModpack: %d", newValue); - gAutoApplySingleModpack = newValue; - // If the value has changed, we store it in the storage. - if (WUPSStorageAPI::Store(AUTO_APPLY_SINGLE_MODPACK_STRING, gAutoApplySingleModpack) != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); - } -} - -void skipPrepareIfSingleModpackChanged(ConfigItemBoolean *item, bool newValue) { - DEBUG_FUNCTION_LINE("New value in gSkipPrepareIfSingleModpack: %d", newValue); - gSkipPrepareIfSingleModpack = newValue; - // If the value has changed, we store it in the storage. - if (WUPSStorageAPI::Store(SKIP_PREPARE_FOR_SINGLE_MODPACK_STRING, gSkipPrepareIfSingleModpack) != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); - } -} - -void sdCafiineEnabledChanged(ConfigItemBoolean *item, bool newValue) { - DEBUG_FUNCTION_LINE("New value in gSDCafiineEnabled: %d", newValue); - gSDCafiineEnabled = newValue; - // If the value has changed, we store it in the storage. - if (WUPSStorageAPI::Store(SDCAFIINE_ENABLED_STRING, gSDCafiineEnabled) != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); - } -} - -WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle) { - try { - WUPSConfigCategory root = WUPSConfigCategory(rootHandle); - - - root.add(WUPSConfigItemBoolean::Create(SDCAFIINE_ENABLED_STRING, - "Enable SDCafiine (game needs to be restarted)", - DEFAULT_SDCAFIINE_ENABLED, gSDCafiineEnabled, - &sdCafiineEnabledChanged)); - - - auto advancedSettings = WUPSConfigCategory::Create("Advanced settings"); - - advancedSettings.add(WUPSConfigItemBoolean::Create(AUTO_APPLY_SINGLE_MODPACK_STRING, - "Auto apply the modpack if only one modpack exists", - DEFAULT_AUTO_APPLY_SINGLE_MODPACK, gAutoApplySingleModpack, - &autoApplySingleModpackChanged)); - - advancedSettings.add(WUPSConfigItemBoolean::Create(SKIP_PREPARE_FOR_SINGLE_MODPACK_STRING, - "Skip \"Preparing modpack...\" screen", - DEFAULT_SKIP_PREPARE_IF_SINGLE_MODPACK, gSkipPrepareIfSingleModpack, - &skipPrepareIfSingleModpackChanged)); - root.add(std::move(advancedSettings)); - - } catch (std::exception &e) { - OSReport("Exception T_T : %s\n", e.what()); - return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; - } - return WUPSCONFIG_API_CALLBACK_RESULT_SUCCESS; -} - -void ConfigMenuClosedCallback() { - // Save all changes - if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); - } -} - ON_APPLICATION_ENDS() { - if (sContentLayerHandle != 0) { - ContentRedirection_RemoveFSLayer(sContentLayerHandle); - sContentLayerHandle = 0; + if (gContentLayerHandle != 0) { + ContentRedirection_RemoveFSLayer(gContentLayerHandle); + gContentLayerHandle = 0; } - if (sAocLayerHandle != 0) { - ContentRedirection_RemoveFSLayer(sAocLayerHandle); - sAocLayerHandle = 0; + if (gAocLayerHandle != 0) { + ContentRedirection_RemoveFSLayer(gAocLayerHandle); + gAocLayerHandle = 0; } deinitLogging(); } \ No newline at end of file diff --git a/src/modpackSelector.cpp b/src/modpackSelector.cpp index b6a2f03..b411b08 100644 --- a/src/modpackSelector.cpp +++ b/src/modpackSelector.cpp @@ -1,5 +1,5 @@ #include "modpackSelector.h" -#include "main.h" +#include "globals.h" #include "utils/input.h" #include "version.h" #include @@ -111,7 +111,7 @@ void HandleMultiModPacks(uint64_t titleID) { DIR *dir = opendir(modTitleIDPathOld.c_str()); if (dir) { if (!ScreenInit()) { - OSFatal("Please migrate sd:/sdcafiine to sd:/wiiu/sdcafiine."); + OSFatal("SDCafiine plugin: Please migrate sd:/sdcafiine to sd:/wiiu/sdcafiine."); } OSScreenClearBufferEx(SCREEN_TV, 0); OSScreenClearBufferEx(SCREEN_DRC, 0); @@ -252,8 +252,12 @@ void HandleMultiModPacks(uint64_t titleID) { selected += per_page; initScreen = 1; } - if (selected < 0) { selected = 0; } - if (selected >= modTitlePath.size()) { selected = modTitlePath.size() - 1; } + if (selected < 0) { + selected = 0; + } + if (selected >= (int) modTitlePath.size()) { + selected = modTitlePath.size() - 1; + } page = selected / per_page; if (initScreen) { @@ -308,20 +312,18 @@ void HandleMultiModPacks(uint64_t titleID) { KPADShutdown(); } -extern CRLayerHandle sContentLayerHandle; -extern CRLayerHandle sAocLayerHandle; bool ReplaceContentInternal(const std::string &basePath, const std::string &subdir, CRLayerHandle *layerHandle); bool ReplaceContent(const std::string &basePath, const std::string &modpack) { - bool contentRes = ReplaceContentInternal(basePath, "content", &sContentLayerHandle); - bool aocRes = ReplaceContentInternal(basePath, "aoc", &sAocLayerHandle); + bool contentRes = ReplaceContentInternal(basePath, "content", &gContentLayerHandle); + bool aocRes = ReplaceContentInternal(basePath, "aoc", &gAocLayerHandle); if (!contentRes && !aocRes) { auto screenWasAllocated = screenBuffer_0 != nullptr; if (!ScreenInit()) { - OSFatal("Failed to apply the modpack."); + OSFatal("SDCafiine plugin: Failed to apply the modpack."); } uint32_t sleepTime = 3000; DEBUG_FUNCTION_LINE_ERR("Failed to apply the modpack. Starting without mods."); @@ -394,7 +396,6 @@ bool ReplaceContentInternal(const std::string &basePath, const std::string &subd DEBUG_FUNCTION_LINE("Redirect /vol/%s to %s", subdir.c_str(), fullPath.c_str()); } else { DEBUG_FUNCTION_LINE_ERR("Failed to redirect /vol/%s to %s", subdir.c_str(), fullPath.c_str()); - return false; } return true; diff --git a/src/utils/config.cpp b/src/utils/config.cpp new file mode 100644 index 0000000..898e149 --- /dev/null +++ b/src/utils/config.cpp @@ -0,0 +1,92 @@ +#include "config.h" +#include "globals.h" +#include "logger.h" +#include +#include +#include +#include + +static void bool_item_callback(ConfigItemBoolean *item, bool newValue) { + if (!item || !item->identifier) { + DEBUG_FUNCTION_LINE_WARN("Invalid item or identifier in bool item callback"); + return; + } + DEBUG_FUNCTION_LINE_VERBOSE("New value in %s changed: %d", item->identifier, newValue); + if (std::string_view(AUTO_APPLY_SINGLE_MODPACK_STRING) == item->identifier) { + gAutoApplySingleModpack = newValue; + } else if (std::string_view(SKIP_PREPARE_FOR_SINGLE_MODPACK_STRING) == item->identifier) { + gSkipPrepareIfSingleModpack = newValue; + } else if (std::string_view(SDCAFIINE_ENABLED_STRING) == item->identifier) { + gSDCafiineEnabled = newValue; + } else { + DEBUG_FUNCTION_LINE_WARN("Unexpected boolean item: %s", item->identifier); + return; + } + WUPSStorageError err; + if ((err = WUPSStorageAPI::Store(item->identifier, newValue)) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_WARN("Failed to store value %d to storage item \"%s\": %s (%d)", newValue, item->identifier, WUPSStorageAPI_GetStatusStr(err), err); + } +} + +static WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle) { + try { + WUPSConfigCategory root = WUPSConfigCategory(rootHandle); + + + root.add(WUPSConfigItemBoolean::Create(SDCAFIINE_ENABLED_STRING, + "Enable SDCafiine (game needs to be restarted)", + DEFAULT_SDCAFIINE_ENABLED, gSDCafiineEnabled, + &bool_item_callback)); + + + auto advancedSettings = WUPSConfigCategory::Create("Advanced settings"); + + advancedSettings.add(WUPSConfigItemBoolean::Create(AUTO_APPLY_SINGLE_MODPACK_STRING, + "Auto apply the modpack if only one modpack exists", + DEFAULT_AUTO_APPLY_SINGLE_MODPACK, gAutoApplySingleModpack, + &bool_item_callback)); + + advancedSettings.add(WUPSConfigItemBoolean::Create(SKIP_PREPARE_FOR_SINGLE_MODPACK_STRING, + "Skip \"Preparing modpack...\" screen", + DEFAULT_SKIP_PREPARE_IF_SINGLE_MODPACK, gSkipPrepareIfSingleModpack, + &bool_item_callback)); + root.add(std::move(advancedSettings)); + + } catch (std::exception &e) { + DEBUG_FUNCTION_LINE_ERR("Exception: %s\n", e.what()); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + return WUPSCONFIG_API_CALLBACK_RESULT_SUCCESS; +} + +static void ConfigMenuClosedCallback() { + // Save all changes + WUPSStorageError err; + if ((err = WUPSStorageAPI::SaveStorage()) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to close storage: %s (%d)", WUPSStorageAPI_GetStatusStr(err), err); + } +} + + +void InitStorageAndConfig() { + WUPSStorageError err; + if ((err = WUPSStorageAPI::GetOrStoreDefault(AUTO_APPLY_SINGLE_MODPACK_STRING, gAutoApplySingleModpack, DEFAULT_AUTO_APPLY_SINGLE_MODPACK)) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\": %s (%d)", AUTO_APPLY_SINGLE_MODPACK_STRING, WUPSStorageAPI_GetStatusStr(err), err); + } + if ((err = WUPSStorageAPI::GetOrStoreDefault(SDCAFIINE_ENABLED_STRING, gSDCafiineEnabled, DEFAULT_SDCAFIINE_ENABLED)) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\": %s (%d)", SDCAFIINE_ENABLED_STRING, WUPSStorageAPI_GetStatusStr(err), err); + } + if ((err = WUPSStorageAPI::GetOrStoreDefault(SKIP_PREPARE_FOR_SINGLE_MODPACK_STRING, gSkipPrepareIfSingleModpack, DEFAULT_SKIP_PREPARE_IF_SINGLE_MODPACK)) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\": %s (%d)", gSkipPrepareIfSingleModpack, WUPSStorageAPI_GetStatusStr(err), err); + } + + if ((err = WUPSStorageAPI::SaveStorage()) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to save storage: %s (%d)", WUPSStorageAPI_GetStatusStr(err), err); + } + + WUPSConfigAPIOptionsV1 configOptions = {.name = "SDCafiine"}; + WUPSConfigAPIStatus configErr; + if ((configErr = WUPSConfigAPI_Init(configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback)) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to init config api: %s (%d)", WUPSConfigAPI_GetStatusStr(configErr), configErr); + } +} diff --git a/src/utils/config.h b/src/utils/config.h new file mode 100644 index 0000000..1794bef --- /dev/null +++ b/src/utils/config.h @@ -0,0 +1,3 @@ +#pragma once + +void InitStorageAndConfig(); \ No newline at end of file