plugin type enum

This commit is contained in:
Dmitry Stepanov
2024-03-17 12:15:17 +03:00
parent bd5cc70032
commit 6b9d488493
5 changed files with 47 additions and 11 deletions
+6 -5
View File
@@ -91,6 +91,7 @@ use std::{
time::Duration,
};
use crate::plugin::PluginContainer;
use winit::{
dpi::{Position, Size},
event_loop::EventLoopWindowTarget,
@@ -422,7 +423,7 @@ pub struct Engine {
sound_engine: SoundEngine,
// A set of plugins used by the engine.
plugins: Vec<Box<dyn Plugin>>,
plugins: Vec<PluginContainer>,
plugins_enabled: bool,
@@ -480,7 +481,7 @@ impl ScriptMessageDispatcher {
&self,
scene: &mut Scene,
scene_handle: Handle<Scene>,
plugins: &mut [Box<dyn Plugin>],
plugins: &mut [PluginContainer],
resource_manager: &ResourceManager,
dt: f32,
elapsed_time: f32,
@@ -652,7 +653,7 @@ impl ScriptProcessor {
fn handle_scripts(
&mut self,
scenes: &mut SceneContainer,
plugins: &mut [Box<dyn Plugin>],
plugins: &mut [PluginContainer],
resource_manager: &ResourceManager,
task_pool: &mut TaskPoolHandler,
graphics_context: &mut GraphicsContext,
@@ -1086,7 +1087,7 @@ where
pub(crate) fn process_scripts<T>(
scene: &mut Scene,
scene_handle: Handle<Scene>,
plugins: &mut [Box<dyn Plugin>],
plugins: &mut [PluginContainer],
resource_manager: &ResourceManager,
message_sender: &ScriptMessageSender,
message_dispatcher: &mut ScriptMessageDispatcher,
@@ -2305,7 +2306,7 @@ impl Engine {
resource_manager: &self.resource_manager,
});
self.plugins.push(Box::new(plugin));
self.plugins.push(PluginContainer::Static(Box::new(plugin)));
}
}
+2 -1
View File
@@ -1,5 +1,6 @@
//! Asynchronous task handler. See [`TaskPoolHandler`] for more info and usage examples.
use crate::plugin::PluginContainer;
use crate::{
core::{
pool::Handle,
@@ -24,7 +25,7 @@ pub(crate) type NodeTaskHandlerClosure = Box<
pub(crate) type PluginTaskHandler = Box<
dyn for<'a, 'b> Fn(
Box<dyn AsyncTaskResult>,
&'a mut [Box<dyn Plugin>],
&'a mut [PluginContainer],
&mut PluginContext<'a, 'b>,
),
>;
+1 -2
View File
@@ -7,8 +7,7 @@ use std::ffi::OsStr;
/// it is strongly advised not to use it in production builds, because it is slower than statically
/// linked plugins and it could be unsafe if different compiler versions are used.
pub struct DynamicPlugin {
/// Loaded plugin.
pub plugin: Box<dyn Plugin>,
pub(super) plugin: Box<dyn Plugin>,
// Keep the library loaded.
// Must be last!
#[allow(dead_code)]
+35 -1
View File
@@ -13,11 +13,45 @@ use crate::{
},
event::Event,
gui::{message::UiMessage, UserInterface},
plugin::dynamic::DynamicPlugin,
scene::{Scene, SceneContainer},
};
use std::{any::Any, path::Path, sync::Arc};
use std::{
any::Any,
ops::{Deref, DerefMut},
path::Path,
sync::Arc,
};
use winit::event_loop::EventLoopWindowTarget;
/// A wrapper for various plugin types.
pub enum PluginContainer {
/// Statically linked plugin.
Static(Box<dyn Plugin>),
/// Dynamically linked plugin.
Dynamic(DynamicPlugin),
}
impl Deref for PluginContainer {
type Target = dyn Plugin;
fn deref(&self) -> &Self::Target {
match self {
PluginContainer::Static(plugin) => &**plugin,
PluginContainer::Dynamic(dynamic) => &*dynamic.plugin,
}
}
}
impl DerefMut for PluginContainer {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
PluginContainer::Static(plugin) => &mut **plugin,
PluginContainer::Dynamic(dynamic) => &mut *dynamic.plugin,
}
}
}
/// Contains plugin environment for the registration stage.
pub struct PluginRegistrationContext<'a> {
/// A reference to serialization context of the engine. See [`SerializationContext`] for more
+3 -2
View File
@@ -2,6 +2,7 @@
//! Script is used to add custom logic to scene nodes. See [ScriptTrait] for more info.
use crate::plugin::PluginContainer;
use crate::scene::base::NodeScriptMessage;
use crate::{
asset::manager::ResourceManager,
@@ -239,10 +240,10 @@ where
/// A simple wrapper for a reference to plugins container. It has some useful methods to fetch
/// a plugin of certain type. See [`PluginsRefMut::of_type_ref`] and [`PluginsRefMut::of_type_mut`].
pub struct PluginsRefMut<'a>(pub &'a mut [Box<dyn Plugin>]);
pub struct PluginsRefMut<'a>(pub &'a mut [PluginContainer]);
impl<'a> Deref for PluginsRefMut<'a> {
type Target = [Box<dyn Plugin>];
type Target = [PluginContainer];
fn deref(&self) -> &Self::Target {
self.0