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();
}
}
}