Initial commit: battlenet-manager tray daemon and CLI
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
use anyhow::{Context, Result};
|
||||
use directories::ProjectDirs;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
/// Wine prefix directory
|
||||
pub prefix_dir: PathBuf,
|
||||
/// GE-Proton version string passed to PROTONPATH ("GE-Proton" tracks latest)
|
||||
pub proton_version: String,
|
||||
/// umu GAMEID used to look up protonfixes
|
||||
pub gameid: String,
|
||||
/// Directory where GE-Proton versions are installed
|
||||
pub proton_compat_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
let home = home_dir();
|
||||
Self {
|
||||
prefix_dir: home.join("Games/battlenet-umu"),
|
||||
proton_version: "GE-Proton".into(),
|
||||
gameid: "umu-battlenet".into(),
|
||||
proton_compat_dir: home.join(".local/share/Steam/compatibilitytools.d"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn home_dir() -> PathBuf {
|
||||
std::env::var("HOME")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|_| PathBuf::from("/tmp"))
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn config_path() -> Result<PathBuf> {
|
||||
let dirs = ProjectDirs::from("co.aleshym", "", "battlenet-manager")
|
||||
.context("Could not determine config directory")?;
|
||||
Ok(dirs.config_dir().join("config.toml"))
|
||||
}
|
||||
|
||||
/// Load config from disk, creating a default one if it doesn't exist.
|
||||
pub fn load() -> Result<Self> {
|
||||
let path = Self::config_path()?;
|
||||
if !path.exists() {
|
||||
let config = Self::default();
|
||||
config.save().context("Failed to write default config")?;
|
||||
return Ok(config);
|
||||
}
|
||||
let content = std::fs::read_to_string(&path)
|
||||
.with_context(|| format!("Failed to read config from {path:?}"))?;
|
||||
toml::from_str(&content).context("Failed to parse config.toml")
|
||||
}
|
||||
|
||||
/// Write config to disk.
|
||||
pub fn save(&self) -> Result<()> {
|
||||
let path = Self::config_path()?;
|
||||
if let Some(parent) = path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
let content = toml::to_string_pretty(self)?;
|
||||
std::fs::write(&path, content)
|
||||
.with_context(|| format!("Failed to write config to {path:?}"))
|
||||
}
|
||||
|
||||
/// Absolute path to the Battle.net Launcher.exe inside the prefix.
|
||||
pub fn launcher_exe(&self) -> PathBuf {
|
||||
self.prefix_dir
|
||||
.join("drive_c/Program Files (x86)/Battle.net/Battle.net Launcher.exe")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user