| using System.Collections; | 
| using System.Collections.Generic; | 
| using UnityEngine; | 
| using System.IO; | 
|   | 
|   | 
| public class AssetVersion | 
| { | 
|     string m_RelativePath = string.Empty; | 
|     public string relativePath { get { return m_RelativePath; } } | 
|   | 
|     string m_FileName; | 
|     public string fileName { get { return m_FileName; } } | 
|   | 
|     string m_Extersion = string.Empty; | 
|     public string extersion { get { return m_Extersion; } } | 
|   | 
|     string m_Md5 = string.Empty; | 
|     public string md5 { get { return m_Md5; } } | 
|   | 
|     int m_Size = 0; | 
|     public int size { get { return m_Size; } }       //字节      | 
|   | 
|     StorageLocation m_FileLocation = StorageLocation.ExternalStore; | 
|     public StorageLocation fileLocation { get { return m_FileLocation; } } | 
|   | 
|     bool m_LocalValid = false; | 
|     public bool localValid | 
|     { | 
|         get { return m_LocalValid; } | 
|         set { m_LocalValid = value; } | 
|     } | 
|   | 
|     public AssetVersion(string _versionString) | 
|     { | 
|         var strings = _versionString.Split('\t'); | 
|   | 
|         m_RelativePath = strings[0]; | 
|         m_Extersion = strings[1]; | 
|         int.TryParse(strings[2], out m_Size); | 
|         m_Md5 = strings[3]; | 
|   | 
|         var paths = m_RelativePath.Split('/'); | 
|   | 
|         var lastPath = paths[paths.Length - 1]; | 
|         var index = lastPath.LastIndexOf('.'); | 
|         if (index != -1) | 
|         { | 
|             m_FileName = lastPath.Substring(0, index); | 
|         } | 
|         else | 
|         { | 
|             m_FileName = lastPath; | 
|         } | 
|     } | 
|   | 
|     public AssetCategory GetAssetCategory() | 
|     { | 
|         if (extersion == ".dll") | 
|             return AssetCategory.Dll; | 
|         else if (m_RelativePath.StartsWith("ui/")) | 
|             return AssetCategory.UI; | 
|         else if (m_RelativePath.StartsWith("audio/")) | 
|             return AssetCategory.Audio; | 
|         else if (m_RelativePath.StartsWith("hero/")) | 
|             return AssetCategory.Mob; | 
|         else if (m_RelativePath.StartsWith("maps/")) | 
|             return AssetCategory.Scene; | 
|         else if (m_RelativePath.StartsWith("uieffect/")) | 
|             return AssetCategory.Effect; | 
|         else if (m_RelativePath.StartsWith("config/")) | 
|             return AssetCategory.Config; | 
|         else if (m_RelativePath.StartsWith("graphic/")) | 
|             return AssetCategory.Shader; | 
|         else if (m_RelativePath.StartsWith("video/")) | 
|             return AssetCategory.Video; | 
|         else | 
|             return AssetCategory.Other; | 
|     } | 
|   | 
|     public bool CheckLocalFileValid(bool _completeFile) | 
|     { | 
|         if (_completeFile) | 
|         { | 
|             var path = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, m_RelativePath); | 
|             var fileInfo = new FileInfo(path); | 
|   | 
|             if (!fileInfo.Exists) | 
|             { | 
|                 Debug.LogFormat("文件不存在 {0}", path); | 
|                 return false; | 
|             } | 
|             if (fileInfo.Length != size) | 
|             { | 
|                 Debug.LogFormat("{0}文件大小不对{1}-{2}", path, size, fileInfo.Length); | 
|                 return false; | 
|             } | 
|             var fileMD5 = FileExtersion.GetMD5HashFromFile(path); | 
|             if (fileMD5 != md5) | 
|             { | 
|                 Debug.LogFormat("{0}文件md5不对{1}-{2}", path, md5, fileMD5); | 
|                 return false; | 
|             } | 
|             return true; | 
|         } | 
|         else | 
|         { | 
|             if (extersion == ".manifest" || extersion == ".bytes" || extersion == ".txt" || extersion == ".dll") | 
|             { | 
|                 var path = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, m_RelativePath); | 
|                 var fileInfo = new FileInfo(path); | 
|   | 
|                 if (!fileInfo.Exists || fileInfo.Length != size || md5 != FileExtersion.GetMD5HashFromFile(path)) | 
|                 { | 
|                     return false; | 
|                 } | 
|             } | 
|             else if (string.IsNullOrEmpty(extersion) || extersion.Length == 0) | 
|             { | 
|                 var path = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, m_RelativePath); | 
|                 var fileInfo = new FileInfo(path); | 
|   | 
|                 var manifestAssetVersion = AssetVersionUtility.GetAssetVersion(StringUtility.Contact(m_RelativePath, ".manifest")); | 
|                 if (!fileInfo.Exists || fileInfo.Length != size || manifestAssetVersion == null || !manifestAssetVersion.CheckLocalFileValid(false)) | 
|                 { | 
|                     return false; | 
|                 } | 
|             } | 
|             else | 
|             { | 
|                 var path = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, m_RelativePath); | 
|                 var fileInfo = new FileInfo(path); | 
|                 if (!fileInfo.Exists || fileInfo.Length != size) | 
|                 { | 
|                     return false; | 
|                 } | 
|             } | 
|         } | 
|   | 
|         return true; | 
|     } | 
|   | 
|     // 检查外部存储文件是否存在 | 
|     // 不存在则读取随包文件进行比较 | 
|     // 存在则比较 | 
|     public bool CheckLocalFileValid(AssetVersion otherAssetInfo) | 
|     { | 
|         string path = string.Concat(ResourcesPath.Instance.ExternalStorePath, m_RelativePath); | 
|   | 
|         if (!File.Exists(path) && !m_RelativePath.Contains(".txt")) | 
|         { | 
|             //Debug.LogFormat("转查StreamingAssetPath - 文件不存在 {0} ", path); | 
|             if (otherAssetInfo == null || string.IsNullOrEmpty(otherAssetInfo.relativePath)) | 
|                 return false; | 
|   | 
|             if (otherAssetInfo.size != size) | 
|             { | 
|                 Debug.LogFormat("StreamingAssetPath 文件大小不对{0}-{1}", size, otherAssetInfo.size); | 
|                 return false; | 
|             } | 
|   | 
|             if (otherAssetInfo.md5 != md5) | 
|             { | 
|                 Debug.LogFormat("StreamingAssetPath 文件md5不对{0}-{1}", md5, otherAssetInfo.md5); | 
|                 return false; | 
|             } | 
|   | 
|             return true; | 
|         } | 
|   | 
|         return CheckLocalFileValid(false); | 
|   | 
|     } | 
|   | 
|   | 
|     public bool IsPriorAsset() | 
|     { | 
|         if (VersionConfig.Get().partAssetPackage) | 
|         { | 
|             //如果这台机器曾经完整的下载过所有资源,那么就不再使用分包下载策略,在资源热更新的时候这种情况会触发。 | 
|             if (AssetVersionUtility.hasDownLoadFullAsset) | 
|             { | 
|                 return true; | 
|             } | 
|             else | 
|             { | 
|                 var category = GetAssetCategory(); | 
|                 // var prior = PriorBundleConfig.GetAssetPrior(category, AssetVersionUtility.DecodeFileName(m_FileName)); | 
|                 // return prior <= 1; | 
|                 return false; | 
|             } | 
|         } | 
|         else | 
|         { | 
|             return true; | 
|         } | 
|   | 
|     } | 
|   | 
|     public enum StorageLocation | 
|     { | 
|         StreamingAsset, | 
|         ExternalStore | 
|     } | 
|   | 
|     public enum AssetCategory | 
|     { | 
|         UI, | 
|         Audio, | 
|         Mob, | 
|         Scene, | 
|         Effect, | 
|         Config, | 
|         Shader, | 
|         Dll, | 
|         Video, | 
|         Other, | 
|         Patch,//补丁 | 
|         Logic,//热更逻辑代码 | 
|     } | 
|   | 
| } |