Skip to main content

lightyear_steam/
lib.rs

1//! # Lightyear Steam
2//!
3//! This crate provides an integration layer for using Steam's networking sockets
4//! (specifically `steamworks::networking_sockets`) as a transport for Lightyear.
5//!
6//! It handles the setup of Steam P2P connections and wraps them in a way that
7//! can be used by Lightyear's `Link` component. This allows Lightyear to send
8//! and receive messages over the Steam network infrastructure.
9//!
10//! Note: This crate requires the `steamworks` crate and a running Steam client.
11#![cfg_attr(docsrs, feature(doc_cfg))]
12#![no_std]
13
14extern crate alloc;
15#[cfg(feature = "std")]
16extern crate std;
17
18use bevy_app::PreUpdate;
19use bevy_ecs::prelude::Res;
20
21#[cfg(feature = "client")]
22pub mod client;
23#[cfg(all(feature = "server", not(target_family = "wasm")))]
24pub mod server;
25
26#[derive(thiserror::Error, Debug)]
27pub enum SteamError {}
28
29pub mod prelude {
30    pub use crate::SteamAppExt;
31    pub use crate::SteamError;
32    pub use aeronet_steam::SessionConfig;
33    pub use aeronet_steam::SteamworksClient;
34    pub use aeronet_steam::steamworks;
35    pub use aeronet_steam::steamworks::SteamId;
36
37    #[cfg(feature = "client")]
38    pub mod client {
39        pub use crate::client::{SteamClientIo, SteamClientPlugin};
40        pub use aeronet_steam::client::ConnectTarget;
41    }
42
43    #[cfg(all(feature = "server", not(target_family = "wasm")))]
44    pub mod server {
45        pub use crate::server::{SteamServerIo, SteamServerPlugin};
46        pub use aeronet_steam::server::ListenTarget;
47    }
48}
49
50pub trait SteamAppExt {
51    /// Creates a steamworks::Client with the given app_id and adds it to the Bevy app.
52    /// Then insert it as a resource as expected by `aeronet_steam`.
53    ///
54    /// The steam resources need to be inserted before the lightyear plugins
55    fn add_steam_resources(&mut self, app_id: u32) -> &mut Self;
56}
57
58impl SteamAppExt for bevy_app::App {
59    fn add_steam_resources(&mut self, app_id: u32) -> &mut Self {
60        let steam =
61            prelude::steamworks::Client::init_app(app_id).expect("failed to initialize steam");
62        steam.networking_utils().init_relay_network_access();
63
64        self.insert_resource(prelude::SteamworksClient(steam))
65            .add_systems(PreUpdate, |steam: Res<prelude::SteamworksClient>| {
66                steam.run_callbacks();
67            });
68        self
69    }
70}