/// <summary> /// 检测版本更新 /// </summary> /// <param name="completeCallBack"></param> public void CheckVersion() { downLs.Clear(); totalSize = 0; currentSize = 0; remoteVersionVo = null; //清空当前缓存的远端 CompareVersion(); }
// private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) // { // //UnityEngine.Debug.Log(e.ProgressPercentage); // /* // UnityEngine.Debug.Log(string.Format("{0} MB's / {1} MB's", // (e.BytesReceived / 1024d / 1024d).ToString("0.00"), // (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"))); // */ // //float value = (float)e.ProgressPercentage / 100f; // // if (e.ProgressPercentage == 100 && e.BytesReceived == e.TotalBytesToReceive) // { // NotiData data = new NotiData(NotiConst.UPDATE_DOWNLOAD, currDownFile); // if (m_SyncEvent != null) m_SyncEvent(data); // } // } /// <summary> /// 检测版本更新 /// </summary> /// <param name="completeCallBack"></param> public void CheckVersion(Action completeCallBack) { downLs.Clear(); totalSize = 0; currentSize = 0; remoteVersionVo = null; //清空当前缓存的远端 versionCompleteCallBack = completeCallBack; StartCoroutine(CheckRemoteXML()); }
/// <summary> /// 更新版本信息,将res下的版本信息更新到streamingAssets /// </summary> public static void UpdateVersionInfo() { File.Copy(Application.dataPath + "/Resources/ExtInfo/versionInfo.xml", Application.streamingAssetsPath + "/versionInfo.xml", true); // File.Copy(Application.dataPath + "/Resources/ExtInfo/currentInfo.xml", Application.streamingAssetsPath + "/currentInfo.xml", true); AssetDatabase.Refresh(); Debug.Log("更新完毕"); VersionVo vo = new VersionVo(Application.dataPath + "/Resources/ExtInfo/versionInfo.xml"); Debug.Log("当前大版本号:" + vo.CurrentVersion + ",当前资源号:" + vo.ResVersion); }
/// <summary> /// 获取当前本地的版本信息 /// </summary> /// <returns></returns> public VersionVo GetCurrentVo() { string currentVersionPath = ""; #if UNITY_EDITOR_WIN currentVersionPath = Util.DataPath + "/versionInfo.xml"; #elif UNITY_ANDROID currentVersionPath = Application.persistentDataPath + "/versionInfo.xml"; #endif VersionVo currentVo = new VersionVo(currentVersionPath); return(currentVo); }
/// <summary> /// 通过streamingassest下的路径初始化currentvo,用于检测大版本 /// </summary> /// <param name="cb"></param> private void InitStreamingAssestVersionInfo(Action cb) { if (Application.platform == RuntimePlatform.Android) { StartCoroutine(LoadStreamingCurrentVer(cb, gameStreamingPath + "versionInfo.xml")); } else { string currentVersionPath = gameStreamingPath + "versionInfo.xml"; streamingVerVo = new VersionVo(currentVersionPath); cb(); } }
private void InitCurrentVerVo(Action cb) { if (Application.platform == RuntimePlatform.Android) { StartCoroutine(LoadCurrentVer(cb, "file://" + gameDataPath + "versionInfo.xml")); } else { string currentVersionPath = gameDataPath + "versionInfo.xml"; currentVo = new VersionVo(currentVersionPath); cb(); } }
/// <summary> /// 开始进行差异更新,下载当前最新版本的差异文件 /// </summary> IEnumerator StartDownDiffList(string version, int resId) { WWW remoteVersion = new WWW(AppConst.RemoteUpdatePath + resId + "/versionInfo.xml"); yield return(remoteVersion); if (remoteVersion.isDone) { if (string.IsNullOrEmpty(remoteVersion.error)) { remoteVersionVo = new VersionVo(remoteVersion.text, true); VersionVo curretnVo = GetCurrentVo(); remoteVersionVo.InitDifferDict(); //初始化所有版本的更新内容 Dictionary <string, VFStruct> updateDict = remoteVersionVo.GetDiffFromBeginVer(curretnVo.ResVersion); List <VFStruct> vfList = new List <VFStruct>(); foreach (KeyValuePair <string, VFStruct> dict in updateDict) { vfList.Add(dict.Value); } totalSize = remoteVersionVo.CalculateVFLSize(vfList); List <string> downedLs = GetDownedList(); //获取已经下载了的文件列表 //开始下载文件 for (int i = 0; i < vfList.Count; i++) { if (downedLs.Contains(vfList[i].file)) { Debug.Log("文件已下载:" + vfList[i].file); DownFileFinish(vfList[i]); continue; } else { StartDownFile(vfList[i]); while (!IsDownFinish(vfList[i].file)) { OnDownFinisCallByWWW(vfList[i]); yield return(new WaitForEndOfFrame()); } } } ResDownFinish(true); } else { Debug.LogError("下载更新列表失败:" + remoteVersion.error); ResDownFinish(false); } } }
/// <summary> /// 下载本地版本 /// </summary> /// <param name="callBack"></param> /// <returns></returns> IEnumerator LoadCurrentVer(Action callBack, string pathStr) { WWW localVer = new WWW(pathStr); yield return(localVer); if (localVer.isDone) { if (string.IsNullOrEmpty(localVer.error)) { currentVo = new VersionVo(localVer.text, true); callBack(); } else { Debug.Log("versionInfo.xml error:" + localVer.error); } } }
public void CompareVersion(string remoteVersion, int resId) { VersionVo currentVo = GetCurrentVo(); if (currentVo.isVersionNeedUpdate(remoteVersion)) { Debug.Log("需要进行大版本更新"); } else if (currentVo.isResVersionNeedUpdate(resId.ToString())) { Debug.Log("资源需要进行差异更新"); StartCoroutine(StartDownDiffList(remoteVersion, resId)); } else { Debug.Log("无更新内容"); if (versionCompleteCallBack != null) { versionCompleteCallBack(); } } }
private int tmpDownedCount = 0; //分帧计数器 /// <summary> /// 开始进行差异更新,下载当前最新版本的差异文件 /// </summary> IEnumerator StartDownDiffList() { WWW remoteVersion = new WWW(ApkInfoVo.GetRemoteVersionXmlPath); yield return(remoteVersion); if (remoteVersion.isDone) { if (string.IsNullOrEmpty(remoteVersion.error)) { remoteVersionVo = new VersionVo(remoteVersion.text, true); remoteVersionVo.InitDifferDict(); //初始化所有版本的更新内容 Dictionary <string, VFStruct> updateDict = remoteVersionVo.GetDiffFromBeginVer(currentVo.ResVersion); List <VFStruct> vfList = new List <VFStruct>(); foreach (KeyValuePair <string, VFStruct> dict in updateDict) { vfList.Add(dict.Value); } totalSize = remoteVersionVo.CalculateVFLSize(vfList); if (Application.internetReachability != NetworkReachability.ReachableViaCarrierDataNetwork) //如果是移动数据网络,则需要提示玩家 { Waiting(OnDownConfirm, "当前是移动数据网络,资源包大小为" + GetFormatFileSize(totalSize) + ",是否继续进行下载?"); } else { OnDownConfirm(); } } else { if (Application.internetReachability == NetworkReachability.NotReachable) { Waiting(CompareVersion, "网络连接失败,请检查网络"); } } } }
static void GenVersionFiles() { if (!Directory.Exists(AppConst.VersionPath)) { Directory.CreateDirectory(AppConst.VersionPath); } if (!File.Exists(Application.streamingAssetsPath + "/versionInfo.xml")) { EditorUtility.DisplayDialog("提示", "没有发现versionInfo.xml版本文件,请先更新版本文件:Zone->更新版本信息", "我知道了"); return; } ///首先拿到本地当前版本信息,如果已经存在则需要进行覆盖操作 VersionVo versionInfo = new VersionVo(Application.streamingAssetsPath + "/versionInfo.xml"); string versionTargetPath = AppConst.VersionPath + "/" + versionInfo.ResVersion; if (Directory.Exists(versionTargetPath)) //检查当前是否已经存在目录,如果有的话检查是否需要覆盖版本文件 { bool ctn = EditorUtility.DisplayDialog("提示", "当前版本已经存在,是否需要覆盖当前版本", "继续", "取消"); if (!ctn) { Debug.Log("取消更新"); return; } else { Directory.Delete(versionTargetPath, true); } } long diffResSize = 0; //差异文件大小 string[] dirs = Directory.GetDirectories(AppConst.VersionPath); string sourcePath = Application.streamingAssetsPath; //如果当前版本文件夹下没有原始版本,先进行备份 if (dirs.Length == 0) { EditorUtility.DisplayDialog("提示", "没有发现版本文件,直接备份文件资源", "继续"); string tarPath = AppConst.VersionPath + "/" + versionInfo.ResVersion; CopyVersionInfoToTarget(tarPath); return; } int baseDir = int.Parse(AppConst.versionBase); //最大版本的版本文件夹,用于与当前streamingasset文件夹做对比 foreach (var dir in dirs) { string tarDir = dir.Replace("\\", "/"); int lId = tarDir.LastIndexOf("/") + 1; string dirName = tarDir.Substring(lId, tarDir.Length - lId); if (!IsNumeric(dirName)) { continue; } int versionId = int.Parse(dirName); if (versionId > baseDir) { baseDir = versionId; } } string compareDir = AppConst.VersionPath + "/" + baseDir; VersionVo compareVersionInfo = new VersionVo(compareDir + "/versionInfo.xml"); string nextVersionDir = AppConst.VersionPath + "/UpdateRes/" + (baseDir + 1).ToString(); //差异生成文件夹 if (!Directory.Exists(nextVersionDir)) { Directory.CreateDirectory(nextVersionDir); } string sourceFileIndex = File.ReadAllText(sourcePath + "/" + "files.txt"); string compFileIndex = File.ReadAllText(compareDir + "/files.txt"); sourceFileIndex = sourceFileIndex.Replace("\r", ""); string[] sFiles = sourceFileIndex.Split('\n'); string appenInfo = ""; for (int i = 0; i < sFiles.Length; i++) { if (String.IsNullOrEmpty(sFiles[i])) { continue; } string[] kv = sFiles[i].Split('|'); string f = kv[0].Trim(); string md5 = kv[1].Trim(); string localFile = compareDir + "/" + f; //对比文件 string newFilePath = nextVersionDir + "/" + f; //新的文件位置 if (!File.Exists(localFile)) //如果没有资源,则是新增的内容,直接复制差异资源 { appenInfo += "\n"; appenInfo += sFiles[i] + "|" + GetFileSize(localFile); string dir = Path.GetDirectoryName(newFilePath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.Copy(sourcePath + "/" + f, newFilePath, true); } else { string oldMd5 = Util.md5file(localFile); if (!oldMd5.Equals(md5)) //md5信息变更,更新文件信息 { appenInfo += "\n"; appenInfo += sFiles[i] + "|" + GetFileSize(localFile); string dir = Path.GetDirectoryName(newFilePath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.Copy(sourcePath + "/" + f, newFilePath, true); } } } //appenInfo; //File.WriteAllText(nextVersionDir + "/files.txt",compFileIndex); //更新资源 File.Copy(sourcePath + "/" + "files.txt", nextVersionDir + "/files.txt", true); //直接更新file.txt文件 //更新游戏资源 string sourceResIndex = File.ReadAllText(sourcePath + "/" + "resIndex.txt"); sourceResIndex = sourceResIndex.Replace("\r", ""); //去除\r string[] rFiles = sourceResIndex.Split('\n'); for (int i = 0; i < rFiles.Length; i++) { if (String.IsNullOrEmpty(rFiles[i])) { continue; } string[] kv = rFiles[i].Split('|'); string f = kv[0].Trim(); string md5 = kv[1].Trim(); string localFile = compareDir + "/Android/" + f; //对比文件 string newFilePath = nextVersionDir + "/Android/" + f; //新的文件位置 string androidFileSource = sourcePath + "/Android/" + f; if (!File.Exists(localFile)) //如果没有资源,则是新增的内容,直接复制差异资源 { appenInfo += "\n"; appenInfo += rFiles[i] + "|" + GetFileSize(androidFileSource);; string dir = Path.GetDirectoryName(newFilePath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.Copy(androidFileSource, newFilePath, true); } else { string oldMd5 = Util.md5file(localFile); if (!oldMd5.Equals(md5)) //md5信息变更,更新文件信息 { appenInfo += "\n"; appenInfo += rFiles[i] + "|" + GetFileSize(androidFileSource);; string dir = Path.GetDirectoryName(newFilePath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.Copy(androidFileSource, newFilePath, true); } } } //最后将当前版本完整资源进行备份 CopyVersionInfoToTarget(AppConst.VersionPath + "/" + (baseDir + 1)); // string[] files = appenInfo.Split('\n'); //通过生成的差异文件找到差异文件的大小进行计算 // for (int i = 0; i < files.Length; i++) // { // string[] str = files[i].Split('|'); // if (str.Length != 2) // { // continue; // } // string calFile = nextVersionDir + "/" + str[0]; // diffResSize += GetFileSize(calFile); // Debug.Log("差异文件信息:"+files[i]); // } //appenInfo = "version:" + (baseDir + 1) + "/n" + appenInfo; appenInfo = appenInfo + "\n" + "version:" + (baseDir + 1); File.Copy(sourcePath + "/" + "resIndex.txt", nextVersionDir + "/resIndex.txt", true); //直接更新file.txt文件 File.WriteAllText(nextVersionDir + "/resUpdate.txt", appenInfo); //更新差异文件资源 compareVersionInfo.ResVersion = versionInfo.ResVersion; compareVersionInfo.AppenVersionDetail(versionInfo.ResVersion, appenInfo); compareVersionInfo.WriteVersionInfo(nextVersionDir + "/versionInfo.xml"); compareVersionInfo.WriteVersionInfo(AppConst.VersionPath + "/" + (baseDir + 1) + "/versionInfo.xml"); //同时需要将最新的版本差异完整信息复制 Process.Start(nextVersionDir); }