From ce579f316ff0fa0d0b3132ccf92493cb4e830993 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Thu, 26 May 2022 22:49:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96AB=E7=9A=84Md5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 获取AB的Md5 --- .../TEngine/Editor/About/TEngineEditorUtil.cs | 80 +++++++++++++++---- .../Editor/About/TEngineEditorUtil.cs.meta | 11 +++ .../TEngine/Runtime/FileSystem/FileSystem.cs | 4 +- 3 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 Assets/TEngine/Editor/About/TEngineEditorUtil.cs.meta diff --git a/Assets/TEngine/Editor/About/TEngineEditorUtil.cs b/Assets/TEngine/Editor/About/TEngineEditorUtil.cs index 85f38182..677b4e36 100644 --- a/Assets/TEngine/Editor/About/TEngineEditorUtil.cs +++ b/Assets/TEngine/Editor/About/TEngineEditorUtil.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; using System.IO; -using TEngineCore.Editor; +using System.Security.Cryptography; +using System.Text; using UnityEditor; using UnityEngine; @@ -10,7 +11,7 @@ namespace TEngine.Editor public class TEngineEditorUtil { [MenuItem("TEngine/GenMd5List", priority = 1500)] - public static void GenMd5List(string source, string target = "") + public static void GenMd5List() { try { @@ -19,10 +20,10 @@ namespace TEngine.Editor //{ // Directory.CreateDirectory(targetPath); //} + string source = FileSystem.ResourceRootInStreamAsset; var files = Directory.GetFiles(source, "*", SearchOption.AllDirectories); - var fileList = new List(); var fileNames = new List(); - var fileSizes = new List(); + var fileInfos = new Dictionary(); foreach (var file in files) { if (file.EndsWith(".meta")) @@ -30,11 +31,10 @@ namespace TEngine.Editor continue; } fileNames.Add(file.Substring(source.Length + 1)); - fileList.Add(file); - fileSizes.Add(file.Length); + fileInfos.Add(file, GetFileSize(file)); } - GeneralMd5CheckList(source, files, fileList, fileNames,fileSizes); + GeneralMd5CheckList(source, files, fileInfos, fileNames); //FastZip.compress_File_List(9, target, fileList.ToArray(), null, false, fileNames.ToArray()); } catch (Exception e) @@ -44,6 +44,20 @@ namespace TEngine.Editor } } + /// + /// 获取文件大小 + /// + /// + /// + public static long GetFileSize(string sFullName) + { + long lSize = 0; + if (File.Exists(sFullName)) + { + lSize = new FileInfo(sFullName).Length; + } + return lSize; + } /// /// 生成md5文件列表 @@ -52,30 +66,31 @@ namespace TEngine.Editor /// 文件列表 /// 压缩的文件列表 /// 文件名字列表 - /// /// 文件大小列表 - private static void GeneralMd5CheckList(string source, string[] files, List fileList, List fileNames, List fileSizes) + private static void GeneralMd5CheckList(string source, string[] files, Dictionary fileInfos, List fileNames) { try { var md5List = new List(); - foreach (var file in files) + foreach (var fileInfo in fileInfos) { + var file = fileInfo.Key; + if (file.EndsWith(".meta") || file.EndsWith(".DS_Store")) { continue; } - var md5 = LoaderUtilities.GetMd5Hash(file); + var md5 = GetMd5Hash(file); var fd5 = new fileMd5 { fileName = file.Substring(source.Length + 1).Replace('\\', '/'), md5 = md5, - //fileSize = //todo + fileSize = fileInfo.Value, }; md5List.Add(fd5); } - var configPath = $"{source}/{FileSystem.Md5CheckList}"; + var configPath = $"{source}/{FileSystem.Md5List}"; var stream = new FileStream(configPath, FileMode.OpenOrCreate); var writer = new StreamWriter(stream); @@ -84,14 +99,49 @@ namespace TEngine.Editor writer.Dispose(); writer.Close(); - fileList.Add(configPath); - fileNames.Add(FileSystem.Md5CheckList); + //fileList.Add(configPath); + fileNames.Add(FileSystem.Md5List); } catch (Exception e) { TLogger.LogError(e.ToString()); throw; } + TLogger.LogInfoSuccessd("Gen Md5 List Success"); + } + + /// + /// 获取文件的md5码 + /// + /// + /// + public static string GetMd5Hash(string fileName) + { + if (!File.Exists(fileName)) + { + TLogger.LogWarning($"not exit file,path:{fileName}"); + return string.Empty; + } + try + { + using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + MD5 md5 = new MD5CryptoServiceProvider(); + byte[] retVal = md5.ComputeHash(file); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < retVal.Length; i++) + { + sb.Append(retVal[i].ToString("x2")); + } + return sb.ToString(); + } + + } + catch (Exception ex) + { + TLogger.LogError("GetMD5Hash() fail,error:" + ex.Message); + return string.Empty; + } } } } diff --git a/Assets/TEngine/Editor/About/TEngineEditorUtil.cs.meta b/Assets/TEngine/Editor/About/TEngineEditorUtil.cs.meta new file mode 100644 index 00000000..d76d6fc8 --- /dev/null +++ b/Assets/TEngine/Editor/About/TEngineEditorUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 73167965a9ecb894388e4fcbe7fa797d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TEngine/Runtime/FileSystem/FileSystem.cs b/Assets/TEngine/Runtime/FileSystem/FileSystem.cs index 5804d4db..dc78b493 100644 --- a/Assets/TEngine/Runtime/FileSystem/FileSystem.cs +++ b/Assets/TEngine/Runtime/FileSystem/FileSystem.cs @@ -17,7 +17,7 @@ namespace TEngine public const string BuildPath = "Build"; public const string AssetBundleBuildPath = BuildPath + "/AssetBundles"; private const string AssetBundleTargetPath = "{0}/AssetBundles"; - public const string Md5CheckList = "Md5CheckList.json"; + public const string Md5List = "Md5List.json"; /// /// 资源更新读取根目录 /// @@ -203,7 +203,7 @@ namespace TEngine { public string fileName; public string md5; - public string fileSize; + public long fileSize; } [Serializable]