fix(config): Don't use C++ exceptions

they almost certainly don't work in this environment, and just returning is better
This commit is contained in:
Ash Logan 2024-05-28 16:47:12 +10:00
parent 695a077ebd
commit 9a9bf0949d

View File

@ -91,14 +91,14 @@ constexpr config_strings get_config_strings(nn::swkbd::LanguageType language) {
case nn::swkbd::LanguageType::German:
return {
.plugin_name = "Inkay",
.network_category = "Netzwerkauswahl",
.connect_to_network_setting = "Verbinde zum Pretendo Network",
.other_category = "Andere Einstellungen",
.reset_wwp_setting = "Wara Wara Plaza zurücksetzen",
.press_a_action = "Drücke A",
.restart_to_apply_action = "Neustarten zum Anwenden",
.need_menu_action = "Nur vom Wii U-Menü aus",
.plugin_name = "Inkay",
.network_category = "Netzwerkauswahl",
.connect_to_network_setting = "Verbinde zum Pretendo Network",
.other_category = "Andere Einstellungen",
.reset_wwp_setting = "Wara Wara Plaza zurücksetzen",
.press_a_action = "Drücke A",
.restart_to_apply_action = "Neustarten zum Anwenden",
.need_menu_action = "Nur vom Wii U-Menü aus",
};
}
}
@ -158,64 +158,60 @@ static int32_t unregister_task_item_get_display_value(void *context, char *out_b
}
static WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle) {
uint64_t current_title_id = OSGetTitleID();
uint64_t current_title_id = OSGetTitleID();
uint64_t wiiu_menu_tid = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_WII_U_MENU);
Config::is_wiiu_menu = (current_title_id == wiiu_menu_tid);
// get translation strings
// get translation strings
strings = get_config_strings(get_system_language());
// create root config category
WUPSConfigCategory root = WUPSConfigCategory(rootHandle);
// create root config category
WUPSConfigCategory root = WUPSConfigCategory(rootHandle);
try {
auto patching_cat = WUPSConfigCategory::Create(strings.network_category);
auto patching_cat = WUPSConfigCategory::Create(strings.network_category);
// config id display name default current value changed callback
patching_cat.add(WUPSConfigItemBoolean::Create("connect_to_network", strings.connect_to_network_setting, true, Config::connect_to_network, &connect_to_network_changed));
root.add(std::move(patching_cat));
// config id display name default current value changed callback
patching_cat.add(WUPSConfigItemBoolean::Create("connect_to_network", strings.connect_to_network_setting, true, Config::connect_to_network, &connect_to_network_changed));
root.add(std::move(patching_cat));
auto other_cat = WUPSConfigCategory::Create(strings.other_category);
auto other_cat = WUPSConfigCategory::Create(strings.other_category);
WUPSConfigAPIItemCallbacksV2 unregisterTasksItemCallbacks = {
.getCurrentValueDisplay = unregister_task_item_get_display_value,
.getCurrentValueSelectedDisplay = unregister_task_item_get_display_value,
.onSelected = nullptr,
.restoreDefault = nullptr,
.isMovementAllowed = nullptr,
.onCloseCallback = nullptr,
.onInput = unregister_task_item_on_input_cb,
.onInputEx = nullptr,
.onDelete = nullptr
};
WUPSConfigAPIItemCallbacksV2 unregisterTasksItemCallbacks = {
.getCurrentValueDisplay = unregister_task_item_get_display_value,
.getCurrentValueSelectedDisplay = unregister_task_item_get_display_value,
.onSelected = nullptr,
.restoreDefault = nullptr,
.isMovementAllowed = nullptr,
.onCloseCallback = nullptr,
.onInput = unregister_task_item_on_input_cb,
.onInputEx = nullptr,
.onDelete = nullptr
};
WUPSConfigAPIItemOptionsV2 unregisterTasksItemOptions = {
.displayName = strings.reset_wwp_setting,
.context = nullptr,
.callbacks = unregisterTasksItemCallbacks,
};
WUPSConfigAPIItemOptionsV2 unregisterTasksItemOptions = {
.displayName = strings.reset_wwp_setting,
.context = nullptr,
.callbacks = unregisterTasksItemCallbacks,
};
WUPSConfigItemHandle unregisterTasksItem;
WUPSConfigAPIStatus err;
if ((err = WUPSConfigAPI_Item_Create(unregisterTasksItemOptions, &unregisterTasksItem)) != WUPSCONFIG_API_RESULT_SUCCESS) {
throw std::runtime_error(std::string("Failed to create config item: ").append(WUPSConfigAPI_GetStatusStr(err)));
}
if ((err = WUPSConfigAPI_Category_AddItem(other_cat.getHandle(), unregisterTasksItem)) != WUPSCONFIG_API_RESULT_SUCCESS) {
throw std::runtime_error(std::string("Failed to add config item: ").append(WUPSConfigAPI_GetStatusStr(err)));
}
root.add(std::move(other_cat));
}
catch (std::exception &e) {
DEBUG_FUNCTION_LINE("Creating config menu failed: %s", e.what());
WUPSConfigItemHandle unregisterTasksItem;
WUPSConfigAPIStatus err;
if ((err = WUPSConfigAPI_Item_Create(unregisterTasksItemOptions, &unregisterTasksItem)) != WUPSCONFIG_API_RESULT_SUCCESS) {
DEBUG_FUNCTION_LINE("Creating config menu failed: %s", WUPSConfigAPI_GetStatusStr(err));
return WUPSCONFIG_API_CALLBACK_RESULT_ERROR;
}
if ((err = WUPSConfigAPI_Category_AddItem(other_cat.getHandle(), unregisterTasksItem)) != WUPSCONFIG_API_RESULT_SUCCESS) {
DEBUG_FUNCTION_LINE("Creating config menu failed: %s", WUPSConfigAPI_GetStatusStr(err));
return WUPSCONFIG_API_CALLBACK_RESULT_ERROR;
}
root.add(std::move(other_cat));
return WUPSCONFIG_API_CALLBACK_RESULT_SUCCESS;
}
static void ConfigMenuClosedCallback() {
// Save all changes
// Save all changes
if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to save storage");
}
@ -229,35 +225,35 @@ static void ConfigMenuClosedCallback() {
}
void Config::Init() {
// Init the config api
// Init the config api
WUPSConfigAPIOptionsV1 configOptions = { .name = "Inkay" };
if (WUPSConfigAPI_Init(configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback) != WUPSCONFIG_API_RESULT_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to initialize WUPS Config API");
return;
}
DEBUG_FUNCTION_LINE("Failed to initialize WUPS Config API");
return;
}
WUPSStorageError storageRes;
// Try to get value from storage
if ((storageRes = WUPSStorageAPI::Get<bool>("connect_to_network", Config::connect_to_network)) == WUPS_STORAGE_ERROR_NOT_FOUND) {
DEBUG_FUNCTION_LINE("Connect to network value not found, attempting to migrate/create");
WUPSStorageError storageRes;
// Try to get value from storage
if ((storageRes = WUPSStorageAPI::Get<bool>("connect_to_network", Config::connect_to_network)) == WUPS_STORAGE_ERROR_NOT_FOUND) {
DEBUG_FUNCTION_LINE("Connect to network value not found, attempting to migrate/create");
bool skipPatches = false;
if (WUPSStorageAPI::Get<bool>("skipPatches", skipPatches) == WUPS_STORAGE_ERROR_SUCCESS) {
// Migrate old config value
Config::connect_to_network = !skipPatches;
WUPSStorageAPI::DeleteItem("skipPatches");
}
// Add the value to the storage if it's missing.
if (WUPSStorageAPI::Store<bool>("connect_to_network", connect_to_network) != WUPS_STORAGE_ERROR_SUCCESS) {
bool skipPatches = false;
if (WUPSStorageAPI::Get<bool>("skipPatches", skipPatches) == WUPS_STORAGE_ERROR_SUCCESS) {
// Migrate old config value
Config::connect_to_network = !skipPatches;
WUPSStorageAPI::DeleteItem("skipPatches");
}
// Add the value to the storage if it's missing.
if (WUPSStorageAPI::Store<bool>("connect_to_network", connect_to_network) != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to store bool");
}
}
else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to get bool %s (%d)", WUPSStorageAPI_GetStatusStr(storageRes), storageRes);
}
}
}
else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to get bool %s (%d)", WUPSStorageAPI_GetStatusStr(storageRes), storageRes);
}
// Save storage
// Save storage
if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to save storage");
}