PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/GameAnalytics/Plugins/Framework/Scripts/GA_GenericInfo.cs

https://bitbucket.org/AgentCodeMonkey/gameframework-unity-project
C# | 265 lines | 148 code | 45 blank | 72 comment | 12 complexity | c575a97d051ea70195f6048f5cb01e11 MD5 | raw file
  1. /// <summary>
  2. /// This class handles user ID, session ID, time stamp, and sends a user message, optionally including system specs, when the game starts
  3. /// </summary>
  4. using UnityEngine;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Runtime.InteropServices;
  8. using System;
  9. using System.Net;
  10. #if !UNITY_WEBPLAYER
  11. using System.Net.NetworkInformation;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. #endif
  15. public class GA_GenericInfo
  16. {
  17. #region public values
  18. /// <summary>
  19. /// The ID of the user/player. A unique ID will be determined the first time the player plays. If an ID has already been created for a player this ID will be used.
  20. /// </summary>
  21. public string UserID
  22. {
  23. get {
  24. if (_userID == null && !GA.Settings.CustomUserID)
  25. {
  26. if (PlayerPrefs.HasKey("GA_uid"))
  27. {
  28. _userID = PlayerPrefs.GetString("GA_uid");
  29. }
  30. else
  31. {
  32. _userID = GetUserUUID();
  33. PlayerPrefs.SetString("GA_uid", _userID);
  34. PlayerPrefs.Save();
  35. }
  36. }
  37. return _userID;
  38. }
  39. }
  40. /// <summary>
  41. /// The ID of the current session. A unique ID will be determined when the game starts. This ID will be used for the remainder of the play session.
  42. /// </summary>
  43. public string SessionID
  44. {
  45. get {
  46. if (_sessionID == null)
  47. {
  48. _sessionID = GetSessionUUID();
  49. }
  50. return _sessionID;
  51. }
  52. }
  53. /// <summary>
  54. /// The current UTC date/time in seconds
  55. /// </summary>
  56. public string TimeStamp
  57. {
  58. get {
  59. return ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
  60. }
  61. }
  62. #endregion
  63. #region private values
  64. private string _userID;
  65. private string _sessionID;
  66. private bool _settingUserID;
  67. #endregion
  68. #region public methods
  69. /// <summary>
  70. /// Gets generic system information at the beginning of a play session
  71. /// </summary>
  72. /// <param name="inclSpecs">
  73. /// Determines if all the system specs should be included <see cref="System.Bool"/>
  74. /// </param>
  75. /// <returns>
  76. /// The message to submit to the GA server is a dictionary of all the relevant parameters (containing user ID, session ID, system information, language information, date/time, build version) <see cref="Dictionary<System.String, System.Object>"/>
  77. /// </returns>
  78. public List<Dictionary<string, object>> GetGenericInfo(string message)
  79. {
  80. List<Dictionary<string, object>> systemspecs = new List<Dictionary<string, object>>();
  81. /*
  82. * Apple does not allow tracking of device specific data:
  83. * "You may not use analytics software in your application to collect and send device data to a third party"
  84. * - iOS Developer Program License Agreement: http://www.scribd.com/doc/41213383/iOS-Developer-Program-License-Agreement
  85. */
  86. #if !UNITY_IPHONE
  87. systemspecs.Add(AddSystemSpecs("unity_wrapper", GA_Settings.VERSION, message));
  88. systemspecs.Add(AddSystemSpecs("os", SystemInfo.operatingSystem, message));
  89. systemspecs.Add(AddSystemSpecs("processor_type", SystemInfo.processorType, message));
  90. systemspecs.Add(AddSystemSpecs("gfx_name", SystemInfo.graphicsDeviceName, message));
  91. systemspecs.Add(AddSystemSpecs("gfx_version", SystemInfo.graphicsDeviceVersion, message));
  92. // Unity provides lots of additional system info which might be worth tracking for some games:
  93. //systemspecs.Add(AddSystemSpecs("process_count", SystemInfo.processorCount.ToString(), message));
  94. //systemspecs.Add(AddSystemSpecs("sys_mem_size", SystemInfo.systemMemorySize.ToString(), message));
  95. //systemspecs.Add(AddSystemSpecs("gfx_mem_size", SystemInfo.graphicsMemorySize.ToString(), message));
  96. //systemspecs.Add(AddSystemSpecs("gfx_vendor", SystemInfo.graphicsDeviceVendor, message));
  97. //systemspecs.Add(AddSystemSpecs("gfx_id", SystemInfo.graphicsDeviceID.ToString(), message));
  98. //systemspecs.Add(AddSystemSpecs("gfx_vendor_id", SystemInfo.graphicsDeviceVendorID.ToString(), message));
  99. //systemspecs.Add(AddSystemSpecs("gfx_shader_level", SystemInfo.graphicsShaderLevel.ToString(), message));
  100. //systemspecs.Add(AddSystemSpecs("gfx_pixel_fillrate", SystemInfo.graphicsPixelFillrate.ToString(), message));
  101. //systemspecs.Add(AddSystemSpecs("sup_shadows", SystemInfo.supportsShadows.ToString(), message));
  102. //systemspecs.Add(AddSystemSpecs("sup_render_textures", SystemInfo.supportsRenderTextures.ToString(), message));
  103. //systemspecs.Add(AddSystemSpecs("sup_image_effects", SystemInfo.supportsImageEffects.ToString(), message));
  104. #else
  105. systemspecs.Add(AddSystemSpecs("os", "iOS", message));
  106. #endif
  107. return systemspecs;
  108. }
  109. /// <summary>
  110. /// Gets a universally unique ID to represent the user. User ID should be device specific to allow tracking across different games on the same device:
  111. /// -- Android uses deviceUniqueIdentifier.
  112. /// -- iOS/PC/Mac uses the first MAC addresses available.
  113. /// -- Webplayer uses deviceUniqueIdentifier.
  114. /// Note: The unique user ID is based on the ODIN specifications. See http://code.google.com/p/odinmobile/ for more information on ODIN.
  115. /// </summary>
  116. /// <returns>
  117. /// The generated UUID <see cref="System.String"/>
  118. /// </returns>
  119. public string GetUserUUID()
  120. {
  121. #if UNITY_WEBPLAYER
  122. return SystemInfo.deviceUniqueIdentifier;
  123. #else
  124. try
  125. {
  126. NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  127. string mac = "";
  128. foreach (NetworkInterface adapter in nics)
  129. {
  130. PhysicalAddress address = adapter.GetPhysicalAddress();
  131. if (address.ToString() != "" && mac == "")
  132. {
  133. byte[] bytes = Encoding.UTF8.GetBytes(address.ToString());
  134. SHA1CryptoServiceProvider SHA = new SHA1CryptoServiceProvider();
  135. mac = BitConverter.ToString(SHA.ComputeHash(bytes)).Replace("-", "");
  136. }
  137. }
  138. return mac;
  139. }
  140. catch
  141. {
  142. return SystemInfo.deviceUniqueIdentifier;
  143. }
  144. #endif
  145. }
  146. /// <summary>
  147. /// Gets a universally unique ID to represent the session.
  148. /// </summary>
  149. /// <returns>
  150. /// The generated UUID <see cref="System.String"/>
  151. /// </returns>
  152. public string GetSessionUUID()
  153. {
  154. return Guid.NewGuid().ToString();
  155. }
  156. /// <summary>
  157. /// Do not call this method (instead use GA_static_api.Settings.SetCustomUserID)! Only the GA class should call this method.
  158. /// </summary>
  159. /// <param name="customID">
  160. /// The custom user ID - this should be unique for each user
  161. /// </param>
  162. public void SetCustomUserID(string customID)
  163. {
  164. _userID = customID;
  165. }
  166. #endregion
  167. #region private methods
  168. /// <summary>
  169. /// Adds detailed system specifications regarding the users/players device to the parameters.
  170. /// </summary>
  171. /// <param name="parameters">
  172. /// The parameters which will be sent to the server <see cref="Dictionary<System.String, System.Object>"/>
  173. /// </param>
  174. private Dictionary<string, object> AddSystemSpecs(string key, string type, string message)
  175. {
  176. string addmessage = "";
  177. if (message != "")
  178. addmessage = ": " + message;
  179. Dictionary<string, object> parameters = new Dictionary<string, object>()
  180. {
  181. { GA_ServerFieldTypes.Fields[GA_ServerFieldTypes.FieldType.EventID], "system:" + key },
  182. { GA_ServerFieldTypes.Fields[GA_ServerFieldTypes.FieldType.Message], type + addmessage },
  183. { GA_ServerFieldTypes.Fields[GA_ServerFieldTypes.FieldType.Level], GA.Settings.CustomArea.Equals(string.Empty)?Application.loadedLevelName:GA.Settings.CustomArea }
  184. };
  185. return parameters;
  186. }
  187. /// <summary>
  188. /// Gets the users system type
  189. /// </summary>
  190. /// <returns>
  191. /// String determining the system the user is currently running <see cref="System.String"/>
  192. /// </returns>
  193. private string GetSystem()
  194. {
  195. #if UNITY_STANDALONE_OSX
  196. return "MAC";
  197. #elif UNITY_STANDALONE_WIN
  198. return "PC";
  199. #elif UNITY_WEBPLAYER
  200. return "WEBPLAYER";
  201. #elif UNITY_WII
  202. return "WII";
  203. #elif UNITY_IPHONE
  204. return "IPHONE";
  205. #elif UNITY_ANDROID
  206. return "ANDROID";
  207. #elif UNITY_PS3
  208. return "PS3";
  209. #elif UNITY_XBOX360
  210. return "XBOX";
  211. #elif UNITY_FLASH
  212. return "FLASH";
  213. #elif UNITY_LINUX
  214. return "LINUX";
  215. #else
  216. return "UNKNOWN";
  217. #endif
  218. }
  219. #endregion
  220. }