Example #1
0
    /// <summary>
    /// 下载版本文件和csv配置表
    /// </summary>
    private void LoadVerAndCSV()
    {
        int count = 3;

        if (!Directory.Exists(loadRootPath))
        {
            Directory.CreateDirectory(loadRootPath);
        }

AAAA:
        string csvPath = loadRootPath + "/data.csv";

        Debug.Log(csvPath);
        bool IsDown = HttpTool.DownLoad(csvPath, "data.csv");

        Thread.Sleep(20);
        if (!IsDown)
        {
            if (count-- > 0)
            {
                goto AAAA;
            }
            else
            {
                Debug.LogError("csv文件下载失败");
                return;
            }
        }

        try
        {
            csv = new StreamReader(csvPath);
            string      line      = string.Empty;
            Queue <Msg> fileQueue = new Queue <Msg>();
            line = csv.ReadLine();

            if (line.Split(',')[1] == version)
            {
                Debug.LogError("版本相同无需下载");
                return;
            }

            line = string.Empty;

            while ((line = csv.ReadLine()) != null)
            {
                string[] tmpFile = line.Split(',');
                Msg      Msg     = new Msg(tmpFile[0], tmpFile[1], tmpFile[2]);

                fileQueue.Enqueue(Msg);
                line = string.Empty;
            }
            Debug.Log(fileQueue.Count);

            ConcurrentQueue <Msg> needLoadFiles = new ConcurrentQueue <Msg>();

            while (fileQueue.Count > 0)
            {
                Msg    Msg      = fileQueue.Dequeue();
                string fullPath = loadRootPath + "/" + Msg.httpDownLoadPath;
                if (File.Exists(fullPath))
                {
                    string localFileMD5 = GetMD5WithFile(fullPath);
                    if (localFileMD5 != Msg.md5)
                    {
                        TotalFilesLength += Msg.fileLen;
                        needLoadFiles.Enqueue(Msg);
                        Thread.Sleep(20);
                        File.Delete(fullPath);
                    }
                }
                else
                {
                    needLoadFiles.Enqueue(Msg);
                    TotalFilesLength += Msg.fileLen;
                }
            }

            TotalNumFilesByLoad = needLoadFiles.Count;
            ConcurrentQueue <Msg> lettleFiles = new ConcurrentQueue <Msg>();
            ConcurrentQueue <Msg> largeFiles  = new ConcurrentQueue <Msg>();

            // 从所有文件里按文件大小进行筛选
            while (needLoadFiles.Count > 0)
            {
                bool IsLoad = needLoadFiles.TryDequeue(out Msg file);
                if (IsLoad)
                {
                    if (file.fileLen > 1024 * 10)
                    {
                        largeFiles.Enqueue(file);
                    }
                    else
                    {
                        lettleFiles.Enqueue(file);
                    }
                }

                if (needLoadFiles.Count == 0)
                {
                    break;
                }
            }

            Debug.Log(lettleFiles.Count);
            Debug.Log(largeFiles.Count);

            while (lettleFiles.Count > 0)
            {
                DownLoadLettelFiles += delegate()
                {
                    //Debug.Log("执行委托");
                    int downloadCount = 3;

                    bool IsLoad = lettleFiles.TryDequeue(out Msg file);

                    if (IsLoad)
                    {
BBBB:
                        bool IsDownLoad = HttpTool.DownLoad(loadRootPath + "/" + file.httpDownLoadPath, file.fileName);
                        if (!IsDownLoad)
                        {
                            if (downloadCount-- > 0)
                            {
                                goto BBBB;
                            }
                            else
                            {
                                return;
                            }
                        }

                        currentFileLen += file.fileLen;
                        loadedFilesNum++;
                        currentLen.Enqueue(currentFileLen);
                    }
                };
            }

            if (lettleFiles.Count <= 0)
            {
                currentLen.Enqueue(currentFileLen);
                DownLoadLargeFiles = null;
            }

            while (largeFiles.Count > 0)
            {
                DownLoadLargeFiles += delegate()
                {
                    //Debug.Log("执行委托");
                    int downloadCount = 3;

                    bool IsLoad = largeFiles.TryDequeue(out Msg file);
                    Debug.Log(file.httpDownLoadPath);
                    string tmpPath = loadRootPath + "/" + file.httpDownLoadPath;

                    if (IsLoad)
                    {
BBBB:
                        if (tmpPath != null)
                        {
                            bool IsDownLoad = HttpTool.DownLoad(tmpPath, file.fileName);
                            if (!IsDownLoad)
                            {
                                if (downloadCount-- > 0)
                                {
                                    goto BBBB;
                                }
                                else
                                {
                                    return;
                                }
                            }

                            currentFileLen += file.fileLen;
                            loadedFilesNum++;
                            currentLen.Enqueue(currentFileLen);
                        }
                    }
                };
            }


            if (largeFiles.Count <= 0 || lettleFiles.Count <= 0)
            {
                currentLen.Enqueue(currentFileLen);
                DownLoadLargeFiles = null;
            }
        }
        catch (Exception)
        {
            Debug.LogError("获取csv文件失败");
        }
        finally
        {
            if (csv != null)
            {
                csv.Close();
                csv = null;
            }
        }
    }