//获取需要下载的文件的总大小 private int GetSizeByteNeedDownloaded(VersionConfig newCfg, List <string> files) { int size = 0; Dictionary <string, VersionFileConfig> newDict = newCfg.fileDict; for (int i = 0; i < files.Count; i++) { VersionFileConfig newVfc = newDict[files[i]]; size += newVfc.sizeByte; } return(size); }
/// <summary> /// 生成版本配置 /// </summary> /// <param name="path">路径</param> /// <param name="version">版本号</param> /// <returns></returns> /// <summary> /// 解析版本配置信息 /// </summary> /// <param name="content"></param> /// <returns></returns> public static VersionConfig ReadVersionConfig(string content) { VersionConfig vc = new VersionConfig(); XmlDocument doc = new XmlDocument(); doc.LoadXml(content); XmlElement root = null; root = doc.DocumentElement; XmlElement version = root.GetElementsByTagName("Version").Item(0) as XmlElement; string sVersion = version.InnerText; int.TryParse(sVersion, out vc.version); XmlElement files = root.GetElementsByTagName("Files").Item(0) as XmlElement; XmlNodeList fileList = files.GetElementsByTagName("File"); for (int i = 0; i < fileList.Count; ++i) { XmlElement file = fileList.Item(i) as XmlElement; XmlElement path = file.GetElementsByTagName("Path").Item(0) as XmlElement; string sPath = path.InnerText; XmlElement md5 = file.GetElementsByTagName("MD5").Item(0) as XmlElement; string sMD5 = md5.InnerText; XmlElement sizeByte = file.GetElementsByTagName("SizeByte").Item(0) as XmlElement; int iSizeByte = Int32.Parse(sizeByte.InnerText); VersionFileConfig vfc = new VersionFileConfig(); vfc.path = sPath; vfc.md5 = sMD5; vfc.sizeByte = iSizeByte; if (vc.fileDict.ContainsKey(vfc.path)) { vc.fileDict.Remove(vfc.path); } vc.fileDict.Add(vfc.path, vfc); } return(vc); }
/// <summary> /// 获取文件需要下载的文件列表 /// </summary> /// <returns></returns> public List <string> GetFileListNeedDownloaded(VersionConfig oldCfg, VersionConfig newCfg) { Dictionary <string, VersionFileConfig> oldDict = oldCfg.fileDict; Dictionary <string, VersionFileConfig> newDict = newCfg.fileDict; List <string> files = new List <string>(); foreach (KeyValuePair <string, VersionFileConfig> kv in newDict) { if (oldDict.ContainsKey(kv.Key)) { VersionFileConfig oldVfc = oldDict[kv.Key]; VersionFileConfig newVfc = kv.Value; if (oldVfc.md5 != newVfc.md5) { files.Add(kv.Key); } oldDict.Remove(kv.Key); } else { files.Add(kv.Key); } } foreach (KeyValuePair <string, VersionFileConfig> kv in oldDict) { string path = PathUtil.GetPersistentDataPath(kv.Key); if (PathUtil.ExistsFile(path)) { File.Delete(path); } } return(files); }
/// <summary> /// 写版本配置 /// </summary> /// <param name="vc"></param> /// <returns></returns> public static void WriteVersionConfig(VersionConfig vc) { var versionFile = new System.IO.StreamWriter(PathUtil.GetFullPath(PathUtil.GetRelativePathToDataPath(PathConfig.version)), false, Encoding.UTF8); StringBuilder sb = new StringBuilder(); versionFile.WriteLine("<Config>"); versionFile.Write("\t<Version>"); versionFile.Write(vc.version.ToString()); versionFile.WriteLine("</Version>"); versionFile.WriteLine("\t<Files>"); foreach (KeyValuePair <string, VersionFileConfig> kv in vc.fileDict) { VersionFileConfig vfc = kv.Value; versionFile.WriteLine("\t\t<File>"); versionFile.Write("\t\t\t<Path>"); versionFile.Write(vfc.path.ToLower()); versionFile.WriteLine("</Path>"); versionFile.Write("\t\t\t<MD5>"); versionFile.Write(vfc.md5); versionFile.WriteLine("</MD5>"); versionFile.Write("\t\t\t<SizeByte>"); versionFile.Write(vfc.sizeByte); versionFile.WriteLine("</SizeByte>"); versionFile.WriteLine("\t\t</File>"); } versionFile.WriteLine("\t</Files>"); versionFile.WriteLine("</Config>"); versionFile.Flush(); versionFile.Close(); }
public static VersionConfig GenerateVersionConfig(AssetBundleCollector collector) { VersionConfig vc = VersionUtil.ReadConfig("config/version") ?? new VersionConfig(); VersionConfig newVC = new VersionConfig(); newVC.version = vc.version + 1; List <AssetBundleBuild> buildList = collector.GetBuildList(); for (int i = 0; i < buildList.Count; ++i) { AssetBundleBuild build = buildList[i]; string file = Application.streamingAssetsPath.Combine(build.assetBundleName); if (PathUtil.ExistsFile(file)) { FileStream fs = new FileStream(file, FileMode.Open); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); string md5 = CryptUtil.GetMD5(bytes); fs.Close(); VersionFileConfig vfc = new VersionFileConfig(); vfc.path = build.assetBundleName; vfc.md5 = md5; vfc.sizeByte = bytes.Length; if (!newVC.fileDict.ContainsKey(vfc.path)) { newVC.fileDict.Add(vfc.path, vfc); } else { Debug.LogError("重复资源:" + vfc.path); } } } return(newVC); }