1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use super::*;
#[cfg(test)]
use serial_test_derive::serial;
pub struct Matchmaking<Manager> {
pub(crate) mm: *mut sys::ISteamMatchmaking,
pub(crate) inner: Arc<Inner<Manager>>,
}
const CALLBACK_BASE_ID: i32 = 500;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum LobbyType {
Private,
FriendsOnly,
Public,
Invisible,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LobbyId(pub(crate) u64);
impl LobbyId {
pub fn from_raw(id: u64) -> LobbyId {
LobbyId(id)
}
pub fn raw(&self) -> u64 {
self.0
}
}
impl<Manager> Matchmaking<Manager> {
pub fn request_lobby_list<F>(&self, cb: F)
where
F: FnOnce(SResult<Vec<LobbyId>>) + 'static + Send,
{
unsafe {
let api_call = sys::SteamAPI_ISteamMatchmaking_RequestLobbyList(self.mm);
register_call_result::<sys::LobbyMatchList_t, _, _>(
&self.inner,
api_call,
CALLBACK_BASE_ID + 10,
move |v, io_error| {
cb(if io_error {
Err(SteamError::IOFailure)
} else {
let mut out = Vec::with_capacity(v.m_nLobbiesMatching as usize);
for idx in 0..v.m_nLobbiesMatching {
out.push(LobbyId(sys::SteamAPI_ISteamMatchmaking_GetLobbyByIndex(
sys::SteamAPI_SteamMatchmaking_v009(),
idx as _,
)));
}
Ok(out)
})
},
);
}
}
pub fn create_lobby<F>(&self, ty: LobbyType, max_members: u32, cb: F)
where
F: FnOnce(SResult<LobbyId>) + 'static + Send,
{
assert!(max_members <= 250);
unsafe {
let ty = match ty {
LobbyType::Private => sys::ELobbyType::k_ELobbyTypePrivate,
LobbyType::FriendsOnly => sys::ELobbyType::k_ELobbyTypeFriendsOnly,
LobbyType::Public => sys::ELobbyType::k_ELobbyTypePublic,
LobbyType::Invisible => sys::ELobbyType::k_ELobbyTypeInvisible,
};
let api_call =
sys::SteamAPI_ISteamMatchmaking_CreateLobby(self.mm, ty, max_members as _);
register_call_result::<sys::LobbyCreated_t, _, _>(
&self.inner,
api_call,
CALLBACK_BASE_ID + 13,
move |v, io_error| {
cb(if io_error {
Err(SteamError::IOFailure)
} else if v.m_eResult != sys::EResult::k_EResultOK {
Err(v.m_eResult.into())
} else {
Ok(LobbyId(v.m_ulSteamIDLobby))
})
},
);
}
}
pub fn join_lobby<F>(&self, lobby: LobbyId, cb: F)
where
F: FnOnce(Result<LobbyId, ()>) + 'static + Send,
{
unsafe {
let api_call = sys::SteamAPI_ISteamMatchmaking_JoinLobby(self.mm, lobby.0);
register_call_result::<sys::LobbyEnter_t, _, _>(
&self.inner,
api_call,
CALLBACK_BASE_ID + 4,
move |v, io_error| {
cb(if io_error {
Err(())
} else if v.m_EChatRoomEnterResponse != 1 {
Err(())
} else {
Ok(LobbyId(v.m_ulSteamIDLobby))
})
},
);
}
}
pub fn lobby_data(&self, lobby: LobbyId, key: &str) -> Option<&str> {
let key = CString::new(key).unwrap();
let data = unsafe {
let data = sys::SteamAPI_ISteamMatchmaking_GetLobbyData(self.mm, lobby.0, key.as_ptr());
let data = CStr::from_ptr(data);
data
};
let data = data.to_str().unwrap();
match data.is_empty() {
false => Some(data),
true => None,
}
}
pub fn leave_lobby(&self, lobby: LobbyId) {
unsafe {
sys::SteamAPI_ISteamMatchmaking_LeaveLobby(self.mm, lobby.0);
}
}
pub fn lobby_member_limit(&self, lobby: LobbyId) -> Option<usize> {
unsafe {
let count = sys::SteamAPI_ISteamMatchmaking_GetLobbyMemberLimit(self.mm, lobby.0);
match count {
0 => None,
_ => Some(count as usize),
}
}
}
pub fn lobby_owner(&self, lobby: LobbyId) -> SteamId {
unsafe {
SteamId(sys::SteamAPI_ISteamMatchmaking_GetLobbyOwner(
self.mm, lobby.0,
))
}
}
pub fn lobby_member_count(&self, lobby: LobbyId) -> usize {
unsafe {
let count = sys::SteamAPI_ISteamMatchmaking_GetNumLobbyMembers(self.mm, lobby.0);
count as usize
}
}
pub fn lobby_members(&self, lobby: LobbyId) -> Vec<SteamId> {
unsafe {
let count = sys::SteamAPI_ISteamMatchmaking_GetNumLobbyMembers(self.mm, lobby.0);
let mut members = Vec::with_capacity(count as usize);
for idx in 0..count {
members.push(SteamId(
sys::SteamAPI_ISteamMatchmaking_GetLobbyMemberByIndex(self.mm, lobby.0, idx),
))
}
members
}
}
pub fn set_lobby_joinable(&self, lobby: LobbyId, joinable: bool) -> bool {
unsafe { sys::SteamAPI_ISteamMatchmaking_SetLobbyJoinable(self.mm, lobby.0, joinable) }
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ChatMemberStateChange {
Entered,
Left,
Disconnected,
Kicked,
Banned,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LobbyChatUpdate {
pub lobby: LobbyId,
pub user_changed: SteamId,
pub making_change: SteamId,
pub member_state_change: ChatMemberStateChange,
}
unsafe impl Callback for LobbyChatUpdate {
const ID: i32 = 506;
const SIZE: i32 = ::std::mem::size_of::<sys::LobbyChatUpdate_t>() as i32;
unsafe fn from_raw(raw: *mut c_void) -> Self {
let val = &mut *(raw as *mut sys::LobbyChatUpdate_t);
LobbyChatUpdate {
lobby: LobbyId(val.m_ulSteamIDLobby),
user_changed: SteamId(val.m_ulSteamIDUserChanged),
making_change: SteamId(val.m_ulSteamIDUserChanged),
member_state_change: match val.m_rgfChatMemberStateChange {
x if x == sys::EChatMemberStateChange::k_EChatMemberStateChangeEntered as u32 => {
ChatMemberStateChange::Entered
}
x if x == sys::EChatMemberStateChange::k_EChatMemberStateChangeLeft as u32 => {
ChatMemberStateChange::Left
}
x if x
== sys::EChatMemberStateChange::k_EChatMemberStateChangeDisconnected as u32 =>
{
ChatMemberStateChange::Disconnected
}
x if x == sys::EChatMemberStateChange::k_EChatMemberStateChangeKicked as u32 => {
ChatMemberStateChange::Kicked
}
x if x == sys::EChatMemberStateChange::k_EChatMemberStateChangeBanned as u32 => {
ChatMemberStateChange::Banned
}
_ => unreachable!(),
},
}
}
}
#[test]
#[serial]
fn test_lobby() {
let (client, single) = Client::init().unwrap();
let mm = client.matchmaking();
mm.request_lobby_list(|v| {
println!("List: {:?}", v);
});
mm.create_lobby(LobbyType::Private, 4, |v| {
println!("Create: {:?}", v);
});
for _ in 0..100 {
single.run_callbacks();
::std::thread::sleep(::std::time::Duration::from_millis(100));
}
}