From 07537b2934edbe3c25ec7e77625374e5f12a9c4d Mon Sep 17 00:00:00 2001 From: LA_DI_DA <11174151+0O0o0oOoO00@users.noreply.github.com> Date: Tue, 1 Apr 2025 12:57:44 +0800 Subject: [PATCH] add: add exception handling when processing requests --- blcrack/cracker/server.cpp | 566 +++++++++++++++++++++++++++---------- 1 file changed, 412 insertions(+), 154 deletions(-) diff --git a/blcrack/cracker/server.cpp b/blcrack/cracker/server.cpp index 72722c0f5..431f6bf51 100644 --- a/blcrack/cracker/server.cpp +++ b/blcrack/cracker/server.cpp @@ -10,168 +10,306 @@ CrackerServer::CrackerServer() { Post("/disable_all", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_all(); + try { + Cracker::Instance().disable_all(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable all failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/enable_global_ship_properties_crack", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().enable_global_ship_properties_crack(); + try { + Cracker::Instance().enable_global_ship_properties_crack(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable global ship properties crack failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_global_ship_properties_crack", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_global_ship_properties_crack(); + try { + Cracker::Instance().disable_global_ship_properties_crack(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable global ship properties crack failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/update_global_ship_properties", [](const httplib::Request& req, httplib::Response& res) { - Json::Reader reader; - Json::Value j; - reader.parse(req.body, j); - Cracker::ShipProperties properties = { - .armor = j["armor"].asDouble(), - .speed = j["speed"].asDouble(), - .antiaircraft = j["antiaircraft"].asDouble(), - .oxy_recovery_bench = j["oxy_recovery_bench"].asDouble(), - .torpedo = j["torpedo"].asDouble(), - .hit = j["hit"].asDouble(), - .sonarRange = j["sonarRange"].asDouble(), - .attack_duration = j["attack_duration"].asDouble(), - .raid_distance = j["raid_distance"].asDouble(), - .oxy_recovery_surface = j["oxy_recovery_surface"].asDouble(), - .oxy_recovery = j["oxy_recovery"].asDouble(), - .dodge = j["dodge"].asDouble(), - .luck = j["luck"].asDouble(), - .reload = j["reload"].asDouble(), - .oxy_cost = j["oxy_cost"].asDouble(), - .durability = j["durability"].asDouble(), - .air = j["air"].asDouble(), - .oxy_max = j["oxy_max"].asDouble(), - .cannon = j["cannon"].asDouble(), - .antisub = j["antisub"].asDouble(), - }; - Cracker::Instance().update_global_ship_properties(properties); + try { + Json::Reader reader; + Json::Value j; + reader.parse(req.body, j); + Cracker::ShipProperties properties = { + .armor = j["armor"].asDouble(), + .speed = j["speed"].asDouble(), + .antiaircraft = j["antiaircraft"].asDouble(), + .oxy_recovery_bench = j["oxy_recovery_bench"].asDouble(), + .torpedo = j["torpedo"].asDouble(), + .hit = j["hit"].asDouble(), + .sonarRange = j["sonarRange"].asDouble(), + .attack_duration = j["attack_duration"].asDouble(), + .raid_distance = j["raid_distance"].asDouble(), + .oxy_recovery_surface = j["oxy_recovery_surface"].asDouble(), + .oxy_recovery = j["oxy_recovery"].asDouble(), + .dodge = j["dodge"].asDouble(), + .luck = j["luck"].asDouble(), + .reload = j["reload"].asDouble(), + .oxy_cost = j["oxy_cost"].asDouble(), + .durability = j["durability"].asDouble(), + .air = j["air"].asDouble(), + .oxy_max = j["oxy_max"].asDouble(), + .cannon = j["cannon"].asDouble(), + .antisub = j["antisub"].asDouble(), + }; + Cracker::Instance().update_global_ship_properties(properties); + } catch (std::exception& e) { + SPDLOG_ERROR("Update global ship properties failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/enable_fast_stage_move", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().enable_fast_stage_move(); + try { + Cracker::Instance().enable_fast_stage_move(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable fast stage move failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_fast_stage_move", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_fast_stage_move(); + try { + Cracker::Instance().disable_fast_stage_move(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable fast stage move failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/enable_remove_hard_mode_ship_properties_limit", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().enable_remove_hard_mode_ship_properties_limit(); + try { + Cracker::Instance().enable_remove_hard_mode_ship_properties_limit(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable remove hard mode ship properties limit failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_remove_hard_mode_ship_properties_limit", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_remove_hard_mode_ship_properties_limit(); + try { + Cracker::Instance().disable_remove_hard_mode_ship_properties_limit(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable remove hard mode ship properties limit failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/enable_remove_hard_mode_ship_type_limit", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().enable_remove_hard_mode_ship_type_limit(); + try { + Cracker::Instance().enable_remove_hard_mode_ship_type_limit(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable remove hard mode ship type limit failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_remove_hard_mode_ship_type_limit", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_remove_hard_mode_ship_type_limit(); + try { + Cracker::Instance().disable_remove_hard_mode_ship_type_limit(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable remove hard mode ship type limit failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/enable_remove_hard_mode_limit", [](const httplib::Request& req, httplib::Response& res) { - auto& cracker = Cracker::Instance(); - cracker.enable_remove_hard_mode_ship_type_limit(); - cracker.enable_remove_hard_mode_ship_properties_limit(); + try { + auto& cracker = Cracker::Instance(); + cracker.enable_remove_hard_mode_ship_type_limit(); + cracker.enable_remove_hard_mode_ship_properties_limit(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable remove hard mode limit failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_remove_hard_mode_limit", [](const httplib::Request& req, httplib::Response& res) { - auto& cracker = Cracker::Instance(); - cracker.disable_remove_hard_mode_ship_type_limit(); - cracker.disable_remove_hard_mode_ship_properties_limit(); + try { + auto& cracker = Cracker::Instance(); + cracker.disable_remove_hard_mode_ship_type_limit(); + cracker.disable_remove_hard_mode_ship_properties_limit(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable remove hard mode limit failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/enable_no_bb_animation", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().enable_no_bb_animation(); + try { + Cracker::Instance().enable_no_bb_animation(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable no bb animation failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_no_bb_animation", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_no_bb_animation(); + try { + Cracker::Instance().disable_no_bb_animation(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable no bb animation failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/print_table_field", [](const httplib::Request& req, httplib::Response& res) { - Json::Reader reader; - Json::Value j; - reader.parse(req.body, j); - std::vector path; - for (auto& key : j["path"]) { - path.push_back(key.asString()); + try { + Json::Reader reader; + Json::Value j; + reader.parse(req.body, j); + std::vector path; + for (auto& key : j["path"]) { + path.push_back(key.asString()); + } + Cracker::Instance().print_table_field(path); + } catch (std::exception& e) { + SPDLOG_ERROR("Print table field failed: {}", e.what()); + res.status = 500; + return; } - Cracker::Instance().print_table_field(path); res.status = 200; }); Post("/print_value", [](const httplib::Request& req, httplib::Response& res) { - Json::Reader reader; - Json::Value j; - reader.parse(req.body, j); - std::vector path; - for (auto& key : j["path"]) { - path.push_back(key.asString()); + try { + Json::Reader reader; + Json::Value j; + reader.parse(req.body, j); + std::vector path; + for (auto& key : j["path"]) { + path.push_back(key.asString()); + } + Cracker::Instance().print_value(path); + } catch (std::exception& e) { + SPDLOG_ERROR("Print value failed: {}", e.what()); + res.status = 500; + return; } - Cracker::Instance().print_value(path); res.status = 200; }); Post("/enable_fake_player", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().enable_fake_player(); + try { + Cracker::Instance().enable_fake_player(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable fake player failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_fake_player", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_fake_player(); + try { + Cracker::Instance().disable_fake_player(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable fake player failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/update_fake_player_info", [](const httplib::Request& req, httplib::Response& res) { - Json::Reader reader; - Json::Value j; - reader.parse(req.body, j); - Cracker::FakePlayerInfo info = { - .name = j["name"].asString(), - .id = j["id"].asString(), - .level = j["level"].asString(), - }; - Cracker::Instance().update_fake_player_info(info); + try { + Json::Reader reader; + Json::Value j; + reader.parse(req.body, j); + Cracker::FakePlayerInfo info = { + .name = j["name"].asString(), + .id = j["id"].asString(), + .level = j["level"].asString(), + }; + Cracker::Instance().update_fake_player_info(info); + } catch (std::exception& e) { + SPDLOG_ERROR("Update fake player info failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/enable_no_emotion_warning", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().enable_no_emotion_warning(); + try { + Cracker::Instance().enable_no_emotion_warning(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable no emotion warning failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_no_emotion_warning", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_no_emotion_warning(); + try { + Cracker::Instance().disable_no_emotion_warning(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable no emotion warning failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/enable_opsi_fast_move", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().enable_opsi_fast_move(); + try { + Cracker::Instance().enable_opsi_fast_move(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable opsi fast move failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_opsi_fast_move", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_opsi_fast_move(); + try { + Cracker::Instance().disable_opsi_fast_move(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable opsi fast move failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); @@ -180,168 +318,288 @@ CrackerServer::CrackerServer() { }); Get("/get_coin", [](const httplib::Request& req, httplib::Response& res) { - auto coin = Cracker::Instance().get_coin(); - res.body = std::to_string(coin); + try { + auto coin = Cracker::Instance().get_coin(); + res.body = std::to_string(coin); + } catch (std::exception& e) { + SPDLOG_ERROR("Get coin failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_oil", [](const httplib::Request& req, httplib::Response& res) { - auto oil = Cracker::Instance().get_oil(); - res.body = std::to_string(oil); + try { + auto oil = Cracker::Instance().get_oil(); + res.body = std::to_string(oil); + } catch (std::exception& e) { + SPDLOG_ERROR("Get oil failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_gems", [](const httplib::Request& req, httplib::Response& res) { - auto gems = Cracker::Instance().get_gems(); - res.body = std::to_string(gems); + try { + auto gems = Cracker::Instance().get_gems(); + res.body = std::to_string(gems); + } catch (std::exception& e) { + SPDLOG_ERROR("Get gems failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_level", [](const httplib::Request& req, httplib::Response& res) { - auto level = Cracker::Instance().get_level(); - res.body = std::to_string(level); + try { + auto level = Cracker::Instance().get_level(); + res.body = std::to_string(level); + } catch (std::exception& e) { + SPDLOG_ERROR("Get level failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_exp", [](const httplib::Request& req, httplib::Response& res) { - auto exp = Cracker::Instance().get_exp(); - res.body = std::to_string(exp); + try { + auto exp = Cracker::Instance().get_exp(); + res.body = std::to_string(exp); + } catch (std::exception& e) { + SPDLOG_ERROR("Get exp failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_merit", [](const httplib::Request& req, httplib::Response& res) { - auto merit = Cracker::Instance().get_merit(); - res.body = std::to_string(merit); + try { + auto merit = Cracker::Instance().get_merit(); + res.body = std::to_string(merit); + } catch (std::exception& e) { + SPDLOG_ERROR("Get merit failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_guild_coin", [](const httplib::Request& req, httplib::Response& res) { - auto guild_coin = Cracker::Instance().get_guild_coin(); - res.body = std::to_string(guild_coin); + try { + auto guild_coin = Cracker::Instance().get_guild_coin(); + res.body = std::to_string(guild_coin); + } catch (std::exception& e) { + SPDLOG_ERROR("Get guild coin failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_design_prt", [](const httplib::Request& req, httplib::Response& res) { - auto design_prt = Cracker::Instance().get_design_prt(); - res.body = std::to_string(design_prt); + try { + auto design_prt = Cracker::Instance().get_design_prt(); + res.body = std::to_string(design_prt); + } catch (std::exception& e) { + SPDLOG_ERROR("Get design prt failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_core_data", [](const httplib::Request& req, httplib::Response& res) { - auto core_data = Cracker::Instance().get_core_data(); - res.body = std::to_string(core_data); + try { + auto core_data = Cracker::Instance().get_core_data(); + res.body = std::to_string(core_data); + } catch (std::exception& e) { + SPDLOG_ERROR("Get core data failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_medal", [](const httplib::Request& req, httplib::Response& res) { - auto medal = Cracker::Instance().get_medal(); - res.body = std::to_string(medal); + try { + auto medal = Cracker::Instance().get_medal(); + res.body = std::to_string(medal); + } catch (std::exception& e) { + SPDLOG_ERROR("Get medal failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_pt", [](const httplib::Request& req, httplib::Response& res) { - auto pt = Cracker::Instance().get_pt(); - res.body = std::to_string(pt); + try { + auto pt = Cracker::Instance().get_pt(); + res.body = std::to_string(pt); + } catch (std::exception& e) { + SPDLOG_ERROR("Get pt failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_specialized_core", [](const httplib::Request& req, httplib::Response& res) { - auto specialized_core = Cracker::Instance().get_specialized_core(); - res.body = std::to_string(specialized_core); + try { + auto specialized_core = Cracker::Instance().get_specialized_core(); + res.body = std::to_string(specialized_core); + } catch (std::exception& e) { + SPDLOG_ERROR("Get specialized core failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/get_curr_action_point", [](const httplib::Request& req, httplib::Response& res) { - auto curr_action_point = Cracker::Instance().get_curr_action_point(); - res.body = std::to_string(curr_action_point); + try { + auto curr_action_point = Cracker::Instance().get_curr_action_point(); + res.body = std::to_string(curr_action_point); + } catch (std::exception& e) { + SPDLOG_ERROR("Get curr action point failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Get("/scan_dock", [](const httplib::Request& req, httplib::Response& res) { - auto dock = Cracker::Instance().scan_dock(); - Json::Value j; - for (auto& ship : dock) { - Json::Value ship_info_j; - ship_info_j["config_id"] = ship.second.config_id; - ship_info_j["emotion"] = ship.second.emotion; - ship_info_j["rarity"] = ship.second.rarity; - ship_info_j["level"] = ship.second.level; - ship_info_j["exp"] = ship.second.exp; - ship_info_j["curr_star"] = ship.second.curr_star; - j[std::to_string(ship.first)] = ship_info_j; + try { + auto dock = Cracker::Instance().scan_dock(); + Json::Value j; + for (auto& ship : dock) { + Json::Value ship_info_j; + ship_info_j["config_id"] = ship.second.config_id; + ship_info_j["emotion"] = ship.second.emotion; + ship_info_j["rarity"] = ship.second.rarity; + ship_info_j["level"] = ship.second.level; + ship_info_j["exp"] = ship.second.exp; + ship_info_j["curr_star"] = ship.second.curr_star; + j[std::to_string(ship.first)] = ship_info_j; + } + Json::FastWriter writer; + res.body = writer.write(j); + } catch (std::exception& e) { + SPDLOG_ERROR("Scan dock failed: {}", e.what()); + res.status = 500; + return; } - Json::FastWriter writer; - res.body = writer.write(j); res.status = 200; }); Get("/scan_storage", [](const httplib::Request& req, httplib::Response& res) { - auto storage = Cracker::Instance().scan_storage(); - Json::Value j; - for (auto& item : storage) { - j[std::to_string(item.first)] = item.second; + try { + auto storage = Cracker::Instance().scan_storage(); + Json::Value j; + for (auto& item : storage) { + j[std::to_string(item.first)] = item.second; + } + Json::FastWriter writer; + res.body = writer.write(j); + } catch (std::exception& e) { + SPDLOG_ERROR("Scan storage failed: {}", e.what()); + res.status = 500; + return; } - Json::FastWriter writer; - res.body = writer.write(j); res.status = 200; }); Get("/get_ship_info", [](const httplib::Request& req, httplib::Response& res) { - auto id = req.get_param_value("id"); - if (id.empty()) { - res.status = 400; - return; - } - auto ship_info = Cracker::Instance().get_ship_info(std::stoi(id)); - if (ship_info.has_value()) { - Cracker::ShipInfo& info = ship_info.value(); - Json::Value j; - j["config_id"] = info.config_id; - j["emotion"] = info.emotion; - j["rarity"] = info.rarity; - j["level"] = info.level; - j["exp"] = info.exp; - j["curr_star"] = info.curr_star; - Json::FastWriter writer; - res.body = writer.write(j); - res.status = 200; - } else { - res.status = 400; + try { + auto id = req.get_param_value("id"); + if (id.empty()) { + res.status = 400; + return; + } + auto ship_info = Cracker::Instance().get_ship_info(std::stoi(id)); + if (ship_info.has_value()) { + Cracker::ShipInfo& info = ship_info.value(); + Json::Value j; + j["config_id"] = info.config_id; + j["emotion"] = info.emotion; + j["rarity"] = info.rarity; + j["level"] = info.level; + j["exp"] = info.exp; + j["curr_star"] = info.curr_star; + Json::FastWriter writer; + res.body = writer.write(j); + res.status = 200; + } else { + res.status = 400; + return; + } + } catch (std::exception& e) { + SPDLOG_ERROR("Get ship info failed: {}", e.what()); + res.status = 500; return; } }); Get("/get_storage_item_count", [](const httplib::Request& req, httplib::Response& res) { - auto item_id = req.get_param_value("item_id"); - if (item_id.empty()) { - res.status = 400; - return; - } - auto count = Cracker::Instance().get_storage_item_count(std::stoi(item_id)); - if (count.has_value()) { - res.body = std::to_string(count.value()); - res.status = 200; - } else { - res.status = 400; + try { + auto item_id = req.get_param_value("item_id"); + if (item_id.empty()) { + res.status = 400; + return; + } + auto count = Cracker::Instance().get_storage_item_count(std::stoi(item_id)); + if (count.has_value()) { + res.body = std::to_string(count.value()); + res.status = 200; + } else { + res.status = 400; + return; + } + } catch (std::exception& e) { + SPDLOG_ERROR("Get storage item count failed: {}", e.what()); + res.status = 500; return; } }); Post("/enable_gg_factor", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().enable_gg_factor(); + try { + Cracker::Instance().enable_gg_factor(); + } catch (std::exception& e) { + SPDLOG_ERROR("Enable gg factor failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/disable_gg_factor", [](const httplib::Request& req, httplib::Response& res) { - Cracker::Instance().disable_gg_factor(); + try { + Cracker::Instance().disable_gg_factor(); + } catch (std::exception& e) { + SPDLOG_ERROR("Disable gg factor failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; }); Post("/update_gg_factor", [](const httplib::Request& req, httplib::Response& res) { - Json::Reader reader; - Json::Value j; - reader.parse(req.body, j); - Cracker::Instance().update_gg_factor(j["factor"].asDouble()); + try { + Json::Reader reader; + Json::Value j; + reader.parse(req.body, j); + Cracker::Instance().update_gg_factor(j["factor"].asDouble()); + } catch (std::exception& e) { + SPDLOG_ERROR("Update gg factor failed: {}", e.what()); + res.status = 500; + return; + } res.status = 200; });