You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.3 KiB

  1. #if ENABLE_CLOUD_SERVICES_ANALYTICS
  2. using System;
  3. using System.Text;
  4. using UnityEngine.Networking;
  5. namespace UnityEngine.Analytics
  6. {
  7. public class DataPrivacy
  8. {
  9. [Serializable]
  10. internal struct UserPostData
  11. {
  12. public string appid;
  13. public string userid;
  14. public long sessionid;
  15. public string platform;
  16. public UInt32 platformid;
  17. public string sdk_ver;
  18. public bool debug_device;
  19. public string deviceid;
  20. public string plugin_ver;
  21. }
  22. [Serializable]
  23. internal struct TokenData
  24. {
  25. public string url;
  26. public string token;
  27. }
  28. const string kVersion = "3.0.0";
  29. const string kVersionString = "DataPrivacyPackage/" + kVersion;
  30. internal const string kBaseUrl = "https://data-optout-service.uca.cloud.unity3d.com";
  31. const string kTokenUrl = kBaseUrl + "/token";
  32. internal static UserPostData GetUserData()
  33. {
  34. var postData = new UserPostData
  35. {
  36. appid = Application.cloudProjectId,
  37. userid = AnalyticsSessionInfo.userId,
  38. sessionid = AnalyticsSessionInfo.sessionId,
  39. platform = Application.platform.ToString(),
  40. platformid = (UInt32)Application.platform,
  41. sdk_ver = Application.unityVersion,
  42. debug_device = Debug.isDebugBuild,
  43. deviceid = SystemInfo.deviceUniqueIdentifier,
  44. plugin_ver = kVersionString
  45. };
  46. return postData;
  47. }
  48. static string GetUserAgent()
  49. {
  50. var message = "UnityPlayer/{0} ({1}/{2}{3} {4})";
  51. return String.Format(message,
  52. Application.unityVersion,
  53. Application.platform.ToString(),
  54. (UInt32)Application.platform,
  55. Debug.isDebugBuild ? "-dev" : "",
  56. kVersionString);
  57. }
  58. static String getErrorString(UnityWebRequest www)
  59. {
  60. var json = www.downloadHandler.text;
  61. var error = www.error;
  62. if (String.IsNullOrEmpty(error))
  63. {
  64. // 5.5 sometimes fails to parse an error response, and the only clue will be
  65. // in www.responseHeadersString, which isn't accessible.
  66. error = "Empty response";
  67. }
  68. if (!String.IsNullOrEmpty(json))
  69. {
  70. error += ": " + json;
  71. }
  72. return error;
  73. }
  74. public static void FetchPrivacyUrl(Action<string> success, Action<string> failure = null)
  75. {
  76. string postJson = JsonUtility.ToJson(GetUserData());
  77. byte[] bytes = Encoding.UTF8.GetBytes(postJson);
  78. var uploadHandler = new UploadHandlerRaw(bytes);
  79. uploadHandler.contentType = "application/json";
  80. var www = UnityWebRequest.Post(kTokenUrl, "");
  81. www.uploadHandler = uploadHandler;
  82. #if !UNITY_WEBGL
  83. www.SetRequestHeader("User-Agent", GetUserAgent());
  84. #endif
  85. var async = www.SendWebRequest();
  86. async.completed += (AsyncOperation async2) =>
  87. {
  88. var json = www.downloadHandler.text;
  89. if (!String.IsNullOrEmpty(www.error) || String.IsNullOrEmpty(json))
  90. {
  91. var error = getErrorString(www);
  92. if (failure != null)
  93. {
  94. failure(error);
  95. }
  96. }
  97. else
  98. {
  99. TokenData tokenData;
  100. tokenData.url = ""; // Just to quell "possibly unassigned" error
  101. try
  102. {
  103. tokenData = JsonUtility.FromJson<TokenData>(json);
  104. }
  105. catch (Exception e)
  106. {
  107. if (failure != null)
  108. {
  109. failure(e.ToString());
  110. }
  111. }
  112. success(tokenData.url);
  113. }
  114. };
  115. }
  116. }
  117. }
  118. #endif //ENABLE_CLOUD_SERVICES_ANALYTICS