intuicio_framework_ecs/
processor.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use crate::{
    entity::Entity,
    world::{World, WorldError},
    Component,
};
use intuicio_data::type_hash::TypeHash;
use std::collections::HashMap;

#[derive(Default)]
pub struct WorldProcessor {
    #[allow(clippy::type_complexity)]
    remap_entities:
        HashMap<TypeHash, Box<dyn Fn(*mut u8, WorldProcessorEntityMapping) + Send + Sync>>,
    #[allow(clippy::type_complexity)]
    related_entities: HashMap<TypeHash, Box<dyn Fn(*const u8) -> Vec<Entity> + Send + Sync>>,
    #[allow(clippy::type_complexity)]
    format: HashMap<
        TypeHash,
        Box<dyn Fn(*const u8, &mut std::fmt::Formatter) -> std::fmt::Result + Send + Sync>,
    >,
}

impl WorldProcessor {
    pub fn register_entity_remapping<T: Component>(
        &mut self,
        f: impl Fn(&mut T, WorldProcessorEntityMapping) + Send + Sync + 'static,
    ) {
        self.register_entity_remapping_raw(TypeHash::of::<T>(), move |pointer, mapping| {
            f(unsafe { pointer.cast::<T>().as_mut().unwrap() }, mapping)
        });
    }

    pub fn register_entity_remapping_raw(
        &mut self,
        type_hash: TypeHash,
        f: impl Fn(*mut u8, WorldProcessorEntityMapping) + Send + Sync + 'static,
    ) {
        self.remap_entities.insert(type_hash, Box::new(f));
    }

    pub fn unregister_entity_remapping<T: Component>(&mut self) {
        self.unregister_entity_remapping_raw(TypeHash::of::<T>());
    }

    pub fn unregister_entity_remapping_raw(&mut self, type_hash: TypeHash) {
        self.remap_entities.remove(&type_hash);
    }

    pub fn remap_entities<T>(&self, data: &mut T, mappings: WorldProcessorEntityMapping) {
        unsafe {
            self.remap_entities_raw(TypeHash::of::<T>(), data as *mut T as *mut u8, mappings);
        }
    }

    /// # Safety
    pub unsafe fn remap_entities_raw(
        &self,
        type_hash: TypeHash,
        pointer: *mut u8,
        mappings: WorldProcessorEntityMapping,
    ) {
        if let Some(remapper) = self.remap_entities.get(&type_hash) {
            remapper(pointer, mappings);
        }
    }

    pub fn register_entity_inspector<T: Component>(
        &mut self,
        f: impl Fn(&T) -> Vec<Entity> + Send + Sync + 'static,
    ) {
        self.register_entity_inspector_raw(TypeHash::of::<T>(), move |pointer| {
            f(unsafe { pointer.cast::<T>().as_ref().unwrap() })
        });
    }

    pub fn register_entity_inspector_raw(
        &mut self,
        type_hash: TypeHash,
        f: impl Fn(*const u8) -> Vec<Entity> + Send + Sync + 'static,
    ) {
        self.related_entities.insert(type_hash, Box::new(f));
    }

    pub fn unregister_entity_inspector<T: Component>(&mut self) {
        self.unregister_entity_inspector_raw(TypeHash::of::<T>());
    }

    pub fn unregister_entity_inspector_raw(&mut self, type_hash: TypeHash) {
        self.related_entities.remove(&type_hash);
    }

    pub fn related_entities<T>(&self, data: &T) -> Vec<Entity> {
        unsafe { self.related_entities_raw(TypeHash::of::<T>(), data as *const T as *const u8) }
    }

    /// # Safety
    pub unsafe fn related_entities_raw(
        &self,
        type_hash: TypeHash,
        pointer: *const u8,
    ) -> Vec<Entity> {
        if let Some(inspector) = self.related_entities.get(&type_hash) {
            inspector(pointer)
        } else {
            Default::default()
        }
    }

    pub fn all_related_entities<const LOCKING: bool>(
        &self,
        world: &World,
        entities: impl IntoIterator<Item = Entity>,
        output: &mut Vec<Entity>,
    ) -> Result<(), WorldError> {
        let mut stack = entities.into_iter().collect::<Vec<_>>();
        while let Some(entity) = stack.pop() {
            if !output.contains(&entity) {
                output.push(entity);
                let row = world.row::<LOCKING>(entity)?;
                for type_hash in row.types() {
                    unsafe {
                        let data = row.data(type_hash)?;
                        stack.extend(self.related_entities_raw(type_hash, data));
                    }
                }
            }
        }
        Ok(())
    }

    pub fn register_display_formatter<T: Component + std::fmt::Display>(&mut self) {
        self.register_formatter::<T>(|data, fmt| data.fmt(fmt));
    }

    pub fn register_debug_formatter<T: Component + std::fmt::Debug>(&mut self) {
        self.register_formatter::<T>(|data, fmt| data.fmt(fmt));
    }

    pub fn register_formatter<T: Component>(
        &mut self,
        f: impl Fn(&T, &mut std::fmt::Formatter) -> std::fmt::Result + Send + Sync + 'static,
    ) {
        self.register_formatter_raw(TypeHash::of::<T>(), move |pointer, fmt| {
            f(unsafe { pointer.cast::<T>().as_ref().unwrap() }, fmt)
        });
    }

    pub fn register_formatter_raw(
        &mut self,
        type_hash: TypeHash,
        f: impl Fn(*const u8, &mut std::fmt::Formatter) -> std::fmt::Result + Send + Sync + 'static,
    ) {
        self.format.insert(type_hash, Box::new(f));
    }

    pub fn unregister_formatter<T: Component>(&mut self) {
        self.unregister_formatter_raw(TypeHash::of::<T>());
    }

    pub fn unregister_formatter_raw(&mut self, type_hash: TypeHash) {
        self.format.remove(&type_hash);
    }

    pub fn format_component<'a, T: Component>(
        &'a self,
        data: &'a T,
    ) -> WorldProcessorComponentFormat<'a, T> {
        WorldProcessorComponentFormat {
            processor: self,
            data,
        }
    }

    /// # Safety
    pub unsafe fn format_component_raw(
        &self,
        type_hash: TypeHash,
        pointer: *const u8,
    ) -> WorldProcessorComponentFormatRaw<'_> {
        WorldProcessorComponentFormatRaw {
            processor: self,
            type_hash,
            pointer,
        }
    }

    pub fn format_world<'a, const LOCKING: bool>(
        &'a self,
        world: &'a World,
    ) -> WorldProcessorWorldFormat<'a, LOCKING> {
        WorldProcessorWorldFormat {
            processor: self,
            world,
        }
    }
}

pub struct WorldProcessorEntityMapping<'a> {
    mapping: &'a HashMap<Entity, Entity>,
}

impl<'a> WorldProcessorEntityMapping<'a> {
    pub fn new(mapping: &'a HashMap<Entity, Entity>) -> Self {
        Self { mapping }
    }

    pub fn remap(&self, entity: Entity) -> Entity {
        self.mapping.get(&entity).copied().unwrap_or_default()
    }
}

pub struct WorldProcessorComponentFormat<'a, T: Component> {
    processor: &'a WorldProcessor,
    data: &'a T,
}

impl<T: Component> WorldProcessorComponentFormat<'_, T> {
    pub fn format(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        if let Some(formatter) = self.processor.format.get(&TypeHash::of::<T>()) {
            formatter(self.data as *const T as *const u8, fmt)
        } else {
            write!(fmt, "<MISSING>")
        }
    }
}

impl<T: Component> std::fmt::Debug for WorldProcessorComponentFormat<'_, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

impl<T: Component> std::fmt::Display for WorldProcessorComponentFormat<'_, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

pub struct WorldProcessorComponentFormatRaw<'a> {
    processor: &'a WorldProcessor,
    type_hash: TypeHash,
    pointer: *const u8,
}

impl WorldProcessorComponentFormatRaw<'_> {
    pub fn format(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        if let Some(formatter) = self.processor.format.get(&self.type_hash) {
            formatter(self.pointer, fmt)
        } else {
            write!(fmt, "<MISSING>")
        }
    }
}

impl std::fmt::Debug for WorldProcessorComponentFormatRaw<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

impl std::fmt::Display for WorldProcessorComponentFormatRaw<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

pub struct WorldProcessorWorldFormat<'a, const LOCKING: bool> {
    processor: &'a WorldProcessor,
    world: &'a World,
}

impl<const LOCKING: bool> WorldProcessorWorldFormat<'_, LOCKING> {
    pub fn format(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        fmt.debug_list()
            .entries(self.world.entities().map(|entity| {
                let access = self.world.row::<LOCKING>(entity).unwrap();
                WorldProcessorWorldRowFormat {
                    entity,
                    components: access
                        .types()
                        .map(|type_hash| WorldProcessorWorldColumnFormat {
                            processor: self.processor,
                            type_hash,
                            data: unsafe { access.data(type_hash).unwrap() },
                        })
                        .collect(),
                }
            }))
            .finish()
    }
}

impl<const LOCKING: bool> std::fmt::Debug for WorldProcessorWorldFormat<'_, LOCKING> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

impl<const LOCKING: bool> std::fmt::Display for WorldProcessorWorldFormat<'_, LOCKING> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

struct WorldProcessorWorldRowFormat<'a> {
    entity: Entity,
    components: Vec<WorldProcessorWorldColumnFormat<'a>>,
}

impl WorldProcessorWorldRowFormat<'_> {
    pub fn format(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        fmt.debug_list()
            .entry(&self.entity)
            .entries(self.components.iter())
            .finish()
    }
}

impl std::fmt::Debug for WorldProcessorWorldRowFormat<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

impl std::fmt::Display for WorldProcessorWorldRowFormat<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

struct WorldProcessorWorldColumnFormat<'a> {
    processor: &'a WorldProcessor,
    type_hash: TypeHash,
    data: *const u8,
}

impl WorldProcessorWorldColumnFormat<'_> {
    pub fn format(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        fmt.debug_struct("Column")
            .field("type_hash", &self.type_hash)
            .field("component", unsafe {
                &self
                    .processor
                    .format_component_raw(self.type_hash, self.data)
            })
            .finish()
    }
}

impl std::fmt::Debug for WorldProcessorWorldColumnFormat<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

impl std::fmt::Display for WorldProcessorWorldColumnFormat<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.format(f)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::world::{Relation, World};

    #[test]
    fn test_world_merge() {
        let mut world = World::default();
        world.spawn((10usize,)).unwrap();

        let mut world2 = World::default();
        let a = world2.spawn((42usize,)).unwrap();
        let b = world2.spawn((false, Relation::new((), a))).unwrap();
        world2.spawn((true, Relation::new((), b))).unwrap();

        let mut processor = WorldProcessor::default();
        Relation::<()>::register_to_processor(&mut processor);

        world.merge::<true>(world2, &processor).unwrap();
        let entities = world.entities().collect::<Vec<_>>();
        assert_eq!(entities.len(), 4);
        assert_eq!(*world.component::<true, usize>(entities[0]).unwrap(), 10);
        assert_eq!(*world.component::<true, usize>(entities[1]).unwrap(), 42);
        assert!(!*world.component::<true, bool>(entities[2]).unwrap());
        assert_eq!(
            *world
                .component::<true, Relation<()>>(entities[2])
                .unwrap()
                .iter()
                .map(|(_, entity)| entity)
                .collect::<Vec<_>>(),
            vec![entities[1]]
        );
        assert!(*world.component::<true, bool>(entities[3]).unwrap());
        assert_eq!(
            *world
                .component::<true, Relation<()>>(entities[3])
                .unwrap()
                .iter()
                .map(|(_, entity)| entity)
                .collect::<Vec<_>>(),
            vec![entities[2]]
        );
    }
}