Skip to content

Commit

Permalink
feat: iOS & macOS native context sync
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed May 20, 2022
1 parent c824da0 commit 74db263
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 0 deletions.
84 changes: 84 additions & 0 deletions package-dev/Plugins/iOS/SentryNativeBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,88 @@ void SentryNativeBridgeUnsetUser()
return cString;
}

inline NSString *_NSStringOrNil(const char *value)
{
return value ? [NSString stringWithUTF8String:value] : nil;
}

inline NSString *_NSNumberOrNil(int32_t value) { return value == 0 ? nil : @(value); }

inline NSNumber *_NSBoolOrNil(int8_t value)
{
if (value == 0) {
return @NO;
}
if (value == 1) {
return @YES;
}
return nil;
}

void SentryNativeBridgeWriteScope( // clang-format off
// // const char *AppStartTime,
// const char *AppBuildType,
// // const char *OperatingSystemRawDescription,
// int DeviceProcessorCount,
// const char *DeviceCpuDescription,
// const char *DeviceTimezone,
// int8_t DeviceSupportsVibration,
// const char *DeviceName,
// int8_t DeviceSimulator,
// const char *DeviceDeviceUniqueIdentifier,
// const char *DeviceDeviceType,
// // const char *DeviceModel,
// // long DeviceMemorySize,
int32_t GpuId,
const char *GpuName,
const char *GpuVendorName,
int32_t GpuMemorySize,
const char *GpuNpotSupport,
const char *GpuVersion,
const char *GpuApiType,
int32_t GpuMaxTextureSize,
int8_t GpuSupportsDrawCallInstancing,
int8_t GpuSupportsRayTracing,
int8_t GpuSupportsComputeShaders,
int8_t GpuSupportsGeometryShaders,
const char *GpuVendorId,
int8_t GpuMultiThreadedRendering,
const char *GpuGraphicsShaderLevel,
const char *UnityInstallMode,
const char *UnityTargetFrameRate,
const char *UnityCopyTextureSupport,
const char *UnityRenderingThreadingMode
) // clang-format on
{
// Note: we're using a NSMutableDictionary because it will skip fields with nil values.
[SentrySDK configureScope:^(SentryScope *scope) {
NSMutableDictionary *gpu = [[NSMutableDictionary alloc] init];
gpu[@"id"] = _NSNumberOrNil(GpuId);
gpu[@"name"] = _NSStringOrNil(GpuName);
gpu[@"vendor_name"] = _NSStringOrNil(GpuVendorName);
gpu[@"memory_size"] = _NSNumberOrNil(GpuMemorySize);
gpu[@"npot_support"] = _NSStringOrNil(GpuNpotSupport);
gpu[@"version"] = _NSStringOrNil(GpuVersion);
gpu[@"api_type"] = _NSStringOrNil(GpuApiType);
gpu[@"max_texture_size"] = _NSNumberOrNil(GpuMaxTextureSize);
gpu[@"supports_draw_call_instancing"] = _NSBoolOrNil(GpuSupportsDrawCallInstancing);
gpu[@"supports_ray_tracing"] = _NSBoolOrNil(GpuSupportsRayTracing);
gpu[@"supports_compute_shaders"] = _NSBoolOrNil(GpuSupportsComputeShaders);
gpu[@"supports_geometry_shaders"] = _NSBoolOrNil(GpuSupportsGeometryShaders);
gpu[@"vendor_id"] = _NSStringOrNil(GpuVendorId);
gpu[@"multi_threaded_rendering"] = _NSBoolOrNil(GpuMultiThreadedRendering);
gpu[@"graphics_shader_level"] = _NSStringOrNil(GpuGraphicsShaderLevel);
[scope performSelector:@selector(setContextValue:forKey:) withObject:gpu withObject:@"gpu"];

NSMutableDictionary *unity = [[NSMutableDictionary alloc] init];
unity[@"install_mode"] = _NSStringOrNil(UnityInstallMode);
unity[@"target_frame_rate"] = _NSStringOrNil(UnityTargetFrameRate);
unity[@"copy_texture_support"] = _NSStringOrNil(UnityCopyTextureSupport);
unity[@"rendering_threading_mode"] = _NSStringOrNil(UnityRenderingThreadingMode);
[scope performSelector:@selector(setContextValue:forKey:)
withObject:unity
withObject:@"unity"];
}];
}

NS_ASSUME_NONNULL_END
84 changes: 84 additions & 0 deletions package-dev/Plugins/macOS/SentryNativeBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,87 @@ void SentryNativeBridgeUnsetUser()
memcpy(cString, nsStringUtf8, len);
return cString;
}

inline NSString *_NSStringOrNil(const char *str)
{
return str ? [NSString stringWithUTF8String:str] : nil;
}

inline NSString *_NSNumberOrNil(int32_t value) { return value == 0 ? nil : @(value); }

inline NSNumber *_NSBoolOrNil(int8_t value)
{
if (value == 0) {
return @NO;
}
if (value == 1) {
return @YES;
}
return nil;
}

void SentryNativeBridgeWriteScope( // clang-format off
// // const char *AppStartTime,
// const char *AppBuildType,
// // const char *OperatingSystemRawDescription,
// int DeviceProcessorCount,
// const char *DeviceCpuDescription,
// const char *DeviceTimezone,
// int8_t DeviceSupportsVibration,
// const char *DeviceName,
// int8_t DeviceSimulator,
// const char *DeviceDeviceUniqueIdentifier,
// const char *DeviceDeviceType,
// // const char *DeviceModel,
// // long DeviceMemorySize,
int32_t GpuId,
const char *GpuName,
const char *GpuVendorName,
int32_t GpuMemorySize,
const char *GpuNpotSupport,
const char *GpuVersion,
const char *GpuApiType,
int32_t GpuMaxTextureSize,
int8_t GpuSupportsDrawCallInstancing,
int8_t GpuSupportsRayTracing,
int8_t GpuSupportsComputeShaders,
int8_t GpuSupportsGeometryShaders,
const char *GpuVendorId,
int8_t GpuMultiThreadedRendering,
const char *GpuGraphicsShaderLevel,
const char *UnityInstallMode,
const char *UnityTargetFrameRate,
const char *UnityCopyTextureSupport,
const char *UnityRenderingThreadingMode
) // clang-format on
{
// Note: we're using a NSMutableDictionary because it will skip fields with nil values.
SentryConfigureScope(^(id scope) {
NSMutableDictionary *gpu = [[NSMutableDictionary alloc] init];
gpu[@"id"] = _NSNumberOrNil(GpuId);
gpu[@"name"] = _NSStringOrNil(GpuName);
gpu[@"vendor_name"] = _NSStringOrNil(GpuVendorName);
gpu[@"memory_size"] = _NSNumberOrNil(GpuMemorySize);
gpu[@"npot_support"] = _NSStringOrNil(GpuNpotSupport);
gpu[@"version"] = _NSStringOrNil(GpuVersion);
gpu[@"api_type"] = _NSStringOrNil(GpuApiType);
gpu[@"max_texture_size"] = _NSNumberOrNil(GpuMaxTextureSize);
gpu[@"supports_draw_call_instancing"] = _NSBoolOrNil(GpuSupportsDrawCallInstancing);
gpu[@"supports_ray_tracing"] = _NSBoolOrNil(GpuSupportsRayTracing);
gpu[@"supports_compute_shaders"] = _NSBoolOrNil(GpuSupportsComputeShaders);
gpu[@"supports_geometry_shaders"] = _NSBoolOrNil(GpuSupportsGeometryShaders);
gpu[@"vendor_id"] = _NSStringOrNil(GpuVendorId);
gpu[@"multi_threaded_rendering"] = _NSBoolOrNil(GpuMultiThreadedRendering);
gpu[@"graphics_shader_level"] = _NSStringOrNil(GpuGraphicsShaderLevel);
[scope performSelector:@selector(setContextValue:forKey:) withObject:gpu withObject:@"gpu"];

NSMutableDictionary *unity = [[NSMutableDictionary alloc] init];
unity[@"install_mode"] = _NSStringOrNil(UnityInstallMode);
unity[@"target_frame_rate"] = _NSStringOrNil(UnityTargetFrameRate);
unity[@"copy_texture_support"] = _NSStringOrNil(UnityCopyTextureSupport);
unity[@"rendering_threading_mode"] = _NSStringOrNil(UnityRenderingThreadingMode);
[scope performSelector:@selector(setContextValue:forKey:)
withObject:unity
withObject:@"unity"];
});
}
119 changes: 119 additions & 0 deletions src/Sentry.Unity.iOS/NativeContextWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;

namespace Sentry.Unity.iOS
{
internal class NativeContextWriter : ContextWriter
{
protected override void WriteScope(
string? AppStartTime,
string? AppBuildType,
string? OperatingSystemRawDescription,
int? DeviceProcessorCount,
string? DeviceCpuDescription,
string? DeviceTimezone,
bool? DeviceSupportsVibration,
string? DeviceName,
bool? DeviceSimulator,
string? DeviceDeviceUniqueIdentifier,
string? DeviceDeviceType,
string? DeviceModel,
long? DeviceMemorySize,
int? GpuId,
string? GpuName,
string? GpuVendorName,
int? GpuMemorySize,
string? GpuNpotSupport,
string? GpuVersion,
string? GpuApiType,
int? GpuMaxTextureSize,
bool? GpuSupportsDrawCallInstancing,
bool? GpuSupportsRayTracing,
bool? GpuSupportsComputeShaders,
bool? GpuSupportsGeometryShaders,
string? GpuVendorId,
bool? GpuMultiThreadedRendering,
string? GpuGraphicsShaderLevel,
string? UnityInstallMode,
string? UnityTargetFrameRate,
string? UnityCopyTextureSupport,
string? UnityRenderingThreadingMode
) => SentryNativeBridgeWriteScope(
// // AppStartTime,
// AppBuildType,
// // OperatingSystemRawDescription,
// DeviceProcessorCount ?? 0,
// DeviceCpuDescription,
// DeviceTimezone,
// marshallNullableBool(DeviceSupportsVibration),
// DeviceName,
// marshallNullableBool(DeviceSimulator),
// DeviceDeviceUniqueIdentifier,
// DeviceDeviceType,
// // DeviceModel,
// // DeviceMemorySize,
GpuId ?? 0,
GpuName,
GpuVendorName,
GpuMemorySize ?? 0,
GpuNpotSupport,
GpuVersion,
GpuApiType,
GpuMaxTextureSize ?? 0,
marshallNullableBool(GpuSupportsDrawCallInstancing),
marshallNullableBool(GpuSupportsRayTracing),
marshallNullableBool(GpuSupportsComputeShaders),
marshallNullableBool(GpuSupportsGeometryShaders),
GpuVendorId,
marshallNullableBool(GpuMultiThreadedRendering),
GpuGraphicsShaderLevel,
UnityInstallMode,
UnityTargetFrameRate,
UnityCopyTextureSupport,
UnityRenderingThreadingMode
);

private static sbyte marshallNullableBool(bool? value) => (sbyte)(value.HasValue ? (value.Value ? 1 : 0) : -1);

// Note: we only forward information that's missing or significantly different in cocoa SDK events.
// Additionally, there's currently no way to update existing contexts, so no more Device info for now...
[DllImport("__Internal")]
private static extern void SentryNativeBridgeWriteScope(
// // string? AppStartTime,
// string? AppBuildType,
// // string? OperatingSystemRawDescription,
// int DeviceProcessorCount,
// string? DeviceCpuDescription,
// string? DeviceTimezone,
// sbyte DeviceSupportsVibration,
// string? DeviceName,
// sbyte DeviceSimulator,
// string? DeviceDeviceUniqueIdentifier,
// string? DeviceDeviceType,
// // string? DeviceModel,
// // long? DeviceMemorySize,
int GpuId,
string? GpuName,
string? GpuVendorName,
int GpuMemorySize,
string? GpuNpotSupport,
string? GpuVersion,
string? GpuApiType,
int GpuMaxTextureSize,
sbyte GpuSupportsDrawCallInstancing,
sbyte GpuSupportsRayTracing,
sbyte GpuSupportsComputeShaders,
sbyte GpuSupportsGeometryShaders,
string? GpuVendorId,
sbyte GpuMultiThreadedRendering,
string? GpuGraphicsShaderLevel,
string? UnityInstallMode,
string? UnityTargetFrameRate,
string? UnityCopyTextureSupport,
string? UnityRenderingThreadingMode
);
}
}
1 change: 1 addition & 0 deletions src/Sentry.Unity.iOS/SentryNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal static void Configure(SentryUnityOptions options, ISentryUnityInfo sent
return;
}

options.NativeContextWriter = new NativeContextWriter();
options.EnableScopeSync = true;
options.CrashedLastRun = () =>
{
Expand Down

0 comments on commit 74db263

Please sign in to comment.