sdcafiine_plugin/src/main.cpp

150 lines
6.3 KiB
C++
Raw Normal View History

2022-09-18 13:04:02 +02:00
#include "main.h"
2021-04-06 16:58:19 +02:00
#include "modpackSelector.h"
2022-02-04 15:05:17 +01:00
#include "utils/logger.h"
#include <content_redirection/redirection.h>
2021-04-06 16:58:19 +02:00
#include <coreinit/title.h>
2022-02-04 15:05:17 +01:00
#include <wups.h>
2022-09-18 13:04:02 +02:00
#include <wups/config/WUPSConfigItemBoolean.h>
2021-04-06 16:58:19 +02:00
WUPS_PLUGIN_NAME("SDCafiine");
WUPS_PLUGIN_DESCRIPTION("SDCafiine");
WUPS_PLUGIN_VERSION(VERSION_FULL);
2021-04-06 16:58:19 +02:00
WUPS_PLUGIN_AUTHOR("Maschell");
WUPS_PLUGIN_LICENSE("GPL");
2021-10-02 11:43:15 +02:00
WUPS_USE_WUT_DEVOPTAB();
2022-09-18 13:04:02 +02:00
WUPS_USE_STORAGE("sdcafiine"); // Unqiue id for the storage api
2021-04-06 16:58:19 +02:00
2023-12-27 20:06:45 +01:00
CRLayerHandle sContentLayerHandle = 0;
CRLayerHandle sAocLayerHandle = 0;
2022-09-18 13:04:02 +02:00
2023-12-27 20:06:45 +01:00
#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();
INITIALIZE_PLUGIN() {
// But then use libcontentredirection instead.
ContentRedirectionStatus error;
if ((error = ContentRedirection_InitLibrary()) != CONTENT_REDIRECTION_RESULT_SUCCESS) {
2022-09-18 13:04:02 +02:00
DEBUG_FUNCTION_LINE_ERR("Failed to init ContentRedirection. Error %s %d", ContentRedirection_GetStatusStr(error), error);
OSFatal("Failed to init ContentRedirection.");
}
2022-09-18 13:04:02 +02:00
2023-12-27 20:06:45 +01:00
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);
2022-09-18 13:04:02 +02:00
}
2023-12-27 20:06:45 +01:00
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);
}
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;
}
2021-04-06 16:58:19 +02:00
/* Entry point */
ON_APPLICATION_START() {
initLogging();
2022-09-18 13:04:02 +02:00
if (gSDCafiineEnabled) {
HandleMultiModPacks(OSGetTitleID());
} else {
DEBUG_FUNCTION_LINE("SDCafiine is disabled");
}
}
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.
2023-12-27 20:06:45 +01:00
if (WUPSStorageAPI::Store(AUTO_APPLY_SINGLE_MODPACK_STRING, gAutoApplySingleModpack) != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("Failed to close storage");
}
2022-09-18 13:04:02 +02:00
}
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.
2023-12-27 20:06:45 +01:00
if (WUPSStorageAPI::Store(SKIP_PREPARE_FOR_SINGLE_MODPACK_STRING, gSkipPrepareIfSingleModpack) != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("Failed to close storage");
}
2022-09-18 13:04:02 +02:00
}
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.
2023-12-27 20:06:45 +01:00
if (WUPSStorageAPI::Store(SDCAFIINE_ENABLED_STRING, gSDCafiineEnabled) != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("Failed to close storage");
}
2022-09-18 13:04:02 +02:00
}
2023-12-27 20:06:45 +01:00
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));
2022-09-18 13:04:02 +02:00
2023-12-27 20:06:45 +01:00
auto advancedSettings = WUPSConfigCategory::Create("Advanced settings");
2022-09-18 13:04:02 +02:00
2023-12-27 20:06:45 +01:00
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));
2022-09-18 13:04:02 +02:00
2023-12-27 20:06:45 +01:00
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;
2022-09-18 13:04:02 +02:00
}
2023-12-27 20:06:45 +01:00
void ConfigMenuClosedCallback() {
2022-09-18 13:04:02 +02:00
// Save all changes
2023-12-27 20:06:45 +01:00
if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
2022-09-18 13:04:02 +02:00
DEBUG_FUNCTION_LINE_ERR("Failed to close storage");
}
}
2022-02-04 15:05:17 +01:00
ON_APPLICATION_ENDS() {
2023-12-27 20:06:45 +01:00
if (sContentLayerHandle != 0) {
ContentRedirection_RemoveFSLayer(sContentLayerHandle);
sContentLayerHandle = 0;
}
2023-12-27 20:06:45 +01:00
if (sAocLayerHandle != 0) {
ContentRedirection_RemoveFSLayer(sAocLayerHandle);
sAocLayerHandle = 0;
2022-09-18 13:04:02 +02:00
}
deinitLogging();
2021-04-06 16:58:19 +02:00
}