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
use bevy_app::{AppBuilder, EventWriter, Plugin};
use bevy_ecs::{schedule::*, system::*};
use bevy_log::error;
use std::sync::{Arc, Mutex};
pub use steamworks::*;
struct SteamEvents<T> {
_callback: CallbackHandle,
pending: Arc<Mutex<Vec<T>>>,
}
pub struct SteamworksPlugin;
impl Plugin for SteamworksPlugin {
fn build(&self, app: &mut AppBuilder) {
if app.world().contains_resource::<Client<ClientManager>>() {
return;
}
match Client::init() {
Err(err) => error!("Failed to initialize Steamworks client: {}", err),
Ok((client, single)) => {
app.insert_resource(client.clone())
.insert_non_send_resource(single)
.add_system(run_steam_callbacks.system().label(STEAM_CALLBACKS));
add_event::<AuthSessionTicketResponse>(app, &client);
add_event::<GameLobbyJoinRequested>(app, &client);
add_event::<P2PSessionConnectFail>(app, &client);
add_event::<P2PSessionRequest>(app, &client);
add_event::<PersonaStateChange>(app, &client);
add_event::<UserAchievementStored>(app, &client);
add_event::<UserStatsReceived>(app, &client);
add_event::<UserStatsStored>(app, &client);
add_event::<ValidateAuthTicketResponse>(app, &client);
}
}
}
}
const STEAM_CALLBACKS: &str = "run_steam_callbacks";
fn run_steam_callbacks(client: NonSend<SingleClient>) {
client.run_callbacks();
}
fn flush_events<T: Send + Sync + 'static>(
events: ResMut<SteamEvents<T>>,
mut output: EventWriter<T>,
) {
let mut pending = events.pending.lock().unwrap();
output.send_batch(pending.drain(0..));
}
fn add_event<T: Callback + Send + Sync + 'static>(
app: &mut AppBuilder,
client: &Client<ClientManager>,
) {
let pending = Arc::new(Mutex::new(Vec::new()));
let pending_in = pending.clone();
let callback = client.register_callback::<T, _>(move |evt| {
pending_in.lock().unwrap().push(evt);
});
let events = SteamEvents::<T> {
_callback: callback,
pending,
};
app.add_event::<T>()
.insert_resource(events)
.add_system(flush_events::<T>.system().after(STEAM_CALLBACKS));
}