DeviceInfo.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Globalization;
  4. using System.Collections.Generic;
  5. using System.Net.NetworkInformation;
  6. using UnityEngine;
  7. using System.Diagnostics;
  8. using TapSDK.Core.Internal.Log;
  9. namespace TapSDK.Core.Standalone.Internal
  10. {
  11. public class DeviceInfo
  12. {
  13. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
  14. [DllImport("TapDBDeviceInfo", CallingConvention = CallingConvention.Cdecl)]
  15. private static extern IntPtr GetDeviceLanguage();
  16. #endif
  17. public static string GetLanguage()
  18. {
  19. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
  20. return Marshal.PtrToStringAnsi(GetDeviceLanguage());
  21. #else
  22. return CultureInfo.CurrentUICulture.IetfLanguageTag;
  23. #endif
  24. }
  25. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  26. [DllImport("kernel32.dll")]
  27. static extern IntPtr GetCurrentProcess();
  28. [DllImport("kernel32.dll")]
  29. static extern uint GetProcessTimes(IntPtr processHandle,
  30. out long creationTime,
  31. out long exitTime,
  32. out long kernelTime,
  33. out long userTime);
  34. static DateTime GetProcessStartTime()
  35. {
  36. IntPtr processHandle = GetCurrentProcess();
  37. long creationTime;
  38. GetProcessTimes(processHandle,
  39. out creationTime,
  40. out _,
  41. out _,
  42. out _);
  43. return DateTime.FromFileTime(creationTime);
  44. }
  45. #endif
  46. //安全组提供的设备识别 ID 算法,用于后续数据串联
  47. public static string GetLaunchUniqueID()
  48. {
  49. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  50. // 获取当前进程对象
  51. Process currentProcess = Process.GetCurrentProcess();
  52. // 获取进程启动时间
  53. DateTime startTime = GetProcessStartTime();
  54. return toMd5(startTime.ToFileTime().ToString() + "-" + currentProcess.Id.ToString());
  55. #else
  56. return "";
  57. #endif
  58. }
  59. public static void GetMacAddress(out string macAddressList, out string firstMacAddress)
  60. {
  61. List<string> mac_addrs = new List<string>();
  62. try
  63. {
  64. NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  65. foreach (NetworkInterface adapter in nics)
  66. {
  67. string physicalAddress = adapter.GetPhysicalAddress().ToString();
  68. if (string.IsNullOrEmpty(physicalAddress))
  69. continue;
  70. physicalAddress = $"\"{physicalAddress}\"";
  71. if (mac_addrs.IndexOf(physicalAddress) == -1)
  72. mac_addrs.Add(physicalAddress);
  73. }
  74. // sort
  75. mac_addrs.Sort();
  76. }
  77. catch (Exception e)
  78. {
  79. TapLog.Log("GetMacAddress Exception " + e.Message);
  80. }
  81. macAddressList = $"[{string.Join(",", mac_addrs)}]";
  82. firstMacAddress = mac_addrs.Count > 0 ? mac_addrs[0].Replace("\"", "") : string.Empty;
  83. }
  84. private static string toMd5(string data)
  85. {
  86. byte[] buffer = System.Text.Encoding.Default.GetBytes(data);
  87. try
  88. {
  89. System.Security.Cryptography.MD5CryptoServiceProvider chk = new System.Security.Cryptography.MD5CryptoServiceProvider();
  90. byte[] some = chk.ComputeHash(buffer);
  91. string ret = "";
  92. foreach (byte a in some)
  93. {
  94. if (a < 16)
  95. ret += "0" + a.ToString("X");
  96. else
  97. ret += a.ToString("X");
  98. }
  99. return ret.ToLower();
  100. }
  101. catch
  102. {
  103. throw;
  104. }
  105. }
  106. public static string RAM
  107. {
  108. get
  109. {
  110. return (SystemInfo.systemMemorySize * 1024L * 1024L).ToString();
  111. }
  112. }
  113. public static string Local
  114. {
  115. get
  116. {
  117. return CultureInfo.CurrentCulture.Name.Replace('-', '_');
  118. }
  119. }
  120. }
  121. }