-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathDeviceCollector.cs
More file actions
130 lines (111 loc) · 4.61 KB
/
Copy pathDeviceCollector.cs
File metadata and controls
130 lines (111 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using Immutable.Audience.Unity.Mobile;
using UnityEngine;
namespace Immutable.Audience.Unity
{
internal static class DeviceCollector
{
internal static Dictionary<string, object> CollectContext()
{
// 256-char cap mirrors Web SDK's identifier truncation.
var ctx = new Dictionary<string, object>
{
["userAgent"] = Truncate(SystemInfo.operatingSystem, 256),
};
var timezone = SafeTimezone();
if (timezone != null) ctx["timezone"] = Truncate(timezone, 256);
var locale = LocaleString();
if (locale != null) ctx["locale"] = Truncate(locale, 256);
var screen = TryResolveScreenString();
if (screen != null) ctx["screen"] = Truncate(screen, 256);
return ctx;
}
private static string? TryResolveScreenString()
{
var resolution = Screen.currentResolution;
int width = resolution.width;
int height = resolution.height;
if (width <= 0 || height <= 0)
{
width = Screen.width;
height = Screen.height;
}
if (width <= 0 || height <= 0) return null;
return $"{width}x{height}";
}
internal static Dictionary<string, object> CollectGameLaunchProperties(
RuntimePlatform? platformOverride = null,
bool? isEditorOverride = null)
{
var platform = platformOverride ?? Application.platform;
var isEditor = isEditorOverride ?? Application.isEditor;
var props = new Dictionary<string, object>
{
["platform"] = PlatformName(platform),
["is_editor"] = isEditor,
["version"] = Truncate(Application.version, 256),
["build_guid"] = Truncate(Application.buildGUID, 256),
["unity_version"] = Truncate(Application.unityVersion, 256),
["os_family"] = SystemInfo.operatingSystemFamily.ToString(),
["device_model"] = Truncate(SystemInfo.deviceModel, 256),
["gpu"] = Truncate(SystemInfo.graphicsDeviceName, 256),
["gpu_vendor"] = Truncate(SystemInfo.graphicsDeviceVendor, 256),
["cpu"] = Truncate(SystemInfo.processorType, 256),
["cpu_cores"] = SystemInfo.processorCount,
["ram_mb"] = SystemInfo.systemMemorySize,
};
// Screen.dpi can be 0 on some Linux WMs.
var dpi = (int)Screen.dpi;
if (dpi > 0) props["screen_dpi"] = dpi;
if (platform == RuntimePlatform.Android)
props["android_id"] = Truncate(SystemInfo.deviceUniqueIdentifier, 256);
if (platform == RuntimePlatform.IPhonePlayer)
{
var idfv = IDFVBridge.GetIDFV();
if (idfv != null) props["idfv"] = Truncate(idfv, 256);
// iOS baseline is 163 DPI (1×); 326 → 2×, 401-460 → 3×.
if (dpi > 0) props["screen_scale"] = (int)Math.Round(dpi / 163.0);
}
return props;
}
private static string? LocaleString()
{
var culture = CultureInfo.CurrentCulture;
if (!string.IsNullOrEmpty(culture?.Name))
return culture.Name;
return null;
}
private static string? SafeTimezone()
{
try
{
return TimeZoneInfo.Local.Id;
}
catch (Exception)
{
return null;
}
}
private static string PlatformName(RuntimePlatform platform) => platform switch
{
RuntimePlatform.WindowsPlayer or RuntimePlatform.WindowsEditor => "Windows",
RuntimePlatform.OSXPlayer or RuntimePlatform.OSXEditor => "macOS",
RuntimePlatform.LinuxPlayer or RuntimePlatform.LinuxEditor => "Linux",
RuntimePlatform.IPhonePlayer => "iOS",
RuntimePlatform.Android => "Android",
_ => platform.ToString(),
};
private static string Truncate(string s, int max)
{
if (string.IsNullOrEmpty(s) || s.Length <= max) return s;
// Step back one if the cut would split a surrogate pair. Leaving
// a lone high-surrogate produces invalid UTF-16 on the wire.
var cut = max;
if (char.IsHighSurrogate(s[cut - 1])) cut--;
return s.Substring(0, cut);
}
}
}