/// <summary>
        /// 开始下载远程Version文件进行本地对比 确认需要下载的AB包文件
        /// </summary>
        /// <returns></returns>
        public async Task StartAsync()
        {
            // 获取远程的Version.txt
            string versionUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    //从Resources文件夹读取“VK”预制体上的TXT文档取得AB资源服务区地址 得到最新的Version.txt
                    versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
                    //Log.Debug(versionUrl);
                    await webRequestAsync.DownloadAsync(versionUrl);                     //下载资源

                    remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                    //Log.Debug(JsonHelper.ToJson(this.VersionConfig));
                }
            }
            catch (Exception e)
            {
                throw new Exception($"url: {versionUrl}", e);
            }

            // 获取streaming目录的Version.txt
            VersionConfig streamingVersionConfig;
            //得到本地Version文件地址
            string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");

            using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())
            {
                await request.DownloadAsync(versionPath);

                streamingVersionConfig = JsonHelper.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)
            {
                // 本地
                string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
                if (fileVersionInfo.MD5 == localFileMD5)
                {
                    continue;
                }
                this.bundles.Enqueue(fileVersionInfo.File);                 //需要重新下载的AB包的名字
                this.TotalSize += fileVersionInfo.Size;                     //需要下载的资源的总大小
            }
        }
        public async Task StartAsync()
        {
            using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
            {
                string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
                //Log.Debug(versionUrl);
                await webRequestAsync.DownloadAsync(versionUrl);

                this.VersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                //Log.Debug(JsonHelper.ToJson(this.VersionConfig));
            }


            VersionConfig localVersionConfig;
            // 对比本地的Version.txt
            string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");

            if (File.Exists(versionPath))
            {
                localVersionConfig = JsonHelper.FromJson <VersionConfig>(File.ReadAllText(versionPath));
            }
            else
            {
                versionPath = Path.Combine(PathHelper.AppResPath, "Version.txt");
                using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    await request.DownloadAsync(versionPath);

                    localVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
                }
            }


            // 先删除服务器端没有的ab
            foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileInfoDict.Values)
            {
                if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File))
                {
                    continue;
                }
                string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File);
                File.Delete(abPath);
            }

            // 再下载
            foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileInfoDict.Values)
            {
                FileVersionInfo localVersionInfo;
                if (localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo))
                {
                    if (fileVersionInfo.MD5 == localVersionInfo.MD5)
                    {
                        continue;
                    }
                }

                if (fileVersionInfo.File == "Version.txt")
                {
                    continue;
                }

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

            if (this.bundles.Count == 0)
            {
                return;
            }

            //Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}");
            await this.WaitAsync();
        }
Exemple #3
0
        /// <summary>
        /// 返回是否需要下载
        /// </summary>
        /// <returns></returns>
        public async Task<bool> LoadInfo()
        {
            isNetNotReach = false;
            string versionUploadSelfResUrl = GlobalConfigComponent.Instance.GlobalProto.VersionUploadSelfResUrl;
            Log.Debug($"versionUploadSelfResUrl:{versionUploadSelfResUrl}");
            if (ETModel.Define.isInnetNet)
            {
                versionUploadSelfResUrl = GlobalConfigComponent.Instance.GlobalProto.VersionUploadSelfResUrl_InnerNet;
                Log.Debug($"VersionUploadSelfResUrl_InnerNet:{versionUploadSelfResUrl}");
            }
            UnityWebRequestAsync webRequestSelfVersionAsync = ComponentFactory.Create<UnityWebRequestAsync>();
            UnityWebRequestAsync webRequestAsync = ComponentFactory.Create<UnityWebRequestAsync>();
            string versionText = string.Empty;
            try
            {
                //下载外层资源文件夹版本,即父目录
                var webRequestSelfResVersionTask = webRequestSelfVersionAsync.DownloadAsync(versionUploadSelfResUrl);
                await webRequestSelfResVersionTask;
                ZLog.Info($"webRequestSelfResVersionText:{webRequestSelfVersionAsync.Request.downloadHandler.text}");
                ETModel.Define.ParentResABDirectory = webRequestSelfVersionAsync.Request.downloadHandler.text;
                ZLog.Info($"ParentDirectory:{ETModel.Define.ParentResABDirectory}");
                webRequestSelfVersionAsync.Dispose();
                //下载bundle流程
                string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
                if (ETModel.Define.IsABNotFromServer)
                {
                    versionUrl = GetUrlWithPlatform(ETModel.Define.SelfResourceServerIpAndPort + "/") + "StreamingAssets/" + "Version.txt";
                }
                Log.Debug($"versionUrl:{versionUrl}");
                var webRequestTask = webRequestAsync.DownloadAsync(versionUrl);
                await webRequestTask;
                versionText = webRequestAsync.Request.downloadHandler.text;
                webRequestSelfVersionAsync.Dispose();
            }
            catch (Exception e)
            {
                if (e.Message.Contains("request error"))
                {
                    webRequestSelfVersionAsync.Dispose();
                    webRequestAsync.Dispose();
                    ZLog.Info("load Version err", e.Message);
                    Define.isUseStreamingAssetRes = true;
                    OnFileServerNotReach(e.Message);
                    return false;
                }
            }
            ZLog.Info($"versionText:{versionText}");
            if (!versionText.StartsWith("{"))
            {
                this.VersionConfig = null;
                return false;
            }
            this.VersionConfig = JsonHelper.FromJson<VersionConfig>(versionText);
            //Log.Debug(JsonHelper.ToJson(this.VersionConfig));


            if (isNetNotReach)//文件服务器没开启
            {
                //var timeTask = DelayFrame();
                //this.TagDownloadFinish();
                //await timeTask;
                return false;
            }
            else //成功的事情
            {
                VersionConfig localVersionConfig;
                // 对比本地的Version.txt
                string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");
                if (File.Exists(versionPath))
                {
                    localVersionConfig = JsonHelper.FromJson<VersionConfig>(File.ReadAllText(versionPath));
                }
                else
                {
                    versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");

                    using (UnityWebRequestAsync request = ComponentFactory.Create<UnityWebRequestAsync>())
                    {
                        try
                        {
                            await request.DownloadAsync(versionPath);
                            localVersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
                        }
                        catch (System.Exception e)
                        {
                            Log.Debug(e.ToString());
                            localVersionConfig = null;
                        }
                    }
                }

                if (localVersionConfig != null)
                {
                    // 先删除服务器端没有的ab
                    foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileInfoDict.Values)
                    {
                        if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File))
                        {
                            continue;
                        }
                        string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File);

                        if(File.Exists(abPath))File.Delete(abPath);
                    }
                }


                // 再下载
                foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileInfoDict.Values)
                {
                    FileVersionInfo localVersionInfo;
                    if (localVersionConfig != null && localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo))
                    {
                        if (fileVersionInfo.MD5 == localVersionInfo.MD5)
                        {
                            continue;
                        }
                    }

                    if (fileVersionInfo.File == "Version.txt")
                    {
                        continue;
                    }

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

                //if (this.bundles.Count == 0)
                //{
                //	return;
                //}

                //Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}");
                return true;
                //await Down();
            }
        }
Exemple #4
0
        /// <summary>
        /// 拿到远程和本地不需要热更的bundle,添加到bundles
        /// </summary>
        /// <returns></returns>
        public async ETTask StartAsync()
        {
            // 获取远程的Version.txt
            string versionUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    //web资源服务器地址 本地为http://127.0.0.1:8080/PC/    (PC为平台)
                    versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
                    Log.Debug("web资源服务器地址" + versionUrl);
                    //开始下载版本信息
                    await webRequestAsync.DownloadAsync(versionUrl);

                    //将下载到的远程版本配置信息反序列化为VersionConfig
                    remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                    Log.Debug("热更新到的VersionConfig:" + JsonHelper.ToJson(this.remoteVersionConfig));
                }
            }
            catch (Exception e)
            {
                throw new Exception($"url: {versionUrl}", e);
            }

            // 获取客户端的Version.txt
            // 获取streaming目录的Version.txt
            VersionConfig streamingVersionConfig;
            //地址合并  拿到本地版本信息文件路径
            string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");

            Log.Debug("本地Version.txt地址:" + versionPath);

            using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())
            {
                await request.DownloadAsync(versionPath);

                streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
                Log.Debug("本地Version.txt内容:" + streamingVersionConfig);
            }


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

            Log.Debug("应用程序外部资源路径存放路径(热更新资源路径):" + PathHelper.AppHotfixResPath);
            if (directoryInfo.Exists)
            {
                //有此AppHotfixResPath我呢见驾就获取其中文件
                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      (远程文件对应的本地文件的MD5)
                string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
                if (fileVersionInfo.MD5 == localFileMD5)
                {
                    continue;
                }
                //添加到bundles队列
                this.bundles.Enqueue(fileVersionInfo.File);
                //总计大小
                this.TotalSize += fileVersionInfo.Size;
            }
        }
Exemple #5
0
        public void GetPool(HttpMessage httpMessage)
        {
            Dictionary <string, BlockSub> transfers = Entity.Root.GetComponent <Rule>().GetTransfers();

            httpMessage.result = JsonHelper.ToJson(transfers);
        }