304 lines
10 KiB
C#
304 lines
10 KiB
C#
using System;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net.Mail;
|
|
using System.Net;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Net.Security;
|
|
|
|
namespace BFEditor
|
|
{
|
|
public struct MailSendConfig
|
|
{
|
|
public string smtpAddress;
|
|
public int port;
|
|
public string sender;
|
|
public string senderPassword;
|
|
public string[] receivers;
|
|
}
|
|
|
|
public static class BFEditorUtils
|
|
{
|
|
public static Texture GetSystemComponentIcon<T>() where T : Component
|
|
{
|
|
return EditorGUIUtility.ObjectContent(null, typeof(T)).image;
|
|
}
|
|
|
|
public static Texture GetSystemIcon(Type type)
|
|
{
|
|
return EditorGUIUtility.ObjectContent(null, type).image;
|
|
}
|
|
|
|
public static Texture GetCustomComponentIcon<T>() where T : Component
|
|
{
|
|
var names = typeof(T).ToString().Split('.');
|
|
var texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/Editor/Res/Icon/Component/" +
|
|
names[names.Length - 1] + ".png");
|
|
if (texture == null)
|
|
texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/Editor/Res/Icon/Component/Default.png");
|
|
return texture;
|
|
}
|
|
|
|
public static Texture GetCustomComponentIcon(Type type)
|
|
{
|
|
var names = type.ToString().Split('.');
|
|
var texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/Editor/Res/Icon/Component/" +
|
|
names[names.Length - 1] + ".png");
|
|
if (texture == null)
|
|
texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/Editor/Res/Icon/Component/Default.png");
|
|
return texture;
|
|
}
|
|
|
|
public static void DrawRect(Vector3[] vector3s, Color color)
|
|
{
|
|
if (vector3s.Length != 4)
|
|
{
|
|
Debug.LogError("draw rect: param position array length must be equal to 4");
|
|
return;
|
|
}
|
|
Handles.color = color;
|
|
Handles.DrawLine(vector3s[0], vector3s[1]);
|
|
Handles.DrawLine(vector3s[1], vector3s[2]);
|
|
Handles.DrawLine(vector3s[2], vector3s[3]);
|
|
Handles.DrawLine(vector3s[3], vector3s[0]);
|
|
}
|
|
|
|
public static void RunCommond(string fileName, string args, string workingDir = null, Action<string> outCallback = null, Action<string> errorCallback = null)
|
|
{
|
|
System.Diagnostics.Process p = new System.Diagnostics.Process();
|
|
var psi = new System.Diagnostics.ProcessStartInfo();
|
|
psi.FileName = fileName;
|
|
psi.Arguments = args;
|
|
if (workingDir != null)
|
|
psi.WorkingDirectory = workingDir;
|
|
psi.UseShellExecute = false; //是否使用操作系统shell启动
|
|
psi.RedirectStandardInput = true; //接受来自调用程序的输入信息
|
|
psi.RedirectStandardOutput = true; //由调用程序获取输出信息
|
|
psi.RedirectStandardError = true; //重定向标准错误输出
|
|
psi.CreateNoWindow = true; //不显示程序窗口
|
|
p.StartInfo = psi;
|
|
p.Start();
|
|
|
|
string outputData = string.Empty;
|
|
string errorData = string.Empty;
|
|
p.BeginOutputReadLine();
|
|
p.BeginErrorReadLine();
|
|
p.OutputDataReceived += (sender, e) =>
|
|
{
|
|
if (!string.IsNullOrEmpty(e.Data))
|
|
{
|
|
Debug.Log(e.Data);
|
|
outCallback?.Invoke(e.Data);
|
|
}
|
|
};
|
|
|
|
p.ErrorDataReceived += (sender, e) =>
|
|
{
|
|
if (!string.IsNullOrEmpty(e.Data))
|
|
{
|
|
Debug.LogError(e.Data);
|
|
errorCallback?.Invoke(e.Data);
|
|
}
|
|
};
|
|
p.WaitForExit();
|
|
p.Close();
|
|
|
|
}
|
|
|
|
public static bool EnsureDirPathExists(string path)
|
|
{
|
|
bool result = Directory.Exists(path);
|
|
if (!result)
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static void CreateFile(string path, string content)
|
|
{
|
|
StreamWriter sw;
|
|
var fileInfo = new FileInfo(path);
|
|
if (!fileInfo.Exists)
|
|
{
|
|
sw = fileInfo.CreateText();
|
|
}
|
|
else
|
|
{
|
|
sw = fileInfo.AppendText();
|
|
}
|
|
sw.WriteLine(content);
|
|
sw.Close();
|
|
sw.Dispose();
|
|
}
|
|
|
|
public static void GetFilesWithSuffix(string path, string suffix, ref List<FileInfo> resultFiles)
|
|
{
|
|
try
|
|
{
|
|
var dirs = Directory.GetDirectories(path);
|
|
var dirInfo = new DirectoryInfo(path);
|
|
var files = dirInfo.GetFiles();
|
|
if (files.Length != 0)
|
|
{
|
|
foreach (var file in files)
|
|
if (suffix.ToLower().IndexOf(file.Extension.ToLower(), StringComparison.Ordinal) >= 0)
|
|
resultFiles.Add(file);
|
|
}
|
|
if (dirs.Length != 0)
|
|
{
|
|
foreach (string dir in dirs)
|
|
GetFilesWithSuffix(dir, suffix, ref resultFiles);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogError(ex.ToString());
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static void SendMail(MailSendConfig config, string subject, string body, string attach = null)
|
|
{
|
|
var mail = new MailMessage();
|
|
mail.From = new MailAddress(config.sender);
|
|
foreach (var address in config.receivers)
|
|
{
|
|
mail.To.Add(address);
|
|
}
|
|
|
|
mail.Subject = subject;
|
|
mail.Body = body;
|
|
if (!string.IsNullOrEmpty(attach))
|
|
{
|
|
mail.Attachments.Add(new Attachment(attach));
|
|
}
|
|
|
|
var sc = new SmtpClient(config.smtpAddress);
|
|
sc.Port = config.port;
|
|
sc.Credentials = new NetworkCredential(config.sender, config.senderPassword) as ICredentialsByHost;
|
|
ServicePointManager.ServerCertificateValidationCallback =
|
|
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
|
{ return true; };
|
|
sc.Send(mail);
|
|
}
|
|
|
|
public static Dictionary<string, string> GetCommandLineArgs()
|
|
{
|
|
var map = new Dictionary<string, string>();
|
|
var args = Environment.GetCommandLineArgs();
|
|
for (int i = 0; i < args.Length; i++)
|
|
{
|
|
var arg = args[i];
|
|
if (arg.StartsWith("-"))
|
|
{
|
|
if (args.Length > (i + 1))
|
|
{
|
|
var key = arg.Replace("-", "");
|
|
var value = args[i + 1];
|
|
if (!value.StartsWith("-"))
|
|
{
|
|
map[key] = value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return map;
|
|
}
|
|
|
|
public static void CopyDirWithIgnore(string sourceFolderName, string destFolderName, List<string> ignore)
|
|
{
|
|
if (!Directory.Exists(sourceFolderName))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var sourceFilesPath = Directory.GetDirectories(sourceFolderName);
|
|
for (var i = 0; i < sourceFilesPath.Length; i++)
|
|
{
|
|
var newDir = sourceFilesPath[i].Replace(sourceFolderName, destFolderName);
|
|
if (!Directory.Exists(newDir))
|
|
{
|
|
Directory.CreateDirectory(newDir);
|
|
}
|
|
CopyDirWithIgnore(sourceFilesPath[i], newDir, ignore);
|
|
}
|
|
|
|
var sourceFile = Directory.GetFiles(sourceFolderName);
|
|
for (var i = 0; i < sourceFile.Length; i++)
|
|
{
|
|
var file = sourceFile[i];
|
|
var fileInfo = new FileInfo(file);
|
|
|
|
if (ignore.Contains(fileInfo.Extension.ToLower()))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var destFile = file.Replace(sourceFolderName, destFolderName);
|
|
if (File.Exists(destFile))
|
|
{
|
|
File.Delete(destFile);
|
|
}
|
|
File.Copy(file, destFile);
|
|
}
|
|
}
|
|
|
|
public static void CopyDir(string sourceFolderName, string destFolderName)
|
|
{
|
|
if (!Directory.Exists(sourceFolderName))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var sourceFilesPath = Directory.GetDirectories(sourceFolderName);
|
|
for (var i = 0; i < sourceFilesPath.Length; i++)
|
|
{
|
|
var newDir = sourceFilesPath[i].Replace(sourceFolderName, destFolderName);
|
|
if (!Directory.Exists(newDir))
|
|
{
|
|
Directory.CreateDirectory(newDir);
|
|
}
|
|
CopyDir(sourceFilesPath[i], newDir);
|
|
}
|
|
|
|
var sourceFile = Directory.GetFiles(sourceFolderName);
|
|
for (var i = 0; i < sourceFile.Length; i++)
|
|
{
|
|
var file = sourceFile[i];
|
|
var destFile = file.Replace(sourceFolderName, destFolderName);
|
|
if (File.Exists(destFile))
|
|
{
|
|
File.Delete(destFile);
|
|
}
|
|
File.Copy(file, destFile);
|
|
}
|
|
}
|
|
|
|
public static string GetTimeStr(long milliseconds)
|
|
{
|
|
var seconds = (int)(milliseconds / 1000);
|
|
var second = seconds % 60;
|
|
var minus = (seconds - second) / 60;
|
|
return string.Format("{0}分{1}秒", minus, second);
|
|
}
|
|
|
|
public static List<string> GetAssetPathsWithSuffix(string path, string suffix)
|
|
{
|
|
var result = new List<string>();
|
|
var fileInfos = new List<FileInfo>();
|
|
GetFilesWithSuffix(path, suffix, ref fileInfos);
|
|
|
|
for (var i = 0; i < fileInfos.Count; i++)
|
|
{
|
|
var resourcePath = "Assets" + fileInfos[i].FullName.Replace("\\", "/").Remove(0, Application.dataPath.Length);
|
|
resourcePath = resourcePath.Replace('\\', '/');
|
|
result.Add(resourcePath);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |