Skip to content

Commit

Permalink
build(windows): fix some compilation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuza committed Nov 3, 2024
1 parent fc5e00b commit 688e7a0
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 52 deletions.
1 change: 1 addition & 0 deletions src/ui/components.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod modal;
pub mod notification;
pub mod textinput_terminal;
26 changes: 26 additions & 0 deletions src/ui/components/notification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use notify_rust::{Notification, Timeout};

pub async fn notification(title: &str, body: impl ToString, timeout: impl Into<Timeout>) {
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();
}
97 changes: 73 additions & 24 deletions src/ui/screen/serverboot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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};
Expand Down Expand Up @@ -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<Message>) {
let binary_path = server.path.join("srcds_run");
let binary_path = server.path.join(SRCDS_EXEC_NAME);

let args = {
let mut temp = format!(
Expand Down Expand Up @@ -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,
Expand All @@ -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<u8> = vec![];

let mut input_bool = false;
Expand All @@ -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();

Expand All @@ -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;

Expand Down
44 changes: 16 additions & 28 deletions src/ui/screen/serverlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use iced_aw::{
style::colors,
Menu, MenuBar,
};
use notify_rust::Notification;
use serde::{Deserialize, Serialize};

use dragking::{self, DropPosition};
Expand All @@ -36,7 +35,7 @@ use crate::{
SourceEngineVersion,
},
ui::{
components::modal::modal,
components::{modal::modal, notification::notification},
style::{self, icon},
},
};
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 688e7a0

Please sign in to comment.