reactive_graph_sys_config/plugin.rs
1use crate::behaviour::component::config_file::ConfigFileFactory;
2use reactive_graph_plugin_api::EntityComponentBehaviourRegistry;
3use reactive_graph_plugin_api::prelude::plugin::*;
4use reactive_graph_plugin_api::prelude::providers::*;
5use reactive_graph_sys_config_model::BEHAVIOUR_CONFIG_FILE;
6use reactive_graph_sys_config_model::COMPONENT_BEHAVIOUR_CONFIG_FILE;
7
8export_plugin!({
9 "dependencies": [
10 { "name": "reactive-graph-std-base", "version": ">=0.10.0, <0.11.0" },
11 { "name": "reactive-graph-std-trigger", "version": ">=0.10.0, <0.11.0" },
12 { "name": "reactive-graph-std-result", "version": ">=0.10.0, <0.11.0" },
13 { "name": "reactive-graph-sys-file", "version": ">=0.10.0, <0.11.0" }
14 ]
15});
16
17#[injectable]
18pub trait ConfigPlugin: Plugin + Send + Sync {}
19
20#[derive(Component)]
21pub struct ConfigPluginImpl {
22 component_provider: Arc<dyn TypeProvider<Components> + Send + Sync>,
23
24 #[component(default = "component_provider_registry")]
25 component_provider_registry: Arc<dyn ComponentProviderRegistry + Send + Sync>,
26
27 entity_types_provider: Arc<dyn TypeProvider<EntityTypes> + Send + Sync>,
28
29 #[component(default = "entity_types_provider_registry")]
30 entity_type_provider_registry: Arc<dyn EntityTypeProviderRegistry + Send + Sync>,
31
32 #[component(default = "entity_component_behaviour_registry")]
33 entity_component_behaviour_registry: Arc<dyn EntityComponentBehaviourRegistry + Send + Sync>,
34}
35
36#[async_trait]
37#[component_alias]
38impl Plugin for ConfigPluginImpl {
39 async fn activate(&self) -> Result<(), PluginActivationError> {
40 self.component_provider_registry.register_provider(self.component_provider.clone()).await;
41 self.entity_type_provider_registry.register_provider(self.entity_types_provider.clone()).await;
42 let factory = Arc::new(ConfigFileFactory::new(BEHAVIOUR_CONFIG_FILE.clone()));
43 self.entity_component_behaviour_registry
44 .register(COMPONENT_BEHAVIOUR_CONFIG_FILE.clone(), factory)
45 .await;
46 Ok(())
47 }
48
49 async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
50 self.entity_component_behaviour_registry.unregister(&COMPONENT_BEHAVIOUR_CONFIG_FILE).await;
51 self.entity_type_provider_registry.unregister_provider(self.entity_types_provider.id()).await;
52 self.component_provider_registry.unregister_provider(self.component_provider.id()).await;
53 Ok(())
54 }
55}
56
57// use std::sync::Arc;
58// use std::sync::RwLock;
59//
60// use async_trait::async_trait;
61//
62// use crate::behaviour::component::config_file::ConfigFileFactory;
63// use crate::di::*;
64// use crate::model_config::BEHAVIOUR_CONFIG_FILE;
65// use crate::model_config::COMPONENT_BEHAVIOUR_CONFIG_FILE;
66// use crate::plugins::component_provider;
67// use crate::plugins::entity_type_provider;
68// use crate::plugins::plugin_context::PluginContext;
69// use crate::plugins::ComponentProvider;
70// use crate::plugins::ComponentProviderError;
71// use crate::plugins::EntityTypeProvider;
72// use crate::plugins::EntityTypeProviderError;
73// use crate::plugins::Plugin;
74// use crate::plugins::PluginActivationError;
75// use crate::plugins::PluginContextDeinitializationError;
76// use crate::plugins::PluginContextInitializationError;
77// use crate::plugins::PluginDeactivationError;
78// use crate::providers::ConfigComponentProviderImpl;
79// use crate::providers::ConfigEntityTypeProviderImpl;
80//
81// #[wrapper]
82// pub struct PluginContextContainer(RwLock<Option<Arc<dyn PluginContext>>>);
83//
84// #[provides]
85// fn create_empty_plugin_context_container() -> PluginContextContainer {
86// PluginContextContainer(RwLock::new(None))
87// }
88//
89// #[async_trait]
90// pub trait ConfigPlugin: Plugin + Send + Sync {}
91//
92// #[component]
93// pub struct ConfigPluginImpl {
94// component_provider: Wrc<ConfigComponentProviderImpl>,
95// entity_type_provider: Wrc<ConfigEntityTypeProviderImpl>,
96//
97// context: PluginContextContainer,
98// }
99//
100// interfaces!(ConfigPluginImpl: dyn Plugin);
101//
102// #[async_trait]
103// #[provides]
104// impl ConfigPlugin for ConfigPluginImpl {}
105//
106// #[async_trait]
107// impl Plugin for ConfigPluginImpl {
108// async fn activate(&self) -> Result<(), PluginActivationError> {
109// let guard = self.context.0.read().unwrap();
110// if let Some(context) = guard.clone() {
111// let entity_component_behaviour_registry = context.get_entity_component_behaviour_registry();
112//
113// // Component Behaviour ConfigFile
114// let factory = Arc::new(ConfigFileFactory::new(BEHAVIOUR_CONFIG_FILE.clone()));
115// entity_component_behaviour_registry.register(COMPONENT_BEHAVIOUR_CONFIG_FILE.clone(), factory);
116// }
117// Ok(())
118// }
119// async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
120// let guard = self.context.0.read().unwrap();
121// if let Some(context) = guard.clone() {
122// let entity_component_behaviour_registry = context.get_entity_component_behaviour_registry();
123// entity_component_behaviour_registry.unregister(&COMPONENT_BEHAVIOUR_CONFIG_FILE);
124// }
125// Ok(())
126// }
127//
128// fn set_context(&self, context: Arc<dyn PluginContext>) -> Result<(), PluginContextInitializationError> {
129// self.context.0.write().unwrap().replace(context);
130// Ok(())
131// }
132//
133// fn remove_context(&self) -> Result<(), PluginContextDeinitializationError> {
134// let mut writer = self.context.0.write().unwrap();
135// *writer = None;
136// Ok(())
137// }
138//
139// fn get_component_provider(&self) -> Result<Option<Arc<dyn ComponentProvider>>, ComponentProviderError> {
140// component_provider!(self.component_provider)
141// }
142//
143// fn get_entity_type_provider(&self) -> Result<Option<Arc<dyn EntityTypeProvider>>, EntityTypeProviderError> {
144// entity_type_provider!(self.entity_type_provider)
145// }
146// }