moonshine-util 0.4.1

Collection of utilities for Bevy
Documentation
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! A multi-purpose tool for validating [`Component`] presence.

use std::marker::PhantomData;

use bevy_ecs::archetype::Archetype;
use bevy_ecs::change_detection::Tick;
use bevy_ecs::component::{ComponentId, Components, Immutable, StorageType};
use bevy_ecs::lifecycle::{ComponentHook, HookContext};
use bevy_ecs::prelude::*;
use bevy_ecs::query::{FilteredAccess, QueryData, ReadOnlyQueryData, WorldQuery};
use bevy_ecs::storage::{Table, TableRow};
use bevy_ecs::world::{unsafe_world_cell::UnsafeWorldCell, DeferredWorld};
use bevy_platform::collections::HashMap;

use crate::Static;

/// A [`QueryData`] decorator which panics if its inner query does not match.
///
/// # Usage
///
/// As a query parameter, this decorator is useful for preventing systems from silently skipping
/// over entities which may erroneously not match the query.
///
/// Consider the following erroneous example:
/// ```
/// use bevy::prelude::*;
///
/// #[derive(Component)]
/// struct A;
///
/// #[derive(Component)]
/// struct B;
///
/// // A and B are always expected to be inserted together:
/// #[derive(Bundle)]
/// struct AB {
///     a: A,
///     b: B,
/// }
///
/// fn bad_system(mut commands: Commands) {
///     commands.spawn(A); // Spawn A without B!
/// }
///
/// fn unsafe_system(q: Query<(&A, &B)>) {
///     for _ in q.iter() {
///         // An instance of `A` does exist.
///         // But because `A` does not exist *with* `B`, this system skips over it silently.
///     }
/// }
/// # bevy_ecs::system::assert_is_system(bad_system);
/// # bevy_ecs::system::assert_is_system(unsafe_system);
/// ````
///
/// This problem can be solved with [`Expect`]:
/// ```
/// # use bevy::prelude::*;
/// # #[derive(Component)] struct A;
/// # #[derive(Component)] struct B;
/// use moonshine_util::expect::Expect;
///
/// fn safe_system(q: Query<(&A, Expect<&B>)>) {
///     for _ in q.iter() {
///        // This system will panic if it finds an instance of `A` without `B`.
///     }
/// }
/// # bevy_ecs::system::assert_is_system(safe_system);
/// ```
///
/// ## Component Requirements
///
/// When used as a [`Component`], this decorator will panic if the given component type `T` does
/// not exist on the entity. This is especially useful as a component requirement:
///
/// ```should_panic
/// # use bevy::prelude::*;
/// # use bevy::ecs::system::RunSystemOnce;
/// use moonshine_util::expect::Expect;
///
/// #[derive(Component)]
/// struct A;
///
/// #[derive(Component)]
/// #[require(Expect<A>)]
/// struct B;
///
/// fn unsafe_system(mut commands: Commands) {
///    commands.spawn(B); // Spawn B without A! This will panic!
/// }
///
/// # bevy_ecs::system::assert_is_system(unsafe_system);
/// # let mut world = World::new();
/// # world.run_system_once(unsafe_system).unwrap();
/// ```
pub struct Expect<T>(PhantomData<T>);

impl<T: Component> Expect<T> {
    fn on_add(mut world: DeferredWorld, ctx: HookContext) {
        world.commands().queue(move |world: &mut World| {
            let expect = world.entity_mut(ctx.entity).take::<Self>().unwrap();
            let entity = world.entity(ctx.entity);
            if world.contains_resource::<ExpectDeferred>() || entity.contains::<ExpectDeferred>() {
                let mut buffer = world.get_resource_or_init::<ExpectDeferredBuffer>();
                buffer.add(ctx.entity, Box::new(expect));
            } else {
                expect.validate(entity);
            }
        });
    }

    fn validate(self, entity: EntityRef) {
        if !entity.contains::<T>() {
            panic!(
                "expected component of type `{}` does not exist on entity {:?}",
                std::any::type_name::<T>(),
                entity.id()
            );
        }
    }
}

impl<T: Component> Component for Expect<T> {
    const STORAGE_TYPE: StorageType = StorageType::SparseSet;

    type Mutability = Immutable;

    fn on_add() -> Option<ComponentHook> {
        Some(Self::on_add)
    }
}

impl<T: Component> Default for Expect<T> {
    fn default() -> Self {
        Self(Default::default())
    }
}

trait ExpectValidate: Static {
    fn validate(self: Box<Self>, entity: EntityRef);
}

impl<T: Component> ExpectValidate for Expect<T> {
    fn validate(self: Box<Self>, entity: EntityRef) {
        (*self).validate(entity);
    }
}

/// When making many large changes to a world at once (such as when loading a saved world),
/// the execution order of [`Expect`] component requirements is not reliable, leading to false panics.
///
/// This [`Resource`] solves this problem by deferring all [`Expect`] requirement checks until [`expect_deferred`] is called.
///
/// You may also use [`ExpectDeferred`] as a [`Component`]. Doing so will defer all [`Expect`] checks until
/// `ExpectDeferred` is removed from that [`Entity`].
///
/// # Usage
///
/// You may run [`expect_deferred`] as a system, or invoke it manually as required.
///
/// If you are using [Moonshine Save](https://crates.io/crates/moonshine-save),
/// [`LoadWorld`](https://docs.rs/moonshine-save/latest/moonshine_save/load/struct.LoadWorld.html)
/// manages this automatically.
#[derive(Resource, Component, Default)]
#[component(on_remove = Self::on_remove)]
pub struct ExpectDeferred;

impl ExpectDeferred {
    fn on_remove(mut world: DeferredWorld, ctx: HookContext) {
        world.commands().queue(move |world: &mut World| {
            let Some(mut buffer) = world.get_resource_mut::<ExpectDeferredBuffer>() else {
                return;
            };

            let Some(expects) = buffer.0.remove(&ctx.entity) else {
                return;
            };

            let entity = world.entity(ctx.entity);
            for expect in expects {
                expect.validate(entity);
            }
        });
    }
}

#[derive(Resource, Default)]
struct ExpectDeferredBuffer(HashMap<Entity, Vec<Box<dyn ExpectValidate>>>);

impl ExpectDeferredBuffer {
    fn add(&mut self, entity: Entity, expect: Box<dyn ExpectValidate>) {
        self.0.entry(entity).or_default().push(expect);
    }
}

/// Call this to resolve all [`ExpectDeferred`] requirement checks and removes the resource.
///
/// # Usage
///
/// See [`ExpectDeferred`] for usage details.
///
/// If you are using [Moonshine Save](https://crates.io/crates/moonshine-save),
/// [`LoadWorld`](https://docs.rs/moonshine-save/latest/moonshine_save/load/struct.LoadWorld.html)
/// calls this automatically.
pub fn expect_deferred(world: &mut World) {
    let Some(ExpectDeferredBuffer(buffer)) = world.remove_resource::<ExpectDeferredBuffer>() else {
        return;
    };

    for (entity, expects) in buffer {
        let Ok(entity) = world.get_entity(entity) else {
            continue;
        };

        if entity.contains::<ExpectDeferred>() {
            continue;
        }

        for expect in expects {
            expect.validate(entity);
        }
    }

    let _ = world.remove_resource::<ExpectDeferred>();
}

#[doc(hidden)]
pub struct ExpectFetch<'w, T: WorldQuery> {
    fetch: T::Fetch<'w>,
    matches: bool,
}

impl<T: WorldQuery> Clone for ExpectFetch<'_, T> {
    fn clone(&self) -> Self {
        Self {
            fetch: self.fetch.clone(),
            matches: self.matches,
        }
    }
}

unsafe impl<T: QueryData> QueryData for Expect<T> {
    type ReadOnly = Expect<T::ReadOnly>;

    const IS_READ_ONLY: bool = true;

    const IS_ARCHETYPAL: bool = T::IS_ARCHETYPAL;

    type Item<'w, 's> = T::Item<'w, 's>;

    fn shrink<'wlong: 'wshort, 'wshort, 's>(
        item: Self::Item<'wlong, 's>,
    ) -> Self::Item<'wshort, 's> {
        T::shrink(item)
    }

    unsafe fn fetch<'w, 's>(
        state: &'s Self::State,
        fetch: &mut Self::Fetch<'w>,
        entity: Entity,
        table_row: TableRow,
    ) -> Option<Self::Item<'w, 's>> {
        if !fetch.matches {
            panic!(
                "expected query of type `{}` does not match entity {:?}",
                std::any::type_name::<T>(),
                entity
            );
        }
        fetch
            .matches
            .then(|| T::fetch(state, &mut fetch.fetch, entity, table_row))
            .flatten()
    }

    fn iter_access(
        state: &Self::State,
    ) -> impl Iterator<Item = bevy_ecs::query::EcsAccessType<'_>> {
        T::iter_access(state)
    }
}

unsafe impl<T: ReadOnlyQueryData> ReadOnlyQueryData for Expect<T> {}

unsafe impl<T: QueryData> WorldQuery for Expect<T> {
    type Fetch<'w> = ExpectFetch<'w, T>;
    type State = T::State;

    fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
        ExpectFetch {
            fetch: T::shrink_fetch(fetch.fetch),
            matches: fetch.matches,
        }
    }

    const IS_DENSE: bool = T::IS_DENSE;

    #[inline]
    unsafe fn init_fetch<'w>(
        world: UnsafeWorldCell<'w>,
        state: &T::State,
        last_run: Tick,
        this_run: Tick,
    ) -> ExpectFetch<'w, T> {
        ExpectFetch {
            fetch: T::init_fetch(world, state, last_run, this_run),
            matches: false,
        }
    }

    #[inline]
    unsafe fn set_archetype<'w>(
        fetch: &mut ExpectFetch<'w, T>,
        state: &T::State,
        archetype: &'w Archetype,
        table: &'w Table,
    ) {
        fetch.matches = T::matches_component_set(state, &|id| archetype.contains(id));
        if fetch.matches {
            T::set_archetype(&mut fetch.fetch, state, archetype, table);
        }
    }

    #[inline]
    unsafe fn set_table<'w>(fetch: &mut ExpectFetch<'w, T>, state: &T::State, table: &'w Table) {
        fetch.matches = T::matches_component_set(state, &|id| table.has_column(id));
        if fetch.matches {
            T::set_table(&mut fetch.fetch, state, table);
        }
    }

    fn update_component_access(state: &T::State, access: &mut FilteredAccess) {
        let mut intermediate = access.clone();
        T::update_component_access(state, &mut intermediate);
        access.extend_access(&intermediate);
    }

    fn get_state(components: &Components) -> Option<Self::State> {
        T::get_state(components)
    }

    fn init_state(world: &mut World) -> T::State {
        T::init_state(world)
    }

    fn matches_component_set(
        _state: &T::State,
        _set_contains_id: &impl Fn(ComponentId) -> bool,
    ) -> bool {
        true
    }
}

#[cfg(test)]
mod tests {
    use bevy_ecs::system::RunSystemOnce;

    use super::*;

    #[derive(Default, Component)]
    struct A;

    #[derive(Default, Component)]
    struct B;

    #[test]
    #[should_panic]
    fn expect_query_panic() {
        let mut w = World::default();
        w.spawn(A);
        w.run_system_once(|q: Query<(&A, Expect<&B>)>| for _ in q.iter() {})
            .unwrap();
    }

    #[test]
    #[should_panic]
    fn expect_require_panic() {
        #[derive(Component)]
        #[require(Expect<B>)]
        struct C;

        let mut w = World::default();
        w.spawn(C);
    }

    #[test]
    fn expect_deferred() {
        #[derive(Component)]
        #[require(Expect<B>)]
        struct C;

        let mut w = World::default();
        let e = w.spawn((ExpectDeferred, C)).id();
        w.entity_mut(e).insert(B).remove::<ExpectDeferred>();
    }

    #[test]
    #[should_panic]
    fn expect_deferred_panic() {
        #[derive(Component)]
        #[require(Expect<B>)]
        struct C;

        let mut w = World::default();
        let e = w.spawn((ExpectDeferred, C)).id();
        w.entity_mut(e).remove::<ExpectDeferred>();
    }
}