From 215eee4ae24f39a462faa7138f96eae9fb4762cc Mon Sep 17 00:00:00 2001 From: Ryan McGrath Date: Thu, 13 Oct 2022 17:15:52 -0400 Subject: [PATCH] Experiment with bundle identifier being attached to generated subclass names (Issue #63) --- src/foundation/class.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/foundation/class.rs b/src/foundation/class.rs index f877f9f..aad1902 100644 --- a/src/foundation/class.rs +++ b/src/foundation/class.rs @@ -4,13 +4,37 @@ use std::sync::{Arc, RwLock}; use lazy_static::lazy_static; +use objc::{class, msg_send, sel, sel_impl}; use objc::declare::ClassDecl; -use objc::runtime::{objc_getClass, Class}; +use objc::runtime::{objc_getClass, Class, Object}; lazy_static! { static ref CLASSES: ClassMap = ClassMap::new(); } +/// A temporary method for testing; this will get cleaned up if it's worth bringing in permanently. +/// +/// (and probably not repeatedly queried...) +/// +/// This accounts for code not running in a standard bundle, and returns `None` if the bundle +/// identifier is nil. +fn get_bundle_id() -> Option { + let identifier: *mut Object = unsafe { + let bundle: *mut Object = msg_send![class!(NSBundle), mainBundle]; + msg_send![bundle, bundleIdentifier] + }; + + if identifier == crate::foundation::nil { + return None; + } + + let identifier = crate::foundation::NSString::retain(identifier).to_string() + .replace(".", "_") + .replace("-", "_"); + + Some(identifier) +} + /// Represents an entry in a `ClassMap`. We store an optional superclass_name for debugging /// purposes; it's an `Option` to make the logic of loading a class type where we don't need to /// care about the superclass type simpler. @@ -119,7 +143,10 @@ where // If we can't find the class anywhere, then we'll attempt to load the superclass and register // our new class type. if let Some(superclass) = CLASSES.load(superclass_name, None) { - let objc_subclass_name = format!("{}_{}", subclass_name, superclass_name); + let objc_subclass_name = match get_bundle_id() { + Some(bundle_id) => format!("{}_{}_{}", subclass_name, superclass_name, bundle_id), + None => format!("{}_{}", subclass_name, superclass_name) + }; match ClassDecl::new(&objc_subclass_name, unsafe { &*superclass }) { Some(mut decl) => {