Skip to content

Commit

Permalink
Reflect::assembly_name to retrieve assembly name of a type
Browse files Browse the repository at this point in the history
- this method is useful in hot reloading, to understand whether a type instance belong to a plugin or not and if so - serialize and destroy the instance before reloading
  • Loading branch information
mrDIMAS committed May 15, 2024
1 parent 58648d6 commit f31bbb3
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 1 deletion.
8 changes: 8 additions & 0 deletions fyrox-animation/src/machine/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ macro_rules! define_two_args_node {
""
}

fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
func(&[
FieldInfo {
Expand Down Expand Up @@ -200,6 +204,10 @@ impl<T: EntityId> Reflect for NotNode<T> {
""
}

fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
func(&[FieldInfo {
owner_type_id: TypeId::of::<Self>(),
Expand Down
5 changes: 5 additions & 0 deletions fyrox-core-derive/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ fn gen_impl(
let as_array_impl = ty_args.as_array_impl();

let doc = args::fetch_doc_comment(&ty_args.attrs);
let assembly_name = std::env::var("CARGO_PKG_NAME").unwrap_or_default();

let set_field = set_field.map(|set_field| {
quote! {
Expand All @@ -432,6 +433,10 @@ fn gen_impl(
#doc
}

fn assembly_name(&self) -> &'static str {
#assembly_name
}

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
#metadata
}
Expand Down
4 changes: 4 additions & 0 deletions fyrox-core/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ where
Ok(Box::new(this))
}

fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}

#[inline]
fn as_array(&self, func: &mut dyn FnMut(Option<&dyn ReflectArray>)) {
func(Some(self))
Expand Down
15 changes: 15 additions & 0 deletions fyrox-core/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ pub trait Reflect: ReflectBase {

fn set(&mut self, value: Box<dyn Reflect>) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>;

/// Returns a parent assembly name of the type that implements this trait. **WARNING:** You should use
/// proc-macro (`#[derive(Reflect)]`) to ensure that this method will return correct assembly
/// name. In other words - there's no guarantee, that any implementation other than proc-macro
/// will return a correct name of the assembly. Alternatively, you can use `env!("CARGO_PKG_NAME")`
/// as an implementation.
fn assembly_name(&self) -> &'static str;

/// Calls user method specified with `#[reflect(setter = ..)]` or falls back to
/// [`Reflect::field_mut`]
#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -1087,6 +1094,10 @@ macro_rules! blank_reflect {
""
}

fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
func(&[])
}
Expand Down Expand Up @@ -1141,6 +1152,10 @@ macro_rules! delegate_reflect {
self.deref().doc()
}

fn assembly_name(&self) -> &'static str {
self.deref().assembly_name()
}

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.deref().fields_info(func)
}
Expand Down
4 changes: 4 additions & 0 deletions fyrox-core/src/reflect/std_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ macro_rules! impl_reflect_inner_mutability {
""
}

fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}

fn fields_info(&$self, func: &mut dyn FnMut(&[FieldInfo])) {
let guard = $acquire_lock_guard;
guard.fields_info(func)
Expand Down
4 changes: 4 additions & 0 deletions fyrox-core/src/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ where
self.value.doc()
}

fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}

#[inline]
fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.value.fields_info(func)
Expand Down
3 changes: 2 additions & 1 deletion fyrox-impl/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,8 @@ impl Engine {
/// graphics_context_params,
/// resource_manager: ResourceManager::new(task_pool.clone()),
/// serialization_context: Arc::new(SerializationContext::new()),
/// task_pool
/// task_pool,
/// widget_constructors: Arc::new(Default::default()),
/// })
/// .unwrap();
/// ```
Expand Down
22 changes: 22 additions & 0 deletions fyrox-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,25 @@ macro_rules! define_with {
}
};
}

#[cfg(test)]
mod test {
use crate::scene::base::BaseBuilder;
use fyrox_core::reflect::Reflect;
use fyrox_core::ImmutableString;
use fyrox_sound::source::Status;
use fyrox_ui::widget::WidgetBuilder;

#[test]
fn test_assembly_names() {
let var = ImmutableString::new("Foobar");
let base = BaseBuilder::new().build_base();
let widget = WidgetBuilder::new().build();
let status = Status::Stopped;

assert_eq!(var.assembly_name(), "fyrox-core");
assert_eq!(base.assembly_name(), "fyrox-impl");
assert_eq!(widget.assembly_name(), "fyrox-ui");
assert_eq!(status.assembly_name(), "fyrox-sound");
}
}
4 changes: 4 additions & 0 deletions fyrox-impl/src/scene/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,10 @@ impl Reflect for Node {
self.0.deref().doc()
}

fn assembly_name(&self) -> &'static str {
self.0.deref().assembly_name()
}

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.0.deref().fields_info(func)
}
Expand Down
4 changes: 4 additions & 0 deletions fyrox-impl/src/scene/ragdoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ impl Reflect for Limb {
""
}

fn assembly_name(&self) -> &'static str {
env!("CARGO_PKG_NAME")
}

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
func(&[
FieldInfo {
Expand Down
4 changes: 4 additions & 0 deletions fyrox-impl/src/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,10 @@ impl Reflect for Script {
self.instance.doc()
}

fn assembly_name(&self) -> &'static str {
self.instance.assembly_name()
}

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.instance.fields_info(func)
}
Expand Down
4 changes: 4 additions & 0 deletions fyrox-ui/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ impl Reflect for UiNode {
self.0.deref().doc()
}

fn assembly_name(&self) -> &'static str {
self.0.deref().assembly_name()
}

fn fields_info(&self, func: &mut dyn FnMut(&[FieldInfo])) {
self.0.deref().fields_info(func)
}
Expand Down

0 comments on commit f31bbb3

Please sign in to comment.