use std::collections::HashMap;
use std::sync::{Arc, Mutex, MutexGuard};
use crate::backend::{BackendEvent, SteamBackend};
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum FakeCall {
#[cfg(feature = "lobby")]
CreateLobby {
kind: crate::LobbyKind,
max_members: u32,
},
#[cfg(feature = "lobby")]
JoinLobby(u64),
#[cfg(feature = "lobby")]
LeaveLobby(u64),
#[cfg(feature = "lobby")]
SetLobbyData {
lobby: u64,
key: String,
value: String,
},
#[cfg(feature = "lobby")]
SetLobbyJoinable {
lobby: u64,
joinable: bool,
},
#[cfg(feature = "lobby")]
SetRichPresence {
key: String,
value: Option<String>,
},
#[cfg(feature = "lobby")]
ClearRichPresence,
#[cfg(any(feature = "lobby", feature = "friends"))]
InviteToGame {
friend: u64,
connect: String,
},
#[cfg(feature = "stats")]
SetStat {
name: String,
value: crate::StatValue,
},
#[cfg(feature = "stats")]
UnlockAchievement(String),
#[cfg(feature = "stats")]
ClearAchievement(String),
#[cfg(feature = "stats")]
IndicateAchievementProgress {
name: String,
current: u32,
max: u32,
},
#[cfg(feature = "stats")]
StoreStats,
#[cfg(feature = "stats")]
ResetAllStats {
achievements_too: bool,
},
#[cfg(feature = "leaderboards")]
FindLeaderboard {
name: String,
create: bool,
},
#[cfg(feature = "leaderboards")]
UploadScore {
board: u64,
method: crate::UploadMethod,
score: i32,
details: Vec<i32>,
},
#[cfg(feature = "leaderboards")]
DownloadScores {
board: u64,
range: crate::ScoreRange,
},
#[cfg(feature = "auth")]
RequestWebApiTicket {
identity: String,
},
#[cfg(feature = "auth")]
CancelAuthTicket {
op: u64,
},
#[cfg(feature = "friends")]
RequestUserInformation {
id: u64,
name_only: bool,
},
#[cfg(feature = "overlay")]
ActivateOverlay(crate::OpenOverlay),
}
#[derive(Debug)]
pub(crate) struct FakeState {
pub(crate) local_id: u64,
pumps: u64,
pub(crate) calls: Vec<FakeCall>,
pub(crate) queued: Vec<BackendEvent>,
pub(crate) friend_names: HashMap<u64, String>,
launch_command_line: String,
panic_next_pump: bool,
steam_gone: Option<crate::SteamLostReason>,
gone_reported: bool,
#[cfg_attr(not(feature = "friends"), allow(dead_code))]
pub(crate) app_id: u32,
#[cfg(feature = "lobby")]
pub(crate) lobby: crate::lobby::fake::FakeLobbyState,
#[cfg(feature = "stats")]
pub(crate) stats: crate::stats::fake::FakeStatsState,
#[cfg(feature = "leaderboards")]
pub(crate) boards: crate::leaderboards::fake::FakeBoardsState,
#[cfg(feature = "auth")]
pub(crate) auth: crate::auth::fake::FakeAuthState,
#[cfg(feature = "friends")]
pub(crate) friends: crate::friends::fake::FakeFriendsState,
#[cfg(feature = "overlay")]
pub(crate) overlay: crate::overlay::fake::FakeOverlayState,
}
#[derive(Clone, Debug)]
pub struct FakeSteamBackend {
state: Arc<Mutex<FakeState>>,
}
impl Default for FakeSteamBackend {
fn default() -> Self {
Self::new()
}
}
impl FakeSteamBackend {
pub fn new() -> Self {
Self {
state: Arc::new(Mutex::new(FakeState {
local_id: 76_561_197_960_265_729,
pumps: 0,
calls: Vec::new(),
queued: Vec::new(),
friend_names: HashMap::new(),
launch_command_line: String::new(),
panic_next_pump: false,
steam_gone: None,
gone_reported: false,
app_id: 480,
#[cfg(feature = "lobby")]
lobby: crate::lobby::fake::FakeLobbyState::default(),
#[cfg(feature = "stats")]
stats: crate::stats::fake::FakeStatsState::default(),
#[cfg(feature = "leaderboards")]
boards: crate::leaderboards::fake::FakeBoardsState::default(),
#[cfg(feature = "auth")]
auth: crate::auth::fake::FakeAuthState::default(),
#[cfg(feature = "friends")]
friends: crate::friends::fake::FakeFriendsState::default(),
#[cfg(feature = "overlay")]
overlay: crate::overlay::fake::FakeOverlayState::default(),
})),
}
}
pub(crate) fn lock(&self) -> MutexGuard<'_, FakeState> {
self.state.lock().unwrap_or_else(|p| p.into_inner())
}
pub fn calls(&self) -> Vec<FakeCall> {
self.lock().calls.clone()
}
pub fn pump_count(&self) -> u64 {
self.lock().pumps
}
pub fn set_local_id(&self, id: u64) {
self.lock().local_id = id;
}
pub fn push_event(&self, event: BackendEvent) {
self.lock().queued.push(event);
}
pub fn set_friend_name(&self, id: u64, name: &str) {
let mut s = self.lock();
s.friend_names.insert(id, name.to_string());
#[cfg(feature = "friends")]
s.queued.push(BackendEvent::PersonaChanged { steam_id: id, flags: 0x0001 });
}
pub fn set_launch_command_line(&self, text: &str) {
self.lock().launch_command_line = text.to_string();
}
pub fn simulate_steam_exit(&self) {
self.lock().queued.push(BackendEvent::SteamLost { reason: crate::SteamLostReason::SteamExited });
}
pub fn simulate_steam_shutdown(&self) {
self.lock().steam_gone = Some(crate::SteamLostReason::SteamExited);
}
pub fn simulate_steam_process_ended(&self) {
self.lock().steam_gone = Some(crate::SteamLostReason::SteamProcessEnded);
}
#[cfg_attr(
not(any(feature = "lobby", feature = "stats", feature = "leaderboards", feature = "auth", feature = "friends", feature = "overlay")),
allow(dead_code)
)]
fn gone(&self) -> bool {
self.lock().steam_gone.is_some()
}
pub fn panic_in_next_pump(&self) {
self.lock().panic_next_pump = true;
}
pub fn set_app_id(&self, app_id: u32) {
self.lock().app_id = app_id;
}
#[cfg(any(feature = "lobby", feature = "friends"))]
pub fn push_rich_presence_join(&self, from: u64, connect: &str) {
let mut s = self.lock();
#[cfg(feature = "lobby")]
s.queued.push(BackendEvent::RichPresenceJoinRequested { from, connect: connect.to_string() });
#[cfg(feature = "friends")]
s.queued.push(BackendEvent::ConnectRequested { from, connect: connect.to_string() });
}
}
impl SteamBackend for FakeSteamBackend {
fn local_id(&self) -> u64 {
self.lock().local_id
}
fn friend_name(&self, id: u64) -> String {
self.lock().friend_names.get(&id).cloned().unwrap_or_default()
}
fn launch_command_line(&self) -> String {
self.lock().launch_command_line.clone()
}
fn pump(&self) -> Vec<BackendEvent> {
let mut s = self.lock();
s.pumps = s.pumps.saturating_add(1);
if let Some(reason) = s.steam_gone {
s.queued.clear();
if std::mem::replace(&mut s.gone_reported, true) {
return Vec::new();
}
return vec![BackendEvent::SteamLost { reason }];
}
if std::mem::take(&mut s.panic_next_pump) {
s.queued.clear();
drop(s);
panic!("FakeSteamBackend: simulated panic in the pump");
}
std::mem::take(&mut s.queued)
}
#[cfg(feature = "lobby")]
fn lobby(&self) -> Option<&dyn crate::LobbyBackend> {
(!self.gone()).then_some(self)
}
#[cfg(feature = "stats")]
fn stats(&self) -> Option<&dyn crate::StatsBackend> {
(!self.gone()).then_some(self)
}
#[cfg(feature = "leaderboards")]
fn leaderboards(&self) -> Option<&dyn crate::LeaderboardBackend> {
(!self.gone()).then_some(self)
}
#[cfg(feature = "auth")]
fn auth(&self) -> Option<&dyn crate::AuthBackend> {
(!self.gone()).then_some(self)
}
#[cfg(feature = "friends")]
fn friends(&self) -> Option<&dyn crate::FriendsBackend> {
(!self.gone()).then_some(self)
}
#[cfg(feature = "overlay")]
fn overlay(&self) -> Option<&dyn crate::OverlayBackend> {
(!self.gone()).then_some(self)
}
}