diff --git a/src/features/game_networking.rs b/src/features/game_networking.rs index 568abeb..1b35a91 100644 --- a/src/features/game_networking.rs +++ b/src/features/game_networking.rs @@ -6,13 +6,22 @@ use crate::{ }, }; use std::error::Error; +use strum::{Display, EnumIter}; const FILTER_NAME_EXE: &str = "[GTA Tools] Block outbound traffic for all of GTA V"; const FILTER_NAME_SAVE_SERVER: &str = "[GTA Tools] Block outbound traffic to Rockstar save server"; +#[derive(Clone, Copy, Debug, Default, Display, EnumIter, PartialEq)] +pub enum BlockedStatus { + #[default] + NotBlocked, + ServerBlocked, + ExeBlocked, +} + #[derive(Debug)] pub struct GameNetworking { - pub blocked: bool, + pub blocked: BlockedStatus, } impl Default for GameNetworking { @@ -20,9 +29,11 @@ impl Default for GameNetworking { let firewall = Firewall::default(); Self { blocked: if firewall.is_blocked(FILTER_NAME_SAVE_SERVER).unwrap() { - true + BlockedStatus::ServerBlocked + } else if firewall.is_blocked(FILTER_NAME_EXE).unwrap() { + BlockedStatus::ExeBlocked } else { - firewall.is_blocked(FILTER_NAME_EXE).unwrap() + BlockedStatus::NotBlocked }, } } @@ -37,20 +48,20 @@ impl GameNetworking { let Some(exe_path) = system_info.get_game_exe_path() else { return Ok(()); }; - firewall.add( - FILTER_NAME_EXE, - RuleMode::Executable(exe_path.to_path_buf()), - RuleDirection::Out, - RuleProtocol::Any, - )?; - self.blocked = firewall.is_blocked(FILTER_NAME_EXE)?; - Ok(()) + firewall + .add( + FILTER_NAME_EXE, + RuleMode::Executable(exe_path.to_path_buf()), + RuleDirection::Out, + RuleProtocol::Any, + ) + .inspect(|_| self.blocked = BlockedStatus::ExeBlocked) } pub fn unblock_exe(&mut self, firewall: &Firewall) -> Result<(), Box> { - firewall.remove(FILTER_NAME_EXE)?; - self.blocked = firewall.is_blocked(FILTER_NAME_EXE)?; - Ok(()) + firewall + .remove(FILTER_NAME_EXE) + .inspect(|_| self.blocked = BlockedStatus::NotBlocked) } pub fn block_save_server( @@ -58,20 +69,20 @@ impl GameNetworking { save_server_ip: &str, firewall: &Firewall, ) -> Result<(), Box> { - firewall.add( - FILTER_NAME_SAVE_SERVER, - RuleMode::Address(save_server_ip.to_owned()), - RuleDirection::Out, - RuleProtocol::Any, - )?; - self.blocked = firewall.is_blocked(FILTER_NAME_SAVE_SERVER)?; - Ok(()) + firewall + .add( + FILTER_NAME_SAVE_SERVER, + RuleMode::Address(save_server_ip.to_owned()), + RuleDirection::Out, + RuleProtocol::Any, + ) + .inspect(|_| self.blocked = BlockedStatus::ServerBlocked) } pub fn unblock_save_server(&mut self, firewall: &Firewall) -> Result<(), Box> { - firewall.remove(FILTER_NAME_SAVE_SERVER)?; - self.blocked = firewall.is_blocked(FILTER_NAME_SAVE_SERVER)?; - Ok(()) + firewall + .remove(FILTER_NAME_SAVE_SERVER) + .inspect(|_| self.blocked = BlockedStatus::NotBlocked) } pub fn ensure_block_exclusivity( @@ -81,15 +92,13 @@ impl GameNetworking { ) -> Result<(), Box> { match block_method { BlockMethod::EntireGame => { - if firewall.is_blocked(FILTER_NAME_SAVE_SERVER)? { + if self.blocked == BlockedStatus::ServerBlocked { self.unblock_save_server(firewall)?; - self.blocked = firewall.is_blocked(FILTER_NAME_EXE)?; } } BlockMethod::SaveServer => { - if firewall.is_blocked(FILTER_NAME_EXE)? { + if self.blocked == BlockedStatus::ExeBlocked { self.unblock_exe(firewall)?; - self.blocked = firewall.is_blocked(FILTER_NAME_SAVE_SERVER)?; } } } diff --git a/src/gui/app.rs b/src/gui/app.rs index be66a63..da3f097 100644 --- a/src/gui/app.rs +++ b/src/gui/app.rs @@ -1,5 +1,5 @@ use crate::{ - features, + features::{self, game_networking::BlockedStatus}, gui::{ settings::{BlockMethod, ROCKSTAR_SAVE_SERVER, Settings}, tools, @@ -163,10 +163,18 @@ impl App { BlockMethod::SaveServer => ui.label("Rockstar save server access"), }; ui.add_space(1.0); - ui.create_indicator_dot(if self.game_networking.blocked { - colours::RED - } else { - colours::GREEN + ui.create_indicator_dot(match self.game_networking.blocked { + BlockedStatus::ExeBlocked + if self.settings.block_method == BlockMethod::EntireGame => + { + colours::RED + } + BlockedStatus::ServerBlocked + if self.settings.block_method == BlockMethod::SaveServer => + { + colours::RED + } + _ => colours::GREEN, }); label }); diff --git a/src/gui/debug.rs b/src/gui/debug.rs index d3acf0b..3519401 100644 --- a/src/gui/debug.rs +++ b/src/gui/debug.rs @@ -2,6 +2,7 @@ use crate::{ gui::{ app::{App, WINDOW_SIZE}, tools, + ui_ext::UiExt, }, util::consts::game::{EXE_ENHANCED, EXE_LEGACY}, }; @@ -26,10 +27,7 @@ impl App { ui.label("blocked"); egui::ComboBox::from_id_salt("blocked") .selected_text(self.game_networking.blocked.to_string()) - .show_ui(ui, |ui| { - ui.selectable_value(&mut self.game_networking.blocked, true, "true"); - ui.selectable_value(&mut self.game_networking.blocked, false, "false"); - }); + .show_ui(ui, |ui| ui.build_menu(&mut self.game_networking.blocked)); }); if ui.add(egui::Button::new("force refresh theme")).clicked() { catppuccin_egui::set_theme(ui.ctx(), self.settings.theme.into());