• 资源更新


    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.IO;
    using System.Net;
    using System.Threading;
    using UnityEngine;
    
    public class Resource_Update : MonoBehaviour
    {
        private Thread m_thread;
    
        private List<string> dn_list_file = new List<string>();
        private List<string> dn_list_url = new List<string>();
        private List<string> dn_list_path = new List<string>();
        private List<long> dn_list_size = new List<long>();
        
        private int download_index;
        private bool download_state;
        private long download_value;
        private long download_progress;
        private string download_url;
        private string download_path;
    
        private void Start()
        {
            StartCoroutine(DownloadFile());
        }
    
        #region Ready
        /// <summary>
        /// 从服务器下载所有资源
        /// </summary>
        /// <returns></returns>
        IEnumerator DownloadFile()
        {
            WWW www = new WWW(Application.streamingAssetsPath + "/File.txt");
    
            yield return www;
    
            if (!string.IsNullOrEmpty(www.error))
            {
                Debug.LogError(www.error);
            }
            else if (www.isDone)
            {
                if (!string.IsNullOrEmpty(www.text))
                {
                    string[] files = www.text.Split('
    ');
                    for (int i = 0; i < files.Length; i++)
                    {
                        dn_list_file.Add(files[i].TrimEnd('
    '));
                    }
                    Compare();
                }
            }
        }
    
        /// <summary>
        /// 服务器资源与本地资源进行比较
        /// </summary>
        private void Compare()
        {
            dn_list_url.Clear();
            dn_list_path.Clear();
    
            for (int i = 0; i < dn_list_file.Count; i++)
            {
                if (!string.IsNullOrEmpty(dn_list_file[i]))
                {
                    string[] line = dn_list_file[i].Split('|');
                    string[] path = line[0].Split('/');
                    string local_file = Application.persistentDataPath + "/Res/" + path[path.Length - 1];
    
                    if (File.Exists(local_file))
                    {
                        if (Utils_MD5.MD5_File(local_file) != line[1])
                        {
                            dn_list_url.Add(line[0]);
                            dn_list_path.Add(local_file);
                        }
                    }
                    else
                    {
                        string dir = Path.GetDirectoryName(local_file);
                        if (!Directory.Exists(dir))
                            Directory.CreateDirectory(dir);
                        dn_list_url.Add(line[0]);
                        dn_list_path.Add(local_file);
                    }
                }
            }
    
            if (dn_list_url.Count > 0)
            {
                download_value = GetFileSize();
    
                StartUp();
            }
            else
            {
                Debug.Log("资源已是最新!");
            }
        }
    
        /// <summary>
        /// 获取更新资源大小
        /// </summary>
        /// <returns>资源大小</returns>
        private long GetFileSize()
        {
            long size_all = 0;
    
            for (int i = 0; i < dn_list_url.Count; i++)
            {
                long szie = GetFileSize(dn_list_url[i]);
                dn_list_size.Add(szie);
                size_all += szie;
            }
    
            return size_all;
        }
    
        private long GetFileSize(string url)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "HEAD";
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    
            return response.ContentLength;
        }
        #endregion
    
        #region Download
        private void StartUp()
        {
            download_index = 0;
    
            Next();
    
            m_thread = new Thread(DownLoad);
            m_thread.Start();
        }
    
        private void DownLoad()
        {
            while (true)
            {
                if (download_state)
                {
                    try
                    {
                        using (WebClient cilent = new WebClient())
                        {
                            cilent.DownloadProgressChanged += Progress;
                            cilent.DownloadFileAsync(new Uri(download_url), download_path);
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e.Message);
                    }
                    download_state = false;
                }
                Thread.Sleep(1);
            }
        }
    
        private void Progress(object sender, DownloadProgressChangedEventArgs e)
        {
            EventMessageArgs arg = new EventMessageArgs();
            arg.AddOrReplaceMessage("progress", download_progress + e.BytesReceived);
            arg.AddOrReplaceMessage("endvalue", download_value);
            EventManager.PostEvent(EventKey.Download_Progress, arg);
    
            if (e.ProgressPercentage == 100)
            {
                if (dn_list_url.Count > download_index)
                {
                    Next();
                }
                else
                {
                    Finish();
                }
            }
        }
    
        private void Next()
        {
            download_url = dn_list_url[download_index];
            download_path = dn_list_path[download_index];
    
            if (download_index > 0)
                download_progress += dn_list_size[download_index - 1];
            else
                download_progress = 0;
    
            download_index++;
    
            download_state = true;
        }
    
        private void Finish()
        {
            if (m_thread != null)
            {
                m_thread.Abort();
                m_thread = null;
            }
        }
        #endregion
    
        private void OnApplicationQuit()
        {
            if (m_thread != null)
            {
                m_thread.Abort();
                m_thread = null;
            }
        }
    
        private void OnDestroy()
        {
            if (m_thread != null)
            {
                m_thread.Abort();
                m_thread = null;
            }
        }
    }
    View Code
  • 相关阅读:
    1722 最优乘车 1997年NOI全国竞赛
    5969 [AK]刻录光盘
    tarjan算法讲解
    求有向图的强连通分量个数 之 Kosaraju算法
    信使
    1405 奶牛的旅行
    P1828 香甜的黄油 Sweet Butter
    洛谷P2235 [HNOI2002]Kathy函数
    「BZOJ1010」[HNOI2008] 玩具装箱toy(斜率优化)
    BZOJ 1974 [Sdoi2010] auction 代码拍卖会(数位dp)
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/10478544.html
Copyright © 2020-2023  润新知