diff options
| author | Matthew Collins <[email protected]> | 2017-09-06 15:51:08 +0100 |
|---|---|---|
| committer | Matthew Collins <[email protected]> | 2017-09-06 15:51:08 +0100 |
| commit | d1be508a5fbee5535593ca246de389f40f1f7b34 (patch) | |
| tree | 70c3235f9db852097b6ec50b7d532342e8d30287 /src/friends.rs | |
| download | steamworks-rs-d1be508a5fbee5535593ca246de389f40f1f7b34.tar.xz steamworks-rs-d1be508a5fbee5535593ca246de389f40f1f7b34.zip | |
Initial commit
Diffstat (limited to 'src/friends.rs')
| -rw-r--r-- | src/friends.rs | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/friends.rs b/src/friends.rs new file mode 100644 index 0000000..b2e6bf8 --- /dev/null +++ b/src/friends.rs @@ -0,0 +1,100 @@ + +use super::*; + +bitflags! { + #[repr(C)] + pub struct FriendFlags: u16 { + const FRIEND_FLAG_NONE = 0x0000; + const FRIEND_FLAG_BLOCKED = 0x0001; + const FRIEND_FLAG_FRIENDSHIP_REQUESTED = 0x0002; + const FRIEND_FLAG_IMMEDIATE = 0x0004; + const FRIEND_FLAG_CLAN_MEMBER = 0x0008; + const FRIEND_FLAG_ON_GAME_SERVER = 0x0010; + // Unused + // Unused + const FRIEND_FLAG_REQUESTING_FRIENDSHIP = 0x0080; + const FRIEND_FLAG_REQUESTING_INFO = 0x0100; + const FRIEND_FLAG_IGNORED = 0x0200; + const FRIEND_FLAG_IGNORED_FRIEND = 0x0400; + // Unused + const FRIEND_FLAG_CHAT_MEMBER = 0x1000; + const FRIEND_FLAG_ALL = 0xFFFF; + } +} + +pub struct Friends { + pub(crate) friends: *mut sys::ISteamFriends, + pub(crate) _client: Rc<ClientInner>, +} + +impl Friends { + pub fn get_friends(&self, flags: FriendFlags) -> Vec<Friend> { + unsafe { + let count = sys::SteamAPI_ISteamFriends_GetFriendCount(self.friends, flags.bits() as _); + let mut friends = Vec::with_capacity(count as usize); + for idx in 0 .. count { + let friend = SteamId(sys::SteamAPI_ISteamFriends_GetFriendByIndex(self.friends, idx, flags.bits() as _)); + friends.push(Friend { + id: friend, + friends: self.friends, + _client: self._client.clone(), + }); + } + + friends + } + } +} + +pub struct Friend { + id: SteamId, + friends: *mut sys::ISteamFriends, + _client: Rc<ClientInner>, +} + +impl Debug for Friend { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "Friend({:?})", self.id) + } +} + +impl Friend { + pub fn id(&self) -> SteamId { + self.id + } + + pub fn name(&self) -> Cow<str> { + unsafe { + let name = sys::SteamAPI_ISteamFriends_GetFriendPersonaName(self.friends, self.id.0); + let name = CStr::from_ptr(name); + name.to_string_lossy() + } + } + + pub fn state(&self) -> FriendState { + unsafe { + let state = sys::SteamAPI_ISteamFriends_GetFriendPersonaState(self.friends, self.id.0); + match state { + sys::PersonaState::Offline => FriendState::Offline, + sys::PersonaState::Online => FriendState::Online, + sys::PersonaState::Busy => FriendState::Busy, + sys::PersonaState::Away => FriendState::Away, + sys::PersonaState::Snooze => FriendState::Snooze, + sys::PersonaState::LookingToPlay => FriendState::LookingToPlay, + sys::PersonaState::LookingToTrade => FriendState::LookingToTrade, + sys::PersonaState::Max => unreachable!(), + } + } + } +} + +#[derive(Clone, Copy, Debug)] +pub enum FriendState { + Offline, + Online, + Busy, + Away, + Snooze, + LookingToTrade, + LookingToPlay, +}
\ No newline at end of file |