mod backend;
pub(crate) mod fake;
#[cfg(feature = "steam")]
pub(crate) mod real;
#[cfg(test)]
mod tests;
pub use backend::{OverlayBackend, StoreFlag};
use bevy_app::{App, First, Update};
use bevy_ecs::prelude::*;
use tracing::{info, warn};
use crate::{is_individual_steam_id64, BackendEvent, PumpedEvents, SteamBackendRes, SteamKitSystems};
const MAX_CONNECT_BYTES: usize = 255;
#[derive(Resource, Debug, Default)]
pub struct SteamOverlay {
active: bool,
enabled: bool,
toggles: u32,
}
impl SteamOverlay {
pub fn is_active(&self) -> bool {
self.active
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn toggles(&self) -> u32 {
self.toggles
}
}
#[derive(Message, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum OpenOverlay {
Dialog {
dialog: String,
},
User {
dialog: String,
steam_id: u64,
},
WebPage {
url: String,
},
Store {
app_id: u32,
flag: StoreFlag,
},
InviteDialog {
lobby: u64,
},
InviteDialogConnect {
connect: String,
},
}
impl OpenOverlay {
pub fn dialog(dialog: impl Into<String>) -> Self {
Self::Dialog { dialog: dialog.into() }
}
pub fn user(dialog: impl Into<String>, steam_id: u64) -> Self {
Self::User { dialog: dialog.into(), steam_id }
}
pub fn web_page(url: impl Into<String>) -> Self {
Self::WebPage { url: url.into() }
}
pub fn store(app_id: u32) -> Self {
Self::Store { app_id, flag: StoreFlag::None }
}
pub fn invite_dialog(lobby: u64) -> Self {
Self::InviteDialog { lobby }
}
pub fn invite_dialog_connect(connect: impl Into<String>) -> Self {
Self::InviteDialogConnect { connect: connect.into() }
}
}
#[derive(Message, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OverlayToggled {
pub active: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OverlayErrorKind {
NoBackend,
InvalidRequest,
Refused,
}
#[derive(Message, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OverlayError {
pub request: OpenOverlay,
pub kind: OverlayErrorKind,
pub message: String,
}
pub(crate) fn build(app: &mut App) {
app.init_resource::<SteamOverlay>()
.add_message::<OpenOverlay>()
.add_message::<OverlayToggled>()
.add_message::<OverlayError>()
.add_systems(First, apply_overlay_events.in_set(SteamKitSystems::Callbacks))
.add_systems(Update, handle_overlay_requests.in_set(SteamKitSystems::Requests));
}
fn apply_overlay_events(pumped: Res<PumpedEvents>, mut state: ResMut<SteamOverlay>, mut toggled: MessageWriter<OverlayToggled>) {
for ev in &pumped.0 {
#[allow(irrefutable_let_patterns)]
let &BackendEvent::OverlayActivated { active } = ev
else {
continue;
};
state.active = active;
state.toggles = state.toggles.wrapping_add(1);
info!(">>> STEAM: overlay {}", if active { "opened" } else { "closed" });
toggled.write(OverlayToggled { active });
}
}
fn invalid(req: &OpenOverlay) -> Option<&'static str> {
let bad = |s: &str| s.is_empty() || s.contains('\0');
match req {
OpenOverlay::Dialog { dialog } if bad(dialog) => Some("empty dialog name or a NUL byte"),
OpenOverlay::User { dialog, .. } if bad(dialog) => Some("empty dialog name or a NUL byte"),
OpenOverlay::User { steam_id, .. } if !is_individual_steam_id64(*steam_id) => Some("not a SteamID64"),
OpenOverlay::WebPage { url } if bad(url) => Some("empty URL or a NUL byte"),
OpenOverlay::InviteDialog { lobby: 0 } => Some("lobby id 0"),
OpenOverlay::InviteDialogConnect { connect } if bad(connect) || connect.len() > MAX_CONNECT_BYTES => {
Some("connect string must be 1..=255 bytes without NUL")
}
_ => None,
}
}
fn handle_overlay_requests(
backend: Option<Res<SteamBackendRes>>,
mut state: ResMut<SteamOverlay>,
mut requests: MessageReader<OpenOverlay>,
mut errors: MessageWriter<OverlayError>,
mut toggled: MessageWriter<OverlayToggled>,
) {
let Some(api) = backend.as_ref().and_then(|b| b.0.overlay()) else {
state.enabled = false;
if state.active {
state.active = false;
state.toggles = state.toggles.wrapping_add(1);
info!(">>> STEAM: overlay closed (backend removed)");
toggled.write(OverlayToggled { active: false });
}
for req in requests.read() {
warn!(">>> STEAM: overlay request: NoBackend");
errors.write(OverlayError { request: req.clone(), kind: OverlayErrorKind::NoBackend, message: "overlay: Steam is not available".into() });
}
return;
};
let enabled = api.overlay_enabled();
if enabled != state.enabled {
state.enabled = enabled;
}
for req in requests.read() {
if let Some(why) = invalid(req) {
warn!(">>> STEAM: overlay request: InvalidRequest ({why})");
errors.write(OverlayError { request: req.clone(), kind: OverlayErrorKind::InvalidRequest, message: why.into() });
continue;
}
if !enabled {
warn!(">>> STEAM: overlay requested while Steam reports it unavailable");
}
if api.open_overlay(req) {
info!(">>> STEAM: overlay request sent");
} else {
warn!(">>> STEAM: overlay request: Refused");
errors.write(OverlayError { request: req.clone(), kind: OverlayErrorKind::Refused, message: "the backend refused the overlay call".into() });
}
}
}