From 166eda0abb7f122037f24d368648b6fe1c681de6 Mon Sep 17 00:00:00 2001
From: hinto-janaiyo <hinto.janaiyo@protonmail.com>
Date: Fri, 30 Dec 2022 19:27:47 -0500
Subject: [PATCH] main: update absolute paths before starting processes

Some situations won't update the path and so starting processes
didn't do anything and failed, this makes sure we have a valid
path before starting.
---
 CHANGELOG.md |  1 +
 src/disk.rs  |  6 ++++++
 src/main.rs  | 17 ++++++++++-------
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index eb806c3..f71a0c6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 ## Fixes
 * macOS: Added warning (and solution) if `Gupax/P2Pool/XMRig` were quarantined by [`Gatekeeper`](https://support.apple.com/en-us/HT202491)
 * P2Pool/XMRig: Added a red `Start` button on errors (bad PATH, invalid file, etc) and a solution in the tooltip
+* P2Pool/XMRig: Fixed processes sometimes not starting after entering a custom PATH
 * P2Pool: Fixed custom node selection sometimes using old values after save
 * Miscellaneous UI changes and fixes
 
diff --git a/src/disk.rs b/src/disk.rs
index 6a4c76c..af4d913 100644
--- a/src/disk.rs
+++ b/src/disk.rs
@@ -170,6 +170,12 @@ impl State {
 		}
 	}
 
+    pub fn update_absolute_path(&mut self) -> Result<(), TomlError> {
+        self.gupax.absolute_p2pool_path = into_absolute_path(self.gupax.p2pool_path.clone())?;
+        self.gupax.absolute_xmrig_path = into_absolute_path(self.gupax.xmrig_path.clone())?;
+        Ok(())
+    }
+
 	// Convert [&str] to [State]
 	pub fn from_str(string: &str) -> Result<Self, TomlError> {
 		match toml::de::from_str(string) {
diff --git a/src/main.rs b/src/main.rs
index 9f9984e..1636cd3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1455,6 +1455,8 @@ impl eframe::App for App {
 								ui.set_enabled(ui_enabled);
 								let color = if ui_enabled { GREEN } else { RED };
 								if (ui_enabled && key.is_up() && !wants_input) || ui.add_sized([width, height], Button::new(RichText::new("▶").color(color))).on_hover_text("Start P2Pool").on_disabled_hover_text(text).clicked() {
+									self.og.lock().unwrap().update_absolute_path();
+									self.state.update_absolute_path(); // The above checks make sure this can unwrap safely, probably should handle it though.
 									Helper::start_p2pool(&self.helper, &self.state.p2pool, &self.state.gupax.absolute_p2pool_path);
 								}
 							}
@@ -1515,14 +1517,15 @@ impl eframe::App for App {
 								}
 								ui.set_enabled(ui_enabled);
 								let color = if ui_enabled { GREEN } else { RED };
-								#[cfg(target_os = "windows")]
 								if (ui_enabled && key.is_up() && !wants_input) || ui.add_sized([width, height], Button::new(RichText::new("▶").color(color))).on_hover_text("Start XMRig").on_disabled_hover_text(text).clicked() {
-									Helper::start_xmrig(&self.helper, &self.state.xmrig, &self.state.gupax.absolute_xmrig_path, Arc::clone(&self.sudo));
-								}
-								#[cfg(target_family = "unix")]
-								if (ui_enabled && key.is_up() && !wants_input) || ui.add_sized([width, height], Button::new(RichText::new("▶").color(color))).on_hover_text("Start XMRig").on_disabled_hover_text(text).clicked() {
-									self.sudo.lock().unwrap().signal = ProcessSignal::Start;
-									self.error_state.ask_sudo(&self.sudo);
+									self.og.lock().unwrap().update_absolute_path();
+									self.state.update_absolute_path(); // The above checks make sure this can unwrap safely, probably should handle it though.
+									if cfg!(windows) {
+										Helper::start_xmrig(&self.helper, &self.state.xmrig, &self.state.gupax.absolute_xmrig_path, Arc::clone(&self.sudo));
+									} else if cfg!(unix) {
+										self.sudo.lock().unwrap().signal = ProcessSignal::Start;
+										self.error_state.ask_sudo(&self.sudo);
+									}
 								}
 							}
 						});