From 37a99d2b3fcab2e32a26f8f5076f6fdb7d7f7710 Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 2 Dec 2022 10:55:53 +0800 Subject: [PATCH] chore: code analysis --- src/MaaCore/Common/AsstBattleDef.h | 10 +++++----- src/MaaCore/Controller.cpp | 28 +++++++++++++--------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/MaaCore/Common/AsstBattleDef.h b/src/MaaCore/Common/AsstBattleDef.h index 2f33dee2e9..613a5f3877 100644 --- a/src/MaaCore/Common/AsstBattleDef.h +++ b/src/MaaCore/Common/AsstBattleDef.h @@ -164,13 +164,13 @@ namespace asst struct ReplacementHome { Point location; - BattleDeployDirection direction; + BattleDeployDirection direction = BattleDeployDirection::Right; }; struct ForceDeployDirection { - BattleDeployDirection direction; - std::unordered_set role; + BattleDeployDirection direction = BattleDeployDirection::Right; + std::unordered_set role = {}; }; struct RoguelikeBattleData @@ -180,7 +180,7 @@ namespace asst std::unordered_set blacklist_location; std::unordered_map force_deploy_direction; std::vector key_kills; - std::array role_order; + std::array role_order = {}; bool use_dice_stage = true; int stop_deploy_blocking_num = INT_MAX; int force_deploy_air_defense_num = 0; @@ -206,7 +206,7 @@ namespace asst struct BattleCharData { std::string name; - BattleRole role; + BattleRole role = BattleRole::Unknown; std::array ranges; int rarity = 0; bool with_direction = true; diff --git a/src/MaaCore/Controller.cpp b/src/MaaCore/Controller.cpp index 9bb230a153..c30b9c2570 100644 --- a/src/MaaCore/Controller.cpp +++ b/src/MaaCore/Controller.cpp @@ -95,7 +95,7 @@ std::optional asst::Controller::call_command(const std::string& cmd std::string pipe_data; std::string sock_data; asst::platform::single_page_buffer pipe_buffer; - std::optional> sock_buffer; + asst::platform::single_page_buffer sock_buffer; auto start_time = steady_clock::now(); std::unique_lock callcmd_lock(m_callcmd_mutex); @@ -151,9 +151,9 @@ std::optional asst::Controller::call_command(const std::string& cmd sockov.hEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr); client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); DWORD dummy; - if (!m_server_accept_ex(m_server_sock, client_socket, sock_buffer.value().get(), - (DWORD)sock_buffer.value().size() - ((sizeof(sockaddr_in) + 16) * 2), - sizeof(sockaddr_in) + 16, sizeof(sockaddr_in) + 16, &dummy, &sockov)) { + if (!m_server_accept_ex(m_server_sock, client_socket, sock_buffer.get(), + (DWORD)sock_buffer.size() - ((sizeof(sockaddr_in) + 16) * 2), sizeof(sockaddr_in) + 16, + sizeof(sockaddr_in) + 16, &dummy, &sockov)) { err = WSAGetLastError(); if (err == ERROR_IO_PENDING) { accept_pending = true; @@ -242,8 +242,7 @@ std::optional asst::Controller::call_command(const std::string& cmd DWORD len = 0; if (GetOverlappedResult(reinterpret_cast(m_server_sock), &sockov, &len, FALSE)) { accept_pending = false; - if (recv_by_socket) - sock_data.insert(sock_data.end(), sock_buffer.value().get(), sock_buffer.value().get() + len); + if (recv_by_socket) sock_data.insert(sock_data.end(), sock_buffer.get(), sock_buffer.get() + len); if (len == 0) { socket_eof = true; @@ -255,8 +254,8 @@ std::optional asst::Controller::call_command(const std::string& cmd sockov = {}; sockov.hEvent = event; - (void)ReadFile(reinterpret_cast(client_socket), sock_buffer.value().get(), - (DWORD)sock_buffer.value().size(), nullptr, &sockov); + (void)ReadFile(reinterpret_cast(client_socket), sock_buffer.get(), + (DWORD)sock_buffer.size(), nullptr, &sockov); } } } @@ -264,15 +263,14 @@ std::optional asst::Controller::call_command(const std::string& cmd // ReadFile DWORD len = 0; if (GetOverlappedResult(reinterpret_cast(client_socket), &sockov, &len, FALSE)) { - if (recv_by_socket) - sock_data.insert(sock_data.end(), sock_buffer.value().get(), sock_buffer.value().get() + len); + if (recv_by_socket) sock_data.insert(sock_data.end(), sock_buffer.get(), sock_buffer.get() + len); if (len == 0) { socket_eof = true; ::closesocket(client_socket); } else { - (void)ReadFile(reinterpret_cast(client_socket), sock_buffer.value().get(), - (DWORD)sock_buffer.value().size(), nullptr, &sockov); + (void)ReadFile(reinterpret_cast(client_socket), sock_buffer.get(), + (DWORD)sock_buffer.size(), nullptr, &sockov); } } else { @@ -332,11 +330,11 @@ std::optional asst::Controller::call_command(const std::string& cmd return std::nullopt; } - ssize_t read_num = ::read(client_socket, sock_buffer.value().get(), sock_buffer.value().size()); + ssize_t read_num = ::read(client_socket, sock_buffer.get(), sock_buffer.size()); while (read_num > 0) { - sock_data.insert(sock_data.end(), sock_buffer.value().get(), sock_buffer.value().get() + read_num); - read_num = ::read(client_socket, sock_buffer.value().get(), sock_buffer.value().size()); + sock_data.insert(sock_data.end(), sock_buffer.get(), sock_buffer.get() + read_num); + read_num = ::read(client_socket, sock_buffer.get(), sock_buffer.size()); } ::close(client_socket);