您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

109 行
3.4 KiB

using System.IO;
using UnityEditor;
using UnityEngine;
using System.Net.Http.Headers;
namespace MetaCity.BundleKit.Editor
{
public class MetacityTokenWindow : EditorWindow
{
//[MenuItem("Metacity/AccessTokenWindow")]
public static void OpenTokenWindow()
{
GetWindow<MetacityTokenWindow>();
}
private static bool _isTokenValid = false;
private static string _lastAccessToken = string.Empty;
private readonly GUIStyle _errorStyle = new GUIStyle();
private readonly GUIStyle _passStyle = new GUIStyle();
private void OnEnable()
{
_errorStyle.normal.textColor = Color.red;
_passStyle.normal.textColor = Color.green;
}
private void OnGUI()
{
if (!_isTokenValid || string.IsNullOrEmpty(AccessToken))
{
EditorGUILayout.HelpBox("\n" +
"Get your access token from metacity website.\n" +
"\n"
, MessageType.Warning);
EditorGUILayout.Space(10);
}
EditorGUILayout.LabelField("Access Token :");
EditorStyles.textField.wordWrap = true;
AccessToken = EditorGUILayout.TextArea(AccessToken);
if (!string.IsNullOrEmpty(AccessToken))
{
if (_isTokenValid)
{
EditorGUILayout.LabelField("Valid", _passStyle);
}
else
{
EditorGUILayout.LabelField(new GUIContent("Invalid access token"), _errorStyle);
}
}
else
{
EditorGUILayout.LabelField("Access token can't be null", _errorStyle);
}
Validate();
}
public static string AccessToken
{
get
{
string dir =Constants.TokenConfigPath;
if (string.IsNullOrEmpty(dir) || !Directory.Exists(dir))
return string.Empty;
string path = Path.Combine(dir, "token.txt");
if (!File.Exists(path))
return string.Empty;
return File.ReadAllText(path);
}
set
{
if (value == AccessToken) return;
string dir =Constants.TokenConfigPath;
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string path = Path.Combine(dir, "token.txt");
File.WriteAllText(path, value);
}
}
public static async void Validate()
{
if (!string.IsNullOrEmpty(AccessToken) && AccessToken != _lastAccessToken)
{
MetacityClient.client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
_isTokenValid = await MetacityClient.CheckAccessToken();
if (_isTokenValid)
{
_lastAccessToken = AccessToken;
}
}
}
public static bool IsAccessTokenValid
{
get
{
return _isTokenValid;
}
}
}
}