1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
//! A crate which efficiently locates any Steam application on the filesystem, and/or the Steam installation itself. //! //! **This crate supports Windows, macOS and Linux.** //! //! # Using steamlocate //! Simply add to your [Cargo.toml](https://doc.rust-lang.org/cargo/reference/manifest.html) file: //! ```toml //! [dependencies] //! steamlocate = "0.*" //! ``` //! //! To use [steamid-ng](#steamid-ng-support) with steamlocate, add this to your [Cargo.toml](https://doc.rust-lang.org/cargo/reference/manifest.html) file: //! ```toml //! [dependencies] //! steamid-ng = "1.*" //! //! [dependencies.steamlocate] //! version = "0.*" //! features = ["steamid_ng"] //! ``` //! //! # Caching //! All functions in this crate cache their results, meaning you can call them as many times as you like and they will always return the same reference. //! //! If you need to get uncached results, simply instantiate a new [SteamDir](https://docs.rs/steamlocate/*/steamlocate/struct.SteamDir.html). //! //! # steamid-ng Support //! This crate supports [steamid-ng](https://docs.rs/steamid-ng) and can automatically convert [SteamApp::last_user](struct.SteamApp.html#structfield.last_user) to a [SteamID](https://docs.rs/steamid-ng/*/steamid_ng/struct.SteamID.html) for you. //! //! To enable this support, [use the `steamid_ng` Cargo.toml feature](#using-steamlocate). //! //! # Examples //! //! ### Locate the installed Steam directory //! ```rust //! extern crate steamlocate; //! use steamlocate::SteamDir; //! //! match SteamDir::locate() { //! Some(steamdir) => println!("{:#?}", steamdir), //! None => panic!("Couldn't locate Steam on this computer!") //! } //! ``` //! ```ignore //! SteamDir ( //! path: PathBuf: "C:\\Program Files (x86)\\Steam" //! ) //! ``` //! //! ### Locate an installed Steam app by its app ID //! This will locate Garry's Mod anywhere on the filesystem. //! ```rust //! extern crate steamlocate; //! use steamlocate::SteamDir; //! //! let mut steamdir = SteamDir::locate().unwrap(); //! match steamdir.app(&4000) { //! Some(app) => println!("{:#?}", app), //! None => panic!("Couldn't locate Garry's Mod on this computer!") //! } //! ``` //! ```ignore //! SteamApp ( //! appid: u32: 4000, //! path: PathBuf: "C:\\Program Files (x86)\\steamapps\\common\\GarrysMod", //! vdf: <steamy_vdf::Table>, //! name: Some(String: "Garry's Mod"), //! last_user: Some(u64: 76561198040894045) //! ) //! ``` //! //! ### Locate all Steam apps on this filesystem //! ```rust //! extern crate steamlocate; //! use steamlocate::{SteamDir, SteamApp}; //! use std::collections::HashMap; //! //! let mut steamdir = SteamDir::locate().unwrap(); //! let apps: &HashMap<u32, Option<SteamApp>> = steamdir.apps(); //! //! println!("{:#?}", apps); //! ``` //! ```ignore //! { //! 4000: SteamApp ( //! appid: u32: 4000, //! path: PathBuf: "C:\\Program Files (x86)\\steamapps\\common\\GarrysMod", //! vdf: <steamy_vdf::Table>, //! name: Some(String: "Garry's Mod"), //! last_user: Some(u64: 76561198040894045) //! ) //! ... //! } //! ``` //! //! ### Locate all Steam library folders //! ```rust //! extern crate steamlocate; //! use steamlocate::{SteamDir, LibraryFolders}; //! use std::{vec, path::PathBuf}; //! //! let mut steamdir: SteamDir = SteamDir::locate().unwrap(); //! let libraryfolders: &LibraryFolders = steamdir.libraryfolders(); //! let paths: &Vec<PathBuf> = &libraryfolders.paths; //! //! println!("{:#?}", paths); //! ``` //! ```ignore //! { //! "C:\\Program Files (x86)\\Steam\\steamapps", //! "D:\\Steam\\steamapps", //! "E:\\Steam\\steamapps", //! "F:\\Steam\\steamapps", //! ... //! } //! ``` #[cfg(not(any(target_os="windows", target_os="macos", target_os="linux")))] compile_error!("Unsupported operating system!"); #[macro_use] extern crate lazy_static; use std::{collections::HashMap, path::PathBuf}; #[cfg(target_os="windows")] use winreg::{RegKey, enums::{HKEY_LOCAL_MACHINE, KEY_READ}}; #[cfg(not(target_os="windows"))] extern crate dirs; #[doc(hidden)] pub mod steamapp; pub use steamapp::SteamApp; #[doc(hidden)] pub mod libraryfolders; pub use libraryfolders::LibraryFolders; mod steamapps; use steamapps::SteamApps; /// An instance of a Steam installation. /// /// All functions of this struct will cache their results. /// /// If you'd like to dispose of the cache or get uncached results, just instantiate a new `SteamDir`. /// /// # Example /// ```rust /// # use steamlocate::SteamDir; /// let steamdir = SteamDir::locate(); /// println!("{:#?}", steamdir.unwrap()); /// ``` /// ```ignore /// SteamDir ( /// path: "C:\\Program Files (x86)\\Steam" /// ) /// ``` #[derive(Default, Clone, Debug)] pub struct SteamDir { /// The path to the Steam installation directory on this computer. /// /// Example: `C:\Program Files (x86)\Steam` pub path: PathBuf, pub(crate) steam_apps: SteamApps, pub(crate) libraryfolders: LibraryFolders, } impl SteamDir { /// Returns a reference to a `LibraryFolders` instance. /// /// You can then index `LibraryFolders.paths` to get a reference to a `Vec<PathBuf>` of every library folder installed on the file system. /// /// This function will cache its result. pub fn libraryfolders(&mut self) -> &LibraryFolders { let libraryfolders = &mut self.libraryfolders; if !libraryfolders.discovered { libraryfolders.discover(&self.path); } &*libraryfolders } /// Returns a reference to `HashMap<u32, Option<SteamApp>>` of all `SteamApp`s located on this computer. /// /// All `Option<SteamApp>`s in this context will be `Some`, so you can safely `unwrap()` them without panicking. /// /// This function will cache its results and will always return a reference to the same `HashMap`. /// # Example /// ```rust /// # use steamlocate::{SteamDir, SteamApp}; /// # use std::collections::HashMap; /// let mut steamdir = SteamDir::locate().unwrap(); /// let apps: &HashMap<u32, Option<SteamApp>> = steamdir.apps(); /// println!("{:#?}", apps); /// ``` /// ```ignore /// { /// 4000: SteamApp ( /// appid: u32: 4000, /// path: PathBuf: "C:\\Program Files (x86)\\steamapps\\common\\GarrysMod", /// vdf: <steamy_vdf::Table>, /// name: Some(String: "Garry's Mod"), /// last_user: Some(u64: 76561198040894045) // This will be a steamid_ng::SteamID if the "steamid_ng" feature is enabled /// ) /// ... /// } /// ``` pub fn apps(&mut self) -> &HashMap<u32, Option<SteamApp>> { let steam_apps = &mut self.steam_apps; if !steam_apps.discovered { let libraryfolders = &mut self.libraryfolders; if !libraryfolders.discovered { libraryfolders.discover(&self.path); } steam_apps.discover_apps(libraryfolders); } &steam_apps.apps } /// Returns a `Some` reference to a `SteamApp` via its app ID. /// /// If the Steam app is not installed on the system, this will return `None`. /// /// This function will cache its (either `Some` and `None`) result and will always return a reference to the same `SteamApp`. /// /// # Example /// ```rust /// # use steamlocate::SteamDir; /// let mut steamdir = SteamDir::locate().unwrap(); /// let gmod = steamdir.app(&4000); /// println!("{:#?}", gmod.unwrap()); /// ``` /// ```ignore /// SteamApp ( /// appid: u32: 4000, /// path: PathBuf: "C:\\Program Files (x86)\\steamapps\\common\\GarrysMod", /// vdf: <steamy_vdf::Table>, /// name: Some(String: "Garry's Mod"), /// last_user: Some(u64: 76561198040894045) // This will be a steamid_ng::SteamID if the "steamid_ng" feature is enabled /// ) /// ``` pub fn app(&mut self, app_id: &u32) -> Option<&SteamApp> { let steam_apps = &mut self.steam_apps; if !steam_apps.apps.contains_key(app_id) { let libraryfolders = &mut self.libraryfolders; if !libraryfolders.discovered { libraryfolders.discover(&self.path); } if let None = steam_apps.discover_app(libraryfolders, app_id) { steam_apps.apps.insert( *app_id, None ); } } steam_apps.apps.get(app_id).unwrap().as_ref() } /// Locates the Steam installation directory on the filesystem and initializes a `SteamDir` (Windows) /// /// Returns `None` if no Steam installation can be located. #[cfg(target_os="windows")] pub fn locate() -> Option<SteamDir> { // Locating the Steam installation location is a bit more complicated on Windows // Steam's installation location can be found in the registry let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); let installation_regkey = match hklm.open_subkey_with_flags("SOFTWARE\\Wow6432Node\\Valve\\Steam", KEY_READ).or_else(|_| // 32-bit hklm.open_subkey_with_flags("SOFTWARE\\Valve\\Steam", KEY_READ)) // 64-bit { Ok(installation_regkey) => installation_regkey, Err(_) => return None }; // The InstallPath key will contain the full path to the Steam directory let install_path_str: String = match installation_regkey.get_value("InstallPath") { Ok(install_path_str) => install_path_str, Err(_) => return None }; let install_path = PathBuf::from(install_path_str); Some(SteamDir { path: install_path, ..Default::default() }) } /// Locates the Steam installation directory on the filesystem and initializes a `SteamDir` (macOS) /// /// Returns `None` if no Steam installation can be located. #[cfg(target_os="macos")] pub fn locate() -> Option<SteamDir> { // Steam's installation location is pretty easy to find on macOS, as it's always in $USER/Library/Application Support let home_dir = match dirs::home_dir() { Some(home_dir) => home_dir, None => return None }; // Find Library/Application Support/Steam let install_path = home_dir.join("Library/Application Support/Steam"); return match install_path.is_dir() { false => None, true => Some(SteamDir { path: install_path, ..Default::default() }) } } /// Locates the Steam installation directory on the filesystem and initializes a `SteamDir` (Linux) /// /// Returns `None` if no Steam installation can be located. #[cfg(target_os="linux")] pub fn locate() -> Option<SteamDir> { // Steam's installation location is pretty easy to find on Linux, too, thanks to the symlink in $USER let home_dir = match dirs::home_dir() { Some(home_dir) => home_dir, None => return None }; // Find .steam/steam let install_path = home_dir.join(".steam/steam"); return match install_path.is_dir() { false => None, true => Some(SteamDir { path: install_path, ..Default::default() }) } } } #[cfg(test)] mod tests;