Add a "owned" version of shortcuts

This commit is contained in:
Philip Kristoffersen
2021-09-05 14:51:13 +02:00
parent 10040bbef6
commit 2b863b2217
+41 -13
View File
@@ -1,12 +1,8 @@
use std::{ use std::{env::{self}, fmt, fs::File, io::Write, path::Path};
env::{self},
fmt,
path::Path,
};
mod egs; mod egs;
use egs::get_egs_manifests; use egs::{get_egs_manifests, ManifestItem};
use std::error::Error; use std::error::Error;
use steam_shortcuts_util::parse_shortcuts; use steam_shortcuts_util::{Shortcut, parse_shortcuts, shortcuts_to_bytes};
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let egs_manifests = get_egs_manifests()?; let egs_manifests = get_egs_manifests()?;
@@ -17,18 +13,51 @@ fn main() -> Result<(), Box<dyn Error>> {
userinfo_shortcuts.iter().for_each(|user| { userinfo_shortcuts.iter().for_each(|user| {
let mut shortcuts = vec![]; let mut shortcuts = vec![];
let mut new_path = user.shortcut_path.clone();
if let Some(shortcut_path) = &user.shortcut_path { if let Some(shortcut_path) = &user.shortcut_path {
//TODO remove unwrap //TODO remove unwrap
let content = std::fs::read(shortcut_path).unwrap(); let content = std::fs::read(shortcut_path).unwrap();
shortcuts = parse_shortcuts(content.as_slice()).unwrap(); shortcuts = parse_shortcuts(content.as_slice())
println!("Found {} shortcuts , for user: {}", shortcuts.len(),user.steam_user_data_folder); .unwrap()
.iter()
.map(|s| s.to_owned())
.collect();
println!(
"Found {} shortcuts , for user: {}",
shortcuts.len(),
user.steam_user_data_folder
);
} else { } else {
println!( println!(
"Did not find a shortcut file for user {}, createing a new", "Did not find a shortcut file for user {}, createing a new",
user.steam_user_data_folder user.steam_user_data_folder
); );
std::fs::create_dir_all(format!("{}/{}", user.steam_user_data_folder, "config")).unwrap();
new_path = Some(format!("{}/{}", user.steam_user_data_folder, "config/shortcuts.vdf"));
} }
let cur_number_of_shortcuts = shortcuts.len();
egs_manifests.iter().enumerate().for_each(|(i, manifest)| {
let exe = format!(
"{}/{}",
manifest.install_location, manifest.launch_executable
);
let shortcut = Shortcut::new(
cur_number_of_shortcuts + i,
manifest.display_name.as_str(),
exe.as_str(),
manifest.install_location.as_str(),
exe.as_str(),
exe.as_str(),
"",
);
shortcuts.push(shortcut.to_owned());
});
let shortcut_refs = shortcuts.iter().map(|f| f.borrow()).collect();
let new_content = shortcuts_to_bytes(&shortcut_refs);
let mut file = File::create(new_path.unwrap()).unwrap();
file.write(new_content.as_slice()).unwrap();
}); });
Ok(()) Ok(())
@@ -99,10 +128,9 @@ fn get_shortcuts_paths() -> Result<Vec<SteamUsersInfo>, Box<dyn Error>> {
let users_info = user_folders let users_info = user_folders
.filter_map(|f| f.ok()) .filter_map(|f| f.ok())
.map(|folder| { .map(|folder| {
let folder_path = folder let folder_path = folder.path();
.path(); let folder_str = folder_path
let folder_str = .to_str()
folder_path.to_str()
.expect("We just checked that this was there"); .expect("We just checked that this was there");
let path = format!("{}//config//shortcuts.vdf", folder_str); let path = format!("{}//config//shortcuts.vdf", folder_str);
let shortcuts_path = Path::new(path.as_str()); let shortcuts_path = Path::new(path.as_str());