Ejemplo n.º 1
0
        /// <summary>
        /// 更新线程进口
        /// </summary>
        private static void HotfixEnter()
        {
            // Debug.Log(ConfigContent.configURL.ManifestHost);

            // 先下载服务器资源列表
            string jsonContent = GetHttpDownloadFile(ConfigContent.configURL.ManifestHost);

            if (!string.IsNullOrEmpty(jsonContent))
            {
                netResList = jsonContent.ToNewtonObjectT <AssetBundleAssetList>();
                Debug.Log(netResList.copyRight);
                // 版本不同需要下载
                if (!netResList.copyRight.Equals(HotFixConfig.LocalResList.copyRight) || !netResList.mainCrc.Equals(HotFixConfig.LocalResList.mainCrc))
                {
                    // 找出差异文件
                    List <AssetBundleAsset> res = HotFixConfig.CheckDifference(netResList.assets, HotFixConfig.LocalResList.assets);
                    if (res.Count == 0)
                    {
                        // 已经下载完,把文件存储下来
                        HotFixConfig.SaveLocalResList(netResList);
                        isDone = true;
                    }
                    else
                    {
                        // 到此不要管版本一样不一样,只要crc不一样就视为不同
                        // 还没下载完,就要和已经下载的比对下还有哪些没有下载
                        Debug.Log("还没下载完,已经下载了: " + HotFixConfig.DownloadedResList.assets.Count);
                        res = HotFixConfig.CheckDifference(res, HotFixConfig.DownloadedResList.assets);
                        Debug.LogFormat("还有 {0} 个没有下载", res.Count);

                        if (res.Count == 0)
                        {
                            // 已经下载完,把文件存储下来
                            HotFixConfig.SaveLocalResList(netResList);
                            isDone = true;
                        }
                        else
                        {
                            // 没有下载完,准备好继续下载
                            readToHotfix = true;

                            // 有多少个文件、多大
                            needToDownloadCount = res.Count;
                            foreach (var r in res)
                            {
                                // 由服务器接口查询文件大小
                                //string rUrl = string.Format("{0}?filename={1}", ConfigContent.configURL.APIHost, r.assetName);
                                //needToDownloadTotalSize += GetNetFileLengthAPI(rUrl);
                                string rUrl = string.Format("{0}/static/{1}", ConfigContent.configURL.APIHost, r.assetName);
                                needToDownloadTotalSize += GetNetFileLength(rUrl);
                                Debug.Log("文件:" + rUrl + ",  " + needToDownloadTotalSize);
                            }
                            for (int i = 0; i < res.Count; i++)
                            {
                                var r = res[i];
                                //string fileSizeUrl = string.Format("{0}?filename={1}", ConfigContent.configURL.APIHost, r.assetName);
                                string fileSizeUrl = string.Format("{0}/static/{1}", ConfigContent.configURL.APIHost, r.assetName);
                                string fileUrl     = string.Format("{0}/{1}", ConfigContent.configURL.ResHost, r.assetName);
                                curDownloadAssetName      = r.assetName;
                                curDownloadAssetSize      = 0;   // 当前文件已经下载多大
                                curDownloadAssetTotalSize = GetNetFileLength(fileSizeUrl);
                                curDownloadAssetIndex     = i + 1;
                                currDownloadProgress      = 0.0f; // 当前文件的进度

                                string localFilePath = Path.Combine(HotFixConfig.HotFixConfigDir, r.assetName);

                                Debug.Log(localFilePath);

                                // 设置参数
                                HttpWebRequest request = WebRequest.Create(fileUrl) as HttpWebRequest;
                                //发送请求并获取相应回应数据
                                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                                using (Stream responseStream = response.GetResponseStream())
                                {
                                    //创建本地文件写入流
                                    using (Stream stream = new FileStream(localFilePath, FileMode.OpenOrCreate))
                                    {
                                        byte[] bArr = new byte[4096];
                                        int    size = responseStream.Read(bArr, 0, (int)bArr.Length);
                                        while (size > 0)
                                        {
                                            curDownloadAssetSize += size;
                                            currDownloadProgress  = curDownloadAssetSize * 1.0 / curDownloadAssetTotalSize;
                                            totalProgress         = (downloadedSize + curDownloadAssetSize) * 1.0 / needToDownloadTotalSize;
                                            stream.Write(bArr, 0, size);
                                            size = responseStream.Read(bArr, 0, (int)bArr.Length);
                                        }
                                    }
                                }
                                HotFixConfig.SaveDownloadedResList(r);

                                downloadedCount = i + 1;
                                downloadedSize += curDownloadAssetSize;
                            }
                            HotFixConfig.SaveLocalResList(netResList);
                        }
                    }
                }
                else
                {
                    isDone = true;
                }
            }
            else
            {
                isDone = true;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 更新好后把服务器中的资源列表存储到内存中,并存储到本地
        /// </summary>
        /// <param name="netResList"></param>
        public static void SaveLocalResList(AssetBundleAssetList netResList)
        {
            string jsonContent = netResList.ToNewtonJson();

            LocalResListFilePath.WriteTextAssetContentStr(jsonContent);
        }