From aa84eb0853e2207014062877d539fca150a6d9c7 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Fri, 5 Mar 2021 13:37:39 +0100 Subject: [PATCH] Add a Valheim controller module. --- Cargo.lock | 117 ++++++++++++++++++++++++++++++ backend/Cargo.toml | 2 + backend/src/main.rs | 7 +- backend/src/tests.rs | 6 ++ backend/src/valheim_controller.rs | 15 ++++ 5 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 backend/src/tests.rs create mode 100644 backend/src/valheim_controller.rs diff --git a/Cargo.lock b/Cargo.lock index c162316..3d42c88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -548,6 +548,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2df960f5d869b2dd8532793fde43eb5427cceb126c929747a26823ab0eeb536" +[[package]] +name = "core-foundation-sys" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" + [[package]] name = "crc32fast" version = "1.2.1" @@ -557,6 +563,51 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2584f639eb95fea8c798496315b297cf81b9b58b6d30ab066a75455333cf4b12" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", + "lazy_static", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49" +dependencies = [ + "autocfg", + "cfg-if 1.0.0", + "lazy_static", +] + [[package]] name = "derive_more" version = "0.99.11" @@ -568,6 +619,12 @@ dependencies = [ "syn", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "dtoa" version = "0.4.7" @@ -1019,6 +1076,15 @@ version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +[[package]] +name = "memoffset" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.16" @@ -1118,6 +1184,15 @@ dependencies = [ "version_check 0.9.2", ] +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "num-integer" version = "0.1.44" @@ -1331,6 +1406,31 @@ dependencies = [ "rand_core", ] +[[package]] +name = "rayon" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" +dependencies = [ + "autocfg", + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "lazy_static", + "num_cpus", +] + [[package]] name = "redox_syscall" version = "0.2.5" @@ -1501,6 +1601,22 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "sysinfo" +version = "0.16.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c280c91abd1aed2e36be1bc8f56fbc7a2acbb2b58fbcac9641510179cc72dd9" +dependencies = [ + "cfg-if 1.0.0", + "core-foundation-sys", + "doc-comment", + "libc", + "ntapi", + "once_cell", + "rayon", + "winapi 0.3.9", +] + [[package]] name = "thread_local" version = "1.1.3" @@ -1777,6 +1893,7 @@ dependencies = [ "listenfd", "ron", "serde", + "sysinfo", ] [[package]] diff --git a/backend/Cargo.toml b/backend/Cargo.toml index c41e05d..944bf96 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -14,6 +14,8 @@ listenfd = "0.3" # To watch file modifications and automatically launch a build ron = "0.6" # Rust object notation, to load configuration files. itertools = "0.10" +sysinfo = "0.16" + futures = "0.3" # Needed by askam with the feature 'with-actix-web'. common = { path = "../common" } diff --git a/backend/src/main.rs b/backend/src/main.rs index 99bebc3..98ad670 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -12,6 +12,8 @@ use ron::{ de::from_reader, ser::{ to_string_pretty, PrettyConfig } }; use serde::{ Deserialize, Serialize }; mod consts; +mod tests; +mod valheim_controller; #[derive(Template)] #[template(path = "main.html")] @@ -94,7 +96,7 @@ async fn main() -> std::io::Result<()> { fn process_args() -> bool { fn print_usage() { println!("Usage:"); - println!(" {} [--help]", get_exe_name()); + println!(" {} [--help] [--status]", get_exe_name()); } let args: Vec = args().collect(); @@ -102,6 +104,9 @@ fn process_args() -> bool { if args.iter().any(|arg| arg == "--help") { print_usage(); return true + } else if args.iter().any(|arg| arg == "--status") { + valheim_controller::is_valheim_running(); + return true } false diff --git a/backend/src/tests.rs b/backend/src/tests.rs new file mode 100644 index 0000000..2d1b0df --- /dev/null +++ b/backend/src/tests.rs @@ -0,0 +1,6 @@ +//use valheim_controller::*; + +#[test] +fn test1() { + assert_eq!(2, 1 + 1); +} \ No newline at end of file diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs new file mode 100644 index 0000000..3fd4970 --- /dev/null +++ b/backend/src/valheim_controller.rs @@ -0,0 +1,15 @@ +use sysinfo::{ ProcessExt, SystemExt }; + +pub fn is_valheim_running() -> bool { + let mut system = sysinfo::System::new_all(); + system.refresh_all(); + let process = system.get_process_by_name("valheim_server"); + + if process.len() >= 1 { + println!("Valheim running"); + true + } else { + println!("Valheim not running"); + false + } +} \ No newline at end of file -- 2.45.2