zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Hotfix/Logic/Behaviour/Game/Helper/IdentifyHelper.cs

133 lines
4.5 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace ET
{
public class IdentifyHelper
{
private static string Localkey()
{
#if UNITY
string path = Path.Combine(new DirectoryInfo(Application.persistentDataPath).Parent.FullName, "client");
2021-04-08 20:09:59 +08:00
if (!Directory.Exists(path))
{
DirectoryInfo di = Directory.CreateDirectory(path);
di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
}
path = Path.Combine(path, "key.key");
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path);
string key = sr.ReadToEnd();
byte[] bytes = Encoding.UTF8.GetBytes(key);
Utility.Encryption.GetSelfXorBytes(bytes, GameKeyComponent.Instance.xorKey);
key= Encoding.UTF8.GetString(bytes);
sr.Close();
return key;
}
else
{
StreamWriter sw = File.CreateText(path);
string guid = Guid.NewGuid().ToString();
string key =$"{guid[3]}{guid[5]}{guid}";
byte[] bytes =Encoding.UTF8.GetBytes(key);
Utility.Encryption.GetSelfXorBytes(bytes,GameKeyComponent.Instance.xorKey);
2021-04-11 19:50:39 +08:00
string newKey = Encoding.UTF8.GetString(bytes);
2021-04-08 20:09:59 +08:00
sw.Write(newKey);
sw.Close();
return key;
}
#else
return "robotTest";
#endif
2021-04-08 20:09:59 +08:00
}
public static string Start()
{
string mac = GetMacAddress();
string id = GetAndroidID();
#if UNITY
2021-04-08 20:09:59 +08:00
string device = SystemInfo.deviceUniqueIdentifier;
#else
string device = null;
#endif
2021-04-08 20:09:59 +08:00
string key = Localkey();
string str = $"{mac.Length}+{mac}+{id.Length}+{id}+{device?.Length}+{device}+{key.Length}+{key}";
2021-04-08 20:09:59 +08:00
byte[] bytes = Encoding.UTF8.GetBytes(str);
Utility.Encryption.GetSelfXorBytes(bytes, GameKeyComponent.Instance.xorKey);
str = Encoding.UTF8.GetString(bytes);
return str;
}
public static string GetAndroidID()
{
string _strAndroidID = "none";
if (string.IsNullOrEmpty(_strAndroidID))
{
_strAndroidID = "none";
#if (UNITY_ANDROID && !UNITY_EDITOR) || ANDROID_CODE_VIEW
try
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
using (AndroidJavaObject contentResolver = currentActivity.Call<AndroidJavaObject>("getContentResolver"))
{
using (AndroidJavaClass secure = new AndroidJavaClass("android.provider.Settings$Secure"))
{
_strAndroidID = secure.CallStatic<string>("getString", contentResolver, "android_id");
if (string.IsNullOrEmpty(_strAndroidID))
{
_strAndroidID = "none";
}
}
}
}
}
}
catch (System.Exception e)
{
}
#endif
return _strAndroidID;
}
return _strAndroidID;
}
private static string GetMacAddress()
{
string physicalAddress = "";
NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adaper in nice)
{
if (adaper.Description == "en0")
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
break;
}
else
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
if (physicalAddress != "")
{
break;
};
}
}
return physicalAddress;
}
}
}