steamworks 0.13.1

Provides rust friendly bindings to the steamworks sdk
Documentation
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use std::net::Ipv4Addr;
use std::ptr;
use std::rc::Rc;
use std::time::Duration;

use super::*;

macro_rules! matchmaking_servers_callback {
    (
        $name:ident;
        $self:ident;
        ($($additional_name:ident : $additional_type:ty where $additional_content:block),*);
        $(
            $fn_name:ident($clear_after_call:tt): ( $( $fn_arg_name:ident: $cpp_fn_arg:ty => $rust_fn_arg:ty where $normalize:tt ),* )
        ),*
    ) => {
        paste::item! {
            $(
                #[allow(unused_variables)]
                extern "C" fn [<$name:lower _ $fn_name _virtual>]($self: *mut [<$name CallbacksReal>] $(, $fn_arg_name: $cpp_fn_arg)*) {
                    unsafe {
                        $(
                            #[allow(unused_parens)]
                            let [<$fn_arg_name _norm>]: $rust_fn_arg = $normalize;
                        )*
                        // In case of dropping rust_callbacks inside $fn_name
                        let rc_fn = Rc::clone(&(*(*$self).rust_callbacks).$fn_name);
                        (*rc_fn)($([<$fn_arg_name _norm>]),*);
                        $clear_after_call;
                    }
                }
            )*

            pub struct [<$name Callbacks>] {
                $(
                    pub (self) $fn_name: Rc<Box<dyn Fn($($rust_fn_arg),*)>>,
                )*
                $(
                    pub (self) $additional_name: $additional_type,
                )*
            }

            impl [<$name Callbacks>] {
                // Arc can also be here, without Box
                pub fn new($($fn_name: Box<dyn Fn($($rust_fn_arg),*)>),*) -> Self {
                    Self {
                        $($fn_name: Rc::new($fn_name),)*
                        $($additional_name: $additional_content,)*
                    }
                }
            }

            #[repr(C)]
            struct [<$name CallbacksReal>] {
                pub vtable: *mut [<$name CallbacksVirtual>],
                pub rust_callbacks: *mut [<$name Callbacks>],
            }

            #[repr(C)]
            struct [<$name CallbacksVirtual>] {
                $(
                    pub $fn_name: extern "C" fn(*mut [<$name CallbacksReal>] $(, $cpp_fn_arg)*)
                ),*
            }

            unsafe fn [<create_ $name:lower>](rust_callbacks: [<$name Callbacks>]) -> *mut [<$name CallbacksReal>] {
                let rust_callbacks = Box::into_raw(Box::new(rust_callbacks));
                let vtable = Box::into_raw(Box::new([<$name CallbacksVirtual>] {
                    $(
                        $fn_name: [<$name:lower _ $fn_name _virtual>]
                    ),*
                }));
                let real = Box::into_raw(Box::new([<$name CallbacksReal>] {
                    vtable,
                    rust_callbacks,
                }));

                real
            }

            unsafe fn [<free_ $name:lower>](real: *mut [<$name CallbacksReal>]) {
                drop(Box::from_raw((*real).rust_callbacks));
                drop(Box::from_raw((*real).vtable));
                drop(Box::from_raw(real));
            }
        }
    };
}

macro_rules! gen_server_list_fn {
    (
        $name:ident, $sys_method:ident
    ) => {
        /// # Usage
        ///
        /// Request must be released at the end of using. For more details see [`ServerListRequest::release`]
        ///
        /// # Arguments
        ///
        /// * app_id: The app to request the server list of.
        /// * filters: An array of filters to only retrieve servers the user cares about.
        /// A list of the keys & values can be found
        /// [here](https://partner.steamgames.com/doc/api/ISteamMatchmakingServers#MatchMakingKeyValuePair_t).
        ///
        /// # Errors
        ///
        /// Every filter's key and value must take 255 bytes or under, otherwise `Err` is returned.
        pub fn $name<ID: Into<AppId>>(
            &self,
            app_id: ID,
            filters: &HashMap<&str, &str>,
            callbacks: ServerListCallbacks,
        ) -> Result<Arc<Mutex<ServerListRequest>>, ()> {
            let app_id = app_id.into().0;
            let mut filters = {
                let mut vec = Vec::with_capacity(filters.len());
                for i in filters {
                    let key_bytes = i.0.as_bytes();
                    let value_bytes = i.1.as_bytes();

                    // Max length is 255, so 256th byte will always be nul-terminator
                    if key_bytes.len() >= 256 || value_bytes.len() >= 256 {
                        return Err(());
                    }

                    let mut key = [0 as c_char; 256];
                    let mut value = [0 as c_char; 256];

                    unsafe {
                        key.as_mut_ptr()
                            .copy_from(key_bytes.as_ptr().cast(), key_bytes.len());
                        value
                            .as_mut_ptr()
                            .copy_from(value_bytes.as_ptr().cast(), value_bytes.len());
                    }

                    vec.push(sys::MatchMakingKeyValuePair_t {
                        m_szKey: key,
                        m_szValue: value,
                    });
                }
                vec.shrink_to_fit();

                vec
            };

            unsafe {
                let callbacks = create_serverlist(callbacks);

                let request_arc = ServerListRequest::get(callbacks);
                let mut request = request_arc.lock().unwrap();

                let handle = sys::$sys_method(
                    self.mms,
                    app_id,
                    &mut filters.as_mut_ptr().cast(),
                    filters.len().try_into().unwrap(),
                    callbacks.cast(),
                );

                request.mms = self.mms;
                request.real = callbacks;
                request.h_req = handle;

                drop(request);

                Ok(request_arc)
            }
        }
    };
}

pub struct GameServerItem {
    pub appid: u32,
    pub players: i32,
    pub do_not_refresh: bool,
    pub successful_response: bool,
    pub have_password: bool,
    pub secure: bool,
    pub bot_players: i32,
    pub ping: Duration,
    pub max_players: i32,
    pub server_version: i32,
    pub steamid: u64,
    pub last_time_played: Duration,
    pub addr: Ipv4Addr,
    pub query_port: u16,
    pub connection_port: u16,
    pub game_description: String,
    pub server_name: String,
    pub game_dir: String,
    pub map: String,
    pub tags: String,
}

impl GameServerItem {
    unsafe fn from_ptr(raw: *const sys::gameserveritem_t) -> Self {
        let raw = *raw;
        Self {
            appid: raw.m_nAppID,
            players: raw.m_nPlayers,
            bot_players: raw.m_nBotPlayers,
            ping: Duration::from_millis(raw.m_nPing.try_into().unwrap()),
            max_players: raw.m_nMaxPlayers,
            server_version: raw.m_nServerVersion,
            steamid: raw.m_steamID.m_steamid.m_unAll64Bits,

            do_not_refresh: raw.m_bDoNotRefresh,
            successful_response: raw.m_bHadSuccessfulResponse,
            have_password: raw.m_bPassword,
            secure: raw.m_bSecure,

            addr: Ipv4Addr::from(raw.m_NetAdr.m_unIP),
            query_port: raw.m_NetAdr.m_usQueryPort,
            connection_port: raw.m_NetAdr.m_usConnectionPort,

            game_description: CStr::from_ptr(raw.m_szGameDescription.as_ptr())
                .to_string_lossy()
                .into_owned(),
            server_name: CStr::from_ptr(raw.m_szServerName.as_ptr())
                .to_string_lossy()
                .into_owned(),
            game_dir: CStr::from_ptr(raw.m_szGameDir.as_ptr())
                .to_string_lossy()
                .into_owned(),
            map: CStr::from_ptr(raw.m_szMap.as_ptr())
                .to_string_lossy()
                .into_owned(),
            tags: CStr::from_ptr(raw.m_szGameTags.as_ptr())
                .to_string_lossy()
                .into_owned(),

            last_time_played: Duration::from_secs(raw.m_ulTimeLastPlayed.into()),
        }
    }
}

matchmaking_servers_callback!(
    Ping;
    _self;
    ();
    responded({}): (info: *const sys::gameserveritem_t => GameServerItem where { GameServerItem::from_ptr(info) }),
    failed({ free_ping(_self) }): ()
);

matchmaking_servers_callback!(
    PlayerDetails;
    _self;
    ();
    add_player({}): (
        name: *const std::os::raw::c_char => &CStr where { CStr::from_ptr(name) },
        score: i32 => i32 where {score},
        time_played: f32 => f32 where {time_played}
    ),
    failed({ free_playerdetails(_self) }): (),
    refresh_complete({ free_playerdetails(_self) }): ()
);

matchmaking_servers_callback!(
    ServerRules;
    _self;
    ();
    add_rule({}): (
        rule: *const std::os::raw::c_char => &CStr where { CStr::from_ptr(rule) },
        value: *const std::os::raw::c_char => &CStr where { CStr::from_ptr(value) }
    ),
    failed({ free_serverrules(_self) }): (),
    refresh_complete({ free_serverrules(_self) }): ()
);

matchmaking_servers_callback!(
    ServerList;
    _self;
    (
        req: Arc<Mutex<ServerListRequest>> where {
            Arc::new(Mutex::new(ServerListRequest {
                h_req: ptr::null_mut(),
                released: false,
                mms: ptr::null_mut(),
                real: ptr::null_mut(),
            }))
        }
    );
    responded({}): (
        request: sys::HServerListRequest => Arc<Mutex<ServerListRequest>> where { ServerListRequest::get(_self) },
        server: i32 => i32 where {server}
    ),
    failed({}): (
        request: sys::HServerListRequest => Arc<Mutex<ServerListRequest>> where { ServerListRequest::get(_self) },
        server: i32 => i32 where {server}
    ),
    refresh_complete({}): (
        request: sys::HServerListRequest => Arc<Mutex<ServerListRequest>> where { ServerListRequest::get(_self) },
        response: ServerResponse => ServerResponse where {response}
    )
);

#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum ServerResponse {
    ServerResponded = 0,
    ServerFailedToRespond = 1,
    NoServersListedOnMasterServer = 2,
}

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum ReleaseError {
    /// Further using methods on this request after `release`
    /// called will always result in `Err(ReleseError::Released)`.
    Released,
    /// Due to wrapper limitations releasing request while query
    /// is still refreshing (`is_refreshing()`) is impossible.
    /// `Err(ReleaseError::Refreshing)` will be returned.
    Refreshing,
}

pub struct ServerListRequest {
    pub(self) h_req: sys::HServerListRequest,
    pub(self) released: bool,
    pub(self) mms: *mut sys::ISteamMatchmakingServers,
    pub(self) real: *mut ServerListCallbacksReal,
}

unsafe impl Send for ServerListRequest {}

impl ServerListRequest {
    pub(self) unsafe fn get(_self: *mut ServerListCallbacksReal) -> Arc<Mutex<Self>> {
        let rust_callbacks = &*(*_self).rust_callbacks;
        Arc::clone(&rust_callbacks.req)
    }

    /// # Usage
    ///
    /// Cancels any pending query on it if there's a pending
    /// query in progress. Releasing all heap allocated
    /// structures used for callbacks. The `refresh_complete`
    /// callback will not be posted when request is released.
    ///
    /// Further using methods on this request after `release`
    /// called will always result in `Err(ReleseError::Released)`.
    ///
    /// Due to wrapper limitations releasing request while query
    /// is still refreshing (`is_refreshing()`) is impossible.
    /// `Err(ReleaseError::Refreshing)` will be returned.
    pub fn release(&mut self) -> Result<(), ReleaseError> {
        unsafe {
            if self.released {
                return Err(ReleaseError::Released);
            }
            if sys::SteamAPI_ISteamMatchmakingServers_IsRefreshing(self.mms, self.h_req) {
                return Err(ReleaseError::Refreshing);
            }

            self.released = true;
            sys::SteamAPI_ISteamMatchmakingServers_ReleaseRequest(self.mms, self.h_req);

            free_serverlist(self.real);

            Ok(())
        }
    }

    fn released(&self) -> Result<(), ()> {
        if self.released {
            Err(())
        } else {
            Ok(())
        }
    }

    /// # Errors
    ///
    /// Err if called on the released request
    pub fn get_server_count(&self) -> Result<i32, ()> {
        unsafe {
            self.released()?;

            Ok(sys::SteamAPI_ISteamMatchmakingServers_GetServerCount(
                self.mms, self.h_req,
            ))
        }
    }

    /// # Errors
    ///
    /// Err if called on the released request
    pub fn get_server_details(&self, server: i32) -> Result<GameServerItem, ()> {
        unsafe {
            self.released()?;

            // Should we then free this pointer?
            let server_item = sys::SteamAPI_ISteamMatchmakingServers_GetServerDetails(
                self.mms, self.h_req, server,
            );

            Ok(GameServerItem::from_ptr(server_item))
        }
    }

    /// # Errors
    ///
    /// Err if called on the released request
    pub fn refresh_query(&self) -> Result<(), ()> {
        unsafe {
            self.released()?;

            sys::SteamAPI_ISteamMatchmakingServers_RefreshQuery(self.mms, self.h_req);

            Ok(())
        }
    }

    /// # Errors
    ///
    /// Err if called on the released request
    pub fn refresh_server(&self, server: i32) -> Result<(), ()> {
        unsafe {
            self.released()?;

            sys::SteamAPI_ISteamMatchmakingServers_RefreshServer(self.mms, self.h_req, server);

            Ok(())
        }
    }

    /// # Errors
    ///
    /// Err if called on the released request
    pub fn is_refreshing(&self) -> Result<bool, ()> {
        unsafe {
            self.released()?;

            Ok(sys::SteamAPI_ISteamMatchmakingServers_IsRefreshing(
                self.mms, self.h_req,
            ))
        }
    }
}

/// Access to the steam MatchmakingServers interface
pub struct MatchmakingServers {
    pub(crate) mms: *mut sys::ISteamMatchmakingServers,
    pub(crate) _inner: Arc<Inner>,
}

impl MatchmakingServers {
    pub fn ping_server(&self, ip: std::net::Ipv4Addr, port: u16, callbacks: PingCallbacks) {
        unsafe {
            let callbacks = create_ping(callbacks);

            sys::SteamAPI_ISteamMatchmakingServers_PingServer(
                self.mms,
                ip.into(),
                port,
                callbacks.cast(),
            );
        }
    }

    pub fn player_details(
        &self,
        ip: std::net::Ipv4Addr,
        port: u16,
        callbacks: PlayerDetailsCallbacks,
    ) {
        unsafe {
            let callbacks = create_playerdetails(callbacks);

            sys::SteamAPI_ISteamMatchmakingServers_PlayerDetails(
                self.mms,
                ip.into(),
                port,
                callbacks.cast(),
            );
        }
    }

    pub fn server_rules(&self, ip: std::net::Ipv4Addr, port: u16, callbacks: ServerRulesCallbacks) {
        unsafe {
            let callbacks = create_serverrules(callbacks);

            sys::SteamAPI_ISteamMatchmakingServers_ServerRules(
                self.mms,
                ip.into(),
                port,
                callbacks.cast(),
            );
        }
    }

    /// # Usage
    ///
    /// Request must be released at the end of using. For more details see [`ServerListRequest::release`]
    ///
    /// # Arguments
    ///
    /// * app_id: The app to request the server list of.
    pub fn lan_server_list<ID: Into<AppId>>(
        &self,
        app_id: ID,
        callbacks: ServerListCallbacks,
    ) -> Arc<Mutex<ServerListRequest>> {
        unsafe {
            let app_id = app_id.into().0;

            let callbacks = create_serverlist(callbacks);

            let request_arc = ServerListRequest::get(callbacks);
            let mut request = request_arc.lock().unwrap();

            let handle = sys::SteamAPI_ISteamMatchmakingServers_RequestLANServerList(
                self.mms,
                app_id,
                callbacks.cast(),
            );

            request.mms = self.mms;
            request.real = callbacks;
            request.h_req = handle;

            drop(request);

            request_arc
        }
    }

    gen_server_list_fn!(
        internet_server_list,
        SteamAPI_ISteamMatchmakingServers_RequestInternetServerList
    );
    gen_server_list_fn!(
        favorites_server_list,
        SteamAPI_ISteamMatchmakingServers_RequestFavoritesServerList
    );
    gen_server_list_fn!(
        history_server_list,
        SteamAPI_ISteamMatchmakingServers_RequestHistoryServerList
    );
    gen_server_list_fn!(
        friends_server_list,
        SteamAPI_ISteamMatchmakingServers_RequestFriendsServerList
    );
}

#[test]
#[serial_test::serial]
fn test_internet_servers() {
    let client = Client::init_app(304930).unwrap();

    let data = std::rc::Rc::new(Mutex::new(0));
    let data2 = std::rc::Rc::clone(&data);
    let data3 = std::rc::Rc::clone(&data);
    let callbacks = ServerListCallbacks::new(
        Box::new(move |list, server| {
            let details = list.lock().unwrap().get_server_details(server).unwrap();
            println!("{} : {}", details.server_name, details.map);
            *data.lock().unwrap() += 1;
        }),
        Box::new(move |_list, _server| {
            *data2.lock().unwrap() += 1;
        }),
        Box::new(move |list, _response| {
            list.lock().unwrap().release().unwrap();
            println!("{}", data3.lock().unwrap());
        }),
    );

    let mut map = HashMap::new();
    map.insert("map", "PEI");
    let _ = client
        .matchmaking_servers()
        .internet_server_list(304930, &map, callbacks)
        .unwrap();

    for _ in 0..2000 {
        client.run_callbacks();
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
}