Example #1
0
        public async Task <bool> StartAsync()
        {
            // 获取远程的Version.txt
            string versionUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = new UnityWebRequestAsync())
                {
                    versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";

                    var result = await webRequestAsync.DownloadAsync(versionUrl);

                    if (string.IsNullOrWhiteSpace(result))
                    {
                        remoteVersionConfig = MongoHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                    }
                    else
                    {
                        Log.Error($"url: {versionUrl}; result:{result}");
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error($"url: {versionUrl} {e}");
                return(false);
            }

            // 获取streaming目录的Version.txt
            VersionConfig streamingVersionConfig = null;

            string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");

            using (UnityWebRequestAsync request = new UnityWebRequestAsync())
            {
                var result = await request.DownloadAsync(versionPath);

                if (string.IsNullOrWhiteSpace(result))
                {
                    streamingVersionConfig = MongoHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
                }
            }

            // 删掉远程不存在的文件
            DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath);

            if (directoryInfo.Exists)
            {
                FileInfo[] fileInfos = directoryInfo.GetFiles();

                foreach (FileInfo fileInfo in fileInfos)
                {
                    if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name))
                    {
                        continue;
                    }

                    if (fileInfo.Name == "Version.txt")
                    {
                        continue;
                    }

                    fileInfo.Delete();
                }
            }
            else
            {
                directoryInfo.Create();
            }

            // 对比MD5
            foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values)
            {
                // 对比md5
                string localFileMD5 = GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);

                if (fileVersionInfo.MD5 == localFileMD5)
                {
                    continue;
                }

                bundles.Enqueue(fileVersionInfo.File);
                TotalSize += fileVersionInfo.Size;
            }

            return(true);
        }
Example #2
0
        public async Task <bool> DownloadAsync()
        {
            var result = true;

            if (bundles.Count == 0 && downloadingBundle == "")
            {
                return(result);
            }

            try
            {
                while (true)
                {
                    if (bundles.Count == 0)
                    {
                        break;
                    }

                    downloadingBundle = bundles.Dequeue();

                    while (true)
                    {
                        try
                        {
                            using (webRequest = new UnityWebRequestAsync())
                            {
                                await webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + downloadingBundle);

                                byte[] data = webRequest.Request.downloadHandler.data;

                                string path = Path.Combine(PathHelper.AppHotfixResPath, downloadingBundle);

                                using (FileStream fs = new FileStream(path, FileMode.Create))
                                {
                                    fs.Write(data, 0, data.Length);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error($"download bundle error: {downloadingBundle}\n{e}");
                            result = false;
                            continue;
                        }

                        break;
                    }

                    downloadedBundles.Add(downloadingBundle);
                    downloadingBundle = "";
                    webRequest        = null;
                }
            }
            catch (Exception e)
            {
                Log.Exception(e);
                result = false;
            }

            return(result);
        }