DataPrivacy.cs
4.32 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
131
132
#if ENABLE_CLOUD_SERVICES_ANALYTICS
using System;
using System.Text;
using UnityEngine.Networking;
namespace UnityEngine.Analytics
{
public class DataPrivacy
{
[Serializable]
internal struct UserPostData
{
public string appid;
public string userid;
public long sessionid;
public string platform;
public UInt32 platformid;
public string sdk_ver;
public bool debug_device;
public string deviceid;
public string plugin_ver;
}
[Serializable]
internal struct TokenData
{
public string url;
public string token;
}
const string kVersion = "3.0.0";
const string kVersionString = "DataPrivacyPackage/" + kVersion;
internal const string kBaseUrl = "https://data-optout-service.uca.cloud.unity3d.com";
const string kTokenUrl = kBaseUrl + "/token";
internal static UserPostData GetUserData()
{
var postData = new UserPostData
{
appid = Application.cloudProjectId,
userid = AnalyticsSessionInfo.userId,
sessionid = AnalyticsSessionInfo.sessionId,
platform = Application.platform.ToString(),
platformid = (UInt32)Application.platform,
sdk_ver = Application.unityVersion,
debug_device = Debug.isDebugBuild,
deviceid = SystemInfo.deviceUniqueIdentifier,
plugin_ver = kVersionString
};
return postData;
}
static string GetUserAgent()
{
var message = "UnityPlayer/{0} ({1}/{2}{3} {4})";
return String.Format(message,
Application.unityVersion,
Application.platform.ToString(),
(UInt32)Application.platform,
Debug.isDebugBuild ? "-dev" : "",
kVersionString);
}
static String getErrorString(UnityWebRequest www)
{
var json = www.downloadHandler.text;
var error = www.error;
if (String.IsNullOrEmpty(error))
{
// 5.5 sometimes fails to parse an error response, and the only clue will be
// in www.responseHeadersString, which isn't accessible.
error = "Empty response";
}
if (!String.IsNullOrEmpty(json))
{
error += ": " + json;
}
return error;
}
public static void FetchPrivacyUrl(Action<string> success, Action<string> failure = null)
{
string postJson = JsonUtility.ToJson(GetUserData());
byte[] bytes = Encoding.UTF8.GetBytes(postJson);
var uploadHandler = new UploadHandlerRaw(bytes);
uploadHandler.contentType = "application/json";
var www = UnityWebRequest.Post(kTokenUrl, "");
www.uploadHandler = uploadHandler;
#if !UNITY_WEBGL
www.SetRequestHeader("User-Agent", GetUserAgent());
#endif
var async = www.SendWebRequest();
async.completed += (AsyncOperation async2) =>
{
var json = www.downloadHandler.text;
if (!String.IsNullOrEmpty(www.error) || String.IsNullOrEmpty(json))
{
var error = getErrorString(www);
if (failure != null)
{
failure(error);
}
}
else
{
TokenData tokenData;
tokenData.url = ""; // Just to quell "possibly unassigned" error
try
{
tokenData = JsonUtility.FromJson<TokenData>(json);
}
catch (Exception e)
{
if (failure != null)
{
failure(e.ToString());
}
}
success(tokenData.url);
}
};
}
}
}
#endif //ENABLE_CLOUD_SERVICES_ANALYTICS