inexor_rgf_plugin_state/
plugin.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
use std::sync::Arc;
use std::sync::RwLock;

use async_trait::async_trait;

use crate::behaviour::component::StateDebuggerFactory;
use crate::behaviour::component::StateFactory;
use crate::behaviour::component::STATE_BEHAVIOURS;
use crate::behaviour::component::STATE_DEBUGGER_BEHAVIOURS;
use crate::di::module;
use crate::di::provides;
use crate::di::wrapper;
use crate::di::Component;
use crate::di::Wrc;
use crate::model::ComponentBehaviourTypeId;
use crate::plugins::component_provider;
use crate::plugins::entity_type_provider;
use crate::plugins::plugin_context::PluginContext;
use crate::plugins::ComponentProvider;
use crate::plugins::ComponentProviderError;
use crate::plugins::EntityTypeProvider;
use crate::plugins::EntityTypeProviderError;
use crate::plugins::Plugin;
use crate::plugins::PluginActivationError;
use crate::plugins::PluginContextDeinitializationError;
use crate::plugins::PluginContextInitializationError;
use crate::plugins::PluginDeactivationError;
use crate::providers::StateComponentProviderImpl;
use crate::providers::StateEntityTypeProviderImpl;

#[wrapper]
pub struct PluginContextContainer(RwLock<Option<Arc<dyn PluginContext>>>);

#[provides]
fn create_empty_plugin_context_container() -> PluginContextContainer {
    PluginContextContainer(RwLock::new(None))
}

#[async_trait]
pub trait StatePlugin: Plugin + Send + Sync {}

#[module]
pub struct StatePluginImpl {
    component_provider: Wrc<StateComponentProviderImpl>,
    entity_type_provider: Wrc<StateEntityTypeProviderImpl>,
    context: PluginContextContainer,
}

interfaces!(StatePluginImpl: dyn Plugin);

#[async_trait]
#[provides]
impl StatePlugin for StatePluginImpl {}

#[async_trait]
impl Plugin for StatePluginImpl {
    async fn activate(&self) -> Result<(), PluginActivationError> {
        let guard = self.context.0.read().unwrap();
        if let Some(context) = guard.clone() {
            let entity_component_behaviour_registry = context.get_entity_component_behaviour_registry();
            for behaviour_ty in STATE_BEHAVIOURS.iter() {
                entity_component_behaviour_registry.register(ComponentBehaviourTypeId::from(behaviour_ty), Arc::new(StateFactory::new(behaviour_ty.clone())));
            }
            for (behaviour_ty, f) in STATE_DEBUGGER_BEHAVIOURS.iter() {
                entity_component_behaviour_registry
                    .register(ComponentBehaviourTypeId::from(behaviour_ty), Arc::new(StateDebuggerFactory::new(behaviour_ty.clone(), *f)));
            }
        }
        Ok(())
    }

    async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
        let guard = self.context.0.read().unwrap();
        if let Some(context) = guard.clone() {
            let entity_component_behaviour_registry = context.get_entity_component_behaviour_registry();
            for behaviour_ty in STATE_BEHAVIOURS.iter() {
                entity_component_behaviour_registry.unregister(&ComponentBehaviourTypeId::from(behaviour_ty));
            }
            for behaviour_ty in STATE_DEBUGGER_BEHAVIOURS.keys() {
                entity_component_behaviour_registry.unregister(&ComponentBehaviourTypeId::from(behaviour_ty));
            }
        }
        Ok(())
    }

    fn set_context(&self, context: Arc<dyn PluginContext>) -> Result<(), PluginContextInitializationError> {
        self.context.0.write().unwrap().replace(context);
        Ok(())
    }

    fn remove_context(&self) -> Result<(), PluginContextDeinitializationError> {
        let mut writer = self.context.0.write().unwrap();
        *writer = None;
        Ok(())
    }

    fn get_component_provider(&self) -> Result<Option<Arc<dyn ComponentProvider>>, ComponentProviderError> {
        component_provider!(self.component_provider)
    }

    fn get_entity_type_provider(&self) -> Result<Option<Arc<dyn EntityTypeProvider>>, EntityTypeProviderError> {
        entity_type_provider!(self.entity_type_provider)
    }
}