/// <summary> /// 下载回调 /// </summary> /// <param name="success"></param> /// <param name="resVersionData"></param> private void OnDownLoadAsset(bool success, AssetBundleConfig resVersionData) { if (success) { mCurrentDownloadAssetSizeCaculate.OnAssetDownLoad(resVersionData); float progress = mCurrentDownloadAssetSizeCaculate.CurrentDownloadSize / mCurrentDownloadAssetSizeCaculate.MaxSize; if (mDownloadType == DownLoadType.FIRST_PHASE && null != mParent) { mParent.UpdateProgress(string.Format("正在下载更新{0}M/{1}M", mCurrentDownloadAssetSizeCaculate.CurrentDownLoadFileSize, mCurrentDownloadAssetSizeCaculate.AllFileSize), progress); } else { CoreEntry.gEventMgr.TriggerEvent(GameEvent.GE_Bundle_Group_Download_Progress, EventParameter.Get(progress)); LogMgr.UnityLog("Bundle group " + mCurrentDownloadAssetSizeCaculate.CurBuild + " progress, " + progress); } } else { mDownLoadErrorVersionDataConfig.Add(resVersionData); } mCurrentDownLoadCount--; if (mNewVersionDataConfig.Count == 0 && mCurrentDownLoadCount == 0) { if (mDownLoadErrorVersionDataConfig.Count <= 0) { CallBackDownLoadSucess(); } else { OnDownLoadError(); } } }
public void PushDownloadFile(string url, string saveFile, AssetBundleConfig config, HttpDownloadFinish finishCallback = null) { int index = -1; for (int i = 0; i < mDownloadTask.Count; i++) { if (mDownloadTask[i].url.Equals(url)) { index = i; break; } } if (-1 != index) { if (mDownloadTask.Count > 1) { HttpDownloadTask tmp = mDownloadTask[index]; mDownloadTask.RemoveAt(index); mDownloadTask.Add(tmp); } } else if (null == mCurTask || !mCurTask.url.Equals(url)) { HttpDownloadTask task = new HttpDownloadTask(url, saveFile, config, finishCallback); mDownloadTask.Add(task); } }
/// <summary> /// 获取远程更新地址 /// </summary> /// <param name="data"></param> /// <returns></returns> private string GetRemoteUrl(AssetBundleConfig data) { string getUrl = BundleCommon.RemoteUrl + data.RelativePath + "_Compress"; #if UNITY_EDITOR getUrl = BundleCommon.RemoteUrl + data.RelativePath.Replace("/", "\\") + "_Compress"; #else getUrl = BundleCommon.RemoteUrl + data.RelativePath + "_Compress"; #endif return(getUrl); }
public void OnAssetDownLoad(AssetBundleConfig dataConfig) { float fileSize = dataConfig.FileSize / 1024f / 1024f; CurrentDownloadSize += fileSize; if (CurBuild != 0) { TotalDownloadSize += fileSize; TotalDownloadNum++; } }
private void ABProcess() { LogMgr.UnityLog("ABProcess # " + mIdx + " Started .."); while (!isExit) { int count = 0; DownloadedItem item = null; string mSaveFile = null; AssetBundleConfig mConfig = null; lock (HttpDownload.shareObject) { count = HttpDownload.downloadedQueue.Count; if (count > 0) { item = HttpDownload.downloadedQueue[0]; HttpDownload.downloadedQueue.RemoveAt(0); } } LogMgr.UnityLog("ABProcessor mid: " + mIdx); if (null != item) { mSaveFile = item.saveFile; mConfig = item.config; string outFilePath = mSaveFile.Replace("_Compress", ""); if (File.Exists(outFilePath)) { File.Delete(outFilePath); } LZMATool.DecompressFileLZMA(mSaveFile, outFilePath); if (File.Exists(mSaveFile)) { File.Delete(mSaveFile); } if (null != mConfig) { List <AssetBundleConfig> bundleConfigGroup = new List <AssetBundleConfig>(); bundleConfigGroup.Add(mConfig); XMLTool.SaveABConfig(HttpDownloadTask.configPath, bundleConfigGroup); LogMgr.UnityLog("HttpDownLoad SaveABConfig: " + count + "," + mConfig.RelativePath + "," + mConfig.ABName + "," + mConfig.Build + "," + Thread.CurrentThread.ManagedThreadId + ",@" + mIdx ); } } else { LogMgr.UnityLog("ABProcessor continue " + mIdx); Thread.Sleep(10); } } }
public static HttpDownload StartDownloadFile(string url, string saveFile, AssetBundleConfig config, HttpDownloadCallback finishCallback = null) { if (null == downloader) { downloader = new HttpDownload(); } downloader.StartTask(url, saveFile, config, finishCallback); return(downloader); }
public HttpDownloadTask(string url, string saveFile, AssetBundleConfig config, HttpDownloadFinish callback) { this.url = url; this.saveFile = saveFile; this.callback = callback; this.config = config; if (string.IsNullOrEmpty(configPath)) { configPath = BundleCommon.UserVersionPath; } }
private void StartTask(string url, string saveFile, AssetBundleConfig config, HttpDownloadCallback finishCallback) { lock (mLockObject) { mUrl = url; mSaveFile = saveFile; mConfig = config; mFinishCallback = finishCallback; mDownloadedSize = 0; mHasTask = true; } }
private void DoClean() { if (null == remoteConfig || remoteConfig.Count == 0) { return; } if ((null == localConfig || localConfig.Count == 0) && (null == streamConfig || streamConfig.Count == 0)) { return; } List <string> fileList = new List <string>(); if (null != localConfig) { for (int i = 0; i < localConfig.Count; i++) { AssetBundleConfig config = localConfig[i]; if (NeedRemove(config.ABName)) { fileList.Add(string.Format("{0}/Out{1}", rootPath, config.RelativePath)); } } } if (null != streamConfig) { for (int i = 0; i < streamConfig.Count; i++) { AssetBundleConfig config = streamConfig[i]; if (NeedRemove(config.ABName)) { fileList.Add(string.Format("{0}/Inner{1}", rootPath, config.RelativePath)); } } } for (int i = 0; i < fileList.Count; i++) { string f = fileList[i]; if (File.Exists(f)) { File.Delete(f); } else { LogMgr.UnityError("clean file doesn't exist at path:" + f); } } }
public static List <AssetBundleConfig> LoadAllABConfig(string filePath, bool isStreamPath = false) { if (isStreamPath) { if (!File.Exists(filePath)) { return(null); } } else { CreateXMLRoot(filePath); } List <AssetBundleConfig> abCfgList = new List <AssetBundleConfig>(); XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(filePath); } catch (System.Exception e) { LogMgr.UnityLog("Read xml error: recreate" + filePath); File.Delete(filePath); CreateXMLRoot(filePath); xmlDoc.Load(filePath); } XmlNode rootNode = xmlDoc.SelectSingleNode("Root"); XmlNodeList nodeList = rootNode.ChildNodes; foreach (XmlNode node in nodeList) { XmlElement element = node as XmlElement; AssetBundleConfig dataCfg = new AssetBundleConfig(); dataCfg.ABName = element.GetAttribute("ABName"); dataCfg.RelativePath = element.GetAttribute("RelativePath"); dataCfg.MD5Value = element.GetAttribute("MD5"); dataCfg.FileSize = long.Parse(element.GetAttribute("FileSize")); dataCfg.Build = int.Parse(element.GetAttribute("Build")); abCfgList.Add(dataCfg); } return(abCfgList); }
/// <summary> /// 下载资源 /// </summary> private void DownLoadAsset() { mCurrentDownLoadCount = mNewVersionDataConfig.Count; for (int i = 0; i < mNewVersionDataConfig.Count; i++) { AssetBundleConfig child = mNewVersionDataConfig[i]; string remoteUrl = BundleCommon.RemoteUrl + child.RelativePath + "_Compress"; string saveFile = Application.persistentDataPath + "/" + BundleCommon.ResVersion + "/Out" + child.RelativePath + "_Compress"; HttpDownloadFinish downLoadAssetComplete = (success) => { OnDownLoadAsset(success, child); }; SG.CoreEntry.gHttpDownloadMgr.PushDownloadFile(remoteUrl, saveFile, child, downLoadAssetComplete); } mNewVersionDataConfig.Clear(); }
/// <summary> /// 写入文件 /// </summary> /// <param name="versionData"></param> private void SaveDownloadBundle(AssetBundleConfig versionData) { mAllVersionDataDic[versionData.ABName] = versionData; mTempDownLoadFileSize += versionData.FileSize; WriteVersionDataToDB(); }
private IEnumerator LoadAssetBundleAsync(string bundlePath, ABDependDelegate callback = null) { string dictKey = string.Format("/{0}", bundlePath); LogMgr.UnityLog("ABDependsLoad LoadAssetBundleAsync dictKey " + dictKey); ABFileState state = ABFileMgr.Instance.GetABFileState(dictKey); string[] abDepends = null; List <string> unfinishedAB = new List <string>(); HashSet <string> abSet = null; AssetBundleManifest manifest = null; Dictionary <string, ABFileState> allABStateDict = new Dictionary <string, ABFileState>(); if (state == ABFileState.DOWNLOADED) { abSet = allABSet; manifest = abManifest; allABStateDict[dictKey] = ABFileState.DOWNLOADED; } else if (state == ABFileState.NONE) { abSet = allABSet; manifest = abManifest; allABStateDict[dictKey] = ABFileState.DOWNLOADED; unfinishedAB.Add(dictKey); } else if (state == ABFileState.STREAMEXIT) { abSet = streamABSet; manifest = streamManifest; allABStateDict[dictKey] = ABFileState.STREAMEXIT; } if (null == abSet || !abSet.Contains(bundlePath)) { if (null != callback) { callback(null); } yield break; } abDepends = manifest.GetAllDependencies(bundlePath); if (null == abDepends) { if (null != callback) { callback(null); } yield break; } for (int i = 0; i < abDepends.Length; i++) { string dpPath = abDepends[i]; dictKey = string.Format("/{0}", dpPath); if (!abLoadDict.ContainsKey(dpPath)) { state = ABFileMgr.Instance.GetABFileState(dictKey); if (state == ABFileState.NONE) { allABStateDict[dictKey] = ABFileState.DOWNLOADED; unfinishedAB.Add(dictKey); } else if (state == ABFileState.DOWNLOADED) { allABStateDict[dictKey] = ABFileState.DOWNLOADED; } else if (state == ABFileState.STREAMEXIT) { allABStateDict[dictKey] = ABFileState.DOWNLOADED; ABFileMgr.Instance.SetABFileState(dictKey, ABFileState.DOWNLOADED); } if (!abSet.Contains(dpPath)) { if (null != callback) { callback(null); } yield break; } } else { allABStateDict[dictKey] = ABFileState.DOWNLOADED; UpdateUsedTime(dpPath); } } for (int i = 0; i < unfinishedAB.Count; i++) { AssetBundleConfig config = ABFileMgr.Instance.GetABConfig(unfinishedAB[i]); if (null == config) { if (null != callback) { LogMgr.UnityError("no config " + unfinishedAB[i]); callback(null); } yield break; } string remoteUrl = BundleCommon.RemoteUrl + config.RelativePath + "_Compress"; string saveFile = Application.persistentDataPath + "/" + BundleCommon.ResVersion + "/Out" + config.RelativePath + "_Compress"; LogMgr.UnityLog("ABDependsLoad remoteUrl " + remoteUrl + "," + saveFile); CoreEntry.gHttpDownloadMgr.PushDownloadFile(remoteUrl, saveFile, config, null); } while (unfinishedAB.Count > 0) { yield return(new WaitForSeconds(0.2f)); for (int i = 0; i < unfinishedAB.Count; i++) { state = ABFileMgr.Instance.GetABFileState(unfinishedAB[i]); if (state == ABFileState.DOWNLOADED) { unfinishedAB.RemoveAt(i); i--; } } } string bundleFile = null; for (int i = 0; i < abDepends.Length; i++) { string dpPath = abDepends[i]; dictKey = string.Format("/{0}", dpPath); if (allABStateDict.ContainsKey(dictKey)) { if (!abLoadDict.ContainsKey(dpPath)) { state = allABStateDict[dictKey]; if (state == ABFileState.DOWNLOADED) { bundleFile = string.Format("{0}/{1}/Out/{2}", Application.persistentDataPath, BundleCommon.ResVersion, dpPath); } else { if (null != callback) { LogMgr.UnityError("state error:" + dpPath); callback(null); } yield break; } AssetBundle dpAB = AssetBundle.LoadFromFile(bundleFile); AssetBundleData dpData = new AssetBundleData(dpPath); dpData.mAssetBundle = dpAB; abLoadDict[dpPath] = dpData; } UpdateUsedTime(dpPath); } else { if (null != callback) { LogMgr.UnityError("no in allABStateDict:" + dpPath); callback(null); } yield break; } } dictKey = string.Format("/{0}", bundlePath); state = allABStateDict[dictKey]; if (state == ABFileState.DOWNLOADED) { bundleFile = string.Format("{0}/{1}/Out/{2}", Application.persistentDataPath, BundleCommon.ResVersion, bundlePath); } else { if (null != callback) { LogMgr.UnityError("state error:" + bundlePath); callback(null); } yield break; } AssetBundleData abData = new AssetBundleData(bundlePath); AssetBundle bundle = AssetBundle.LoadFromFile(bundleFile); abData.mAssetBundle = bundle; abLoadDict[bundlePath] = abData; if (null != callback) { callback(abData); } }
public void AddABFile(AssetBundleConfig config, ABFileState state = ABFileState.NONE) { mABStateDict[config.RelativePath] = state; mABAllDict[config.RelativePath] = config; }
private void DoCompare() { updateABConfigs.Clear(); Dictionary <string, AssetBundleConfig> tmpDict = new Dictionary <string, AssetBundleConfig>(); ABFileMgr mABFileMgr = ABFileMgr.Instance; if (null != remoteConfigs) { int totalSize = remoteConfigs.Count; for (int i = 0; i < totalSize; i++) { dispatcher.AddAction(() => { progressCallback(i * 1.0f / totalSize); }); AssetBundleConfig localCfg = null; AssetBundleConfig streamCfg = null; bool isNew = false; if (localConfigDict.TryGetValue(remoteConfigs[i].ABName, out localCfg)) { if (localCfg.MD5Value.Equals(remoteConfigs[i].MD5Value)) { isNew = false; } else { isNew = true; } } else { if (streamConfigDict.TryGetValue(remoteConfigs[i].ABName, out streamCfg)) { if (streamCfg.MD5Value.Equals(remoteConfigs[i].MD5Value)) { isNew = false; if (!tmpDict.ContainsKey(remoteConfigs[i].RelativePath)) { mABFileMgr.SetABFileState(remoteConfigs[i].RelativePath, ABFileState.STREAMEXIT); } } else { isNew = true; } } else { isNew = true; } } if (isNew) { if (!tmpDict.ContainsKey(remoteConfigs[i].RelativePath)) { string relativePath = remoteConfigs[i].RelativePath; string[] dp = dps[relativePath]; for (int j = 0; j < dp.Length; j++) { if (!tmpDict.ContainsKey("/" + dp[j])) { AssetBundleConfig reTmpCfg = null; if (remoteConfigDict.TryGetValue("/" + dp[j], out reTmpCfg)) { AssetBundleConfig tmpCfg = null; if (localConfigDict.TryGetValue(reTmpCfg.ABName, out tmpCfg)) { if (!tmpCfg.MD5Value.Equals(reTmpCfg.MD5Value)) { mABFileMgr.SetABFileState(reTmpCfg.RelativePath, ABFileState.NONE); updateABConfigs.Add(reTmpCfg); tmpDict[reTmpCfg.RelativePath] = reTmpCfg; } } else { if (streamConfigDict.TryGetValue(reTmpCfg.ABName, out tmpCfg)) { if (tmpCfg.MD5Value.Equals(reTmpCfg.MD5Value)) { reTmpCfg.Build = remoteConfigs[i].Build; } } else { reTmpCfg.Build = remoteConfigs[i].Build; } mABFileMgr.SetABFileState(reTmpCfg.RelativePath, ABFileState.NONE); updateABConfigs.Add(reTmpCfg); tmpDict[reTmpCfg.RelativePath] = reTmpCfg; } } } else { AssetBundleConfig reTmpCfg = tmpDict["/" + dp[j]]; if (reTmpCfg.Build > remoteConfigs[i].Build) { reTmpCfg.Build = remoteConfigs[i].Build; } } } updateABConfigs.Add(remoteConfigs[i]); tmpDict[remoteConfigs[i].RelativePath] = remoteConfigs[i]; } else { AssetBundleConfig reTmpCfg = tmpDict[remoteConfigs[i].RelativePath]; if (reTmpCfg.Build > remoteConfigs[i].Build) { reTmpCfg.Build = remoteConfigs[i].Build; } } } } } dispatcher.AddAction(finishCallback); }
/// <summary> /// Step 4:对比本地和服务器配置数据信息 /// </summary> /// <param name="remoteVersionPath"></param> private void CompareVersion(string remoteVersionPath) { mParent.UpdateProgress("比对更新文件中", 0); string remoteABPath = Application.persistentDataPath + "/" + BundleCommon.ResVersion + "/Out/assetbundle"; AssetBundle remoteAB = null; AssetBundleManifest remoteABM = null; if (File.Exists(remoteABPath)) { remoteAB = AssetBundle.LoadFromFile(remoteABPath); } if (null != remoteAB) { remoteABM = remoteAB.LoadAsset("AssetBundleManifest") as AssetBundleManifest; remoteAB.Unload(false); } remoteConfigs = XMLTool.LoadAllABConfig(remoteVersionPath); localConfigs = XMLTool.LoadAllABConfig(BundleCommon.UserVersionPath); streamConfigs = XMLTool.LoadAllABConfig(BundleCommon.StreamUserVersionPath, true); updateABConfigs = new List <AssetBundleConfig>(); if (null != remoteConfigs) { Dictionary <string, AssetBundleConfig> localConfigDict = new Dictionary <string, AssetBundleConfig>(); Dictionary <string, AssetBundleConfig> remoteConfigDict = new Dictionary <string, AssetBundleConfig>(); Dictionary <string, AssetBundleConfig> streamConfigDict = new Dictionary <string, AssetBundleConfig>(); Dictionary <string, string[]> dps = new Dictionary <string, string[]>(); for (int i = 0; i < remoteConfigs.Count; i++) { remoteConfigDict[remoteConfigs[i].RelativePath] = remoteConfigs[i]; mABFileMgr.AddABFile(remoteConfigs[i]); } for (int i = 0; i < localConfigs.Count; i++) { localConfigDict[localConfigs[i].ABName] = localConfigs[i]; } if (null != streamConfigs) { for (int i = 0; i < streamConfigs.Count; i++) { streamConfigDict[streamConfigs[i].ABName] = streamConfigs[i]; } } int totalSize = remoteConfigs.Count; for (int i = 0; i < totalSize; i++) { AssetBundleConfig localCfg = null; AssetBundleConfig streamCfg = null; bool isNew = false; if (localConfigDict.TryGetValue(remoteConfigs[i].ABName, out localCfg)) { if (localCfg.MD5Value.Equals(remoteConfigs[i].MD5Value)) { isNew = false; mABFileMgr.SetABFileState(remoteConfigs[i].RelativePath, ABFileState.DOWNLOADED); } else { isNew = true; } } else { if (streamConfigDict.TryGetValue(remoteConfigs[i].ABName, out streamCfg)) { if (streamCfg.MD5Value.Equals(remoteConfigs[i].MD5Value)) { isNew = false; mABFileMgr.SetABFileState(remoteConfigs[i].RelativePath, ABFileState.STREAMEXIT); } else { isNew = true; } } else { isNew = true; } } if (isNew) { string relativePath = remoteConfigs[i].RelativePath; string[] dp = remoteABM.GetAllDependencies(relativePath.Substring(1, relativePath.Length - 1)); dps[relativePath] = dp; } } if (dps.Count > 0) { CompareThread.StartCompare(remoteConfigs, remoteConfigDict, localConfigDict, streamConfigDict, dps, updateABConfigs, CompareProgress, CompareFinish); } else { CompareFinish(); } } else { CompareFinish(); } }