-
Notifications
You must be signed in to change notification settings - Fork 38
/
lib.rs
391 lines (337 loc) · 12.5 KB
/
lib.rs
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
//! bevy_ggrs is a bevy plugin for the P2P rollback networking library GGRS.
#![forbid(unsafe_code)] // let us try
use bevy::{
ecs::{
schedule::{IntoSystemDescriptor, ShouldRun, Stage},
system::Command,
},
prelude::*,
reflect::{FromType, GetTypeRegistration},
};
use ggrs::{P2PSession, P2PSpectatorSession, PlayerHandle, SessionState, SyncTestSession};
use ggrs_stage::GGRSStage;
use reflect_resource::ReflectResource;
pub(crate) mod ggrs_stage;
pub(crate) mod reflect_resource;
pub(crate) mod world_snapshot;
/// Stage label for the Custom GGRS Stage.
pub const GGRS_UPDATE: &str = "ggrs_update";
/// Stage label for the default internal GGRS System Stage, where all rollback systems will be added to by default.
pub const ROLLBACK_DEFAULT: &str = "rollback_default";
/// Defines the Session that the GGRS Plugin should expect as a resource.
/// Use `with_session_type(type)` to set accordingly.
pub enum SessionType {
SyncTestSession,
P2PSession,
P2PSpectatorSession,
}
impl Default for SessionType {
fn default() -> Self {
SessionType::SyncTestSession
}
}
/// Add this component to all entities you want to be loaded/saved on rollback.
/// The `id` has to be unique. Consider using the `RollbackIdProvider` resource.
pub struct Rollback {
id: u32,
}
impl Rollback {
/// Creates a new rollback tag with the given id.
pub fn new(id: u32) -> Self {
Self { id }
}
/// Returns the rollback id.
pub const fn id(&self) -> u32 {
self.id
}
}
/// Provides unique ids for your Rollback components.
/// When you add the GGRS Plugin, this should be available as a resource.
#[derive(Default)]
pub struct RollbackIdProvider {
next_id: u32,
}
impl RollbackIdProvider {
/// Returns an unused, unique id.
pub fn next_id(&mut self) -> u32 {
if self.next_id >= u32::MAX {
// TODO: do something smart?
panic!("RollbackIdProvider: u32::MAX has been reached.");
}
let ret = self.next_id;
self.next_id += 1;
ret
}
}
fn ggrs_poll_system(
p2p_session: Option<ResMut<P2PSession>>,
spectator_session: Option<ResMut<P2PSpectatorSession>>,
) {
if let Some(mut sess) = p2p_session {
sess.poll_remote_clients();
}
if let Some(mut sess) = spectator_session {
sess.poll_remote_clients();
}
}
/// Provides all functionality for the GGRS p2p rollback networking library.
pub struct GGRSPlugin;
impl Plugin for GGRSPlugin {
fn build(&self, app: &mut App) {
// everything for the GGRS stage, where all rollback systems will be executed
let mut schedule = Schedule::default();
schedule.add_stage(ROLLBACK_DEFAULT, SystemStage::single_threaded());
let mut ggrs_stage = GGRSStage::default();
ggrs_stage.schedule = schedule;
app.add_stage_before(CoreStage::Update, GGRS_UPDATE, ggrs_stage);
// insert a rollback id provider
app.insert_resource(RollbackIdProvider::default());
// insert a system that polls endpoints as often as possible
app.add_system(ggrs_poll_system);
}
}
/// Extension trait for the `App`.
pub trait GGRSApp {
/// Adds the given `ggrs::SyncTestSession` to your app.
fn with_synctest_session(&mut self, sess: SyncTestSession) -> &mut Self;
/// Adds the given `ggrs::P2PSession` to your app.
fn with_p2p_session(&mut self, sess: P2PSession) -> &mut Self;
/// Adds the given `ggrs::P2PSpectatorSession` to your app.
fn with_p2p_spectator_session(&mut self, sess: P2PSpectatorSession) -> &mut Self;
/// Sets the run criteria for the ggrs update, including all rollback systems. If this is never called, the session will never run.
fn with_rollback_run_criteria(
&mut self,
run_criteria: impl System<In = (), Out = ShouldRun>,
) -> &mut Self;
/// Registers a given system as the input system. This system should provide encoded inputs for a given player.
fn with_input_system(
&mut self,
input_fn: impl System<In = PlayerHandle, Out = Vec<u8>>,
) -> &mut Self;
/// Registers a type of component for saving and loading during rollbacks.
fn register_rollback_type<T>(&mut self) -> &mut Self
where
T: GetTypeRegistration + Reflect + Default;
/// Adds a system that is executed as part of the ggrs update.
fn add_rollback_system<Params>(
&mut self,
system: impl IntoSystemDescriptor<Params>,
) -> &mut Self;
/// Adds a system set that is executed as part of the ggrs update.
fn add_rollback_system_set(&mut self, system: SystemSet) -> &mut Self;
/// Adds a system set to a specific stage inside the GGRS schedule.
fn add_rollback_system_set_to_stage(
&mut self,
stage_label: impl StageLabel,
system_set: SystemSet,
) -> &mut Self;
/// Adds a system to a specific stage inside the GGRS schedule.
fn add_rollback_system_to_stage<Params>(
&mut self,
stage_label: impl StageLabel,
system: impl IntoSystemDescriptor<Params>,
) -> &mut Self;
/// Adds a stage into the GGRS schedule.
fn add_stage<S: Stage>(&mut self, label: impl StageLabel, stage: S) -> &mut Self;
/// Adds a stage into the GGRS schedule after another stage inside the GGRS schedule.
fn add_stage_after<S: Stage>(
&mut self,
target: impl StageLabel,
label: impl StageLabel,
stage: S,
) -> &mut Self;
/// Adds a stage into the GGRS schedule before another stage inside the GGRS schedule.
fn add_stage_before<S: Stage>(
&mut self,
target: impl StageLabel,
label: impl StageLabel,
stage: S,
) -> &mut Self;
}
impl GGRSApp for App {
fn with_synctest_session(&mut self, session: SyncTestSession) -> &mut Self {
self.insert_resource(SessionType::SyncTestSession);
self.insert_resource(session);
self
}
fn with_p2p_session(&mut self, session: P2PSession) -> &mut Self {
self.insert_resource(SessionType::P2PSession);
self.insert_resource(session);
self
}
fn with_p2p_spectator_session(&mut self, session: P2PSpectatorSession) -> &mut Self {
self.insert_resource(SessionType::P2PSpectatorSession);
self.insert_resource(session);
self
}
fn with_rollback_run_criteria(
&mut self,
mut run_criteria: impl System<In = (), Out = ShouldRun>,
) -> &mut Self {
run_criteria.initialize(&mut self.world);
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
ggrs_stage.run_criteria = Some(Box::new(run_criteria));
self
}
fn with_input_system(
&mut self,
mut input_system: impl System<In = PlayerHandle, Out = Vec<u8>>,
) -> &mut Self {
input_system.initialize(&mut self.world);
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
ggrs_stage.input_system = Some(Box::new(input_system));
self
}
fn register_rollback_type<T>(&mut self) -> &mut Self
where
T: GetTypeRegistration + Reflect + Default,
{
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
let mut registry = ggrs_stage.type_registry.write();
registry.register::<T>();
let registration = registry.get_mut(std::any::TypeId::of::<T>()).unwrap();
registration.insert(<ReflectComponent as FromType<T>>::from_type());
registration.insert(<ReflectResource as FromType<T>>::from_type());
drop(registry);
self
}
fn add_rollback_system<Params>(
&mut self,
system: impl IntoSystemDescriptor<Params>,
) -> &mut Self {
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
ggrs_stage
.schedule
.add_system_to_stage(ROLLBACK_DEFAULT, system);
self
}
fn add_rollback_system_set(&mut self, system_set: SystemSet) -> &mut Self {
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
ggrs_stage
.schedule
.add_system_set_to_stage(ROLLBACK_DEFAULT, system_set);
self
}
fn add_rollback_system_to_stage<Params>(
&mut self,
stage_label: impl StageLabel,
system: impl IntoSystemDescriptor<Params>,
) -> &mut Self {
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
ggrs_stage.schedule.add_system_to_stage(stage_label, system);
self
}
fn add_rollback_system_set_to_stage(
&mut self,
stage_label: impl StageLabel,
system_set: SystemSet,
) -> &mut Self {
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
ggrs_stage
.schedule
.add_system_set_to_stage(stage_label, system_set);
self
}
fn add_stage<S: Stage>(&mut self, label: impl StageLabel, stage: S) -> &mut Self {
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
ggrs_stage.schedule.add_stage(label, stage);
self
}
fn add_stage_after<S: Stage>(
&mut self,
target: impl StageLabel,
label: impl StageLabel,
stage: S,
) -> &mut Self {
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
ggrs_stage.schedule.add_stage_after(target, label, stage);
self
}
fn add_stage_before<S: Stage>(
&mut self,
target: impl StageLabel,
label: impl StageLabel,
stage: S,
) -> &mut Self {
let ggrs_stage = self
.schedule
.get_stage_mut::<GGRSStage>(&GGRS_UPDATE)
.expect("No GGRSStage found! Did you install the GGRSPlugin?");
ggrs_stage.schedule.add_stage_before(target, label, stage);
self
}
}
pub trait CommandsExt {
fn start_p2p_session(&mut self, session: P2PSession);
fn start_p2p_spectator_session(&mut self, session: P2PSpectatorSession);
fn start_synctest_session(&mut self, session: SyncTestSession);
}
impl CommandsExt for Commands<'_, '_> {
fn start_p2p_session(&mut self, session: P2PSession) {
self.add(StartP2PSessionCommand(session));
}
fn start_p2p_spectator_session(&mut self, session: P2PSpectatorSession) {
self.add(StartP2PSpectatorSessionCommand(session));
}
fn start_synctest_session(&mut self, session: SyncTestSession) {
self.add(StartSyncTestSessionCommand(session));
}
}
struct StartP2PSpectatorSessionCommand(P2PSpectatorSession);
impl Command for StartP2PSessionCommand {
fn write(mut self, world: &mut World) {
// caller is responsible that the session is either already running...
if self.0.current_state() == SessionState::Initializing {
// ...or ready to be started
self.0.start_session().unwrap();
}
world.insert_resource(self.0);
world.insert_resource(SessionType::P2PSession);
}
}
struct StartP2PSessionCommand(P2PSession);
impl Command for StartP2PSpectatorSessionCommand {
fn write(mut self, world: &mut World) {
// caller is responsible that the session is either already running...
if self.0.current_state() == SessionState::Initializing {
// ...or ready to be started
self.0.start_session().unwrap();
}
world.insert_resource(self.0);
world.insert_resource(SessionType::P2PSpectatorSession);
}
}
struct StartSyncTestSessionCommand(SyncTestSession);
impl Command for StartSyncTestSessionCommand {
fn write(self, world: &mut World) {
world.insert_resource(self.0);
world.insert_resource(SessionType::SyncTestSession);
}
}