2021-08-01 14:43:18 +02:00
|
|
|
use log::*;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-01 17:20:57 +02:00
|
|
|
use std::io::{BufReader, Write};
|
2021-03-26 00:47:44 +01:00
|
|
|
use std::path::Path;
|
2021-08-01 18:34:13 +02:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use std::{cell::Cell, fs::File};
|
2021-07-31 18:24:49 +02:00
|
|
|
use steamguard::SteamGuardAccount;
|
2021-03-25 22:45:41 +01:00
|
|
|
|
2021-03-26 18:14:59 +01:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-03-25 22:45:41 +01:00
|
|
|
pub struct Manifest {
|
2021-08-01 14:43:18 +02:00
|
|
|
pub encrypted: bool,
|
|
|
|
pub entries: Vec<ManifestEntry>,
|
|
|
|
pub first_run: bool,
|
|
|
|
pub periodic_checking: bool,
|
|
|
|
pub periodic_checking_interval: i32,
|
|
|
|
pub periodic_checking_checkall: bool,
|
|
|
|
pub auto_confirm_market_transactions: bool,
|
|
|
|
pub auto_confirm_trades: bool,
|
2021-03-25 22:45:41 +01:00
|
|
|
|
2021-08-01 14:43:18 +02:00
|
|
|
#[serde(skip)]
|
2021-08-01 18:34:13 +02:00
|
|
|
pub accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
|
2021-08-01 14:43:18 +02:00
|
|
|
#[serde(skip)]
|
|
|
|
folder: String, // I wanted to use a Path here, but it was too hard to make it work...
|
2021-03-25 22:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct ManifestEntry {
|
2021-08-01 14:43:18 +02:00
|
|
|
pub encryption_iv: Option<String>,
|
|
|
|
pub encryption_salt: Option<String>,
|
|
|
|
pub filename: String,
|
|
|
|
#[serde(rename = "steamid")]
|
|
|
|
pub steam_id: u64,
|
2021-03-25 22:45:41 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 00:47:44 +01:00
|
|
|
impl Manifest {
|
2021-08-01 15:47:50 +02:00
|
|
|
pub fn load(path: &Path) -> anyhow::Result<Manifest> {
|
2021-08-01 14:43:18 +02:00
|
|
|
debug!("loading manifest: {:?}", &path);
|
2021-08-01 15:47:50 +02:00
|
|
|
let file = File::open(path)?;
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
let mut manifest: Manifest = serde_json::from_reader(reader)?;
|
|
|
|
manifest.folder = String::from(path.parent().unwrap().to_str().unwrap());
|
|
|
|
return Ok(manifest);
|
2021-08-01 14:43:18 +02:00
|
|
|
}
|
2021-03-26 19:05:54 +01:00
|
|
|
|
2021-08-01 15:47:50 +02:00
|
|
|
pub fn load_accounts(&mut self) -> anyhow::Result<()> {
|
2021-08-01 14:43:18 +02:00
|
|
|
for entry in &self.entries {
|
|
|
|
let path = Path::new(&self.folder).join(&entry.filename);
|
|
|
|
debug!("loading account: {:?}", path);
|
2021-08-01 15:47:50 +02:00
|
|
|
let file = File::open(path)?;
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
let account: SteamGuardAccount = serde_json::from_reader(reader)?;
|
2021-08-01 18:34:13 +02:00
|
|
|
self.accounts.push(Arc::new(Mutex::new(account)));
|
2021-08-01 14:43:18 +02:00
|
|
|
}
|
2021-08-01 15:47:50 +02:00
|
|
|
Ok(())
|
2021-08-01 14:43:18 +02:00
|
|
|
}
|
2021-08-01 17:20:57 +02:00
|
|
|
|
2021-08-01 18:34:13 +02:00
|
|
|
pub fn add_account(&mut self, account: SteamGuardAccount) {
|
2021-08-01 17:20:57 +02:00
|
|
|
debug!("adding account to manifest: {}", account.account_name);
|
|
|
|
let steamid = account.session.clone().unwrap().steam_id;
|
|
|
|
self.entries.push(ManifestEntry {
|
2021-08-01 18:34:13 +02:00
|
|
|
filename: format!("{}.maFile", &account.account_name),
|
2021-08-01 17:20:57 +02:00
|
|
|
steam_id: steamid,
|
|
|
|
encryption_iv: None,
|
|
|
|
encryption_salt: None,
|
|
|
|
});
|
2021-08-01 18:34:13 +02:00
|
|
|
self.accounts.push(Arc::new(Mutex::new(account)));
|
2021-08-01 17:20:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self) -> anyhow::Result<()> {
|
|
|
|
ensure!(
|
|
|
|
self.entries.len() == self.accounts.len(),
|
|
|
|
"Manifest entries don't match accounts."
|
|
|
|
);
|
|
|
|
for (entry, account) in self.entries.iter().zip(&self.accounts) {
|
|
|
|
debug!("saving {}", entry.filename);
|
2021-08-01 18:34:13 +02:00
|
|
|
let serialized = serde_json::to_string(account.as_ref())?;
|
2021-08-01 17:20:57 +02:00
|
|
|
ensure!(
|
|
|
|
serialized.len() > 2,
|
|
|
|
"Something extra weird happened and the account was serialized into nothing."
|
|
|
|
);
|
|
|
|
let path = Path::new(&self.folder).join(&entry.filename);
|
|
|
|
let mut file = File::create(path)?;
|
|
|
|
file.write_all(serialized.as_bytes())?;
|
|
|
|
file.sync_data()?;
|
|
|
|
}
|
|
|
|
debug!("saving manifest");
|
|
|
|
let manifest_serialized = serde_json::to_string(&self)?;
|
|
|
|
let path = Path::new(&self.folder).join("manifest.json");
|
|
|
|
let mut file = File::create(path)?;
|
|
|
|
file.write_all(manifest_serialized.as_bytes())?;
|
|
|
|
file.sync_data()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-26 00:47:44 +01:00
|
|
|
}
|