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
//! Extension trait that provides ergonomic methods for registering rollback plugins.
//!
//! [`RollbackApp`] is the primary API for users who want to opt specific
//! [`Component`] or [`Resource`] types into rollback and/or checksum tracking.
//! It is implemented for [`App`] and delegates to the low-level snapshot plugins.
//!
//! # Example
//! ```rust,ignore
//! app.rollback_component_with_clone::<Transform>();
//! app.checksum_component_with_hash::<Health>();
//! ```
use crate::snapshot::{
CloneStrategy, ComponentChecksumPlugin, ComponentMapEntitiesPlugin, ComponentSnapshotPlugin,
ResourceChecksumPlugin, ResourceSnapshotPlugin,
};
use bevy::{
ecs::{
component::{Immutable, Mutable},
entity::MapEntities,
},
prelude::*,
};
use std::hash::Hash;
use super::{
CopyStrategy, ImmutableComponentSnapshotPlugin, ReflectStrategy, ResourceMapEntitiesPlugin,
};
/// Extension trait to ergonomically add rollback plugins to Bevy Apps
pub trait RollbackApp {
/// Registers a component type for saving and loading from the world. This
/// uses [`Copy`] based snapshots for rollback.
fn rollback_component_with_copy<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Mutable> + Copy;
/// Registers an immutable component type for saving and loading from the world. This
/// uses [`Copy`] based snapshots for rollback.
fn rollback_immutable_component_with_copy<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Immutable> + Copy;
/// Registers a resource type for saving and loading from the world. This
/// uses [`Copy`] based snapshots for rollback.
fn rollback_resource_with_copy<Type>(&mut self) -> &mut Self
where
Type: Resource + Copy;
/// Registers a component type for saving and loading from the world. This
/// uses [`Clone`] based snapshots for rollback.
fn rollback_component_with_clone<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Mutable> + Clone;
/// Registers a component type for saving and loading from the world. This
/// uses [`Clone`] based snapshots for rollback.
fn rollback_immutable_component_with_clone<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Immutable> + Clone;
/// Registers a resource type for saving and loading from the world. This
/// uses [`Clone`] based snapshots for rollback.
fn rollback_resource_with_clone<Type>(&mut self) -> &mut Self
where
Type: Resource + Clone;
/// Registers a component type for saving and loading from the world. This
/// uses [`reflection`](`Reflect`) based snapshots for rollback.
///
/// NOTE: Unlike previous versions of `bevy_ggrs`, this will no longer automatically
/// apply entity mapping through the [`MapEntities`](`bevy::ecs::entity::MapEntities`) trait.
/// If you require this behavior, see [`ComponentMapEntitiesPlugin`].
fn rollback_component_with_reflect<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Mutable> + Reflect + FromWorld;
/// Registers an immutable component type for saving and loading from the world. This
/// uses [`reflection`](`Reflect`) based snapshots for rollback.
///
/// NOTE: Unlike previous versions of `bevy_ggrs`, this will no longer automatically
/// apply entity mapping through the [`MapEntities`](`bevy::ecs::entity::MapEntities`) trait.
/// If you require this behavior, see [`ComponentMapEntitiesPlugin`].
fn rollback_immutable_component_with_reflect<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Immutable> + Reflect + FromWorld;
/// Registers a resource type for saving and loading from the world. This
/// uses [`reflection`](`Reflect`) based snapshots for rollback.
///
/// NOTE: Unlike previous versions of `bevy_ggrs`, this will no longer automatically
/// apply entity mapping through the [`MapEntities`](`bevy::ecs::entity::MapEntities`) trait.
/// If you require this behavior, see [`ComponentMapEntitiesPlugin`].
fn rollback_resource_with_reflect<Type>(&mut self) -> &mut Self
where
Type: Resource + Reflect + FromWorld;
/// Adds a component type to the checksum generation pipeline using [`Hash`].
fn checksum_component_with_hash<Type>(&mut self) -> &mut Self
where
Type: Component + Hash;
/// Updates a component after rollback using [`MapEntities`].
fn update_component_with_map_entities<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Mutable> + MapEntities;
/// Adds a resource type to the checksum generation pipeline using [`Hash`].
fn checksum_resource_with_hash<Type>(&mut self) -> &mut Self
where
Type: Resource + Hash;
/// Updates a resource after rollback using [`MapEntities`].
fn update_resource_with_map_entities<Type>(&mut self) -> &mut Self
where
Type: Resource + MapEntities;
/// Adds a component type to the checksum generation pipeline.
fn checksum_component<Type>(&mut self, hasher: for<'a> fn(&'a Type) -> u64) -> &mut Self
where
Type: Component;
/// Adds a resource type to the checksum generation pipeline.
fn checksum_resource<Type>(&mut self, hasher: for<'a> fn(&'a Type) -> u64) -> &mut Self
where
Type: Resource;
/// Registers [`Rollback`](`super::Rollback`) as a required component for `Type`.
/// This is useful for third-party components where you can't add `#[require(Rollback)]`.
fn require_rollback<Type>(&mut self) -> &mut Self
where
Type: Component;
}
impl RollbackApp for App {
fn rollback_component_with_reflect<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Mutable> + Reflect + FromWorld,
{
self.add_plugins(ComponentSnapshotPlugin::<ReflectStrategy<Type>>::default())
}
fn rollback_immutable_component_with_reflect<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Immutable> + Reflect + FromWorld,
{
self.add_plugins(ImmutableComponentSnapshotPlugin::<ReflectStrategy<Type>>::default())
}
fn rollback_resource_with_reflect<Type>(&mut self) -> &mut Self
where
Type: Resource + Reflect + FromWorld,
{
self.add_plugins(ResourceSnapshotPlugin::<ReflectStrategy<Type>>::default())
}
fn rollback_component_with_copy<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Mutable> + Copy,
{
self.add_plugins(ComponentSnapshotPlugin::<CopyStrategy<Type>>::default())
}
fn rollback_immutable_component_with_copy<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Immutable> + Copy,
{
self.add_plugins(ImmutableComponentSnapshotPlugin::<CopyStrategy<Type>>::default())
}
fn rollback_resource_with_copy<Type>(&mut self) -> &mut Self
where
Type: Resource + Copy,
{
self.add_plugins(ResourceSnapshotPlugin::<CopyStrategy<Type>>::default())
}
fn rollback_component_with_clone<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Mutable> + Clone,
{
self.add_plugins(ComponentSnapshotPlugin::<CloneStrategy<Type>>::default())
}
fn rollback_immutable_component_with_clone<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Immutable> + Clone,
{
self.add_plugins(ImmutableComponentSnapshotPlugin::<CloneStrategy<Type>>::default())
}
fn rollback_resource_with_clone<Type>(&mut self) -> &mut Self
where
Type: Resource + Clone,
{
self.add_plugins(ResourceSnapshotPlugin::<CloneStrategy<Type>>::default())
}
fn checksum_component_with_hash<Type>(&mut self) -> &mut Self
where
Type: Component + Hash,
{
self.add_plugins(ComponentChecksumPlugin::<Type>::default())
}
fn update_component_with_map_entities<Type>(&mut self) -> &mut Self
where
Type: Component<Mutability = Mutable> + MapEntities,
{
self.add_plugins(ComponentMapEntitiesPlugin::<Type>::default())
}
fn checksum_resource_with_hash<Type>(&mut self) -> &mut Self
where
Type: Resource + Hash,
{
self.add_plugins(ResourceChecksumPlugin::<Type>::default())
}
fn update_resource_with_map_entities<Type>(&mut self) -> &mut Self
where
Type: Resource + MapEntities,
{
self.add_plugins(ResourceMapEntitiesPlugin::<Type>::default())
}
fn checksum_component<Type>(&mut self, hasher: for<'a> fn(&'a Type) -> u64) -> &mut Self
where
Type: Component,
{
self.add_plugins(ComponentChecksumPlugin::<Type>(hasher))
}
fn checksum_resource<Type>(&mut self, hasher: for<'a> fn(&'a Type) -> u64) -> &mut Self
where
Type: Resource,
{
self.add_plugins(ResourceChecksumPlugin::<Type>(hasher))
}
fn require_rollback<Type>(&mut self) -> &mut Self
where
Type: Component,
{
self.register_required_components::<Type, super::Rollback>();
self
}
}