AkiraPixelWind/Assets/Axibug/Script/Editor/HotUpdate/AppTools_Editor.cs

263 lines
8.6 KiB
C#
Raw Normal View History

2022-12-29 18:20:40 +08:00
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEditor;
using UnityEngine;
/*
* 20211212
* MD5 txt
* */
namespace Axibug
{
public class AddChild : ScriptableObject
{
public static bool isHotUpdate = true; //是否热更新版本
[MenuItem("Axibug/createVersion_Win64")]
static void createWindows64()
{
writeFileInfo(BuildTarget.StandaloneWindows64);
}
[MenuItem("Axibug/createVersion_Android")]
static void createAndroid()
{
writeFileInfo(BuildTarget.Android);
}
[MenuItem("Axibug/createVersion_IOS")]
static void createIOS()
{
writeFileInfo(BuildTarget.iOS);
}
public static void writeFileInfo(BuildTarget target)
{
string filePath = "";
//根据目标平台创建路径
switch (target)
{
//case BuildTarget.StandaloneWindows64:
// filePath = $"AssetBundles/StandaloneWindows64/";
// break;
//case BuildTarget.Android:
// filePath = $"AssetBundles/Android/";
// break;
//case BuildTarget.iOS:
// filePath = $"AssetBundles/IOS/";
// break;
case BuildTarget.StandaloneWindows64:
filePath = $"StandaloneWindows64";
break;
case BuildTarget.Android:
filePath = $"Android";
break;
case BuildTarget.iOS:
filePath = $"IOS";
break;
}
writeFileInfo(filePath);
}
public static string computeMd5(string filename)
{
string filemd5 = null;
try
{
using (var fileStream = File.OpenRead(filename))
{
var md5 = MD5.Create();
var fileMD5Bytes = md5.ComputeHash(fileStream);//计算指定Stream 对象的哈希值
filemd5 = FormatMD5(fileMD5Bytes);
}
}
catch (System.Exception ex)
{
}
return filemd5;
}
//字节数组转为字符串
public static string FormatMD5(byte[] data)
{
return System.BitConverter.ToString(data).Replace("-", "").ToLower();//将byte[]装换成字符串
}
public static void writeFileInfo(string targetPath)
{
string projectPath = System.Environment.CurrentDirectory;
string fullPath = $"{projectPath}/AssetBundles/{targetPath}/";
string path = fullPath + "/version.txt";
if (File.Exists(path))
File.Delete(path);
int totalSize = 0;
//获取指定路径下面的所有资源文件
if (Directory.Exists(fullPath))
{
DirectoryInfo direction = new DirectoryInfo(fullPath);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
//文件列表
FileList list = new FileList();
list.listinfo = new List<FileData>();
writeLoginSeverInfo(list);
Debug.Log(files.Length);
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(".meta")/* || files[i].Name.EndsWith(".txt")*/)
{
continue;
}
string md5 = computeMd5(files[i].FullName);
string[] condition = { $"{targetPath}\\" };
string[] arr = files[i].FullName.Split(condition, StringSplitOptions.None);
if (arr.Length != 2)
continue;
string filepath = arr[1].Replace("\\", "/");
FileData jd = new FileData();
jd.filename = filepath;
jd.hash = md5;
jd.length = (int)files[i].Length;
totalSize += jd.length;
list.listinfo.Add(jd);
}
//回写总大小
list.listinfo[1].length = totalSize;
Debug.Log("list.listinfo:" + list.listinfo.Count);
//写入jscon文件
string str = JsonUtility.ToJson(list);
File.AppendAllText(path, str);
Debug.Log("生成version成功..." + list.listinfo.Count);
}
}
//写入登录服务器配置信息
private static void writeLoginSeverInfo(FileList list)
{
//0 写入送检版本号
FileData d = new FileData();
d.filename = "versionCheck";
if (isHotUpdate)
d.hash = "9.9.9";
else
d.hash = Application.version;
d.length = 0;
list.listinfo.Add(d);
//1 写入当前版本号
FileData d2 = new FileData();
d2.filename = "version.txt";
d2.hash = Application.version;
d2.length = 0;
list.listinfo.Add(d2);
string src = "DE,130,20180408,testcode,113.250.55.88:15100";
string str1 = ToEncrypt("1981", src);
//2 配置登录服务器IP PORT
FileData d3 = new FileData();
d3.filename = "loginServer";
d3.hash = str1;
d3.length = 0;
list.listinfo.Add(d3);
}
//对字符串进行加密
static string ToEncrypt(string encryptKey, string str)
{
try
{
byte[] P_byte_key = Encoding.Unicode.GetBytes(encryptKey);//将密钥字符串转换为字节序列
byte[] P_byte_data = Encoding.Unicode.GetBytes(str); //将字符串转换为字节序列
MemoryStream P_Stream_MS = new MemoryStream();//创建内存流对象
{
using (CryptoStream P_CryptStream_Stream = new CryptoStream(P_Stream_MS, new DESCryptoServiceProvider().CreateEncryptor(P_byte_key, P_byte_key), CryptoStreamMode.Write))
{
P_CryptStream_Stream.Write(P_byte_data, 0, P_byte_data.Length);//向加密流中写入字节序列
P_CryptStream_Stream.FlushFinalBlock();//将数据压入基础流
byte[] P_bt_temp = P_Stream_MS.ToArray();//从内存流中获取字节序列
return Convert.ToBase64String(P_bt_temp);
}
}
}
catch (CryptographicException ce)
{
throw new Exception(ce.Message);
}
}
[MenuItem("Axibug/Create ClassTypes")]
public static void FindAllUnityClass()
{
EditorUtility.DisplayProgressBar("Progress", "Find Class...", 0);
string[] dirs = { "Assets/Resources/Prefabs" };
var asstIds = AssetDatabase.FindAssets("t:Prefab", dirs);
int count = 0;
List<string> classList = new List<string>();
for (int i = 0; i < asstIds.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(asstIds[i]);
var pfb = AssetDatabase.LoadAssetAtPath<GameObject>(path);
foreach (Transform item in pfb.transform)
{
var coms = item.GetComponentsInChildren<Component>();
foreach (var com in coms)
{
string tName = com.GetType().FullName;
if (!classList.Contains(tName)/* && tName.StartsWith("UnityEngine")*/)
{
classList.Add(tName);
}
}
}
count++;
EditorUtility.DisplayProgressBar("Find Class", pfb.name, count / (float)asstIds.Length);
}
for (int i = 0; i < classList.Count; i++)
{
classList[i] = string.Format("<type fullname=\"{0}\" preserve=\"all\"/>", classList[i]);
}
System.IO.File.WriteAllLines(Application.dataPath + "/ClassTypes.txt", classList);
EditorUtility.ClearProgressBar();
}
}
}
#endif