Skip to main content

Mountain/IPC/WindServiceHandlers/NativeHost/
OSProperties.rs

1#![allow(unused_variables, dead_code, unused_imports)]
2
3//! Wire method: `nativeHost:getOSProperties` (cross-platform).
4//! Returns Electron-shaped `{ type, release, arch, platform, cpus }` tuple.
5//! Cached for the process lifetime - OS type, version, arch, and CPU topology
6//! are stable; spawning `sw_vers`/`uname` on every call wastes ~10 ms each.
7
8use std::sync::OnceLock;
9
10use serde_json::{Value, json};
11
12static OS_PROPERTIES_CACHE:OnceLock<Value> = OnceLock::new();
13
14pub async fn Fn() -> Result<Value, String> {
15	if let Some(Cached) = OS_PROPERTIES_CACHE.get() {
16		return Ok(Cached.clone());
17	}
18
19	let Result = compute_os_properties();
20
21	let _ = OS_PROPERTIES_CACHE.set(Result.clone());
22
23	Ok(Result)
24}
25
26fn compute_os_properties() -> Value {
27	use sysinfo::System;
28
29	let OsType = match std::env::consts::OS {
30		"macos" => "Darwin",
31
32		"windows" => "Windows_NT",
33
34		"linux" => "Linux",
35
36		_ => std::env::consts::OS,
37	};
38
39	let Release = {
40		#[cfg(target_os = "macos")]
41		{
42			std::process::Command::new("sw_vers")
43				.arg("-productVersion")
44				.output()
45				.ok()
46				.map(|O| String::from_utf8_lossy(&O.stdout).trim().to_string())
47				.unwrap_or_else(|| "14.0".to_string())
48		}
49
50		#[cfg(target_os = "windows")]
51		{
52			std::process::Command::new("cmd")
53				.args(["/c", "ver"])
54				.output()
55				.ok()
56				.map(|O| {
57					let Output = String::from_utf8_lossy(&O.stdout);
58					Output
59						.split('[')
60						.nth(1)
61						.and_then(|S| S.split(']').next())
62						.and_then(|S| S.strip_prefix("Version "))
63						.unwrap_or("10.0.0")
64						.to_string()
65				})
66				.unwrap_or_else(|| "10.0.0".to_string())
67		}
68
69		#[cfg(target_os = "linux")]
70		{
71			std::process::Command::new("uname")
72				.arg("-r")
73				.output()
74				.ok()
75				.map(|O| String::from_utf8_lossy(&O.stdout).trim().to_string())
76				.unwrap_or_else(|| "6.1.0".to_string())
77		}
78
79		#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
80		{
81			"0.0.0".to_string()
82		}
83	};
84
85	let mut Sys = System::new();
86
87	Sys.refresh_cpu_all();
88
89	let Cpus:Vec<Value> = Sys
90		.cpus()
91		.iter()
92		.map(|Cpu| {
93			json!({
94				"model": Cpu.brand(),
95				"speed": Cpu.frequency()
96			})
97		})
98		.collect();
99
100	json!({
101		"type": OsType,
102		"release": Release,
103		"arch": std::env::consts::ARCH,
104		"platform": std::env::consts::OS,
105		"cpus": Cpus
106	})
107}