diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index c0c48bad..1fbadc52 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2983,14 +2983,14 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" [[package]] name = "plist" -version = "1.5.1" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a4a0cfc5fb21a09dc6af4bf834cf10d4a32fccd9e2ea468c4b1751a097487aa" +checksum = "e5699cc8a63d1aa2b1ee8e12b9ad70ac790d65788cd36101fa37f87ea46c4cef" dependencies = [ "base64 0.21.5", - "indexmap 1.9.3", + "indexmap 2.1.0", "line-wrap", - "quick-xml", + "quick-xml 0.31.0", "serde", "time", ] @@ -3120,6 +3120,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "quick-xml" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1004a344b30a54e2ee58d66a71b32d2db2feb0a31f9a2d302bf0536f15de2a33" +dependencies = [ + "memchr", +] + [[package]] name = "quote" version = "1.0.33" @@ -3260,15 +3269,6 @@ dependencies = [ "bitflags 1.3.2", ] -[[package]] -name = "redox_syscall" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" -dependencies = [ - "bitflags 1.3.2", -] - [[package]] name = "redox_users" version = "0.4.4" @@ -4251,7 +4251,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "006851c9ccefa3c38a7646b8cec804bb429def3da10497bfa977179869c3e8e2" dependencies = [ - "quick-xml", + "quick-xml 0.30.0", "windows 0.51.1", ] diff --git a/src-tauri/src/commands/binaries.rs b/src-tauri/src/commands/binaries.rs index 12ef1aad..5b72d9e5 100644 --- a/src-tauri/src/commands/binaries.rs +++ b/src-tauri/src/commands/binaries.rs @@ -2,11 +2,12 @@ use std::os::windows::process::CommandExt; use std::{ collections::HashMap, + fs::File, + io::prelude::*, path::{Path, PathBuf}, process::Command, + thread, time::Instant, - fs::File, - io::prelude::*, thread, }; use log::{info, warn}; @@ -753,22 +754,19 @@ pub async fn launch_game( let start_time = Instant::now(); // get the start time of the game if let Ok(mut child) = command.spawn() { - // move all playtime tracking to a separate thread thread::spawn(move || { - // start waiting for the game to exit if let Err(err) = child.wait() { log::error!("Error occured when waiting for game to exit: {}", err); return; } - + // once the game exits pass the time the game started to the track_playtine function if let Err(err) = track_playtime(start_time, app_handle) { log::error!("Error occured when tracking playtime: {}", err); - return; + return; } - }); } Ok(()) @@ -778,14 +776,15 @@ fn track_playtime( start_time: std::time::Instant, app_handle: tauri::AppHandle, ) -> Result, CommandError> { - // get the playtime of the session let mut elapsed_time = start_time.elapsed().as_secs(); let config_dir = match path::config_dir() { None => { log::error!("Couldn't determine application config directory"); - return Err(CommandError::BinaryExecution("Couldn't determine application config directory".to_owned())) + return Err(CommandError::BinaryExecution( + "Couldn't determine application config directory".to_owned(), + )); } Some(path) => path, }; @@ -802,7 +801,10 @@ fn track_playtime( if let Err(err) = file.read_to_string(&mut contents) { log::error!("Could not read playtime.txt: {}", err); - return Err(CommandError::BinaryExecution(format!("Could not read playtime.txt: {}", err))); + return Err(CommandError::BinaryExecution(format!( + "Could not read playtime.txt: {}", + err + ))); } // if there is a int parse error and set the existing playtime to 0 @@ -821,13 +823,19 @@ fn track_playtime( // add the new value to the playtime file if let Err(err) = file.write_all(elapsed_time.to_string().as_bytes()) { log::error!("Could not write playtime to file: {}", err); - return Err(CommandError::BinaryExecution(format!("Could not write playtime to file: {}", err))); + return Err(CommandError::BinaryExecution(format!( + "Could not write playtime to file: {}", + err + ))); } // send an event to the front end so that it can refresh the playtime on screen if let Err(err) = app_handle.emit_all("playtimeUpdated", ()) { log::error!("Failed to emit playtimeUpdated event: {}", err); - return Err(CommandError::BinaryExecution(format!("Failed to emit playtimeUpdated event: {}", err))); + return Err(CommandError::BinaryExecution(format!( + "Failed to emit playtimeUpdated event: {}", + err + ))); } Ok(None) diff --git a/src/components/games/GameControls.svelte b/src/components/games/GameControls.svelte index c7752c60..5f99e7f5 100644 --- a/src/components/games/GameControls.svelte +++ b/src/components/games/GameControls.svelte @@ -18,8 +18,8 @@ import { getLaunchGameString, launchGame, openREPL } from "$lib/rpc/binaries"; import { _ } from "svelte-i18n"; import { navigate } from "svelte-navigator"; - import { readTextFile, BaseDirectory } from '@tauri-apps/api/fs'; - import { listen } from '@tauri-apps/api/event'; + import { readTextFile, BaseDirectory } from "@tauri-apps/api/fs"; + import { listen } from "@tauri-apps/api/event"; import { toastStore } from "$lib/stores/ToastStore"; export let activeGame: SupportedGame; @@ -44,10 +44,12 @@ "saves", ); }); - + // get the playtime from the txt file in the launcher config folder async function getPlaytime() { - const playtimeRaw = await readTextFile("playtime.txt", { dir: BaseDirectory.App }); + const playtimeRaw = await readTextFile("playtime.txt", { + dir: BaseDirectory.App, + }); const playtime = formatPlaytime(parseInt(playtimeRaw)); return playtime; } @@ -57,29 +59,29 @@ // calculate the number of hours, minutes, and seconds const hours = Math.floor(playtimeRaw / 3600); const minutes = Math.floor((playtimeRaw % 3600) / 60); - + // initialize the formatted playtime string - let formattedPlaytime = ''; + let formattedPlaytime = ""; // add the hours to the formatted playtime string if (hours > 0) { - formattedPlaytime += `${hours} hour${hours > 1 ? 's' : ''}`; + formattedPlaytime += `${hours} hour${hours > 1 ? "s" : ""}`; } // add the minutes to the formatted playtime string if (minutes > 0) { // add a comma if there are already hours in the formatted playtime string if (formattedPlaytime.length > 0) { - formattedPlaytime += ', '; + formattedPlaytime += ", "; } - formattedPlaytime += `${minutes} minute${minutes > 1 ? 's' : ''}`; + formattedPlaytime += `${minutes} minute${minutes > 1 ? "s" : ""}`; } // return the formatted playtime string return formattedPlaytime; } - let playtime = ''; + let playtime = ""; // run the function and assign the result to the playtime variable when the page first loads getPlaytime().then((result) => { @@ -92,7 +94,6 @@ playtime = result; }); }); -
@@ -102,7 +103,9 @@ {$_(`gameName_${getInternalName(activeGame)}`)} {#if playtime} -

{`Played For ${playtime}`}

+

+ {`Played For ${playtime}`} +

{/if}