greatly simplify blocked status by removing temporary failure state

This commit is contained in:
2025-11-22 09:27:02 +00:00
parent 28486174f2
commit 660a478fbb
4 changed files with 20 additions and 69 deletions
+10 -50
View File
@@ -8,9 +8,7 @@ use crate::{
use std::{
error::Error,
path::{Path, PathBuf},
time::{Duration, Instant},
};
use strum::{Display, EnumIter};
use windows::{
Win32::{
NetworkManagement::WindowsFirewall::{
@@ -28,44 +26,21 @@ use windows::{
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";
const INTERVAL: Duration = Duration::from_secs(3);
#[derive(Clone, Copy, Debug, Display, PartialEq, Eq, EnumIter)]
pub enum BlockedStatus {
Blocked,
Failed,
Unblocked,
}
impl From<bool> for BlockedStatus {
fn from(value: bool) -> Self {
if value {
Self::Blocked
} else {
Self::Unblocked
}
}
}
#[derive(Debug)]
pub struct GameNetworking {
com_initialized: bool,
pub blocked_status: BlockedStatus,
timer: Instant,
counting: bool,
pub blocked: bool,
}
impl Default for GameNetworking {
fn default() -> Self {
Self {
blocked_status: if Self::is_save_server_blocked().unwrap() {
Self::is_save_server_blocked().unwrap().into()
blocked: if Self::is_save_server_blocked().unwrap() {
Self::is_save_server_blocked().unwrap()
} else {
Self::is_exe_blocked().unwrap().into()
Self::is_exe_blocked().unwrap()
},
com_initialized: unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) }.is_ok(),
timer: Instant::now(),
counting: false,
}
}
}
@@ -130,17 +105,16 @@ impl GameNetworking {
pub fn block_exe(&mut self, system_info: &mut SystemInfo) -> Result<(), Box<dyn Error>> {
let Some(exe_path) = get_game_exe_path(system_info) else {
self.blocked_status = BlockedStatus::Failed;
return Ok(());
};
self.block_generic(Mode::EntireGame(exe_path.to_path_buf()))?;
self.blocked_status = Self::is_exe_blocked()?.into();
self.blocked = Self::is_exe_blocked()?;
Ok(())
}
pub fn unblock_exe(&mut self) -> Result<(), Box<dyn Error>> {
self.unblock_generic(FILTER_NAME_EXE)?;
self.blocked_status = Self::is_exe_blocked()?.into();
self.blocked = Self::is_exe_blocked()?;
Ok(())
}
@@ -150,13 +124,13 @@ impl GameNetworking {
pub fn block_save_server(&mut self, save_server_ip: &str) -> Result<(), Box<dyn Error>> {
self.block_generic(Mode::SaveServer(save_server_ip.to_owned()))?;
self.blocked_status = Self::is_save_server_blocked()?.into();
self.blocked = Self::is_save_server_blocked()?;
Ok(())
}
pub fn unblock_save_server(&mut self) -> Result<(), Box<dyn Error>> {
self.unblock_generic(FILTER_NAME_SAVE_SERVER)?;
self.blocked_status = Self::is_save_server_blocked()?.into();
self.blocked = Self::is_save_server_blocked()?;
Ok(())
}
@@ -164,34 +138,20 @@ impl GameNetworking {
Self::is_blocked_generic(FILTER_NAME_SAVE_SERVER)
}
pub fn reset_indicator_if_failed(&mut self) {
if self.blocked_status == BlockedStatus::Failed && !self.counting {
self.counting = true;
self.timer = Instant::now();
}
if self.blocked_status == BlockedStatus::Failed
&& self.counting
&& self.timer.elapsed() >= INTERVAL
{
self.counting = false;
self.blocked_status = Self::is_exe_blocked().unwrap().into();
}
}
pub fn ensure_not_both_blocked_simultaneously(&mut self, block_method: BlockMethod) {
match block_method {
BlockMethod::EntireGame => {
if Self::is_save_server_blocked().unwrap() {
// ignoring the return because if this is an error the user can just thug it out at that point
let _ = self.unblock_save_server();
self.blocked_status = Self::is_exe_blocked().unwrap().into();
self.blocked = Self::is_exe_blocked().unwrap();
}
}
BlockMethod::SaveServer => {
if Self::is_exe_blocked().unwrap() {
// ignoring the return because if this is an error the user can just thug it out at that point
let _ = self.unblock_exe();
self.blocked_status = Self::is_save_server_blocked().unwrap().into();
self.blocked = Self::is_save_server_blocked().unwrap();
}
}
}
+5 -2
View File
@@ -164,8 +164,11 @@ impl App {
BlockMethod::SaveServer => ui.label("Rockstar save server access"),
};
ui.add_space(1.0);
ui.create_indicator_dot(self.game_networking.blocked_status);
self.game_networking.reset_indicator_if_failed();
ui.create_indicator_dot(if self.game_networking.blocked {
colours::RED
} else {
colours::GREEN
});
label
});
ui.allocate_ui_with_layout(
+5 -5
View File
@@ -2,7 +2,6 @@ use crate::{
gui::{
app::{App, WINDOW_SIZE},
tools,
ui_ext::UiExt,
},
util::consts::{
game::{EXE_ENHANCED, EXE_LEGACY},
@@ -30,11 +29,12 @@ impl App {
ui.label(format!("focused: \"{current_title}\""));
});
ui.horizontal(|ui| {
ui.label("blocked_status");
egui::ComboBox::from_id_salt("blocked_status")
.selected_text(self.game_networking.blocked_status.to_string())
ui.label("blocked");
egui::ComboBox::from_id_salt("blocked")
.selected_text(self.game_networking.blocked.to_string())
.show_ui(ui, |ui| {
ui.build_menu(&mut self.game_networking.blocked_status);
ui.selectable_value(&mut self.game_networking.blocked, true, "true");
ui.selectable_value(&mut self.game_networking.blocked, false, "false");
});
});
if ui.add(egui::Button::new("force refresh theme")).clicked() {
-12
View File
@@ -15,20 +15,8 @@ pub mod game {
}
pub mod colours {
use crate::features::game_networking::BlockedStatus;
use eframe::egui;
pub const RED: egui::Color32 = egui::Color32::from_rgb(255, 96, 96);
pub const YELLOW: egui::Color32 = egui::Color32::from_rgb(255, 255, 96);
pub const GREEN: egui::Color32 = egui::Color32::from_rgb(96, 255, 96);
impl From<BlockedStatus> for egui::Color32 {
fn from(value: BlockedStatus) -> Self {
match value {
BlockedStatus::Blocked => RED,
BlockedStatus::Failed => YELLOW,
BlockedStatus::Unblocked => GREEN,
}
}
}
}