Skip to content

Commit 6445434

Browse files
committed
Update cocoa class loading for Mac
1 parent f76bb09 commit 6445434

File tree

6 files changed

+34
-31
lines changed

6 files changed

+34
-31
lines changed

plugin-dev/Source/Sentry/Private/Apple/AppleSentryId.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@
77

88
FAppleSentryId::FAppleSentryId()
99
{
10-
#if PLATFORM_MAC
11-
IdApple = [[SENTRY_APPLE_CLASS(_TtC6Sentry8SentryId) alloc] init];
12-
#elif PLATFORM_IOS
1310
IdApple = [[SENTRY_APPLE_CLASS(SentryId) alloc] init];
14-
#endif
1511
}
1612

1713
FAppleSentryId::FAppleSentryId(const FString& id)
1814
{
19-
#if PLATFORM_MAC
20-
IdApple = [[SENTRY_APPLE_CLASS(_TtC6Sentry8SentryId) alloc] initWithUUIDString:id.GetNSString()];
21-
#elif PLATFORM_IOS
2215
IdApple = [[SENTRY_APPLE_CLASS(SentryId) alloc] initWithUUIDString:id.GetNSString()];
23-
#endif
2416
}
2517

2618
FAppleSentryId::FAppleSentryId(SentryId* id)

plugin-dev/Source/Sentry/Private/Apple/AppleSentrySubsystem.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,7 @@ TSharedPtr<ISentryTransactionContext> FAppleSentrySubsystem::ContinueTrace(const
395395
sampleDecision = traceParts[2].Equals(TEXT("1")) ? kSentrySampleDecisionYes : kSentrySampleDecisionNo;
396396
}
397397

398-
#if PLATFORM_MAC
399-
SentryId* traceId = [[SENTRY_APPLE_CLASS(_TtC6Sentry8SentryId) alloc] initWithUUIDString:traceParts[0].GetNSString()];
400-
#elif PLATFORM_IOS
401398
SentryId* traceId = [[SENTRY_APPLE_CLASS(SentryId) alloc] initWithUUIDString:traceParts[0].GetNSString()];
402-
#endif
403399

404400
SentryTransactionContext* transactionContext = [[SENTRY_APPLE_CLASS(SentryTransactionContext) alloc] initWithName:@"<unlabeled transaction>" operation:@"default"
405401
traceId:traceId

plugin-dev/Source/Sentry/Private/Apple/AppleSentryUserFeedback.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,12 @@ FAppleSentryUserFeedback::FAppleSentryUserFeedback(TSharedPtr<ISentryId> eventId
1313
TSharedPtr<FAppleSentryId> idIOS = StaticCastSharedPtr<FAppleSentryId>(eventId);
1414
SentryId* id = idIOS->GetNativeObject();
1515

16-
#if PLATFORM_MAC
17-
UserFeedbackApple = [[SENTRY_APPLE_CLASS(_TtC6Sentry14SentryFeedback) alloc] initWithMessage:@""
18-
name:nil
19-
email:nil
20-
source:SentryFeedbackSourceCustom
21-
associatedEventId:id
22-
attachments:nil];
23-
#elif PLATFORM_IOS
2416
UserFeedbackApple = [[SENTRY_APPLE_CLASS(SentryFeedback) alloc] initWithMessage:@""
2517
name:nil
2618
email:nil
2719
source:SentryFeedbackSourceCustom
2820
associatedEventId:id
2921
attachments:nil];
30-
#endif
3122
}
3223

3324
FAppleSentryUserFeedback::~FAppleSentryUserFeedback()
@@ -80,19 +71,10 @@ SentryFeedback* FAppleSentryUserFeedback::CreateSentryFeedback(TSharedPtr<FApple
8071
TSharedPtr<FAppleSentryId> idIOS = StaticCastSharedPtr<FAppleSentryId>(feedback->EventId);
8172
SentryId* id = idIOS->GetNativeObject();
8273

83-
#if PLATFORM_MAC
84-
return [[SENTRY_APPLE_CLASS(_TtC6Sentry14SentryFeedback) alloc] initWithMessage:feedback->Comment.GetNSString()
85-
name:feedback->Name.GetNSString()
86-
email:feedback->Email.GetNSString()
87-
source:SentryFeedbackSourceCustom
88-
associatedEventId:id
89-
attachments:nil];
90-
#elif PLATFORM_IOS
9174
return [[SENTRY_APPLE_CLASS(SentryFeedback) alloc] initWithMessage:feedback->Comment.GetNSString()
9275
name:feedback->Name.GetNSString()
9376
email:feedback->Email.GetNSString()
9477
source:SentryFeedbackSourceCustom
9578
associatedEventId:id
9679
attachments:nil];
97-
#endif
9880
}

plugin-dev/Source/Sentry/Private/Apple/Convenience/AppleSentryMacro.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#if PLATFORM_MAC
1010
#define SENTRY_APPLE_CLASS(Name) \
11-
(__bridge Class) dlsym(FSentryModule::Get().GetSentryLibHandle(), "OBJC_CLASS_$_" #Name)
11+
FSentryModule::Get().GetSentryCocoaClass(#Name)
1212
#elif PLATFORM_IOS
1313
#define SENTRY_APPLE_CLASS(Name) \
1414
Name

plugin-dev/Source/Sentry/Private/SentryModule.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,30 @@ bool FSentryModule::IsMarketplaceVersion()
128128
}
129129

130130
#if PLATFORM_MAC
131+
131132
void* FSentryModule::GetSentryLibHandle() const
132133
{
133134
return mDllHandleSentry;
134135
}
136+
137+
Class FSentryModule::GetSentryCocoaClass(const ANSICHAR* ClassName)
138+
{
139+
ANSICHAR ClassNamePattern[256];
140+
FCStringAnsi::Snprintf(ClassNamePattern, sizeof(ClassNamePattern), "OBJC_CLASS_$_%s", ClassName);
141+
Class FoundClass = (__bridge Class) dlsym(GetSentryLibHandle(), ClassNamePattern);
142+
143+
if (!FoundClass)
144+
{
145+
// Try to load with Swift mangled class name
146+
ANSICHAR MangledClassNamePattern[256];
147+
int32 ClassNameLen = FCStringAnsi::Strlen(ClassName);
148+
FCStringAnsi::Snprintf(MangledClassNamePattern, sizeof(MangledClassNamePattern), "OBJC_CLASS_$__TtC6Sentry%d%s", ClassNameLen, ClassName);
149+
FoundClass = (__bridge Class) dlsym(GetSentryLibHandle(), MangledClassNamePattern);
150+
}
151+
152+
return FoundClass;
153+
}
154+
135155
#endif
136156

137157
#undef LOCTEXT_NAMESPACE

plugin-dev/Source/Sentry/Public/SentryModule.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ class SENTRY_API FSentryModule : public IModuleInterface
4747
#if PLATFORM_MAC
4848
/** Gets handle to dynamically loaded sentry library. */
4949
void* GetSentryLibHandle() const;
50+
51+
/** Gets an Objective-C/Swift class from the dynamically loaded sentry library.
52+
*
53+
* @param ClassName The unqualified class name (e.g., "SentrySDK", "SentryId")
54+
* @return The Objective-C/Swift Class object if found, nil otherwise
55+
*
56+
* @note: First tries pattern OBJC_CLASS_$_{ClassName}
57+
* Then tries Swift mangled pattern OBJC_CLASS_$__TtC6Sentry{len}{ClassName} where:
58+
* `_Tt` is Swift type symbol prefix
59+
* `C6Sentry` is a class (`C`) in the Sentry module (`6Sentry` means the module name is 6 characters long)
60+
* {len} is the length of the class name as a decimal string
61+
*/
62+
Class GetSentryCocoaClass(const ANSICHAR* ClassName);
5063
#endif
5164

5265
static const FName ModuleName;

0 commit comments

Comments
 (0)