reactive_graph_sys_json/
plugin.rs1use reactive_graph_plugin_api::EntityComponentBehaviourRegistry;
2use reactive_graph_plugin_api::prelude::plugin::*;
3use reactive_graph_plugin_api::prelude::providers::*;
4
5use crate::behaviour::component::load_json::LoadJsonFactory;
6use crate::behaviour::component::save_json::SaveJsonFactory;
7use reactive_graph_sys_json_model::BEHAVIOUR_LOAD_JSON;
8use reactive_graph_sys_json_model::BEHAVIOUR_SAVE_JSON;
9use reactive_graph_sys_json_model::COMPONENT_BEHAVIOUR_LOAD_JSON;
10use reactive_graph_sys_json_model::COMPONENT_BEHAVIOUR_SAVE_JSON;
11
12export_plugin!({
13 "dependencies": [
14 { "name": "reactive-graph-std-base", "version": ">=0.10.0, <0.11.0" },
15 { "name": "reactive-graph-std-result", "version": ">=0.10.0, <0.11.0" },
16 { "name": "reactive-graph-std-trigger", "version": ">=0.10.0, <0.11.0" },
17 { "name": "reactive-graph-sys-file", "version": ">=0.10.0, <0.11.0" }
18 ]
19});
20
21#[injectable]
22pub trait JsonPlugin: Plugin + Send + Sync {}
23
24#[derive(Component)]
25pub struct JsonPluginImpl {
26 component_provider: Arc<dyn TypeProvider<Components> + Send + Sync>,
27
28 #[component(default = "component_provider_registry")]
29 component_provider_registry: Arc<dyn ComponentProviderRegistry + Send + Sync>,
30
31 entity_types_provider: Arc<dyn TypeProvider<EntityTypes> + Send + Sync>,
32
33 #[component(default = "entity_types_provider_registry")]
34 entity_type_provider_registry: Arc<dyn EntityTypeProviderRegistry + Send + Sync>,
35
36 #[component(default = "entity_component_behaviour_registry")]
37 entity_component_behaviour_registry: Arc<dyn EntityComponentBehaviourRegistry + Send + Sync>,
38}
39
40#[async_trait]
41#[component_alias]
42impl Plugin for JsonPluginImpl {
43 async fn activate(&self) -> Result<(), PluginActivationError> {
44 self.component_provider_registry.register_provider(self.component_provider.clone()).await;
45 self.entity_type_provider_registry.register_provider(self.entity_types_provider.clone()).await;
46
47 let factory = Arc::new(LoadJsonFactory::new(BEHAVIOUR_LOAD_JSON.clone()));
49 self.entity_component_behaviour_registry
50 .register(COMPONENT_BEHAVIOUR_LOAD_JSON.clone(), factory)
51 .await;
52
53 let factory = Arc::new(SaveJsonFactory::new(BEHAVIOUR_SAVE_JSON.clone()));
55 self.entity_component_behaviour_registry
56 .register(COMPONENT_BEHAVIOUR_SAVE_JSON.clone(), factory)
57 .await;
58
59 Ok(())
60 }
61
62 async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
63 self.entity_component_behaviour_registry.unregister(&COMPONENT_BEHAVIOUR_LOAD_JSON).await;
64 self.entity_component_behaviour_registry.unregister(&COMPONENT_BEHAVIOUR_SAVE_JSON).await;
65
66 self.entity_type_provider_registry.unregister_provider(self.entity_types_provider.id()).await;
67 self.component_provider_registry.unregister_provider(self.component_provider.id()).await;
68 Ok(())
69 }
70}