From 1689848851d4a9f64b17aef3c71cdfbc8cf33db7 Mon Sep 17 00:00:00 2001 From: Nikita Podvirnyi Date: Fri, 19 Jul 2024 14:12:08 +0200 Subject: [PATCH] fix: don't write empty stdout and stderr --- src/games/genshin/game.rs | 22 ++++++++++++++-------- src/games/honkai/game.rs | 20 ++++++++++++-------- src/games/pgr/game.rs | 20 ++++++++++++-------- src/games/star_rail/game.rs | 20 ++++++++++++-------- src/games/wuwa/game.rs | 20 ++++++++++++-------- src/games/zzz/game.rs | 20 ++++++++++++-------- 6 files changed, 74 insertions(+), 48 deletions(-) diff --git a/src/games/genshin/game.rs b/src/games/genshin/game.rs index 3788573..c23c062 100644 --- a/src/games/genshin/game.rs +++ b/src/games/genshin/game.rs @@ -321,10 +321,12 @@ pub fn run() -> anyhow::Result<()> { stdout.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } @@ -334,10 +336,12 @@ pub fn run() -> anyhow::Result<()> { stderr.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } @@ -347,6 +351,8 @@ pub fn run() -> anyhow::Result<()> { } } + game_output.flush()?; + // Workaround for fast process closing (is it still a thing?) loop { std::thread::sleep(std::time::Duration::from_secs(3)); diff --git a/src/games/honkai/game.rs b/src/games/honkai/game.rs index 415297c..32dfd8a 100644 --- a/src/games/honkai/game.rs +++ b/src/games/honkai/game.rs @@ -274,10 +274,12 @@ pub fn run() -> anyhow::Result<()> { stdout.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } @@ -287,10 +289,12 @@ pub fn run() -> anyhow::Result<()> { stderr.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } diff --git a/src/games/pgr/game.rs b/src/games/pgr/game.rs index 7cbd69c..a58d14c 100644 --- a/src/games/pgr/game.rs +++ b/src/games/pgr/game.rs @@ -264,10 +264,12 @@ pub fn run() -> anyhow::Result<()> { stdout.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } @@ -277,10 +279,12 @@ pub fn run() -> anyhow::Result<()> { stderr.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } diff --git a/src/games/star_rail/game.rs b/src/games/star_rail/game.rs index a43be00..f9d303c 100644 --- a/src/games/star_rail/game.rs +++ b/src/games/star_rail/game.rs @@ -275,10 +275,12 @@ pub fn run() -> anyhow::Result<()> { stdout.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } @@ -288,10 +290,12 @@ pub fn run() -> anyhow::Result<()> { stderr.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } diff --git a/src/games/wuwa/game.rs b/src/games/wuwa/game.rs index 9d5464a..4e74b52 100644 --- a/src/games/wuwa/game.rs +++ b/src/games/wuwa/game.rs @@ -255,10 +255,12 @@ pub fn run() -> anyhow::Result<()> { stdout.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } @@ -268,10 +270,12 @@ pub fn run() -> anyhow::Result<()> { stderr.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } diff --git a/src/games/zzz/game.rs b/src/games/zzz/game.rs index 8a5e6c4..9f52262 100644 --- a/src/games/zzz/game.rs +++ b/src/games/zzz/game.rs @@ -273,10 +273,12 @@ pub fn run() -> anyhow::Result<()> { stdout.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } } @@ -286,10 +288,12 @@ pub fn run() -> anyhow::Result<()> { stderr.read_to_end(&mut buf)?; - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + } } }