From 2af5a8cc75100ea713975f5cab7a78fe78afa783 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 6 Apr 2024 23:37:40 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20linux=20=E5=AD=90=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E4=B8=8D=E4=BD=BF=E7=94=A8=20wstring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MaaUtils/IOStream/ChildPipeIOStream.cpp | 45 ++++++++++++------- .../Utils/IOStream/ChildPipeIOStream.h | 10 ++++- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/source/MaaUtils/IOStream/ChildPipeIOStream.cpp b/source/MaaUtils/IOStream/ChildPipeIOStream.cpp index fd90c5cb9..b844828d0 100644 --- a/source/MaaUtils/IOStream/ChildPipeIOStream.cpp +++ b/source/MaaUtils/IOStream/ChildPipeIOStream.cpp @@ -5,21 +5,45 @@ MAA_NS_BEGIN +#ifdef _WIN32 +std::vector conv_args(const std::vector& args) +{ + std::vector wargs; + for (const auto& arg : args) { + wargs.emplace_back(to_u16(arg)); + } + return wargs; +} +#else +std::vector conv_args(const std::vector& args) +{ + return args; +} +#endif + ChildPipeIOStream::ChildPipeIOStream( const std::filesystem::path& exec, const std::vector& args) - : ChildPipeIOStream(exec, to_wargs(args)) + : ChildPipeIOStream(exec, conv_args(args), false) { } ChildPipeIOStream::ChildPipeIOStream( const std::filesystem::path& exec, const std::vector& wargs) + : ChildPipeIOStream(exec, wargs, false) +{ +} + +ChildPipeIOStream::ChildPipeIOStream( + const std::filesystem::path& exec, + const std::vector& args, + bool) : exec_(exec) - , wargs_(wargs) + , args_(args) , child_( exec_, - wargs_, + args_, boost::process::std_out > pin_, boost::process::std_err > boost::process::null, boost::process::std_in < pout_ @@ -29,7 +53,7 @@ ChildPipeIOStream::ChildPipeIOStream( #endif ) { - LogTrace << VAR(exec_) << VAR(wargs_) << VAR(child_.id()); + LogTrace << VAR(exec_) << VAR(args_) << VAR(child_.id()); } ChildPipeIOStream::~ChildPipeIOStream() @@ -40,7 +64,7 @@ ChildPipeIOStream::~ChildPipeIOStream() bool ChildPipeIOStream::write(std::string_view data) { if (!pout_.good()) { - LogError << "pout is not good" << VAR(exec_) << VAR(wargs_) << VAR(child_.id()); + LogError << "pout is not good" << VAR(exec_) << VAR(args_) << VAR(child_.id()); return false; } @@ -66,7 +90,7 @@ bool ChildPipeIOStream::release() int code = child_.exit_code(); if (code != 0) { - LogWarn << "child exit with" << code << VAR(exec_) << VAR(wargs_) << VAR(child_.id()); + LogWarn << "child exit with" << code << VAR(exec_) << VAR(args_) << VAR(child_.id()); return false; } @@ -91,13 +115,4 @@ std::string ChildPipeIOStream::read_once(size_t max_count) return std::string(buffer_.get(), read); } -std::vector ChildPipeIOStream::to_wargs(const std::vector& args) -{ - std::vector wargs; - for (const auto& arg : args) { - wargs.emplace_back(to_u16(arg)); - } - return wargs; -} - MAA_NS_END \ No newline at end of file diff --git a/source/include/Utils/IOStream/ChildPipeIOStream.h b/source/include/Utils/IOStream/ChildPipeIOStream.h index 14e53630e..7aab619da 100644 --- a/source/include/Utils/IOStream/ChildPipeIOStream.h +++ b/source/include/Utils/IOStream/ChildPipeIOStream.h @@ -11,7 +11,10 @@ class MAA_UTILS_API ChildPipeIOStream : public IOStream { public: ChildPipeIOStream(const std::filesystem::path& exec, const std::vector& args); + +#ifdef _WIN32 ChildPipeIOStream(const std::filesystem::path& exec, const std::vector& wargs); +#endif // NonCopyButMovable // https://stackoverflow.com/questions/29289956/c11-virtual-destructors-and-auto-generation-of-move-special-functions @@ -32,10 +35,13 @@ class MAA_UTILS_API ChildPipeIOStream : public IOStream virtual std::string read_once(size_t max_count) override; private: - static std::vector to_wargs(const std::vector& args); + using os_string = std::filesystem::path::string_type; + + ChildPipeIOStream(const std::filesystem::path& exec, const std::vector& args, bool); +private: std::filesystem::path exec_; - std::vector wargs_; + std::vector args_; boost::process::ipstream pin_; boost::process::opstream pout_;