From 688e7a008760bcf3de630d6bb104937824ec77cc Mon Sep 17 00:00:00 2001 From: tsuza Date: Sun, 3 Nov 2024 20:32:17 +0100 Subject: [PATCH] build(windows): fix some compilation issues --- src/ui/components.rs | 1 + src/ui/components/notification.rs | 26 +++++++++ src/ui/screen/serverboot.rs | 97 +++++++++++++++++++++++-------- src/ui/screen/serverlist.rs | 44 +++++--------- 4 files changed, 116 insertions(+), 52 deletions(-) create mode 100644 src/ui/components/notification.rs diff --git a/src/ui/components.rs b/src/ui/components.rs index 10348c3..e37ac43 100644 --- a/src/ui/components.rs +++ b/src/ui/components.rs @@ -1,2 +1,3 @@ pub mod modal; +pub mod notification; pub mod textinput_terminal; diff --git a/src/ui/components/notification.rs b/src/ui/components/notification.rs new file mode 100644 index 0000000..277fb59 --- /dev/null +++ b/src/ui/components/notification.rs @@ -0,0 +1,26 @@ +use notify_rust::{Notification, Timeout}; + +pub async fn notification(title: &str, body: impl ToString, timeout: impl Into) { + let mut notification = Notification::new(); + + notification + .appname("MANNager") + .summary(title) + .body(&body.to_string()) + .timeout(timeout.into()); + + #[cfg(target_os = "linux")] + notification.icon("org.tsuza.mannager"); + + #[cfg(target_os = "windows")] + notification.app_id("org.tsuza.mannager"); + + #[cfg(target_os = "linux")] + let _ = notification + .show_async() + .await + .and_then(|notification| Ok(notification.on_close(|_| ()))); + + #[cfg(target_os = "windows")] + let _ = notification.show(); +} diff --git a/src/ui/screen/serverboot.rs b/src/ui/screen/serverboot.rs index 0fa977b..27b3f56 100644 --- a/src/ui/screen/serverboot.rs +++ b/src/ui/screen/serverboot.rs @@ -10,7 +10,6 @@ use iced::{ Color, Element, Length, Subscription, Task, }; use iced_aw::style::colors; -use notify_rust::Notification; use portforwarder_rs::port_forwarder::PortMappingProtocol; use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, @@ -19,7 +18,10 @@ use tokio::{ use crate::{ core::portforwarder::{self, PortForwarderIP}, - ui::{components::textinput_terminal, style}, + ui::{ + components::{notification::notification, textinput_terminal}, + style, + }, }; use super::serverlist::{get_arg_game_name, ServerInfo, SourceAppIDs}; @@ -57,9 +59,15 @@ pub enum TerminalText { pub const DEFAULT_PORT: u16 = 27015; pub const PORT_OFFSET: u16 = 10; +#[cfg(target_os = "linux")] +const SRCDS_EXEC_NAME: &str = "srcds_run"; + +#[cfg(target_os = "windows")] +const SRCDS_EXEC_NAME: &str = "srcds-fix.exe"; + impl State { pub fn new(server: &ServerInfo, port: u16) -> (Self, Task) { - let binary_path = server.path.join("srcds_run"); + let binary_path = server.path.join(SRCDS_EXEC_NAME); let args = { let mut temp = format!( @@ -259,18 +267,53 @@ fn start_server( .send(ServerCommunicationTwoWay::Input(sender)) .await?; - let mut pty = - pty_process::Pty::new().map_err(|err| Error::SpawnProcessError(err.to_string()))?; + #[cfg(target_os = "linux")] + let mut pty = { + let test = + pty_process::Pty::new().map_err(|err| Error::SpawnProcessError(err.to_string()))?; + + let _ = test.resize(pty_process::Size::new(24, 80)); + + test + }; + + let mut _process = { + #[cfg(target_os = "linux")] + { + pty_process::Command::new(executable_path) + .args(args.split_whitespace()) + .spawn( + &pty.pts() + .map_err(|err| Error::SpawnProcessError(err.to_string()))?, + ) + .map_err(|err| Error::SpawnProcessError(err.to_string()))? + } + + #[cfg(target_os = "windows")] + { + use std::process::Stdio; + + tokio::process::Command::new(executable_path) + .args(args.split_whitespace()) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .creation_flags(0x08000000) + .spawn() + .map_err(|err| Error::SpawnProcessError(err.to_string()))? + } + }; - let _ = pty.resize(pty_process::Size::new(24, 80)); + let (mut process_reader, mut process_writer) = { + #[cfg(target_os = "linux")] + { + pty.split() + } - let mut _process = pty_process::Command::new(executable_path) - .args(args.split_whitespace()) - .spawn( - &pty.pts() - .map_err(|err| Error::SpawnProcessError(err.to_string()))?, - ) - .map_err(|err| Error::SpawnProcessError(err.to_string()))?; + #[cfg(target_os = "windows")] + { + (_process.stdout.take().unwrap(), _process.stdin.unwrap()) + } + }; let forwarder = portforwarder::PortForwarder::open( PortForwarderIP::Any, @@ -281,17 +324,13 @@ fn start_server( ); if let Err(_) = forwarder { - let _ = Notification::new() - .appname("MANNager") - .summary("[ MANNager ] Server running...") - .body("Port forwarding failed.") - .timeout(5) - .show_async() - .await; + let _ = notification( + "[ MANNager ] Server running...", + "Port forwarding failed.", + 5, + ); } - let (mut process_reader, mut process_writer) = pty.split(); - let mut buffer: Vec = vec![]; let mut input_bool = false; @@ -310,11 +349,17 @@ fn start_server( continue; } - let Some(last_byte) = buffer.get(buffer.len() - 2) else { + let Some(_last_byte) = buffer.get(buffer.len() - 2) else { continue; }; - if last_byte == &13u8 && byte == 10u8 { + #[cfg(target_os = "windows")] + let line_break = byte == 10u8; + + #[cfg(target_os = "linux")] + let line_break = _last_byte == &13u8 && byte == 10u8; + + if line_break { let Ok(string) = String::from_utf8(buffer.clone()) else { buffer.clear(); @@ -337,8 +382,12 @@ fn start_server( }, input = input_future => { + #[cfg(target_os = "linux")] let formatted_string = format!("{}\n\0", input); + #[cfg(target_os = "windows")] + let formatted_string = format!("{}", input); + let _ = process_writer.write_all(formatted_string.as_bytes()).await; let _ = process_writer.flush().await; diff --git a/src/ui/screen/serverlist.rs b/src/ui/screen/serverlist.rs index 6a6d529..ca93fb7 100644 --- a/src/ui/screen/serverlist.rs +++ b/src/ui/screen/serverlist.rs @@ -24,7 +24,6 @@ use iced_aw::{ style::colors, Menu, MenuBar, }; -use notify_rust::Notification; use serde::{Deserialize, Serialize}; use dragking::{self, DropPosition}; @@ -36,7 +35,7 @@ use crate::{ SourceEngineVersion, }, ui::{ - components::modal::modal, + components::{modal::modal, notification::notification}, style::{self, icon}, }, }; @@ -190,14 +189,11 @@ impl State { let servers = Self::get_server_list().unwrap_or_else(|_| { task = Task::future(async move { - Notification::new() - .appname("MANNager") - .summary("[ MANNager ] Server List") - .body("The server list file was not found.") - .timeout(5) - .show_async() - .await - .and_then(|notification| Ok(notification.on_close(|_| ()))) + notification( + "[ MANNager ] Server List", + "The server list file was not found.", + 5, + ) }) .discard(); @@ -381,14 +377,11 @@ impl State { Task::future(async move { let _ = Self::save_server_list_to_file(servers.into_iter()).await; - Notification::new() - .appname("MANNager") - .summary("[ MANNager ] Server Deletion") - .body(&format!("{server_name} has been successfully deleted.")) - .timeout(5) - .show_async() - .await - .and_then(|notification| Ok(notification.on_close(|_| ()))) + notification( + "[ MANNager ] Server Deletion", + format!("{server_name} has been successfully deleted."), + 5, + ) }) .discard() } @@ -482,16 +475,11 @@ impl State { let server_name = server.info.name.clone(); Task::future(async move { - let _ = Notification::new() - .appname("MANNager") - .summary("[ MANNager ] Sourcemod Download") - .body(&format!( - "Sourcemod has been successfully downloaded for {server_name}." - )) - .timeout(5) - .show_async() - .await - .and_then(|notification| Ok(notification.on_close(|_| ()))); + notification( + "[ MANNager ] Sourcemod Download", + format!("Sourcemod has been successfully downloaded for {server_name}."), + 5, + ) }) .discard() }