public static string GetBundleMD5(VersionConfig streamingVersionConfig, string bundleName) { string path = Path.Combine(PathHelper.AppHotfixResPath, bundleName); if (File.Exists(path)) { return(MD5Helper.FileMD5(path)); } if (streamingVersionConfig != null && streamingVersionConfig.FileInfoDict.ContainsKey(bundleName)) { return(streamingVersionConfig.FileInfoDict[bundleName].MD5); } return(""); }
public async Task <bool> StartAsync() { // 获取远程的Version.txt string versionUrl = ""; try { using (UnityWebRequestAsync webRequestAsync = new UnityWebRequestAsync()) { versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; var result = await webRequestAsync.DownloadAsync(versionUrl); if (string.IsNullOrWhiteSpace(result)) { remoteVersionConfig = MongoHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text); } else { Log.Error($"url: {versionUrl}; result:{result}"); return(false); } } } catch (Exception e) { Log.Error($"url: {versionUrl} {e}"); return(false); } // 获取streaming目录的Version.txt VersionConfig streamingVersionConfig = null; string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt"); using (UnityWebRequestAsync request = new UnityWebRequestAsync()) { var result = await request.DownloadAsync(versionPath); if (string.IsNullOrWhiteSpace(result)) { streamingVersionConfig = MongoHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } } // 删掉远程不存在的文件 DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath); if (directoryInfo.Exists) { FileInfo[] fileInfos = directoryInfo.GetFiles(); foreach (FileInfo fileInfo in fileInfos) { if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name)) { continue; } if (fileInfo.Name == "Version.txt") { continue; } fileInfo.Delete(); } } else { directoryInfo.Create(); } // 对比MD5 foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values) { // 对比md5 string localFileMD5 = GetBundleMD5(streamingVersionConfig, fileVersionInfo.File); if (fileVersionInfo.MD5 == localFileMD5) { continue; } bundles.Enqueue(fileVersionInfo.File); TotalSize += fileVersionInfo.Size; } return(true); }