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}