intuicio_framework_ecs/
systems.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
use crate::{
    bundle::Bundle,
    entity::Entity,
    prelude::Res,
    universe::{Universe, UniverseCondition, UniverseFetch},
    world::{World, WorldError},
    Component,
};
use intuicio_core::{
    context::Context,
    function::{FunctionHandle, FunctionQuery},
    registry::Registry,
    types::TypeQuery,
};
use intuicio_data::managed::DynamicManaged;
use std::{
    borrow::Cow,
    error::Error,
    ops::{Deref, DerefMut},
    sync::RwLock,
};

pub struct SystemContext<'a> {
    pub universe: &'a Universe,
    entity: Entity,
}

impl<'a> SystemContext<'a> {
    pub fn new(universe: &'a Universe, entity: Entity) -> Self {
        Self { universe, entity }
    }

    pub fn entity(&self) -> Entity {
        self.entity
    }

    pub fn fetch<Fetch: UniverseFetch<'a>>(&'a self) -> Result<Fetch::Value, Box<dyn Error>> {
        Fetch::fetch(self.universe, self.entity)
    }
}

impl Clone for SystemContext<'_> {
    fn clone(&self) -> Self {
        *self
    }
}

impl Copy for SystemContext<'_> {}

pub trait System: Component {
    fn run(&self, context: SystemContext) -> Result<(), Box<dyn Error>>;

    fn should_run(&self, context: SystemContext) -> bool {
        context
            .universe
            .systems
            .component::<true, SystemRunCondition>(context.entity)
            .map(|condition| condition.evaluate(context))
            .unwrap_or(true)
    }

    fn try_run(&self, context: SystemContext) -> Result<(), Box<dyn Error>> {
        if self.should_run(context) {
            self.run(context)
        } else {
            Ok(())
        }
    }
}

impl<T: Fn(SystemContext) -> Result<(), Box<dyn Error>> + Component> System for T {
    fn run(&self, context: SystemContext) -> Result<(), Box<dyn Error>> {
        (self)(context)
    }
}

pub struct ScriptedFunctionSystem<const LOCKING: bool> {
    run: FunctionHandle,
}

impl<const LOCKING: bool> ScriptedFunctionSystem<LOCKING> {
    pub fn new(run: FunctionHandle) -> Self {
        Self { run }
    }
}

impl<const LOCKING: bool> System for ScriptedFunctionSystem<LOCKING> {
    fn run(&self, context: SystemContext) -> Result<(), Box<dyn Error>> {
        let (registry, mut ctx) =
            context.fetch::<(Res<LOCKING, &Registry>, Res<LOCKING, &mut Context>)>()?;
        self.run.invoke(&mut ctx, &registry);
        Ok(())
    }
}

enum ScriptedObjectFunction {
    Name(Cow<'static, str>),
    Handle(FunctionHandle),
}

pub struct ScriptedObjectSystem<const LOCKING: bool> {
    object: DynamicManaged,
    function: RwLock<ScriptedObjectFunction>,
}

impl<const LOCKING: bool> ScriptedObjectSystem<LOCKING> {
    pub fn new(object: DynamicManaged) -> Self {
        Self {
            object,
            function: RwLock::new(ScriptedObjectFunction::Name("run".into())),
        }
    }

    pub fn new_custom(object: DynamicManaged, name: Cow<'static, str>) -> Self {
        Self {
            object,
            function: RwLock::new(ScriptedObjectFunction::Name(name)),
        }
    }
}

impl<const LOCKING: bool> System for ScriptedObjectSystem<LOCKING> {
    fn run(&self, context: SystemContext) -> Result<(), Box<dyn Error>> {
        let (registry, mut ctx) =
            context.fetch::<(Res<LOCKING, &Registry>, Res<LOCKING, &mut Context>)>()?;
        let mut function = self.function.write().map_err::<Box<dyn Error>, _>(|_| {
            "Could not get write access to scripted object function!".into()
        })?;
        if let ScriptedObjectFunction::Name(name) = &*function {
            *function = ScriptedObjectFunction::Handle(
                registry
                    .find_function(FunctionQuery {
                        name: Some(name.clone()),
                        type_query: Some(TypeQuery {
                            type_hash: Some(*self.object.type_hash()),
                            ..Default::default()
                        }),
                        ..Default::default()
                    })
                    .ok_or_else::<Box<dyn Error>, _>(|| {
                        "Could not find type of scripted object!".into()
                    })?,
            );
        }
        if let ScriptedObjectFunction::Handle(function) = &*function {
            let this = self
                .object
                .borrow()
                .ok_or_else::<Box<dyn Error>, _>(|| "Could not borrow scripted object!".into())?;
            ctx.stack().push(this);
            function.invoke(&mut ctx, &registry);
            Ok(())
        } else {
            Err("Scripted object function is not resolved into a handle!".into())
        }
    }
}

pub struct SystemObject(Box<dyn System>);

impl SystemObject {
    pub fn new(system: impl System) -> Self {
        Self(Box::new(system))
    }

    pub fn should_run(&self, context: SystemContext) -> bool {
        self.0.should_run(context)
    }

    pub fn run(&self, context: SystemContext) -> Result<(), Box<dyn Error>> {
        self.0.run(context)
    }

    pub fn try_run(&self, context: SystemContext) -> Result<(), Box<dyn Error>> {
        self.0.try_run(context)
    }
}

pub struct SystemRunCondition(Box<dyn Fn(SystemContext) -> bool + Send + Sync>);

impl SystemRunCondition {
    pub fn new<T: UniverseCondition>() -> Self {
        Self(Box::new(|context| T::evaluate(context)))
    }

    pub fn evaluate(&self, context: SystemContext) -> bool {
        (self.0)(context)
    }
}

#[derive(Default)]
pub struct Systems {
    world: World,
}

impl Deref for Systems {
    type Target = World;

    fn deref(&self) -> &Self::Target {
        &self.world
    }
}

impl DerefMut for Systems {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.world
    }
}

impl Systems {
    pub fn add(
        &mut self,
        system: impl System,
        locals: impl Bundle,
    ) -> Result<Entity, Box<dyn Error>> {
        let result = self.world.spawn((SystemObject::new(system),))?;
        WorldError::allow(
            self.world.insert(result, locals),
            [WorldError::EmptyColumnSet],
            (),
        )?;
        Ok(result)
    }

    pub fn add_locals(
        &mut self,
        entity: Entity,
        bundle: impl Bundle,
    ) -> Result<(), Box<dyn Error>> {
        WorldError::allow(
            self.world.insert(entity, bundle),
            [WorldError::EmptyColumnSet],
            (),
        )?;
        Ok(())
    }

    pub fn run<const LOCKING: bool>(
        &self,
        universe: &Universe,
        entity: Entity,
    ) -> Result<(), Box<dyn Error>> {
        self.world
            .component::<LOCKING, SystemObject>(entity)?
            .run(SystemContext::new(universe, entity))
    }

    pub fn try_run<const LOCKING: bool>(
        &self,
        universe: &Universe,
        entity: Entity,
    ) -> Result<(), Box<dyn Error>> {
        self.world
            .component::<LOCKING, SystemObject>(entity)?
            .try_run(SystemContext::new(universe, entity))
    }

    pub fn run_one_shot<const LOCKING: bool>(
        universe: &Universe,
        system: impl System,
    ) -> Result<(), Box<dyn Error>> {
        system.run(SystemContext::new(universe, Default::default()))
    }

    pub fn try_run_one_shot<const LOCKING: bool>(
        universe: &Universe,
        system: impl System,
    ) -> Result<(), Box<dyn Error>> {
        system.try_run(SystemContext::new(universe, Default::default()))
    }
}