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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Entity Component System

use std::hash::Hash;

use core::ops::{ Deref, DerefMut };

use crate::application::{ Services, IntoService };

/// Entity structure has only id field and represent an agregation of components
#[derive(Eq, PartialEq, Debug, Hash, Clone, Copy)]
pub struct Entity(u64);

impl From<u64> for Entity {
    fn from(id: u64) -> Self {
        Entity(id)
    }
}

/// Any data structure can be a component
pub trait Component: Send + Sync + 'static {}
impl<T: Send + Sync + 'static> Component for T {}

/// System Priority
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Priority {
    /// Low priority (1024)
    Low,
    /// Normal priority (2048)
    Normal,
    /// High priority (4096)
    High,
    /// User defined value
    Custom(u32),
}

impl From<Priority> for u32 {
    fn from(priority: Priority) -> u32 {
        match priority {
            Priority::Low => 1024,
            Priority::Normal => 2048,
            Priority::High => 4096,
            Priority::Custom(value) => value,
        }
    }
}

/// Wrapper for system functions
pub struct System {
    /// Boxed system data
    pub data: Box<dyn Systemized>,
    /// System run level
    pub run_level: RunLevel,
}

/// Defines when and how often a system should run.
#[derive(Debug, Eq, PartialEq)]
pub enum RunLevel {
    /// Only once on application startup
    Startup,
    /// Begin of rendering request handling
    Bind,
    /// Before rendering stage
    Standard,
    /// Rendering stage
    Render,
    /// End of the rendering request handling
    Release,
    /// Execution after window resizing
    Resize,
}

impl From<&str> for RunLevel {
    fn from(name: &str) -> Self {
        if name.ends_with("::startup") {
            RunLevel::Startup
        } else if name.ends_with("::bind") {
            RunLevel::Bind
        } else if name.ends_with("::render") {
            RunLevel::Render
        } else if name.ends_with("::release") {
            RunLevel::Release
        } else if name.ends_with("::resize") {
            RunLevel::Resize
        } else {
            RunLevel::Standard
        }
    }
}

impl System {
    /// Constructs a system from function at default runlevel
    pub fn from<Fun, Ctx, Srv>(func: Fun) -> Self
    where
        Fun: IntoSystem<Ctx, Srv> + Sync + Send,
        Ctx: Sync + Send,
        Srv: Sync + Send,
    {
        let data = func.into_system();
        let run_level = RunLevel::from(data.name());

        Self {
            data,
            run_level,
        }
    }

    /// Adds an option to the system
    pub fn with<T>(mut self, option: T) -> Self
    where
        Self: SystemOption<T>,
    {
        self.set_option(option);
        self
    }
}

/// System option abstract interface
pub trait SystemOption<T> {
    /// customize the syystem with an option
    fn set_option(&mut self, option: T);
}

/// [`RunLevel`] option implementation
impl SystemOption<RunLevel> for System {
    fn set_option(&mut self, option: RunLevel) {
        self.run_level = option;
    }
}

/// [`RunLevel`] option implementation
impl SystemOption<Priority> for System {
    fn set_option(&mut self, option: Priority) {
        self.data.set_priority(option);
    }
}

struct SystemData<Run, Ctx> 
where
    Run: FnMut(&mut Ctx, &mut Services) + Send + Sync,
{
    name: &'static str,
    run: Run,
    ctx: Ctx,
    priority: Priority,
}

/// Abstraction for [`System`] prepared to be integrated into engine
pub trait Systemized: Send + Sync {
    /// Returns name of the system
    fn name(&self) -> &'static str;
    /// Executes system cylce
    fn run(&mut self, app: &mut Services);
    /// Returns priority of the system
    fn priority(&self) -> Priority;
    /// Sets priority for the system
    fn set_priority(&mut self, priority: Priority);
}

impl<Run, Ctx> Systemized for SystemData<Run, Ctx>
where
    Run: FnMut(&mut Ctx, &mut Services) + Send + Sync,
    Ctx: SystemContext,
{
    fn name(&self) -> &'static str {
        self.name
    }

    fn run(&mut self, app: &mut Services) {
        (self.run)(&mut self.ctx, app);
    }

    fn priority(&self) -> Priority {
        self.priority
    }

    fn set_priority(&mut self, priority: Priority) {
        self.priority = priority;
    }
}

/// Abstraction for a function that can be turned into a [`System`]
pub trait IntoSystem<C, S> {
    /// Converts a function into [`System`]
    fn into_system(self) -> Box<dyn Systemized>;
}

macro_rules! impl_into_system {
    (($($context: ident),*), ($($i: ident),*)) => {

        impl<Fun, $($context,)* $($i,)*> IntoSystem<($($context,)*), ($($i,)*)> for Fun
        where
            Fun: FnMut($(Context<$context>,)* $($i,)*) + Send + Sync + 'static,
            $($context: SystemContext,)*
            $($i: Accessor,)*
        {
            #[allow(non_snake_case)]
            #[allow(unused)]
            fn into_system(mut self: Fun) -> Box<dyn Systemized>
            {
                let data: SystemData<_, ($($context)*)> = SystemData {
                    name: std::any::type_name::<Fun>(),
                    run: move |ctx, app| {
                        // context (none or one)
                        $(
                            let $context = Context::new(ctx);
                        )*
                        // services
                        $(
                            let $i = $i::fetch(app);
                        )*
                        (self)($($context,)* $($i,)*);
                    },
                    priority: Priority::Normal,
                    ctx: ($($context::default())*),
                };
                Box::new(data)
            }
        }
    }
}

/// Abstraction for [`System`] context
pub trait SystemContext: Default + Send + Sync + 'static {}
impl<T: Default + Send + Sync + 'static> SystemContext for T {}

impl_into_system!((), (A));
impl_into_system!((), (A, B));
impl_into_system!((), (A, B, C));
impl_into_system!((), (A, B, C, D));
impl_into_system!((), (A, B, C, D, E));
impl_into_system!((), (A, B, C, D, E, F));
impl_into_system!((), (A, B, C, D, E, F, G));
impl_into_system!((), (A, B, C, D, E, F, G, H));

impl_into_system!((CTX), (A));
impl_into_system!((CTX), (A, B));
impl_into_system!((CTX), (A, B, C));
impl_into_system!((CTX), (A, B, C, D));
impl_into_system!((CTX), (A, B, C, D, E));
impl_into_system!((CTX), (A, B, C, D, E, F));
impl_into_system!((CTX), (A, B, C, D, E, F, G));
impl_into_system!((CTX), (A, B, C, D, E, F, G, H));

/// Accessor for [`System`] context to privately store data between runs
pub struct Context<T> {
    value: *mut T,
}

impl<T> Context<T> {
    /// Constructs an accessor instance
    fn new(ctx: &mut T) -> Self {
        Context {
            value: ctx as *mut T,
        }
    }
}

impl<T> Deref for Context<T>
where
    T: SystemContext,
{
    type Target = T;
    fn deref(&self) -> &Self::Target {
        unsafe {
            &*self.value
        }
    }
}

impl<T> DerefMut for Context<T>
where
    T: SystemContext,
{
    fn deref_mut(&mut self) -> &mut T {
        unsafe {
            &mut *self.value
        }
    }
}

/// Mutable accessor for [`IntoService`] instance
pub struct Mut<T>
where
    T: IntoService
{
    value: *mut T,
}

impl<T> Deref for Mut<T>
where
    T: IntoService,
{
    type Target = T;
    fn deref(&self) -> &Self::Target {
        unsafe {
            &*self.value
        }
    }
}

impl<T> DerefMut for Mut<T>
where
    T: IntoService,
{
    fn deref_mut(&mut self) -> &mut T {
        unsafe {
            &mut *self.value
        }
    }
}

unsafe impl<T: IntoService> Send for Mut<T> {}
unsafe impl<T: IntoService> Sync for Mut<T> {}

/// Imutable accessor for a Service instance
pub struct Const<T> {
    value: *const T,
}

impl<T> Deref for Const<T>
where
    T: IntoService,
{
    type Target = T;
    fn deref(&self) -> &Self::Target {
        unsafe {
            &*self.value
        }
    }
}

unsafe impl<T: IntoService> Send for Const<T> {}
unsafe impl<T: IntoService> Sync for Const<T> {}

/// Abstraction to access Service in the storage
pub trait Accessor: Send + Sync {
    /// Type of Service to be accessed
    type Item: IntoService;
    /// Fetches the Service from the storage
    fn fetch(services: &mut Services) -> Self;
}

impl<T> Accessor for Mut<T>
where
    T: IntoService,
{
    type Item = T;
    fn fetch(services: &mut Services) -> Self {
        let service: &mut T = services.get_mut::<T>()
            .unwrap_or_else(|| panic!("Service {} does not exist", std::any::type_name::<T>()));
        Mut {
            value: service as *mut T
        }
    }
}

impl<T> Accessor for Const<T>
where
    T: IntoService,
{
    type Item = T;
    fn fetch(service: &mut Services) -> Self {
        let service: &T = service.get::<T>()
            .unwrap_or_else(|| panic!("Service {} does not exist", std::any::type_name::<T>()));
        Const {
            value: service as *const T
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        application::Services,
        ecs::{ Context, System, Const, Mut, RunLevel },
        world::World,
    };

    struct MyComponent(u64);

    fn my_system(mut world: Mut<World>) {
        world.spawn(Some((MyComponent(123),)));
    }

    #[test]
    fn world_system() {
        let mut services = Services::new();
        services.add(World::new());
        let mut s = System::from(my_system);
        s.data.run(&mut services);
        assert_eq!(services.get::<World>().unwrap().counter(), 1);
    }

    #[derive(Default)]
    struct MyContext(u64);

    struct MyService {
        data: u64
    }

    fn my_system_with_context(ctx: Context<MyContext>, mut service: Mut<MyService>) {
        service.data = ctx.0;
    }

    #[test]
    fn custom_system() {
        let mut services = Services::new();
        services.add(MyService { data: 123 });
        let mut s = System::from(my_system_with_context);
        s.data.run(&mut services);
        assert_eq!(services.get::<MyService>().unwrap().data, 0);
    }

    fn startup(_service: Const<MyService>) {}
    fn bind(_service: Const<MyService>) {}
    fn render(_service: Const<MyService>) {}
    fn release(_service: Const<MyService>) {}
    fn resize(_service: Const<MyService>) {}

    #[test]
    fn system_runlevel_autodetect() {
        let system = System::from(startup);
        assert_eq!(system.run_level, RunLevel::Startup);

        let system = System::from(bind);
        assert_eq!(system.run_level, RunLevel::Bind);

        let system = System::from(render);
        assert_eq!(system.run_level, RunLevel::Render);

        let system = System::from(release);
        assert_eq!(system.run_level, RunLevel::Release);

        let system = System::from(resize);
        assert_eq!(system.run_level, RunLevel::Resize);

        let system = System::from(my_system_with_context);
        assert_eq!(system.run_level, RunLevel::Standard);
    }
}