From f3974bb86f294f7acf19bb96437a9c6d5c3846ff Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Sun, 31 Aug 2025 02:22:36 +0900 Subject: [PATCH] =?UTF-8?q?=EC=93=B8=EB=8D=B0=EC=97=86=EB=8A=94=20?= =?UTF-8?q?=EA=B3=B5=EC=9C=A0=20=EB=9D=BC=EC=9D=B4=EB=B8=8C=EB=9F=AC?= =?UTF-8?q?=EB=A6=AC=20=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20=EC=BD=98=EC=86=94?= =?UTF-8?q?=20=EC=8B=A4=ED=96=89=20=EC=BB=A4=EB=A7=A8=EB=93=9C=20=EC=95=88?= =?UTF-8?q?=EC=A0=95=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 29 +++----------- include/utils/console.h | 10 ++--- include/utils/update_checker.h | 2 +- src/main.cc | 69 ++++++++++++++++++++++------------ src/utils/console.cc | 44 +++++++++++----------- src/utils/file_downloader.cc | 13 ++++++- src/utils/update_checker.cc | 56 +++++++++++++++++---------- 7 files changed, 125 insertions(+), 98 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e1153c3..ca6c244 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,11 +14,11 @@ if(WIN32) set(OPENSSL_INCLUDE_DIR "${OPENSSL_ROOT_DIR}/include") if(CMAKE_BUILD_TYPE STREQUAL Debug) - set(OPENSSL_CRYPTO_LIBRARY "${OPENSSL_ROOT_DIR}/lib/VC/x64/MDd/libcrypto.lib") - set(OPENSSL_SSL_LIBRARY "${OPENSSL_ROOT_DIR}/lib/VC/x64/MDd/libssl.lib") + set(OPENSSL_CRYPTO_LIBRARY "${OPENSSL_ROOT_DIR}/lib/VC/x64/MTd/libcrypto.lib") + set(OPENSSL_SSL_LIBRARY "${OPENSSL_ROOT_DIR}/lib/VC/x64/MTd/libssl.lib") else() - set(OPENSSL_CRYPTO_LIBRARY "${OPENSSL_ROOT_DIR}/lib/VC/x64/MD/libcrypto.lib") - set(OPENSSL_SSL_LIBRARY "${OPENSSL_ROOT_DIR}/lib/VC/x64/MD/libssl.lib") + set(OPENSSL_CRYPTO_LIBRARY "${OPENSSL_ROOT_DIR}/lib/VC/x64/MT/libcrypto.lib") + set(OPENSSL_SSL_LIBRARY "${OPENSSL_ROOT_DIR}/lib/VC/x64/MT/libssl.lib") endif() endif() @@ -31,8 +31,8 @@ FetchContent_Declare( URL "https://github.com/boostorg/boost/releases/download/boost-1.89.0/boost-1.89.0-cmake.7z" DOWNLOAD_EXTRACT_TIMESTAMP ON CMAKE_ARGS - -DBOOST_USE_STATIC_LIBS=OFF # DLL 사용 - -DBOOST_USE_STATIC_RUNTIME=OFF # /MD, /MDd 사용 + -DBOOST_USE_STATIC_LIBS=ON + -DBOOST_USE_STATIC_RUNTIME=ON ) message(STATUS "Fetching and making available Boost...") FetchContent_MakeAvailable(Boost) @@ -52,13 +52,11 @@ FetchContent_MakeAvailable(dpp) # ------------------------------------------------------------- if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") set(FFMPEG_URL "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-lgpl-shared.zip") - set(FFMPEG_SHA256HASH "SHA256=c95a9d4e030f694f33c85a7611204383a0bca906514a08d83d3858496b122f76") set(FFMPEG_ARCHIVE_NAME "ffmpeg-windows") set(FFMPEG_LIB_DIR "lib") set(FFMPEG_INCLUDE_DIR "include") elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") set(FFMPEG_URL "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-lgpl-shared.tar.xz") - set(FFMPEG_SHA256HASH "SHA256=ebf6b197ffe52d798504895b4c2b84114ad5d2b406ac76e18374b44d1184ade3") set(FFMPEG_ARCHIVE_NAME "ffmpeg-linux") set(FFMPEG_LIB_DIR "lib") set(FFMPEG_INCLUDE_DIR "include") @@ -69,7 +67,6 @@ endif() FetchContent_Declare( ffmpeg URL ${FFMPEG_URL} - URL_HASH ${FFMPEG_SHA256HASH} DOWNLOAD_EXTRACT_TIMESTAMP ON SOURCE_DIR ${CMAKE_BINARY_DIR}/${FFMPEG_ARCHIVE_NAME} ) @@ -136,20 +133,6 @@ target_include_directories(${BOT_NAME} PRIVATE ${FFMPEG_INCLUDE_PATH}) target_precompile_headers(${BOT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include/precomp.h") -add_custom_command(TARGET ${BOT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_directory - "$" - "$" - COMMENT "Copying Boost.Filesystem DLL/so files to output directory" -) - -add_custom_command(TARGET ${BOT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_directory - "$" - "$" - COMMENT "Copying Boost.Process DLL/so files to output directory" -) - add_custom_command(TARGET ${BOT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_BINARY_DIR}/${FFMPEG_ARCHIVE_NAME}/bin" diff --git a/include/utils/console.h b/include/utils/console.h index 5044ac4..c0ad6d7 100644 --- a/include/utils/console.h +++ b/include/utils/console.h @@ -7,31 +7,31 @@ namespace utils { // @brief 명령어가 실행 가능한지 체크합니다 // @param cmd 실행할 명령 // @return int error_code -int ValidateCommand(std::string cmd); +int ValidateCommand(boost::asio::io_context& ctx, std::string cmd, const std::vector& args = std::vector()); // @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다 // @param cmd 실행할 명령 // @param args 아규먼트 // @return int error_code -int ExecuteCommand( +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args = std::vector()); // @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다 // @param cmd 실행할 명령 // @param result 실행결과 // @return int error_code -int ExecuteCommand(const std::string& cmd, std::string& result); +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, std::string& result); // @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다 // @param cmd 실행할 명령 // @param args 아규먼트 // @param result 실행결과 // @return int error_code -int ExecuteCommand(const std::string& cmd, const std::vector& args, +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args, std::string& result); // @brief 명령어를 쉘에서 실행하고 결과를 파이프로 연결하여 반환합니다 // @param cmd 실행할 명령 // @param args 아규먼트 // @return boost::process::popen -boost::process::popen OpenPipe( +boost::process::popen OpenPipe(boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args = std::vector()); } // namespace utils diff --git a/include/utils/update_checker.h b/include/utils/update_checker.h index d07fefc..36b1019 100644 --- a/include/utils/update_checker.h +++ b/include/utils/update_checker.h @@ -7,7 +7,7 @@ namespace utils { int InstallYtdlp(); -int CheckUpdate(); +int CheckUpdate(boost::asio::io_context& ctx); } // namespace utils diff --git a/src/main.cc b/src/main.cc index db2efbc..ba26b75 100644 --- a/src/main.cc +++ b/src/main.cc @@ -3,33 +3,54 @@ #include "utils/update_checker.h" int main(int argc, char* argv[]) { - boost::system::error_code ec; - char buf[16384]; - std::string output; + boost::asio::io_context ctx; + boost::system::error_code ec; + char buf[8192]; + std::string output; - utils::CheckUpdate(); + utils::CheckUpdate(ctx); - // utils::ExecuteCommand("yt-dlp", {"-U"}, output); - // std::cout << output; +#ifdef WIN32 + /*try { + auto ytdlp_pipe = utils::OpenPipe(ctx, + "yt-dlp.exe", { "-o", "-", "--quiet", "--ignore-errors", "-f", "bestaudio", + "https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR" }); - // auto ytdlp_pipe = utils::OpenPipe( - // "yt-dlp", {"-o", "-", "--quiet", "--ignore-errors", "-f", "bestaudio", - // "https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR"}); + while (true) { + boost::system::error_code read_ec; + size_t bytes_read = + boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 8192), + read_ec); + if (bytes_read > 0) { + std::cout.write(buf, bytes_read); + } + if (read_ec == boost::asio::error::eof || read_ec) { + break; + } + } + } + catch (const boost::process::system_error& e) { + std::string error = e.what(); + return -1; + }*/ +#else + auto ytdlp_pipe = utils::OpenPipe( + "yt-dlp", { "-o", "-", "--quiet", "--ignore-errors", "-f", "bestaudio", + "https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR" }); - // while (true) { - // boost::system::error_code read_ec; - // size_t bytes_read = - // boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 16384), - // read_ec); + while (true) { + boost::system::error_code read_ec; + size_t bytes_read = + boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 16384), + read_ec); + if (bytes_read > 0) { + std::cout.write(buf, bytes_read); + } + if (read_ec == boost::asio::error::eof || read_ec) { + break; + } + } +#endif - // if (bytes_read > 0) { - // std::cout.write(buf, bytes_read); - // } - - // if (read_ec == boost::asio::error::eof || read_ec) { - // break; - // } - // } - - return 0; + return 0; } \ No newline at end of file diff --git a/src/utils/console.cc b/src/utils/console.cc index 2085eb7..2b18f03 100644 --- a/src/utils/console.cc +++ b/src/utils/console.cc @@ -4,50 +4,48 @@ namespace utils { -int ValidateCommand(std::string cmd) { +int ValidateCommand(boost::asio::io_context& ctx, std::string cmd, const std::vector& args) { try { - ExecuteCommand(cmd); + ExecuteCommand(ctx, cmd, args); } catch (const boost::process::system_error& e) { return -1; } return 0; } -int ExecuteCommand(const std::string& cmd, +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args) { std::string ignored; - return ExecuteCommand(cmd, args, ignored); + return ExecuteCommand(ctx, cmd, args, ignored); } -int ExecuteCommand(const std::string& cmd, std::string& result) { +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, std::string& result) { std::vector ignored; - return ExecuteCommand(cmd, ignored, result); + return ExecuteCommand(ctx, cmd, ignored, result); } -int ExecuteCommand(const std::string& cmd, const std::vector& args, +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args, std::string& result) { - boost::system::error_code ec; - boost::asio::io_context ctx; - boost::process::popen proc( - ctx, cmd, args, boost::process::process_stdio{{}, nullptr, nullptr}); + try { + boost::system::error_code ec; + boost::process::popen proc( + ctx, cmd, args, boost::process::process_stdio{ {}, nullptr, nullptr }); - boost::asio::read(proc, boost::asio::dynamic_buffer(result), ec); + boost::asio::read(proc, boost::asio::dynamic_buffer(result), ec); - if (ec != boost::asio::error::eof) { - return -1; + proc.wait(); + + return proc.exit_code(); + } + catch (const boost::process::system_error& e) { + return -1; } - - proc.wait(); - - return proc.exit_code(); } -boost::process::popen OpenPipe(const std::string& cmd, +boost::process::popen OpenPipe(boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args) { - boost::asio::io_context ctx; - boost::process::popen proc( - ctx, cmd, args, boost::process::process_stdio{{}, nullptr, nullptr}); - return proc; + return boost::process::popen( + ctx, cmd, args, boost::process::process_stdio{nullptr, nullptr, nullptr}); }; } // namespace utils \ No newline at end of file diff --git a/src/utils/file_downloader.cc b/src/utils/file_downloader.cc index 64df34e..976c770 100644 --- a/src/utils/file_downloader.cc +++ b/src/utils/file_downloader.cc @@ -55,8 +55,17 @@ void DownloadFileFromHTTPS(std::string url, std::string filename) { boost::beast::http::write(stream, req); boost::beast::flat_buffer buffer; - boost::beast::http::response res; - boost::beast::http::read(stream, buffer, res); + + // 응답 파서 만들기 + boost::beast::http::response_parser parser; + + // body 제한 해제 (무제한) + parser.body_limit((std::numeric_limits::max)()); + + // 응답 읽기 + boost::beast::http::read(stream, buffer, parser); + + auto res = parser.release(); int status = res.result_int(); if (status / 100 == 3) { diff --git a/src/utils/update_checker.cc b/src/utils/update_checker.cc index 7e08ba9..bde5f0c 100644 --- a/src/utils/update_checker.cc +++ b/src/utils/update_checker.cc @@ -22,28 +22,44 @@ int InstallYtdlp() { return 0; } -int CheckUpdate() { - char buf[16384]; +static int FindNewLinePos(std::string& string, int start_pos) { + int newline_pos = start_pos; + while (newline_pos < string.size()) { + if (string[newline_pos] == '\n') { + return newline_pos; + } + newline_pos++; + } + return newline_pos; +} - if (ValidateCommand("yt-dlp") != 0) { - InstallYtdlp(); - return 0; +int CheckUpdate(boost::asio::io_context& ctx) { + std::string output = ""; + int old_newline_pos = 0; + int newline_pos = 0; + +#ifdef WIN32 + if (ExecuteCommand(ctx, "yt-dlp.exe", { "--version", "--newline" }, output) != 0) { + InstallYtdlp(); + ExecuteCommand(ctx, "yt-dlp.exe", { "--version", "--newline" }, output); } - - auto ytdlp_pipe = utils::OpenPipe("yt-dlp", {"-U"}); - - while (true) { - boost::system::error_code read_ec; - size_t bytes_read = - boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 16384), read_ec); - - if (bytes_read > 0) { - BOOST_LOG_TRIVIAL(info) << buf; - } - - if (read_ec == boost::asio::error::eof || read_ec) { - break; - } + BOOST_LOG_TRIVIAL(info) << "yt-dlp version: " << output.substr(0, output.size()-1); + output = ""; + ExecuteCommand(ctx, "yt-dlp.exe", { "-U", "--newline" }, output); +#else + if (ExecuteCommand(ctx, "yt-dlp", { "--version", "--newline" }, output) != 0) { + InstallYtdlp(); + ExecuteCommand(ctx, "yt-dlp", { "--version", "--newline" }, output); + } + BOOST_LOG_TRIVIAL(info) << "yt-dlp version: " << output.substr(0, output.size()-1); + output = ""; + ExecuteCommand(ctx, "yt-dlp", { "-U", "--newline" }, output); +#endif + while (newline_pos < output.size()) { + old_newline_pos = newline_pos; + newline_pos = FindNewLinePos(output, newline_pos); + BOOST_LOG_TRIVIAL(info) << output.substr(old_newline_pos, newline_pos - 1); + newline_pos++; } return 0;