Пример #1
0
    /// <summary>
    /// 下载相应的文件列表文档,检查其中对应的文件,更新这些文件
    /// </summary>
    /// <param name="fileListTxtName"></param>
    /// <returns></returns>
    IEnumerator DownloadFileListAndCheck()
    {
        SendEvent(EventDef.DownloadFileList, AppConst.fileListTxtName);

        string listFilePath = AppConst.WebUrl + AppConst.platformFolderName + AppConst.fileListTxtName;

        Debug.Log(listFilePath);
        WWW www = new WWW(listFilePath);

        yield return(www);

        if (!string.IsNullOrEmpty(www.error))
        {
            SendEvent(EventDef.DownloadFileListError, www.error);
            yield break;
        }

        localFileListTxtName = AppConst.fileSavePathPlatform + AppConst.fileListTxtName;
        Dictionary <string, string> oldFileInfos = new Dictionary <string, string>();
        FileInfo fInfo = new FileInfo(localFileListTxtName);

        if (fInfo.Exists)
        {
            FileStream   fs       = File.Open(localFileListTxtName, FileMode.Open);
            StreamReader sr       = new StreamReader(fs);
            string[]     strBlock = sr.ReadToEnd().Split('\n');

            for (int i = 0; i < strBlock.Length; i++)
            {
                string   s    = strBlock[i].Replace("\r", string.Empty);
                string[] pair = strBlock[i].Split('|');
                if (pair.Length == 2)
                {
                    oldFileInfos.Add(pair[0].TrimStart().TrimEnd(), pair[1].TrimStart().TrimEnd());
                }
            }

            sr.Close();
            fs.Close();
        }

        existedFileNameAndMd5s.Clear();
        string fileNames = www.text;

        string[] files = fileNames.Split('\n');
        Dictionary <string, string> latestFileInfos = new Dictionary <string, string>();

        for (int i = 0; i < files.Length; i++)
        {
            string   newInfo = files[i].Replace("\r", string.Empty);
            string[] info    = newInfo.Split('|');
            if (info.Length == 2)
            {
                latestFileInfos.Add(info[0].TrimStart().TrimEnd(), info[1].TrimStart().TrimEnd());
            }
        }
        Debug.Log("读取最新文件列表完成");
        Dictionary <string, string> wantedFileInfos = new Dictionary <string, string>();

        foreach (KeyValuePair <string, string> p in latestFileInfos)
        {
            if (oldFileInfos.ContainsKey(p.Key))
            {
                if (oldFileInfos[p.Key] != p.Value)
                {
                    wantedFileInfos.Add(p.Key, p.Value);
                    Debug.Log("需要更新的文件:" + p.Key);
                }
                else
                {
                    existedFileNameAndMd5s.Add(p.Key, p.Value);
                    Debug.Log("不需要更新的文件:" + p.Key);
                }
            }
            else
            {
                wantedFileInfos.Add(p.Key, p.Value);
                Debug.Log("需要更新的文件:" + p.Key);
            }
        }

        foreach (KeyValuePair <string, string> info in wantedFileInfos)
        {
            string url           = AppConst.WebUrl + AppConst.platformFolderName + '/' + info.Key;
            string localSaveName = info.Key;
            string md5           = info.Value;

            DownloadTask dTask         = downloadTaskContainer.AddComponent <DownloadTask>();
            string       localSavePath = AppConst.fileSavePathPlatform + localSaveName;
            string       saveDirectory = Path.GetDirectoryName(localSavePath);
            if (!Directory.Exists(saveDirectory))
            {
                Directory.CreateDirectory(saveDirectory);
            }
            dTask.SetDownloadTask(url, localSaveName, localSavePath, md5);
            //TODO 一些下载事件的设置
            dTask.AddEventListener(EventDef.DownloadFileCompleted, this);
            dTask.AddEventListener(EventDef.DownloadFileProgressChanged, this);

            waitingTasks.Enqueue(dTask);
        }

        StartCoroutine(DownloadFiles());
    }