From b5ed8786597241434ceed915c8eedf83e7d370f5 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 1 Aug 2021 12:34:13 -0400 Subject: [PATCH] change `accounts` to type `Vec>>` to make sure state stays in one place in memory --- src/accountmanager.rs | 15 ++++++++------- src/main.rs | 13 +++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/accountmanager.rs b/src/accountmanager.rs index 7e6ee83..423e03e 100644 --- a/src/accountmanager.rs +++ b/src/accountmanager.rs @@ -1,8 +1,9 @@ use log::*; use serde::{Deserialize, Serialize}; -use std::fs::File; use std::io::{BufReader, Write}; use std::path::Path; +use std::sync::{Arc, Mutex}; +use std::{cell::Cell, fs::File}; use steamguard::SteamGuardAccount; #[derive(Debug, Serialize, Deserialize)] @@ -17,7 +18,7 @@ pub struct Manifest { pub auto_confirm_trades: bool, #[serde(skip)] - pub accounts: Vec, + pub accounts: Vec>>, #[serde(skip)] folder: String, // I wanted to use a Path here, but it was too hard to make it work... } @@ -48,21 +49,21 @@ impl Manifest { let file = File::open(path)?; let reader = BufReader::new(file); let account: SteamGuardAccount = serde_json::from_reader(reader)?; - self.accounts.push(account); + self.accounts.push(Arc::new(Mutex::new(account))); } Ok(()) } - pub fn add_account(&mut self, account: &SteamGuardAccount) { + pub fn add_account(&mut self, account: SteamGuardAccount) { debug!("adding account to manifest: {}", account.account_name); let steamid = account.session.clone().unwrap().steam_id; - self.accounts.push(account.clone()); self.entries.push(ManifestEntry { - filename: format!("{}.maFile", account.account_name), + filename: format!("{}.maFile", &account.account_name), steam_id: steamid, encryption_iv: None, encryption_salt: None, }); + self.accounts.push(Arc::new(Mutex::new(account))); } pub fn save(&self) -> anyhow::Result<()> { @@ -72,7 +73,7 @@ impl Manifest { ); for (entry, account) in self.entries.iter().zip(&self.accounts) { debug!("saving {}", entry.filename); - let serialized = serde_json::to_string(&account)?; + let serialized = serde_json::to_string(account.as_ref())?; ensure!( serialized.len() > 2, "Something extra weird happened and the account was serialized into nothing." diff --git a/src/main.rs b/src/main.rs index 1822b10..679f069 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use std::collections::HashSet; use std::{ io::{stdin, stdout, Write}, path::Path, + sync::{Arc, Mutex}, }; use steamguard::{steamapi, Confirmation, ConfirmationType, SteamGuardAccount}; use termion::{ @@ -122,12 +123,12 @@ fn main() { if matches.is_present("setup") { info!("setup"); let mut linker = accountlinker::AccountLinker::new(); - do_login(&mut linker.account); + // do_login(&mut linker.account); // linker.link(linker.account.session.expect("no login session")); return; } - let mut selected_accounts: Vec = vec![]; + let mut selected_accounts: Vec>> = vec![]; if matches.is_present("all") { // manifest.accounts.iter().map(|a| selected_accounts.push(a.b)); for account in &manifest.accounts { @@ -139,7 +140,7 @@ fn main() { selected_accounts.push(account.clone()); break; } - if matches.value_of("username").unwrap() == account.account_name { + if matches.value_of("username").unwrap() == account.lock().unwrap().account_name { selected_accounts.push(account.clone()); break; } @@ -150,14 +151,14 @@ fn main() { "selected accounts: {:?}", selected_accounts .iter() - .map(|a| a.account_name.clone()) + .map(|a| a.lock().unwrap().account_name.clone()) .collect::>() ); if let Some(trade_matches) = matches.subcommand_matches("trade") { info!("trade"); for a in selected_accounts.iter_mut() { - let mut account = a; // why is this necessary? + let mut account = a.lock().unwrap(); info!("Checking for trade confirmations"); let confirmations: Vec; @@ -205,7 +206,7 @@ fn main() { let server_time = steamapi::get_server_time(); for account in selected_accounts { trace!("{:?}", account); - let code = account.generate_code(server_time); + let code = account.lock().unwrap().generate_code(server_time); println!("{}", code); } }