Skip to content

Commit

Permalink
v0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Leastrio committed Nov 30, 2023
1 parent 4ef516d commit 6ec4124
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.showUnlinkedFileNotification": false
}
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
- Close riot client if dolos closes
- Read which games are installed and only show the ones installed
- Read which games are installed and only show the ones installed
- Toggleable statuses
- Fake user
7 changes: 3 additions & 4 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dolos"
version = "0.1.2"
version = "0.1.3"
description = "Dolos Desktop Application"
default-run = "dolos"
license = "GPLv3"
Expand All @@ -16,7 +16,7 @@ tauri-build = { version = "1.5.0", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.5.2", features = [ "window-close", "updater", "system-tray"] }
tauri = { version = "1.5.2", features = [ "updater", "system-tray", "notification"] }
tokio = { version = "1.34.0", features = ["full"] }
hyper = { version = "1", features = ["full"] }
http-body-util = "0.1"
Expand All @@ -26,8 +26,7 @@ native-tls = "0.2"
tokio-native-tls = "0.3.1"
jsonwebtoken = "9.1.0"
quick-xml = { version = "0.31.0", features = ["async-tokio"] }
sysinfo = "0.29.11"
winapi = "0.3.9"
winapi = { version = "0.3.9", features = ["tlhelp32"] }

[features]
custom-protocol = [ "tauri/custom-protocol" ]
55 changes: 43 additions & 12 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
mod http_proxy;
mod tcp_proxy;
mod utils;
mod sys_tray;
mod state;

use std::{error::Error, env, fs};

use tauri::Manager;
use tokio::{sync::OnceCell, process::Command};
use tauri::api::notification::Notification;

pub static RIOT_CLIENT_PATH: OnceCell<String> = OnceCell::const_new();

Expand All @@ -25,39 +28,67 @@ fn main() {
let open_pids_c = open_pids.clone();

tauri::Builder::default()
.invoke_handler(tauri::generate_handler![launch_game])
.manage(state::DolosState::default())
.invoke_handler(tauri::generate_handler![launch_game, mark_shutdown])
.on_page_load(move |window, _| {
if !open_pids_c.is_empty() {
if open_pids_c.is_some() {
window.eval("showRiotClientPopup()").unwrap();
window.eval(&format!("gamesToClose = {}", open_pids_c.iter().count())).unwrap();
}
})
.system_tray(sys_tray::create_tray())
.on_system_tray_event(sys_tray::handle_event)
.setup(|app| {

let handle = app.handle();
handle.once_global("closeClients", move |_| {
for (pid, name) in open_pids {
utils::kill_process(pid, name)
if let Some(pids) = open_pids {
for (pid, name) in pids {
utils::kill_process(pid, name)
}
}
});

tauri::async_runtime::spawn(async {
tokio::spawn(tcp_proxy::proxy_tcp_chat());
tokio::spawn(http_proxy::listen_http());
});
tauri::async_runtime::spawn(tcp_proxy::proxy_tcp_chat());
tauri::async_runtime::spawn(http_proxy::listen_http());

Ok(())
})
.run(tauri::generate_context!())
.expect("[Dolos] [Main] error while running tauri application");
.build(tauri::generate_context!())
.expect("[Dolos] [Main] error while running tauri application")
.run(|app_handle, event| match event {
tauri::RunEvent::ExitRequested { api, .. } => {
if app_handle.state::<state::DolosState>().shutdown.load(std::sync::atomic::Ordering::Relaxed) {
app_handle.exit(0);
} else {
api.prevent_exit();
let _ = Notification::new(&app_handle.config().tauri.bundle.identifier)
.title("Dolos is running")
.body("Dolos is running in the background! View the tray icon for more options.")
.show();
}
}
_ => {}
});
}

#[tauri::command]
fn launch_game(game: &str) {
async fn launch_game(app: tauri::AppHandle, game: String) -> () {
Command::new(RIOT_CLIENT_PATH.get().unwrap())
.arg(format!("--client-config-url=http://127.0.0.1:{}", http_proxy::HTTP_PORT.get().unwrap()))
.arg(format!("--launch-product={}", game))
.arg("--launch-patchline=live")
.spawn()
.expect("[Dolos] [Main] Could not launch riot client!");

tokio::time::sleep(tokio::time::Duration::from_millis(2500)).await;
app.get_window("main").unwrap().close().unwrap();
let _ = Notification::new(&app.config().tauri.bundle.identifier)
.title("Dolos is running")
.body("Dolos is running in the background setting your status to offline! View the tray icon for more options.")
.show();
}

#[tauri::command]
fn mark_shutdown(state: tauri::State<state::DolosState>) {
state.shutdown.store(true, std::sync::atomic::Ordering::Relaxed);
}
11 changes: 11 additions & 0 deletions src-tauri/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::sync::atomic::AtomicBool;

pub struct DolosState {
pub shutdown: AtomicBool
}

impl Default for DolosState {
fn default() -> Self {
Self { shutdown: AtomicBool::new(false) }
}
}
34 changes: 34 additions & 0 deletions src-tauri/src/sys_tray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use tauri::{SystemTray, CustomMenuItem, SystemTrayMenu,
// SystemTrayMenuItem,
AppHandle, SystemTrayEvent,
// SystemTraySubmenu
};

pub fn create_tray() -> SystemTray {
// let status = SystemTraySubmenu::new("Status", SystemTrayMenu::new().add_item(CustomMenuItem::new("Online".to_string(), "Online").selected()));
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let open = CustomMenuItem::new("open".to_string(), "Open");
let menu = SystemTrayMenu::new()
// .add_submenu(status)
// .add_native_item(SystemTrayMenuItem::Separator)
.add_item(open)
.add_item(quit);
SystemTray::new().with_menu(menu)
}

pub fn handle_event(app: &AppHandle, event: SystemTrayEvent) {
match event {
SystemTrayEvent::MenuItemClick { id, .. } => {
match id.as_ref() {
"open" => {
tauri::WindowBuilder::from_config(app, app.config().tauri.windows.get(0).unwrap().clone()).build().unwrap();
},
"quit" => {
app.exit(0);
},
_ => {}
}
}
_ => {}
}
}
42 changes: 33 additions & 9 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::ffi::CStr;
use serde_json::Value;
use sysinfo::{System, SystemExt, ProcessExt, PidExt};
use winapi::um::{processthreadsapi::{OpenProcess, TerminateProcess}, winnt::PROCESS_ALL_ACCESS, handleapi::CloseHandle, errhandlingapi::GetLastError};
use winapi::um::{processthreadsapi::{OpenProcess, TerminateProcess}, winnt::PROCESS_ALL_ACCESS, handleapi::{CloseHandle, INVALID_HANDLE_VALUE}, errhandlingapi::GetLastError, tlhelp32::{TH32CS_SNAPPROCESS, PROCESSENTRY32, Process32First, Process32Next}};
use winapi::um::tlhelp32::CreateToolhelp32Snapshot;

const PROCESS_NAMES: [&str; 3] = ["RiotClientServices.exe", "LeagueClient.exe", "VALORANT-Win64-Shipping.exe"];

Expand All @@ -16,15 +17,38 @@ pub fn choose_channel(data: Value) -> Option<String> {
None
}

pub fn get_pids() -> Vec<(u32, String)> {
let sys = System::new_all();
sys.processes().iter().filter_map(|(pid, process)| {
if PROCESS_NAMES.contains(&process.name()) {
Some((pid.as_u32(), process.name().to_string()))
} else {
pub fn get_pids() -> Option<Vec<(u32, String)>> {
unsafe {
let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if snapshot == INVALID_HANDLE_VALUE {
eprintln!("[DOLOS] Error creating snapshot of processes");
return None;
}

let mut pids: Vec<(u32, String)> = vec![];
let mut pe: PROCESSENTRY32 = std::mem::zeroed();
pe.dwSize = std::mem::size_of::<PROCESSENTRY32>() as u32;

if Process32First(snapshot, &mut pe) == 0 {
eprintln!("[DOLOS] Error getting process snapshot");
CloseHandle(snapshot);
return None;
}

while Process32Next(snapshot, &mut pe) != 0 {
let process_name = CStr::from_ptr(pe.szExeFile.as_ptr()).to_string_lossy();

if PROCESS_NAMES.contains(&process_name.as_ref()) {
pids.push((pe.th32ProcessID, process_name.to_string()));
}
}
CloseHandle(snapshot);
if pids.is_empty() {
None
} else {
Some(pids)
}
}).collect::<Vec<(u32, String)>>()
}
}

pub fn kill_process(pid: u32, name: String) {
Expand Down
6 changes: 2 additions & 4 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
},
"package": {
"productName": "Dolos",
"version": "0.1.2"
"version": "0.1.3"
},
"tauri": {
"allowlist": {
"window": {
"close": true
}
"all": false
},
"systemTray": {
"iconPath": "icons/icon.png",
Expand Down
10 changes: 8 additions & 2 deletions ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ <h1>
}

document.getElementById('btnNo').addEventListener('click', function() {
hideRiotClientPopup();
const { invoke } = window.__TAURI__.tauri
invoke('mark_shutdown', {})
let pElem = document.getElementById("popup-text")
let yElem = document.getElementById("btnYes")
let nElem = document.getElementById("btnNo")
yElem.style.display = 'none';
nElem.style.display = 'none';
pElem.textContent = "Dolos must launch the Riot Games launcher with a custom configuration to work. Please close Dolos and stop any Riot Games processes and retry."
});

document.getElementById('btnYes').addEventListener('click', function() {
Expand All @@ -106,7 +113,6 @@ <h1>
hideRiotClientPopup();
}, 2500)
});

</script>
</body>
</html>

0 comments on commit 6ec4124

Please sign in to comment.