Skip to content

Commit

Permalink
Add debug options for resources-processes
Browse files Browse the repository at this point in the history
  • Loading branch information
nokyan committed Jul 26, 2024
1 parent 6372c75 commit 97bd0df
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 14 deletions.
130 changes: 130 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ plotters = { version = "0.3.6", default-features = false, features = [
"area_series",
] }
plotters-cairo = "0.7.0"
rmp-serde = "1.3.0"
rust-ini = "0.21.0"
gtk-macros = "0.3.0"
strum = "0.26.3"
Expand All @@ -38,3 +37,6 @@ hashbrown = "0.14.5"
paste = "1.0.15"
num_cpus = "1.16.0"
async-channel = "2.3.1"
clap = { version = "4.5.11", features = ["derive"] }
ron = "0.8.1"
rmp-serde = "1.3.0"
52 changes: 43 additions & 9 deletions src/bin/resources-processes.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
use anyhow::Result;
use process_data::ProcessData;
use ron::ser::PrettyConfig;
use std::io::{Read, Write};

use clap::Parser;

#[derive(Parser, Debug)]
#[command(version, about)]
struct Args {
/// Output once and then exit
#[arg(short, long, default_value_t = false)]
once: bool,

/// Use Rusty Object Notation (use this only for debugging this binary on its own, Resources won't be able to decode RON)
#[arg(short, long, default_value_t = false)]
ron: bool,
}

fn main() -> Result<()> {
let args = Args::parse();

if args.once {
output(args.ron)?;
return Ok(());
}

loop {
let mut buffer = [0; 1];

std::io::stdin().read_exact(&mut buffer)?;

let data = ProcessData::all_process_data()?;
let encoded = rmp_serde::encode::to_vec(&*data)?;
output(args.ron)?;
}
}

fn output(ron: bool) -> Result<()> {
let data = ProcessData::all_process_data()?;

let len_byte_array = encoded.len().to_le_bytes();
let encoded = if ron {
ron::ser::to_string_pretty(&data, PrettyConfig::default())?
.as_bytes()
.to_vec()
} else {
rmp_serde::to_vec(&data)?
};

let stdout = std::io::stdout();
let mut handle = stdout.lock();
let len_byte_array = encoded.len().to_le_bytes();

handle.write_all(&len_byte_array)?;
let stdout = std::io::stdout();
let mut handle = stdout.lock();

handle.write_all(&encoded)?;
handle.write_all(&len_byte_array)?;

handle.flush()?;
}
handle.write_all(&encoded)?;

handle.flush()?;
Ok(())
}
7 changes: 3 additions & 4 deletions src/utils/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static OTHER_PROCESS: LazyLock<Mutex<(ChildStdin, ChildStdout)>> = LazyLock::new
};

let child = if *IS_FLATPAK {
debug!("Spawning resources-processes in Flatpak mode");
debug!("Spawning resources-processes in Flatpak mode ({proxy_path})");
Command::new(FLATPAK_SPAWN)
.args(["--host", proxy_path.as_str()])
.stdin(Stdio::piped())
Expand All @@ -36,7 +36,7 @@ static OTHER_PROCESS: LazyLock<Mutex<(ChildStdin, ChildStdout)>> = LazyLock::new
.spawn()
.unwrap()
} else {
debug!("Spawning resources-processes in native mode");
debug!("Spawning resources-processes in native mode ({proxy_path})");
Command::new(proxy_path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
Expand Down Expand Up @@ -123,8 +123,7 @@ impl Process {
output_bytes
};

rmp_serde::from_slice::<Vec<ProcessData>>(&output)
.context("error decoding resources-processes' output")
rmp_serde::from_slice(&output).context("error decoding resources-processes' output")
}

pub fn from_process_data(process_data: ProcessData) -> Self {
Expand Down

0 comments on commit 97bd0df

Please sign in to comment.