/// <summary> /// 计算主Manifest的hash和crc值 /// </summary> /// <param name="manifestName"></param> /// <returns></returns> private static SingleBundleInfo ComputeManifestHashAndCRC(string manifestName) { if (string.IsNullOrEmpty(manifestName)) { Debug.LogError("manifestName不能为空值"); return(null); } SingleBundleInfo info = new SingleBundleInfo(); Hash128 hash128; //BuildPipeline.GetHashForAssetBundle 和 BuildPipeline.GetCRCForAssetBundle 的第一个参数都必须是完全路径 if (BuildPipeline.GetHashForAssetBundle(manifestName, out hash128)) { info.bundleHash128 = hash128.ToString(); } else { Debug.LogError(string.Format("未获取到{0}的hash值", manifestName)); } uint crc; if (BuildPipeline.GetCRCForAssetBundle(manifestName, out crc)) { info.bundleCRC = crc; } else { Debug.LogError(string.Format("未获取到{0}的crc值", manifestName)); } info.bundleName = manifestName.Replace(GetAssetBundleDirectory() + "/", string.Empty); Debug.Log(string.Format("成功获取主Manifest信息,{0}", info)); return(info); }
/// <summary> /// 比较AssetBundle的名字、crc、hash值是否相同 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { if (!(obj is SingleBundleInfo)) { return(false); } SingleBundleInfo info = (SingleBundleInfo)obj; return(bundleName.Equals(info.bundleName) && bundleCRC == info.bundleCRC && bundleHash128.Equals(info.bundleHash128)); }
private static SingleBundleInfo GetInfo(string path) { if (string.Empty == path || !File.Exists(path)) { throw new Exception("找不到文件"); } SingleBundleInfo info = new SingleBundleInfo(); info.Name = Path.GetFileName(path); FileStream file = new FileStream(path, FileMode.Open); info.Size = file.Length; info.MD5 = GetMD5HashFromFile(file); file.Close(); return(info); }
/// <summary> /// 初始化核心的数据 /// </summary> private void InitBasicData() { //下载完成后,记录所有资源的加载路径 _bundleInfoDic = GetAllLoadBundleInfo(); var serverAllBundleInfo = JsonConvert.DeserializeObject <AllBundleInfo>(_serverABConfig); //加载 AssetBundleManifest SingleBundleInfo mainManifestBundleInfo = GetMainManifestBundleInfo(serverAllBundleInfo); LoadBundleInfo manifestInfo; if (_bundleInfoDic.TryGetValue(mainManifestBundleInfo.bundleName, out manifestInfo)) { AssetBundleManifest manifest = LoadAsset <AssetBundleManifest>(mainManifestBundleInfo.bundleName, "AssetBundleManifest"); SetAssetBundleManifest(manifest); } }
static SingleBundleInfo ComputeManifestHashAndCRC(string manifestName) { SingleBundleInfo info = new SingleBundleInfo(); Hash128 hash128 = new Hash128(); if (BuildPipeline.GetHashForAssetBundle(manifestName, out hash128)) { info.bundleHash128 = hash128.ToString(); } uint crc = new uint(); if (BuildPipeline.GetCRCForAssetBundle(manifestName, out crc)) { info.bundleCRC = crc; } info.bundleName = GetBundleDirectory().Split('/')[GetBundleDirectory().Split('/').Length - 1]; return(info); }
//static void SetManifestVersion() //{ // string outPaht = GetBundleDirectory(); // int index = outPaht.LastIndexOf('/'); // string manifest = outPaht.Substring(index + 1); // string md5 = ComputeMD5(outPaht + "/" + manifest); // ComputeHashAndCRC(outPaht + "/" + manifest); // SingleBundleInfo info = new SingleBundleInfo(); // info.bundleName = manifest; // // info.bundleMD5 = md5; // string json = JsonUtil.Instance.ObjectToJson<SingleBundleInfo>(info); // WriteManifestJsonConfig(json); //} //static string ComputeMD5(string fileName) //计算文件的MD5,网上直接摘得 //{ // string hashMD5 = string.Empty; // //检查文件是否存在,如果文件存在则进行计算,否则返回空值 // if (File.Exists(fileName)) // { // using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) // { // //计算文件的MD5值 // MD5 calculator = MD5.Create(); // Byte[] buffer = calculator.ComputeHash(fs); // calculator.Clear(); // //将字节数组转换成十六进制的字符串形式 // StringBuilder stringBuilder = new StringBuilder(); // for (int i = 0; i < buffer.Length; i++) // { // stringBuilder.Append(buffer[i].ToString("x2")); // } // hashMD5 = stringBuilder.ToString(); // }//关闭文件流 // }//结束计算 // return hashMD5; //} ///// <summary> ///// 计算指定文件的SHA1值 ///// </summary> ///// <param name="fileName">指定文件的完全限定名称</param> ///// <returns>返回值的字符串形式</returns> //static String ComputeSHA1(String fileName) //{ // String hashSHA1 = String.Empty; // //检查文件是否存在,如果文件存在则进行计算,否则返回空值 // if (File.Exists(fileName)) // { // using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) // { // //计算文件的SHA1值 // SHA1 calculator = SHA1.Create(); // Byte[] buffer = calculator.ComputeHash(fs); // calculator.Clear(); // //将字节数组转换成十六进制的字符串形式 // StringBuilder stringBuilder = new StringBuilder(); // for (int i = 0; i < buffer.Length; i++) // { // stringBuilder.Append(buffer[i].ToString("x2")); // } // hashSHA1 = stringBuilder.ToString(); // }//关闭文件流 // } // return hashSHA1; //} static SingleBundleInfo ComputeHashAndCRC(AssetImporter asset) { SingleBundleInfo info = new SingleBundleInfo(); Hash128 hash128 = new Hash128(); string fileName = GetBundleDirectory() + "/" + asset.assetBundleName + "." + asset.assetBundleVariant; if (BuildPipeline.GetHashForAssetBundle(fileName, out hash128)) { info.bundleHash128 = hash128.ToString(); } uint crc = new uint(); if (BuildPipeline.GetCRCForAssetBundle(fileName, out crc)) { info.bundleCRC = crc; } info.bundleName = asset.assetBundleName + "." + asset.assetBundleVariant; return(info); }
/// <summary> /// 计算AssetBundle的hash和crc值。 /// 参数AssetImporter类型asset变量为导入unity的资源信息,包含了很多需要的信息。 /// 如:资源的assetBundleName、assetBundleVariant、assetPath等。 /// </summary> /// <param name="asset"></param> /// <returns></returns> //[Obsolete("这个方法不能得到变体的")] private static SingleBundleInfo ComputeHashAndCRC(AssetImporter asset) { if (asset == null) { Debug.LogError("asset 不能为空值"); return(null); } SingleBundleInfo info = new SingleBundleInfo(); //assetBundleName有变种需要接上变种的后缀。 string fileName = GetAssetBundleDirectory() + "/" + asset.assetBundleName + (string.IsNullOrEmpty(asset.assetBundleVariant) ? "" : "." + asset.assetBundleVariant); //BuildPipeline.GetHashForAssetBundle 和 BuildPipeline.GetCRCForAssetBundle 的第一个参数都必须是完全路径 Hash128 hash128; if (BuildPipeline.GetHashForAssetBundle(fileName, out hash128)) { info.bundleHash128 = hash128.ToString(); } else { Debug.LogError(string.Format("未获取到{0}的hash值", fileName)); } uint crc; if (BuildPipeline.GetCRCForAssetBundle(fileName, out crc)) { info.bundleCRC = crc; } else { Debug.LogError(string.Format("未获取到{0}的crc值", fileName)); } info.bundleName = asset.assetBundleName + (string.IsNullOrEmpty(asset.assetBundleVariant) ? "" : "." + asset.assetBundleVariant); using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { info.size = (float)stream.Length / 1024 / 1024; } Debug.Log(string.Format("成功获取AssetBundle信息,{0}", info)); return(info); }
public LoadBundleInfo(SingleBundleInfo bundleInfo, string loadUrl) { BundleInfo = bundleInfo; LoadUrl = loadUrl; }
/// <summary> /// 检测是否是主Manifest /// </summary> /// <param name="bundleInfo"></param> /// <returns></returns> private bool CheckIsMainManifest(SingleBundleInfo bundleInfo) { return(bundleInfo.bundleHash128.Equals("00000000000000000000000000000000")); }