DRP_Creative/src/main.rs

138 lines
5.5 KiB
Rust
Raw Normal View History

2022-09-09 20:11:57 +00:00
#![windows_subsystem = "windows"] //UNCOMMENT ONLY WHEN BUILDING FOR RELEASE TO NOT SHOW TERMINAL WINDOW!
2023-05-08 22:30:44 +00:00
#[macro_use]
extern crate self_update;
2022-09-08 18:33:18 +00:00
pub mod app;
pub mod config;
pub mod program_status;
2023-05-08 22:30:44 +00:00
pub mod self_updater;
2022-09-08 18:33:18 +00:00
pub mod tray_icon;
use crate::config::Config;
use crate::program_status::*;
2023-05-08 22:30:44 +00:00
use crate::self_updater::try_update;
2022-09-08 18:33:18 +00:00
use app::{App, Apps};
use discord_rich_presence::{activity, DiscordIpc, DiscordIpcClient};
use std::time::{SystemTime, UNIX_EPOCH};
use std::{thread, time};
fn main() {
2023-05-08 22:30:44 +00:00
match try_update() {
Ok(_) => {}
Err(e) => println!("Failed to update! {e}"),
}
2022-09-08 18:33:18 +00:00
let apps = Apps::construct_apps();
let mut config = Config::load();
let timeout = time::Duration::from_millis(5000);
let (tray_receiver, _tray) = tray_icon::create_tray();
let mut prev_project_name = String::new();
let mut project_name = String::new();
let mut start_time = 0;
let mut drp_is_running = false;
let mut discord_client: Option<DiscordIpcClient> = None;
let mut current_app_option: Option<&App> = None;
'main: loop {
if let Some(current_app) = current_app_option {
if let Some(real_project_name) = is_program_still_running(&current_app) {
//if project name includes filtered words, use default project name
for excluded_word in &config.exclude_keywords_list {
if real_project_name.contains(excluded_word.as_str()) {
project_name = current_app.default_project_name.clone();
} else {
project_name = real_project_name.clone();
}
}
//If discord client isn't connected create and conenct it
if !drp_is_running {
//make a new client with current_app ID
discord_client =
match DiscordIpcClient::new(&current_app.drp_client_id.as_str()) {
Ok(ok) => Some(ok),
Err(_err) => None,
};
if let Some(dc_client) = discord_client.as_mut() {
match dc_client.connect() {
Ok(_ok) => drp_is_running = true,
Err(_err) => drp_is_running = false,
}
} else {
panic!();
}
//only if new app is made refresh the start time
start_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("time went backwards")
.as_secs();
}
//if project name changed, update it
if prev_project_name != project_name {
prev_project_name = project_name.clone();
let details = format!("Working on {}", &project_name.clone());
//update activity
2023-01-29 16:17:04 +00:00
let mut activity = activity::Activity::new()
2022-09-08 18:33:18 +00:00
.details(&details.as_str())
.assets(
activity::Assets::new().large_image(current_app.drp_client_id.as_str()),
)
.timestamps(activity::Timestamps::new().start(start_time as i64));
2023-01-29 16:17:04 +00:00
let state;
if !config.hide_portfolio_row {
state = format!("Portfolio: {}", &config.portfolio_link);
activity = activity.state(state.as_str());
}
2022-09-08 18:33:18 +00:00
//if discord client exists, update status
if let Some(dc) = discord_client.as_mut() {
match dc.set_activity(activity) {
_ => (),
}
}
}
}
//if program no longer running
else {
current_app_option = None;
prev_project_name = "".to_string();
}
//respond to tray icon messages
match tray_receiver.try_recv() {
Ok(msg) => match msg {
tray_icon::Message::AnonymiseProject => {
//only exclude project name if it's not the default one
for app in apps.as_iter() {
if app.kind == current_app.kind {
if app.default_project_name != project_name {
config.exclude_project(&project_name);
}
}
}
}
tray_icon::Message::Quit => break 'main,
},
Err(_err) => (),
}
} else {
//If nothing is running try to find it
if let Some(proj_name) = get_running_program(&apps) {
let temp_curr_app: &App;
(temp_curr_app, project_name) = proj_name;
current_app_option = Some(temp_curr_app);
} else {
current_app_option = None;
}
if drp_is_running {
match discord_client
.as_mut()
.expect("Somehow it dissapeared?")
.close()
{
Ok(_ok) => drp_is_running = false,
Err(_err) => (),
}
}
}
thread::sleep(timeout);
}
}