Skip to content

Commit

Permalink
use assembly name to search for plugin entities on scenes/guis
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed May 15, 2024
1 parent efef97a commit 3b6eecb
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 34 deletions.
2 changes: 1 addition & 1 deletion editor/src/inspector/editors/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn create_items(

ctx[item].user_data = Some(Arc::new(Mutex::new((
*type_uuid,
Some(constructor.source_path.clone()),
Some(constructor.source_path.to_string()),
))));

item
Expand Down
21 changes: 6 additions & 15 deletions fyrox-impl/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2450,14 +2450,6 @@ impl Engine {
resource_manager: &ResourceManager,
plugin: &dyn Plugin,
) {
*serialization_context
.script_constructors
.context_type_id
.lock() = plugin.type_id();
*serialization_context
.node_constructors
.context_type_id
.lock() = plugin.type_id();
*widget_constructors.context_type_id.lock() = plugin.type_id();
plugin.register(PluginRegistrationContext {
serialization_context,
Expand Down Expand Up @@ -2594,15 +2586,14 @@ impl Engine {
script: &Script,
plugin: &dyn Plugin,
) -> bool {
let plugin_type_id = plugin.type_id();
let script_id = script.deref().id();

if let Some(constructor) = serialization_context
.script_constructors
.map()
.get(&script_id)
{
if constructor.source_type_id == plugin_type_id {
if constructor.assembly_name == plugin.assembly_name() {
return true;
}
}
Expand All @@ -2614,11 +2605,10 @@ impl Engine {
node: &Node,
plugin: &dyn Plugin,
) -> bool {
let plugin_type_id = plugin.type_id();
let node_id = (*node).id();

if let Some(constructor) = serialization_context.node_constructors.map().get(&node_id) {
if constructor.source_type_id == plugin_type_id {
if constructor.assembly_name == plugin.assembly_name() {
return true;
}
}
Expand Down Expand Up @@ -2674,6 +2664,7 @@ impl Engine {
}

let plugin_type_id = state.as_loaded_ref().plugin().type_id();
let plugin_assembly_name = state.as_loaded_ref().plugin().assembly_name();

// Collect all the data that belongs to the plugin
struct ScriptState {
Expand Down Expand Up @@ -2770,7 +2761,7 @@ impl Engine {
let mut constructors = FxHashSet::default();
for (type_uuid, constructor) in self.serialization_context.script_constructors.map().iter()
{
if constructor.source_type_id == plugin_type_id {
if constructor.assembly_name == plugin_assembly_name {
constructors.insert(*type_uuid);
}
}
Expand All @@ -2783,7 +2774,7 @@ impl Engine {
// Search for node constructors, that belongs to dynamic plugins and remove them.
let mut constructors = FxHashSet::default();
for (type_uuid, constructor) in self.serialization_context.node_constructors.map().iter() {
if constructor.source_type_id == plugin_type_id {
if constructor.assembly_name == plugin_assembly_name {
constructors.insert(*type_uuid);
}
}
Expand All @@ -2796,7 +2787,7 @@ impl Engine {
// Search for widget constructors, that belongs to dynamic plugins and remove them.
let mut constructors = FxHashSet::default();
for (type_uuid, constructor) in self.widget_constructors.map().iter() {
if constructor.source_type_id == plugin_type_id {
if constructor.assembly_name == plugin_assembly_name {
constructors.insert(*type_uuid);
}
}
Expand Down
9 changes: 3 additions & 6 deletions fyrox-impl/src/scene/node/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,25 @@ use crate::{
},
};
use fxhash::FxHashMap;
use std::any::{Any, TypeId};

/// Node constructor.
pub struct NodeConstructor {
/// A simple type alias for boxed node constructor.
closure: Box<dyn FnMut() -> Node + Send>,

/// A type of the source of the script constructor.
pub source_type_id: TypeId,
/// A name of the assembly this node constructor is from.
pub assembly_name: &'static str,
}

/// A special container that is able to create nodes by their type UUID.

pub struct NodeConstructorContainer {
pub(crate) context_type_id: Mutex<TypeId>,
map: Mutex<FxHashMap<Uuid, NodeConstructor>>,
}

impl Default for NodeConstructorContainer {
fn default() -> Self {
Self {
context_type_id: Mutex::new(().type_id()),
map: Default::default(),
}
}
Expand Down Expand Up @@ -94,7 +91,7 @@ impl NodeConstructorContainer {
T::type_uuid(),
NodeConstructor {
closure: Box::new(|| Node::new(T::default())),
source_type_id: *self.context_type_id.lock(),
assembly_name: T::type_assembly_name(),
},
);

Expand Down
2 changes: 1 addition & 1 deletion fyrox-impl/src/scene/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ mod test {
.script_constructors
.map()
.iter()
.any(|s| &s.1.source_path == file!()));
.any(|s| s.1.source_path == file!()));

engine::initialize_resource_manager_loaders(
&resource_manager,
Expand Down
13 changes: 5 additions & 8 deletions fyrox-impl/src/script/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
},
script::{Script, ScriptTrait},
};
use std::any::{Any, TypeId};
use std::collections::BTreeMap;

/// Script constructor contains all required data and methods to create script instances
Expand All @@ -21,23 +20,21 @@ pub struct ScriptConstructor {
pub name: String,

/// Script source path.
pub source_path: String,
pub source_path: &'static str,

/// A type of the source of the script constructor.
pub source_type_id: TypeId,
/// A name of the assembly this script constructor belongs to.
pub assembly_name: &'static str,
}

/// A special container that is able to create nodes by their type UUID.
pub struct ScriptConstructorContainer {
pub(crate) context_type_id: Mutex<TypeId>,
// BTreeMap allows to have sorted list of constructors.
map: Mutex<BTreeMap<Uuid, ScriptConstructor>>,
}

impl Default for ScriptConstructorContainer {
fn default() -> Self {
Self {
context_type_id: Mutex::new(().type_id()),
map: Default::default(),
}
}
Expand All @@ -63,8 +60,8 @@ impl ScriptConstructorContainer {
ScriptConstructor {
constructor: Box::new(|| Script::new(T::default())),
name: name.to_owned(),
source_path: T::source_path().to_owned(),
source_type_id: *self.context_type_id.lock(),
source_path: T::source_path(),
assembly_name: T::type_assembly_name(),
},
);

Expand Down
6 changes: 3 additions & 3 deletions fyrox-ui/src/node/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub struct WidgetConstructor {
/// A simple type alias for boxed widget constructor.
closure: Box<dyn FnMut() -> UiNode + Send>,

/// A type of the source of the script constructor.
pub source_type_id: TypeId,
/// A name of the assembly this widget constructor belongs to.
pub assembly_name: &'static str,
}

/// A special container that is able to create widgets by their type UUID.
Expand Down Expand Up @@ -233,7 +233,7 @@ impl WidgetConstructorContainer {
T::type_uuid(),
WidgetConstructor {
closure: Box::new(|| UiNode::new(T::default())),
source_type_id: *self.context_type_id.lock(),
assembly_name: T::type_assembly_name(),
},
);

Expand Down

0 comments on commit 3b6eecb

Please sign in to comment.