123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Xml;
- using UnityEngine.UI;
- public class AssetGame_ResourceUpdate : AssetGame_Base<AssetGame_ResourceUpdate>
- {
-
- /// <summary>
- /// 需要下载的总资源数
- /// </summary>
- private int MaxBundleNum = 0;
- /// <summary>
- /// 需要下载的资源总大小
- /// </summary>
- private int MaxBundleSize = 0;
- /// <summary>
- /// 废弃的变量
- /// </summary>
- private float mProgress = 0f;
- /// <summary>
- /// 在下载更新界面显示的进度条
- /// </summary>
- public Slider mSlider = null;
- /// <summary>
- /// 在下载更新界面显示的进度数字
- /// </summary>
- public Text mSliderText = null;
- /// <summary>
- /// 每下载一个文件,占总额度的百分比
- /// </summary>
- private float mPerPropress = 0f;
- /// <summary>
- ///需要下载的资源
- /// </summary>
- private List<string> NeedDownFiles=new List<string>();
- /// <summary>
- /// 增加新要下载的资源名
- /// </summary>
- /// <param name="nameString"></param>
- public void AddLoadRes(string nameString)
- {
- if (!NeedDownFiles.Contains(nameString))
- {
- NeedDownFiles.Add(nameString);
- }
- }
- /// <summary>
- /// 废弃的功能
- /// </summary>
- public float GetProgress
- {
- get {
- return mProgress ;
- }
- }
- /// <summary>
- /// 获得剩余需要下载的资源数量
- /// </summary>
- public int GetNeedDownFilesCount
- {
- get { return NeedDownFiles.Count; }
- }
- /// <summary>
- /// 获得需要下载的总资源数量
- /// </summary>
- public int GetMaxBundleNum
- {
- get { return MaxBundleNum; }
- }
- /// <summary>
- /// 设置需要下载的资源总数
- /// </summary>
- public int SetMaxBundleNum
- {
- set { MaxBundleNum = value; }
- }
- /// <summary>
- /// 获取资源总大小
- /// </summary>
- public int GetMaxBundleSize
- {
- get { return MaxBundleSize; }
- }
- /// <summary>
- /// 设置资源总大小
- /// </summary>
- public int SetMaxBundleSize
- {
- set { MaxBundleSize = value; }
- }
- /// <summary>
- /// 将赋值进度条
- /// </summary>
- /// <param name="sld"></param>
- public void SetSlider(Slider sld,Text txt)
- {
- mSlider = sld;
- mSlider.value = 0;
- mSliderText = txt;
- }
- /// <summary>
- /// 依次加载需要更新的资源
- /// </summary>
- public void DownLoadRes(string serverVersion)
- {
- if (NeedDownFiles.Count == 0)
- {
-
- AssetGame_VersionManager.Ins.SetUpdateLocalVersionFile();
- return;
- }
- string ServerMD5;
- string ServerResSize;
- string file = NeedDownFiles[0];
- LogHelper.Log("还有" + NeedDownFiles .Count +"个未更新资源,总数" + MaxBundleNum + "个。");
-
- float sizemega = 0;
- sizemega =(float ) MaxBundleSize / 1000000;
- LogHelper.Log(string .Format ("剩余{0:N1}", sizemega) + string .Format ("兆,{0:N0}" , MaxBundleSize) + "字节需要下载");
- LogHelper.Log(AssetConfig_GlobalSetting.strServerUrl + file);
- string cdnpath = AssetConfig_GlobalSetting.strServerUrl + serverVersion + "/";
- AssetSystem_CoroutineProvider.Instance.StartCoroutine(DownLoad(cdnpath + file, delegate(WWW w)
- {
- if (w.error != null)
- {
- LogHelper.Log("没有更新到资源 file=" + file);
- LogHelper.Log(w.error);
- }
- else
- {
- AssetGame_VersionManager.Ins.ServerResVersion.TryGetValue(file ,out ServerMD5);
- AssetGame_VersionManager.Ins.ServerResSizeVersion.TryGetValue(file, out ServerResSize);
- LogHelper.Log("将下载的资源替换本地的资源 ");
- //将下载的资源替换本地的资源
- // AssetSystem_Log.Ins.Log("wenjian " + file);
- // LogHelper.Log("wenjian "+ file);
-
- MaxBundleSize -= int.Parse (ServerResSize);
- //LogHelper.Log("当前数据" + ServerResSize + "/"+ MaxBundleSize );
- AssetSystem_AndroidFileHelper.Instance.ReWriteFileByByte(file, w.bytes);
- AddOrModifyXml(file,ServerMD5,ServerResSize);
-
- }
- NeedDownFiles.RemoveAt(0);
- DownLoadRes(serverVersion);
- }));
- }
- private void AddOrModifyXml(string strFilename,string strMD5,string strSize)
- {
- bool find = false;
- string savePath = AssetConfig_GlobalSetting.strLocalSavePath;
- XmlDocument XmlDoc = new XmlDocument();
- XmlDoc.Load(savePath + "/VersionMD5.txt");
- XmlNodeList nodeList = XmlDoc.SelectSingleNode("Files").ChildNodes;//获取Files节点的所有子节点
- foreach (XmlNode xn in nodeList)//遍历所有子节点
- {
- XmlElement xmlElem = (XmlElement)xn;//将子节点类型转换为XmlElement类型
- if (xmlElem.GetAttribute("FileName") == strFilename)//如果genre属性值为“李赞红”
- {
- xmlElem.SetAttribute("FileName", strFilename);
- xmlElem.SetAttribute("MD5", strMD5);
- xmlElem.SetAttribute("Size", strSize);
- find = true;
- }
- }
- if (!find)
- {
- XmlNode XmlRoot = XmlDoc.SelectSingleNode("Files");
- XmlElement xmlElem = XmlDoc.CreateElement("File");
- xmlElem.SetAttribute("FileName", strFilename);
- xmlElem.SetAttribute("MD5", strMD5);
- xmlElem.SetAttribute("Size", strSize);
- XmlRoot.AppendChild(xmlElem);
-
- }
- XmlDoc.Save(savePath + "/VersionMD5.txt");
- XmlDoc = null;
- }
- /// <summary>
- /// 读取string
- /// 逐行读入,以“,”分割
- /// </summary>
- /// <param name="content"></param>
- /// <param name="dict"></param>
- public void ToParseVersionFile(string content, Dictionary<string, string> dict, string log)
- {
- if (content == null || content.Length == 0)
- {
- LogHelper.Log(log + " is null");
- return;
- }
-
- string[] items = content.Split(new char[] { '\n' });
- foreach (string item in items)
- {
- string[] info = item.Split(new char[] { ',' });
- if (info != null && info.Length == 2)
- {
- dict.Add(info[0], info[1]);
- }
- }
-
- }
- /// <summary>
- /// 读取XmlDocument
- /// 固定格式node,以属性添加数据【FileName】+【MD5】
- /// </summary>
- /// <param name="content"></param>
- /// <param name="dict"></param>
- /// <param name="log"></param>
- /// <returns></returns>
- public void ToParseVersionFileXml(string content, Dictionary<string, string> dict, string log)
- {
- if (content == null || content.Length == 0)
- {
- LogHelper.Log(log + " is null");
- return;
- }
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(content);
- LogHelper.Log("ParseVersionFileXml:1");
- //获取根节点
- XmlElement rootNodeList = xmlDoc.DocumentElement;//Response
- //获取FlightDatas节点
- XmlNodeList xmlChildFlightNodeList = rootNodeList.GetElementsByTagName("Files");
- LogHelper.Log("ParseVersionFileXml:2");
- foreach (XmlNode node in rootNodeList.ChildNodes)
- {
- //node.Attributes[]
- string fileName = node.Attributes["FileName"].Value.ToString();
- string fileMd5 = node.Attributes["MD5"].Value.ToString();
- dict.Add(fileName, fileMd5);
-
- }
- LogHelper.Log("ParseVersionFileXml:3");
- }
- /// <summary>
- /// 读取XmlDocument
- /// 固定格式node,以属性添加数据【FileName】+【MD5】
- /// </summary>
- /// <param name="content"></param>
- /// <param name="dict"></param>
- /// <param name="log"></param>
- /// <returns></returns>
- public void ToParseVersionFileSizeXml(string content, Dictionary<string, string> dict, string log)
- {
- if (content == null || content.Length == 0)
- {
- LogHelper.Log(log + " is null");
- return;
- }
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(content);
- //获取根节点
- XmlElement rootNodeList = xmlDoc.DocumentElement;//Response
- //获取FlightDatas节点
- XmlNodeList xmlChildFlightNodeList = rootNodeList.GetElementsByTagName("Files");
- foreach (XmlNode node in rootNodeList.ChildNodes)
- {
- //node.Attributes[]
- string fileName = node.Attributes["FileName"].Value.ToString();
- string fileSize = node.Attributes["Size"].Value.ToString();
- // LogHelper.Log(fileName + " ***** " + fileSize);
- dict.Add(fileName, fileSize);
- // LogHelper.LogError(fileName + "/" + fileSize );
- }
- }
-
- /// <summary>
- /// 加载这里,加入了和进度条有关的,这个以前测试过,是可以用的.ljp20170323
- /// </summary>
- /// <param name="url"></param>
- /// <param name="finishFun"></param>
- /// <returns></returns>
- public IEnumerator DownLoad(string url, HandleFinishDownload finishFun)
- {
-
- WWW www = new WWW(url);
- if (MaxBundleNum > 0)
- {
- mPerPropress = (float)1 / MaxBundleNum;
- }
- int displayProgress = 0;
- int toProgress = 0;
- while (!www.isDone)
- {
- // LogHelper.Log("01 "+displayProgress+" " + toProgress + " "+mPerPropress );
- toProgress = (int)(www.progress * mPerPropress * 100);
- while (displayProgress < toProgress)
- {
- ++displayProgress;
- SetProgress(displayProgress);
- yield return null;
- }
- yield return null;
- }
- toProgress = (int)(mPerPropress * 100);
- while (displayProgress < toProgress)
- {
- // LogHelper.Log("02");
- ++displayProgress;
- SetProgress(displayProgress);
- yield return null;
- }
- yield return www;
- if (finishFun != null)
- {
- finishFun(www);
- }
- www.Dispose();
- }
- public delegate void HandleFinishDownload(WWW www);
- /// <summary>
- /// 设置进度条
- /// </summary>
- /// <param name="progress"></param>
- public void SetProgress(int progress)
- {
- if (mSlider != null)
- {
- int xx = (int)(((1.0f) * MaxBundleNum - NeedDownFiles.Count) / MaxBundleNum * 100);
- float progressValue = (xx + progress) * 0.01f;
- if(progressValue > 1)
- {
- progressValue = 1.0f;
- }
- if(progressValue > mSlider.value)
- {
- mSlider.value = progressValue;
- mSliderText.text = (xx + progress) + "%";
- }
- }
- }
- /*
- *
- * 现在上面加的都是和进度条有关的,这个以前测试过,是可以用的.ljp20170323
- *
- *
- *
- *
- *
- *
- *
- *
- */
- }
|