|
| 1 | +use std::fs::{File, remove_file}; |
| 2 | +use std::io::Write; |
| 3 | +use std::error::Error; |
| 4 | +use std::process::Command; |
| 5 | +use std::{thread, time}; |
| 6 | +use std::sync::{Arc, atomic}; |
| 7 | +use slint::{SharedString, ToSharedString}; |
| 8 | +use sysinfo::System; |
| 9 | +use crate::{app_data::{SetupData, ProcessData, PROCESSES_DATA_FILE_NAME, SETUP_FILE_NAME, STATUS_LOGIC_BATCH_FILE_NAME, DEFAULT_STATUS_BATCH_FILE_NAME}, json_utils::{write_setup_data_to_json, read_setup_data_from_json}}; |
| 10 | + |
| 11 | +// Create new SetupData and saves it as JSON |
| 12 | +pub fn set_setup(path_to_cli: SharedString, default_status_text: SharedString, |
| 13 | + default_status_emoji: SharedString) -> Result<(), Box<dyn Error>> { |
| 14 | + // Creates a new SetupData instance from the provided parameters |
| 15 | + let setup_data = SetupData::new(path_to_cli.to_string(), |
| 16 | + default_status_text.to_string(), default_status_emoji.to_string()); |
| 17 | + |
| 18 | + // Write it to JSON |
| 19 | + write_setup_data_to_json(SETUP_FILE_NAME, setup_data).expect("Error saving setup in Json in core logic!"); |
| 20 | + |
| 21 | + Ok(()) |
| 22 | +} |
| 23 | + |
| 24 | +// Delete all data files |
| 25 | +pub fn delete_all_data() -> Result<(), Box<dyn Error>> { |
| 26 | + // Remove batch files |
| 27 | + remove_file(STATUS_LOGIC_BATCH_FILE_NAME)?; |
| 28 | + remove_file(DEFAULT_STATUS_BATCH_FILE_NAME)?; |
| 29 | + |
| 30 | + // Remove JSON data files |
| 31 | + remove_file(SETUP_FILE_NAME)?; |
| 32 | + remove_file(PROCESSES_DATA_FILE_NAME)?; |
| 33 | + |
| 34 | + Ok(()) |
| 35 | +} |
| 36 | + |
| 37 | +// Create a batch file that updates the user status |
| 38 | +pub fn make_process_batch(status_text: SharedString, status_emoji: SharedString) -> Result<(), Box<dyn Error>> { |
| 39 | + |
| 40 | + // Create the batch file itself |
| 41 | + let mut file = File::create(STATUS_LOGIC_BATCH_FILE_NAME)?; |
| 42 | + |
| 43 | + // Read SetupData from JSON |
| 44 | + let readed_setup_data = read_setup_data_from_json(SETUP_FILE_NAME).expect("Error reading setup data from Json in core logic!"); |
| 45 | + |
| 46 | + // Get GitHub CLI path from setup data |
| 47 | + let cli_path = readed_setup_data.cli_path; |
| 48 | + |
| 49 | + // Batch file command content |
| 50 | + let bat_message = String::from(format!( |
| 51 | + "@echo off \n cd {} \n gh user-status set --emoji \"{}\" \"{}\"", |
| 52 | + cli_path, status_emoji, status_text)); |
| 53 | + |
| 54 | + // Write commands to the batch file |
| 55 | + file.write_all(bat_message.as_bytes())?; |
| 56 | + |
| 57 | + // Run the batch file using cmd |
| 58 | + let status = Command::new("cmd").args(["/C", STATUS_LOGIC_BATCH_FILE_NAME]).status()?; |
| 59 | + |
| 60 | + // Check if the batch file executed successfully |
| 61 | + if status.success() { |
| 62 | + println!("Process batch ran successfully in core logic"); |
| 63 | + } else { |
| 64 | + println!("Process batch failed to run correctly in core logic"); |
| 65 | + } |
| 66 | + |
| 67 | + // Delete the batch file after execution |
| 68 | + remove_file(STATUS_LOGIC_BATCH_FILE_NAME)?; |
| 69 | + |
| 70 | + Ok(()) |
| 71 | +} |
| 72 | + |
| 73 | +// Set the default user status if no matching process is found (via batch file) |
| 74 | +pub fn set_default_status_batch() -> Result<(), Box<dyn Error>> { |
| 75 | + // Create the batch file |
| 76 | + let mut file = File::create(DEFAULT_STATUS_BATCH_FILE_NAME)?; |
| 77 | + |
| 78 | + // Read SetupData from JSON |
| 79 | + let readed_setup_data = read_setup_data_from_json(SETUP_FILE_NAME).expect("Error reading setup data from Json in core logic!"); |
| 80 | + |
| 81 | + // Get required data |
| 82 | + let cli_path = readed_setup_data.cli_path; |
| 83 | + let default_status_text = readed_setup_data.default_status_text; |
| 84 | + let default_status_emoji = readed_setup_data.default_status_emoji; |
| 85 | + |
| 86 | + // Batch file command content |
| 87 | + let bat_message = String::from(format!( |
| 88 | + "@echo off \n cd {} \n gh user-status set --emoji \"{}\" \"{}\"", |
| 89 | + cli_path, default_status_emoji, default_status_text)); |
| 90 | + |
| 91 | + // Write commands to the batch file |
| 92 | + file.write_all(bat_message.as_bytes())?; |
| 93 | + |
| 94 | + // Run the batch file using cmd |
| 95 | + let status = Command::new("cmd").args(["/C", DEFAULT_STATUS_BATCH_FILE_NAME]).status()?; |
| 96 | + |
| 97 | + // Check if the batch file executed successfully |
| 98 | + if status.success() { |
| 99 | + println!("Default batch ran successfully in core logic"); |
| 100 | + } else { |
| 101 | + println!("Default batch failed to run correctly in core logic"); |
| 102 | + } |
| 103 | + |
| 104 | + // Delete the batch file after execution |
| 105 | + remove_file(DEFAULT_STATUS_BATCH_FILE_NAME)?; |
| 106 | + |
| 107 | + Ok(()) |
| 108 | +} |
| 109 | + |
| 110 | +// Monitor running processes |
| 111 | +#[allow(unused_assignments)] |
| 112 | +pub fn monitor_processes(running: Arc<atomic::AtomicBool>, readed_data: Vec<ProcessData>) -> Result<(), Box<dyn Error>> { |
| 113 | + // Clone the shared running flag(Rust moment) |
| 114 | + let running_clone = running.clone(); |
| 115 | + let running = running_clone.clone(); |
| 116 | + |
| 117 | + // Clone game process data |
| 118 | + let readed_data_clone = readed_data.clone(); |
| 119 | + |
| 120 | + // Start a new thread |
| 121 | + thread::spawn(move || { |
| 122 | + // Initialize system information |
| 123 | + let mut sys = System::new_all(); |
| 124 | + |
| 125 | + // Runs while the flag is set to true |
| 126 | + while running.load(atomic::Ordering::Relaxed) { |
| 127 | + // Refreshes system information |
| 128 | + sys.refresh_all(); |
| 129 | + |
| 130 | + // Check if at least one monitored process is running |
| 131 | + let matching_process_data = readed_data_clone.iter().find(|data| { |
| 132 | + sys.processes().values().any(|p| { |
| 133 | + p.exe() |
| 134 | + .and_then(|path| path.file_name()) |
| 135 | + .and_then(|name| name.to_str()) |
| 136 | + .map(|name| name.to_lowercase() == data.game_file_name.to_lowercase()) |
| 137 | + .unwrap_or(false) |
| 138 | + }) |
| 139 | + }); |
| 140 | + |
| 141 | + // React based on whether a matching process is found |
| 142 | + match matching_process_data { |
| 143 | + Some(process_data) => { |
| 144 | + // Update status according to the running process |
| 145 | + make_process_batch(process_data.status_text.to_shared_string(), |
| 146 | + process_data.status_emoji.to_shared_string()) |
| 147 | + .expect("Error creating batch file in core logic!"); |
| 148 | + }, |
| 149 | + None => { |
| 150 | + // Set default status if no matching process is found |
| 151 | + set_default_status_batch().expect("Error setting default status in core logic!"); |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + // Wait for 10 seconds before the next check |
| 156 | + thread::sleep(time::Duration::from_secs(10)); |
| 157 | + } |
| 158 | + }); |
| 159 | + Ok(()) |
| 160 | +} |
0 commit comments