From 6dde8c7a664ed6a41da442e76bb9b1e4c6806814 Mon Sep 17 00:00:00 2001 From: FreezePhoenix Date: Tue, 2 Apr 2024 16:49:27 -0400 Subject: [PATCH] Fix for forms --- CMakeLists.txt | 8 ++++---- CODE/BScorpion/CMakeLists.txt | 4 ++-- include/albot/HttpWrapper.hpp | 3 ++- src/HttpWrapper.cpp | 23 ++++++++++++++--------- src/MapProcessing/MapProcessing.cpp | 2 +- src/SocketWrapper.cpp | 5 +++-- src/albot-cpp.cpp | 17 +++++++++-------- 7 files changed, 35 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2543a38..c543984 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,11 +16,11 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") set(USE_TLS TRUE) set(USE_OPEN_SSL TRUE) -set(SPDLOG_USE_STD_FORMAT ON) +set(SPDLOG_USE_STD_FORMAT OFF) add_subdirectory(lib/libuv EXCLUDE_FROM_ALL) -add_subdirectory(lib/spdlog lib/spdlog EXCLUDE_FROM_ALL) +# add_subdirectory(lib/spdlog lib/spdlog EXCLUDE_FROM_ALL) add_subdirectory(lib/IXWebSocket lib/IXWebSocket EXCLUDE_FROM_ALL) -add_library(spdlog_HEADERS ALIAS spdlog_header_only) +# add_library(spdlog_HEADERS ALIAS spdlog_header_only) add_subdirectory(lib/TriangleManipulator lib/TriangleManipulator EXCLUDE_FROM_ALL) @@ -73,7 +73,7 @@ target_link_libraries( target_link_libraries( Bot PUBLIC - spdlog_header_only + spdlog ixwebsocket uv ) diff --git a/CODE/BScorpion/CMakeLists.txt b/CODE/BScorpion/CMakeLists.txt index 4d71c88..7c922c7 100644 --- a/CODE/BScorpion/CMakeLists.txt +++ b/CODE/BScorpion/CMakeLists.txt @@ -13,7 +13,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") set(USE_TLS TRUE) set(USE_OPEN_SSL TRUE) -set(SPDLOG_USE_STD_FORMAT ON) +set(SPDLOG_USE_STD_FORMAT OFF) add_subdirectory(../../lib/TriangleManipulator ./build/TriangleManipulator EXCLUDE_FROM_ALL) add_subdirectory(../../lib/spdlog ./build/spdlog EXCLUDE_FROM_ALL) @@ -22,7 +22,7 @@ if(NOT DEFINED CHARACTER_NAME_STRING) set(CHARACTER_NAME_STRING "DEFAULT") endif() -add_library(spdlog_HEADERS ALIAS spdlog_header_only) +# add_library(spdlog_HEADERS ALIAS spdlog_header_only) get_filename_component(prjName "${CMAKE_SOURCE_DIR}" NAME_WLE) diff --git a/include/albot/HttpWrapper.hpp b/include/albot/HttpWrapper.hpp index 62779b3..2152152 100644 --- a/include/albot/HttpWrapper.hpp +++ b/include/albot/HttpWrapper.hpp @@ -4,6 +4,7 @@ #define ALBOT_HTTPWRAPPER_HPP_ #include +#include #include @@ -88,7 +89,7 @@ class HttpWrapper { * @return true * @return false */ - bool static do_post(const std::string& url, const std::string& args, std::optional> out = std::nullopt, std::optional>> cookies = std::nullopt); + bool static do_post(const std::string& url, const std::string& args, const std::string& method, std::optional> out = std::nullopt, std::optional>> cookies = std::nullopt); /** * @brief Send a GET request. diff --git a/src/HttpWrapper.cpp b/src/HttpWrapper.cpp index 26d499c..4dbee8d 100644 --- a/src/HttpWrapper.cpp +++ b/src/HttpWrapper.cpp @@ -158,7 +158,7 @@ bool HttpWrapper::get_config(nlohmann::json& config) { return false; } } -bool HttpWrapper::do_post(const std::string& url, const std::string& args, std::optional> out, std::optional>> cookies) { +bool HttpWrapper::do_post(const std::string& url, const std::string& args, const std::string& method, std::optional> out, std::optional>> cookies) { Poco::URI uri(url); std::string path(uri.getPathAndQuery()); if (path.empty()) { @@ -174,7 +174,11 @@ bool HttpWrapper::do_post(const std::string& url, const std::string& args, std:: if (!session_cookie.empty()) { request.setCookies(HttpWrapper::cookie); } - session.sendRequest(request) << args; + Poco::Net::HTMLForm form; + form.add("method", method); + form.add("arguments", args); + form.prepareSubmit(request); + form.write(session.sendRequest(request)); std::istream &rs = session.receiveResponse(response); @@ -249,12 +253,13 @@ bool HttpWrapper::login() { // Attempt to connect to the server. Since we don't need to copy the output, // We pass a nullptr for the output. TODO: Get support for HTTP HEADERS verb. if (HttpWrapper::get_game_version(HttpWrapper::online_version)) { - mLogger->info("Successfully connected to server!"); - std::string args = std::format("{{\"email\":\"{}\",\"password\":\"{}\",\"only_login\":true}}", email, password); + mLogger->info("Successfully connected to server! {}", HttpWrapper::online_version); + std::string args = fmt::format("{{\"email\":\"{}\",\"password\":\"{}\",\"only_login\":true}}", email, password); std::vector cookies; // Again, we don't *really* care about the output the server sends us... // We just want the cookies. - if (HttpWrapper::api_method("signup_or_login", args, std::nullopt, cookies)) { + std::string out; + if (HttpWrapper::api_method("signup_or_login", args, out, cookies)) { for (size_t i = 0; i < cookies.size(); i++) { Poco::Net::HTTPCookie _cookie = cookies[i]; if (_cookie.getName() == "auth") { @@ -352,7 +357,7 @@ bool HttpWrapper::process_characters(const nlohmann::json& char_jsons) { for (size_t i = 0; i < char_jsons.size(); i++) { Character& character = HttpWrapper::characters.emplace_back(char_jsons[i]); HttpWrapper::NAME_TO_NUMBER.emplace(character.name, i); - std::string macro_section = std::format("{}={}", character.name, i); + std::string macro_section = fmt::format("{}={}", character.name, i); std::transform(character.name.begin(), character.name.end(), macro_section.begin(), ::toupper); HttpWrapper::NAME_MACROS += macro_section; if (i < char_jsons.size() - 1) { @@ -426,10 +431,10 @@ void from_json(const nlohmann::json& server_json, Server& server) { server.region = server_json.at("region").get(); server.port = server_json.at("port").get(); server.ip = server_json.at("addr").get(); - server.url = std::format("{}:{}", server.ip, server.port); - server.fullName = std::format("{} {}", server.region, server.identifier); + server.url = fmt::format("{}:{}", server.ip, server.port); + server.fullName = fmt::format("{} {}", server.region, server.identifier); } bool HttpWrapper::api_method(const std::string& method, const std::string& args, std::optional> out, std::optional>> cookies) { - return HttpWrapper::do_post(std::format("https://adventure.land/api/{}", method), std::format("arguments={}&method={}", args, method), out, cookies); + return HttpWrapper::do_post(fmt::format("https://adventure.land/api/{}", method), args, method, out, cookies); } \ No newline at end of file diff --git a/src/MapProcessing/MapProcessing.cpp b/src/MapProcessing/MapProcessing.cpp index c21a0f7..e5d9550 100644 --- a/src/MapProcessing/MapProcessing.cpp +++ b/src/MapProcessing/MapProcessing.cpp @@ -4,7 +4,7 @@ namespace MapProcessing { std::shared_ptr parse_map(const nlohmann::json& json) { // Create a new info smart pointer. - std::shared_ptr info = std::shared_ptr(new MapInfo()); + std::shared_ptr info = std::make_shared(); auto& info_x_lines = info->x_lines; auto& info_y_lines = info->y_lines; diff --git a/src/SocketWrapper.cpp b/src/SocketWrapper.cpp index af840ed..cee55e1 100644 --- a/src/SocketWrapper.cpp +++ b/src/SocketWrapper.cpp @@ -5,6 +5,7 @@ #include #include +#include template inline T getOrElse(const nlohmann::json& n, K key, T defaultValue) { @@ -258,7 +259,7 @@ void SocketWrapper::login(const CharacterGameInfo& info) { void SocketWrapper::emit(const std::string& event, const nlohmann::json& json) { if (this->webSocket.getReadyState() == ix::ReadyState::Open) { - this->webSocket.send(std::format("42[\"{}\",{}]", event, json.dump())); + this->webSocket.send(fmt::format("42[\"{}\",{}]", event, json.dump())); } else { this->mLogger->error("{} attempting to call emit on a socket that hasn't opened yet.", this->characterId); } @@ -266,7 +267,7 @@ void SocketWrapper::emit(const std::string& event, const nlohmann::json& json) { void SocketWrapper::emitRawJsonString(std::string event, std::string json) { if (this->webSocket.getReadyState() == ix::ReadyState::Open) { - this->webSocket.send(std::format("42[\"{}\",{}]", event, json)); + this->webSocket.send(fmt::format("42[\"{}\",{}]", event, json)); } else { this->mLogger->error("{} attempting to call emit on a socket that hasn't opened yet.", this->characterId); } diff --git a/src/albot-cpp.cpp b/src/albot-cpp.cpp index 43acf57..5d54dce 100644 --- a/src/albot-cpp.cpp +++ b/src/albot-cpp.cpp @@ -1,6 +1,7 @@ #include "albot/albot-cpp.hpp" #include "albot/HttpWrapper.hpp" #include +#include std::shared_ptr mLogger = spdlog::stdout_color_mt("ALBotC++"); namespace ALBot { @@ -33,7 +34,7 @@ namespace ALBot { } void build_service_code(const std::string& name) { - std::string FOLDER = std::format("SERVICES/{}", name); + std::string FOLDER = fmt::format("SERVICES/{}", name); std::string build_type = "Release"; if (HttpWrapper::config->contains("debug")) { if (HttpWrapper::config->at("debug").get()) { @@ -42,9 +43,9 @@ namespace ALBot { } else { mLogger->warn("config file does not contain a debug flag, assuming false"); } - std::string CMAKE = std::format("cmake -DCMAKE_BUILD_TYPE={} -DSERVICE_NAME_STRING={} -S {}/. -B {}/.", build_type, name, FOLDER, FOLDER); - std::string MAKE = std::format("make --quiet -C {}/. -j8", FOLDER); - std::string CP = std::format("cp {}/lib{}.so SERVICES/{}.so {}", FOLDER, name, name, NULL_PIPE_OUT); + std::string CMAKE = fmt::format("cmake -DCMAKE_BUILD_TYPE={} -DSERVICE_NAME_STRING={} -S {}/. -B {}/.", build_type, name, FOLDER, FOLDER); + std::string MAKE = fmt::format("make --quiet -C {}/. -j8", FOLDER); + std::string CP = fmt::format("cp {}/lib{}.so SERVICES/{}.so {}", FOLDER, name, name, NULL_PIPE_OUT); mLogger->info("Running CMake on: SERVICES/{}", name); system(CMAKE.c_str()); mLogger->info("Finished. Compiling..."); @@ -85,15 +86,15 @@ namespace ALBot { } else { mLogger->warn("config file does not contain a debug flag, assuming false"); } - std::string CMAKE = std::format("cmake -DCMAKE_BUILD_TYPE={} -DNAME_DEFINITIONS=\"{}\" -DCHARACTER_NAME_STRING={} -DCHARACTER_NAME={} -DCHARACTER_CLASS={} -S {}/. -B {}/.", build_type, HttpWrapper::NAME_MACROS, char_names, char_name_ints, char_klasses, FOLDER, FOLDER); - std::string MAKE = std::format("make --quiet -C {}/. -j8", FOLDER); + std::string CMAKE = fmt::format("cmake -DCMAKE_BUILD_TYPE={} -DNAME_DEFINITIONS=\"{}\" -DCHARACTER_NAME_STRING={} -DCHARACTER_NAME={} -DCHARACTER_CLASS={} -S {}/. -B {}/.", build_type, HttpWrapper::NAME_MACROS, char_names, char_name_ints, char_klasses, FOLDER, FOLDER); + std::string MAKE = fmt::format("make --quiet -C {}/. -j8", FOLDER); mLogger->info("Running CMake on: CODE/{} with character names {}", name, pretty_names); system(CMAKE.c_str()); mLogger->info("Finished. Compiling..."); system(MAKE.c_str()); mLogger->info("Finished. Copying..."); for (const std::string& char_name : names) { - std::string CP = std::format("cp {}/lib{}_{}.so CODE/{}.so {}", FOLDER, name, char_name, char_name, NULL_PIPE_OUT); + std::string CP = fmt::format("cp {}/lib{}_{}.so CODE/{}.so {}", FOLDER, name, char_name, char_name, NULL_PIPE_OUT); system(CP.c_str()); } mLogger->info("Finished."); @@ -104,7 +105,7 @@ namespace ALBot { ServiceInfo& info = SERVICE_HANDLERS.emplace(std::piecewise_construct, std::forward_as_tuple(service.name), std::forward_as_tuple(HttpWrapper::data)).first->second; build_service_code(service.name); - std::string file = std::format("SERVICES/{}.so", service.name); + std::string file = fmt::format("SERVICES/{}.so", service.name); void* handle = dlopen(file.c_str(), RTLD_LAZY); if (!handle) { mLogger->error("Cannot open library: {}", dlerror());