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