Skip to main content

steamkit/steam/steam_client/configuration/
steam_configuration_state.rs

1//! The raw values of a configuration, filled in by [`SteamConfigurationBuilder`](crate::steam::steam_client::configuration::steam_configuration_builder::SteamConfigurationBuilder).
2
3use std::fmt;
4use std::ops::BitOr;
5use std::sync::Arc;
6use std::time::Duration;
7
8use crate::base::generated::steam_language::{EClientPersonaStateFlag, EUniverse};
9use crate::networking::steam3::protocol_type::ProtocolTypes;
10use crate::steam::discovery::i_server_list_provider::IServerListProvider;
11
12use super::i_machine_info_provider::IMachineInfoProvider;
13use super::steam_configuration::HttpClientFactory;
14
15/// A bit mask of [`EClientPersonaStateFlag`]s (the generated enum can only
16/// hold one).
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
18pub struct PersonaStateFlags(i32);
19
20impl PersonaStateFlags {
21    /// The flags requested by default: player name, presence, source id, game
22    /// extra info and last seen.
23    pub const DEFAULT: PersonaStateFlags = PersonaStateFlags(
24        EClientPersonaStateFlag::PlayerName.value()
25            | EClientPersonaStateFlag::Presence.value()
26            | EClientPersonaStateFlag::SourceID.value()
27            | EClientPersonaStateFlag::GameExtraInfo.value()
28            | EClientPersonaStateFlag::LastSeen.value(),
29    );
30
31    /// Creates a mask from raw bits.
32    pub const fn new(bits: i32) -> Self {
33        Self(bits)
34    }
35
36    /// The raw bits.
37    pub const fn bits(self) -> i32 {
38        self.0
39    }
40
41    /// Whether the flag is set.
42    pub const fn contains(self, flag: EClientPersonaStateFlag) -> bool {
43        self.0 & flag.value() != 0
44    }
45
46    /// Sets a flag.
47    pub fn insert(&mut self, flag: EClientPersonaStateFlag) {
48        self.0 |= flag.value();
49    }
50
51    /// The first flag that's set, for APIs that take a single flag.
52    pub fn representative(&self) -> Option<EClientPersonaStateFlag> {
53        EClientPersonaStateFlag::ALL
54            .iter()
55            .copied()
56            .find(|flag| self.contains(*flag))
57    }
58
59    /// The union of two masks.
60    pub const fn union(self, other: Self) -> Self {
61        Self(self.0 | other.0)
62    }
63}
64
65impl BitOr for PersonaStateFlags {
66    type Output = PersonaStateFlags;
67
68    fn bitor(self, rhs: Self) -> Self::Output {
69        self.union(rhs)
70    }
71}
72
73impl From<EClientPersonaStateFlag> for PersonaStateFlags {
74    fn from(flag: EClientPersonaStateFlag) -> Self {
75        Self(flag.value())
76    }
77}
78
79/// The raw values of a [`SteamConfiguration`](crate::steam::steam_client::configuration::steam_configuration::SteamConfiguration).
80#[derive(Clone)]
81pub struct SteamConfigurationState {
82    /// Whether to fetch the server list from the Steam Directory.
83    pub allow_directory_fetch: bool,
84    /// The Steam cell to prefer.
85    pub cell_id: u32,
86    /// The timeout for connecting to Steam servers.
87    pub connection_timeout: Duration,
88    /// The default persona state flags requested for a new friend.
89    pub default_persona_state_flags: PersonaStateFlags,
90    /// The factory that creates HTTP clients.
91    pub http_client_factory: HttpClientFactory,
92    /// The machine info provider used when logging on.
93    pub machine_info_provider: Arc<dyn IMachineInfoProvider>,
94    /// The transports to use when connecting.
95    pub protocol_types: ProtocolTypes,
96    /// The server list provider to use.
97    pub server_list_provider: Arc<dyn IServerListProvider>,
98    /// The universe to connect to.
99    pub universe: EUniverse,
100    /// The base address of the Steam Web API.
101    pub web_api_base_address: reqwest::Url,
102    /// The Web API key attached to requests.
103    pub web_api_key: String,
104}
105
106impl fmt::Debug for SteamConfigurationState {
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        f.debug_struct("SteamConfigurationState")
109            .field("allow_directory_fetch", &self.allow_directory_fetch)
110            .field("cell_id", &self.cell_id)
111            .field("connection_timeout", &self.connection_timeout)
112            .field("default_persona_state_flags", &self.default_persona_state_flags)
113            .field("machine_info_provider", &"Arc<dyn IMachineInfoProvider>")
114            .field("protocol_types", &self.protocol_types)
115            .field("server_list_provider", &"Arc<dyn IServerListProvider>")
116            .field("universe", &self.universe)
117            .field("web_api_base_address", &self.web_api_base_address)
118            .field("web_api_key", &self.web_api_key)
119            .finish()
120    }
121}
122
123impl Default for SteamConfigurationState {
124    fn default() -> Self {
125        Self {
126            allow_directory_fetch: true,
127            cell_id: 0,
128            connection_timeout: Duration::from_secs(5),
129            default_persona_state_flags: PersonaStateFlags::DEFAULT,
130            http_client_factory: super::steam_configuration::default_http_client_factory(),
131            machine_info_provider: crate::util::hardware_utils::MachineInfoProvider::get_default_provider(),
132            protocol_types: ProtocolTypes::TCP | ProtocolTypes::WEB_SOCKET,
133            server_list_provider: Arc::new(crate::steam::discovery::memory_server_list_provider::MemoryServerListProvider::new()),
134            universe: EUniverse::Public,
135            web_api_base_address: super::steam_configuration::default_web_api_base_address(),
136            web_api_key: String::new(),
137        }
138    }
139}