Beispiel #1
0
        public static async Task <bool> IsGameVersionCodeEqual()
        {
            string versionCodeInner = GlobalConfigComponent.Instance.GlobalProto.VersionCodeInner;

            Log.Debug($"versionCodeInner:{versionCodeInner}");
            string versionCodeOuter = GlobalConfigComponent.Instance.GlobalProto.VersionCodeOuter;

            Log.Debug($"versionCodeOuter:{versionCodeOuter}");
            string versionCode = versionCodeOuter;

            if (ETModel.Define.isInnetNet)
            {
                versionCode = versionCodeInner;
            }
            UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>();

            try
            {
                //下载VersionCode
                var   webRequestGameVersion = webRequestAsync.DownloadAsync(versionCode);
                await webRequestGameVersion;
                var   versionCodeText = webRequestAsync.Request.downloadHandler.text;
                ZLog.Info($"versionCode:{versionCodeText}");
                webRequestAsync.Dispose();
                //比较VersionCode
                ZLog.Info($"ETModel.Define.versionGameCode:{ETModel.Define.versionGameCode}");
                if (versionCodeText.Equals(ETModel.Define.versionGameCode))
                {
                    ZLog.Info($"versionGameCode Equal");
                    return(true);
                }
                else
                {
                    ZLog.Info($"versionGameCode Not Equal");
                    //while (!VideoUtil.videoFinished)
                    //    await UniRx.Async.UniTask.DelayFrame(1);
                    UIComponent uiComponent        = Game.Scene.GetComponent <UIComponent>();
                    UI          uiLoading          = uiComponent.Get(UIType.UILoading);
                    var         uiLoadingComponent = uiLoading.GetComponent <UILoadingComponent>();
                    //弹提示.
                    var trans = uiLoadingComponent.view.transform.Find("ConfirmWindowVersion");
                    var tip   = new UIUpdateVersionTip(trans);
                    tip.OnConfirm = () =>
                    {
                        ETModel.Define.QuitApplication();
                    };
                    return(false);
                }
            }
            catch (Exception e)
            {
                if (e.Message.Contains("request error"))
                {
                    webRequestAsync.Dispose();
                    ZLog.Info("load VersionGameCode err", e.Message);
                    return(false);
                }
            }
            return(false);
        }
Beispiel #2
0
        public async ETTask DownloadAsync()
        {
            if (this.bundles.Count == 0 && this.downloadingBundle == "")
            {
                return;
            }

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

                    this.downloadingBundle = this.bundles.Dequeue();

                    while (true)
                    {
                        try
                        {
                            using (this.webRequest = ComponentFactory.Create <UnityWebRequestAsync>())
                            {
                                await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);

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

                                alreadyDownloadBytes += (long)this.webRequest.Request.downloadedBytes;

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

                                using (FileStream fs = new FileStream(path, FileMode.Create))
                                {
                                    fs.Write(data, 0, data.Length);
                                }

                                Log.Debug($"<color=green>current downloadingBundle ------->>> {this.downloadingBundle}</color>");
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
                            continue;
                        }

                        break;
                    }
                    this.downloadedBundles.Add(this.downloadingBundle);

                    this.downloadingBundle = "";

                    this.webRequest = null;
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Beispiel #3
0
        private async void UpdateAsync()
        {
            try
            {
                while (true)
                {
                    if (this.bundles.Count == 0)
                    {
                        TagDownloadFinish();
                        break;
                    }

                    this.downloadingBundle = this.bundles.Dequeue();

                    await DownServerBundle();
                    this.downloadedBundles.Add(this.downloadingBundle);
                    this.downloadingBundle = "";
                    this.webRequest = null;
                }

                using (FileStream fs = new FileStream(Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"), FileMode.Create))
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(JsonHelper.ToJson(this.VersionConfig));
                }

                this.Tcs?.SetResult(true);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Beispiel #4
0
        public override async void Start(UILoadResComponent self)
        {
            // 检测apk更新
            if (true)
            {
                using (UnityWebRequestAsync webRequestAsync = ETModel.ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    await webRequestAsync.DownloadAsync("http://fwdown.hy51v.com/njmj/online/files/versionconfig.json");

                    string data = webRequestAsync.Request.downloadHandler.text;
                    ApkVersionConfig.getInstance().init(data);
                    Log.Debug("channel_name = " + PlatformHelper.GetChannelName());
                    Log.Debug("VersionName = " + PlatformHelper.GetVersionName());
                    VersionInfo versionInfo = ApkVersionConfig.getInstance().getDataById(PlatformHelper.GetChannelName());
                    if (versionInfo != null)
                    {
                        // apk更新
                        if (versionInfo.version.CompareTo(PlatformHelper.GetVersionName()) > 0)
                        {
                            DownApkScript.create();

                            return;
                        }
                    }
                }
            }

            // 取消预加载
            // await LoadRes();

            ToastScript.createToast("加载完毕");

            Game.EventSystem.Run(EventIdType.LoadingFinish);
        }
        private async void UpdateAsync()
        {
            try
            {
                while (true)
                {
                    if (this.bundles.Count == 0)
                    {
                        break;
                    }

                    this.downloadingBundle = this.bundles.Dequeue();

                    while (true)
                    {
                        try
                        {
                            using (this.webRequest = ComponentFactory.Create <UnityWebRequestAsync>())
                            {
                                await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);

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

                                string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
                                if (!Directory.Exists(Path.GetDirectoryName(path)))
                                {
                                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                                }
                                using (FileStream fs = new FileStream(path, FileMode.Create))
                                {
                                    fs.Write(data, 0, data.Length);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
                            continue;
                        }

                        break;
                    }
                    this.downloadedBundles.Add(this.downloadingBundle);
                    this.downloadingBundle = "";
                    this.webRequest        = null;
                }

                using (FileStream fs = new FileStream(Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"), FileMode.Create))
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.Write(JsonHelper.ToJson(this.VersionConfig));
                    }

                this.Tcs?.SetResult(true);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Beispiel #6
0
        public async Task DownloadAsync()
        {
            if (this.bundles.Count == 0 && this.downloadingBundle == "")
            {
                // 请自行设计刷新UI进度操作 *这边只做测试代码
                FGUI.GetShowingUI <UI_CheckUpdate>()?.UpdateState(1, 1, 0, "");
                FGUI.GetShowingUI <UI_CheckUpdate>()?.SetState(DownloadState.Done);
                return;
            }

            try
            {
                while (true)
                {
                    if (this.bundles.Count == 0)
                    {
                        // 请自行设计刷新UI进度操作 *这边只做测试代码
                        FGUI.GetShowingUI <UI_CheckUpdate>()?.UpdateState(1, 1, 0, "");
                        FGUI.GetShowingUI <UI_CheckUpdate>()?.SetState(DownloadState.Done);
                        break;
                    }

                    this.downloadingBundle = this.bundles.Dequeue();

                    while (true)
                    {
                        try
                        {
                            using (this.webRequest = ComponentFactory.Create <UnityWebRequestAsync>())
                            {
                                await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);

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

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

                        break;
                    }
                    this.downloadedBundles.Add(this.downloadingBundle);
                    this.downloadingBundle = "";
                    this.webRequest        = null;
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
        public async ETTask DownloadAsync()
        {
            if (this.bundles.Count == 0 && this.downloadingBundle == "")
            {
                return;
            }

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

                    //从队列 出列 获取到要下载的文件
                    this.downloadingBundle = this.bundles.Dequeue();

                    while (true)
                    {
                        try
                        {
                            //UnityWebRequest进行下载请求
                            using (this.webRequest = ComponentFactory.Create <UnityWebRequestAsync>())
                            {
                                //要下载的路径:GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle
                                await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);

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

                                //要写入本地文件夹的路径
                                string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
                                using (FileStream fs = new FileStream(path, FileMode.Create))
                                {
                                    fs.Write(data, 0, data.Length);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
                            continue;
                        }

                        break;
                    }
                    //每下载一个 就会添加到哈希表中
                    this.downloadedBundles.Add(this.downloadingBundle);
                    this.downloadingBundle = "";
                    this.webRequest        = null;
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Beispiel #8
0
        public async Task Req(string url)
        {
            using (UnityWebRequestAsync webRequestAsync = ETModel.ComponentFactory.Create <UnityWebRequestAsync>())
            {
                await webRequestAsync.DownloadAsync(url);

                init(webRequestAsync.Request.downloadHandler.text);
            }
        }
Beispiel #9
0
        public async Task DownloadApkAsync()
        {
            using (webRequest = ComponentFactory.Create <UnityWebRequestAsync>())
            {
                await webRequest.DownloadAsync(remoteInstallPacketConfig.AndroidUrl);

                FileHelper.WriteFile(Path.Combine(PathHelper.AppHotfixResPath, remoteInstallPacketConfig.ApkName), webRequest.Request.downloadHandler.data);
            }
        }
        //初始化服务器版本配置信息
        public async ETTask InitServerVersions()
        {
            using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
            {
                string serverGameVersionsUrl = GlobalConfigComponent.Instance.GlobalProto.GetGameVersionsUrl();
                await webRequestAsync.DownloadAsync(serverGameVersionsUrl);

                ServerGameVersionsConfig = JsonMapper.ToObject <GameVersionsConfig>(webRequestAsync.Request.downloadHandler.text);
            }
        }
Beispiel #11
0
        /// <summary>
        /// 开始下载
        /// </summary>
        /// <returns></returns>
        public async ETTask DownloadAsync()
        {
            Log.Debug("ab包数量:" + this.bundles.Count + "下载名称:" + downloadingBundle);
            if (this.bundles.Count == 0 && this.downloadingBundle == "")
            {
                return;
            }

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

                    this.downloadingBundle = this.bundles.Dequeue();

                    while (true)
                    {
                        try
                        {
                            using (this.webRequest = ComponentFactory.Create <UnityWebRequestAsync>())
                            {
                                //Log.Debug("下载文件:" + GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);
                                await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);

                                byte[] data = this.webRequest.Request.downloadHandler.data;
                                //下载完成 拷贝到本地PD
                                string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
                                using (FileStream fs = new FileStream(path, FileMode.Create))
                                {
                                    fs.Write(data, 0, data.Length);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
                            continue;
                        }

                        break;
                    }
                    this.downloadedBundles.Add(this.downloadingBundle);
                    this.downloadingBundle = "";
                    this.webRequest        = null;
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Beispiel #12
0
        //开始下载资源
        private async ETTask DownloadAsync()
        {
            if (this.bundles.Count == 0 && this.downloadingBundle == "")
            {
                return;
            }
            try
            {
                while (true)
                {
                    if (this.bundles.Count == 0)
                    {
                        break;
                    }
                    this.downloadingBundle = this.bundles.Dequeue();
                    while (true)
                    {
                        try
                        {
                            using (this.webRequest = ComponentFactory.Create <UnityWebRequestAsync>())
                            {
                                string cmbinPath = Path.Combine(GlobalConfigComponent.Instance.GlobalProto.GetUrl(), "StreamingAssets", downloadFoldr, this.downloadingBundle);
                                await this.webRequest.DownloadAsync(cmbinPath);

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

                                string path          = Path.Combine(PathHelper.AppHotfixResPath, downloadFoldr, this.downloadingBundle);
                                string directoryName = path.Substring(0, path.LastIndexOf('/'));
                                PathHelper.CreateDirectory(directoryName);//如果文件夹不存在就创建
                                using (FileStream fs = new FileStream(path, FileMode.Create))
                                {
                                    fs.Write(data, 0, data.Length);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
                            continue;
                        }
                        break;
                    }
                    this.downloadedBundles.Add(this.downloadingBundle);
                    this.downloadingBundle = "";
                    this.webRequest        = null;
                }
            }
            catch (Exception e)
            {
                pLoserCall?.Invoke(e.ToString());
                Log.Error(e);
            }
        }
Beispiel #13
0
        /// <summary>
        /// 遍历目录
        /// </summary>
        /// <param name="path"></param>
        public static async Task <bool> CopyFileToHotfixRes(string orginPath, string abName)
        {
            using (UnityWebRequestAsync www = ComponentFactory.Create <UnityWebRequestAsync>())
            {
                Log.Debug("加载本地orginPath ===> " + orginPath);
                await www.DownloadAsync(orginPath);

                Log.Debug("读取本地ab包成功,准备写入热更新目录 size ==> " + www.Request.downloadHandler.data.Length);
                string hotfixResPath = Path.Combine(PathHelper.AppHotfixResPath + abName);

                Log.Debug("hotfixResPath ==> " + hotfixResPath);
                File.WriteAllBytes(hotfixResPath, www.Request.downloadHandler.data);
            }

            return(true);
        }
Beispiel #14
0
        private async Task DownServerBundle()
        {
            while (true)
            {
                try
                {
                    using (this.webRequest = ComponentFactory.Create<UnityWebRequestAsync>())
                    {
                        DownloadInfo.IsStart = true;
                        if (ETModel.Define.IsABNotFromServer)
                        {
                            await this.webRequest.DownloadAsync(GetUrlWithPlatform(ETModel.Define.SelfResourceServerIpAndPort + "/") + "StreamingAssets/" + this.downloadingBundle);
                        }
                        else
                        {
                            await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);
                        }
                        //await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);
                        byte[] data = this.webRequest.Request.downloadHandler.data;
                        string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
                        if (!Directory.Exists(Path.GetDirectoryName(path)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(path));
                        }


                        using (FileStream fs = new FileStream(path, FileMode.Create))
                        {
                            fs.Write(data, 0, data.Length);
                            ZLog.Info($"更新Bundle:{path} 完成");
                        }
                        var p = this.Progress;
                        BundleRealProgress?.Invoke(p);
                    }
                }
                catch (Exception e)
                {
                    Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
                    //如果报错了,等1秒
                    await UniRx.Async.UniTask.Delay(1);
                    continue;
                }

                break;
            }
        }
Beispiel #15
0
        public async Task StartAsync()
        {
            string installPacketUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    installPacketUrl = $"{NetLineSwitchComponent.Instance.CurrentServerResDomain()}InstallPacket.txt";
                    await webRequestAsync.DownloadAsync(installPacketUrl);

                    remoteInstallPacketConfig = JsonHelper.FromJson <InstallPacketConfig>(webRequestAsync.Request.downloadHandler.text);
                }
            }
            catch (Exception e)
            {
                throw new Exception($"url: {installPacketUrl}", e);
            }
        }
Beispiel #16
0
        /// <summary>
        /// 下载超时检测
        /// </summary>
        /// <returns></returns>
        public async ETTask CheckDownLoadTimeOut()
        {
            if (this.tokenSource != null)
            {
                this.tokenSource.Cancel();
            }

            this.tokenSource = new CancellationTokenSource();

            this.cancelToken = this.tokenSource.Token;

            TimerComponent timerComponent = Game.Scene.GetComponent <TimerComponent>();

            while (true)
            {
                await timerComponent.WaitAsync(this.downLoadTimeOut * 1000, this.cancelToken);

                if (this.Progress < 100)
                {
                    if (this.webRequest != null)
                    {
                        this.webRequest.isCancel = true;

                        this.webRequest = null;
                    }

                    if (this.LoadSlider != null)
                    {
                        this.LoadSlider.SetActive(false);
                    }

                    if (this.UpdateFlag != null)
                    {
                        this.UpdateFlag.gameObject.SetActive(true);
                    }

                    Game.EventSystem.Run("SubGameDownLoadFail", this.downloadingBundle);

                    break;
                }
            }
        }
Beispiel #17
0
        /// <summary>
        /// 获取本地版本文件
        /// </summary>
        /// <returns></returns>
        public static async ETTask <VersionConfig> GetLocalVersionConfig()
        {
            VersionConfig streamingVersionConfig = new VersionConfig();
            string        versionPath            = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");

            try
            {
                using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    //Log.Debug("本地版本文件路径:" + versionPath);
                    await request.DownloadAsync(versionPath);

                    streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
                }
            }
            catch (Exception e)
            {
                Log.Error($"获取本地版本文件 version.txt error:{e}");
            }
            return(streamingVersionConfig);
        }
Beispiel #18
0
        /// <summary>
        /// 获取远程版本文件
        /// </summary>
        /// <returns></returns>
        public static async ETTask <VersionConfig> GetRemoteVersion()
        {
            VersionConfig remoteVersionConfig;

            string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    await webRequestAsync.DownloadAsync(versionUrl);

                    remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                }
            }
            catch (Exception e)
            {
                Log.Error($"download version.txt error:{e}");

                throw;
            }

            return(remoteVersionConfig);
        }
Beispiel #19
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;
            }
        }
Beispiel #20
0
        public async Task StartAsync()
        {
            using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())
            {
                string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
                Log.Debug(versionUrl);
                await request.DownloadAsync(versionUrl);

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

            Log.Debug("WebVersion:\n" + JsonHelper.ToJson(this.VersionConfig));

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

            if (!File.Exists(versionPath))
            {
                foreach (FileVersionInfo versionInfo in this.VersionConfig.FileInfoDict.Values)
                {
                    if (versionInfo.File == "Version.txt")
                    {
                        continue;
                    }
                    this.bundles.Enqueue(versionInfo.File);
                    this.TotalSize += versionInfo.Size;
                }
            }
            else
            {
                VersionConfig localVersionConfig = JsonHelper.FromJson <VersionConfig>(File.ReadAllText(versionPath));
                Log.Debug("LocalVersion:\n" + JsonHelper.ToJson(localVersionConfig));
                // 先删除服务器端没有的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();
        }
Beispiel #21
0
        public static async Task <bool> IsGameVersionCodeEqual()
        {
            string versionCodeInner = GlobalConfigComponent.Instance.GlobalProto.VersionCodeInner;

            Log.Debug($"versionCodeInner:{versionCodeInner}");
            string versionCodeOuter = GlobalConfigComponent.Instance.GlobalProto.VersionCodeOuter;

            Log.Debug($"versionCodeOuter:{versionCodeOuter}");
            string versionCode = versionCodeOuter;

            if (ETModel.Define.isInnetNet)
            {
                versionCode = versionCodeInner;
            }
            UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>();

            try
            {
                //下载VersionCode
                var   webRequestGameVersion = webRequestAsync.DownloadAsync(versionCode);
                await webRequestGameVersion;
                var   versionCodeText = webRequestAsync.Request.downloadHandler.text;
                Log.Info($"versionCode:{versionCodeText}");
                webRequestAsync.Dispose();
                //比较VersionCode
                Log.Info($"ETModel.Define.versionGameCode:{ETModel.Define.versionGameCode}");
                if (versionCodeText.Equals(ETModel.Define.versionGameCode))
                {
                    Log.Info($"versionGameCode Equal");
                    return(true);
                }
                else
                {
#if FOR_JIAOJIE_2021_9_22
#else
                    return(true);
#endif
                    Log.Info($"versionGameCode Not Equal");
                    //just send a message out is enough. let someone do the job happily
                    Game.EventSystem.Run(EventIdType.BigVersionCheckFailure);
                    return(false);

                    //while (!VideoUtil.videoFinished)
                    //    await UniRx.Async.UniTask.DelayFrame(1);
                    //UIComponent uiComponent = Game.Scene.GetComponent<UIComponent>();
                    //UI uiLoading = uiComponent.Get(UIType.UILoading);
                    //var uiLoadingComponent = uiLoading.GetComponent<UILoadingComponent>();
                    ////弹提示.
                    //var trans = uiLoadingComponent.view.transform.Find("ConfirmWindowVersion");
                    //var tip = new UIUpdateVersionTip(trans);
                    //tip.OnConfirm = () =>
                    //{
                    //    ETModel.Define.QuitApplication();
                    //};
                }
            }
            catch (Exception e)
            {
                if (e.Message.Contains("request error"))
                {
                    webRequestAsync.Dispose();
                    Log.Info("load VersionGameCode err", e.Message);
#if FOR_JIAOJIE_2021_9_22
#else
                    return(true);
#endif
                    return(false);
                }
            }
#if FOR_JIAOJIE_2021_9_22
#else
            return(true);
#endif
            return(false);
        }
Beispiel #22
0
        public async ETTask StartAsync()
        {
            alreadyDownloadBytes = 0;

            TotalSize = 0;

            CurrVersionBytes = 0;

            string versionUrl = "";

            versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";

            Log.Debug($"当前资源为AB模式,远程版本文件路径:" + versionUrl);

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    await webRequestAsync.DownloadAsync(versionUrl);

                    CurrVersionBytes += (long)webRequestAsync.Request.downloadedBytes;

                    // 获取远程的Version.txt
                    remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                }
            }
            catch (Exception e)
            {
                Log.Error($"download version.txt error: {e}");
                return;
            }

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

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

            using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())
            {
                Log.Debug("本地版本文件路径:" + versionPath);

                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)
            {
                // 对比md5
                string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
                //如果MD5相同,且当前项目不含有这个文件
                if (fileVersionInfo.MD5 == localFileMD5)
                {
                    continue;
                }

                if (IsFilterRes(fileVersionInfo.File))
                {
                    continue;
                }

                this.bundles.Enqueue(fileVersionInfo.File);

                this.TotalSize += fileVersionInfo.Size;
            }
        }
        public async Task StartAsync()
        {
            // 获取远程的Version.txt
            string versionUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/Version.txt";
                    Log.Error("server version=" + versionUrl);
                    await webRequestAsync.DownloadAsync(versionUrl);

                    remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                    Log.Warning("server version json=" + JsonHelper.ToJson(remoteVersionConfig));
                }
            }
            catch (Exception e)
            {
                throw new Exception($"url: {versionUrl}", e);
            }
            // 获取HotfixResPath目录的Version.txt
            string versionPath = Path.Combine($"file://{PathHelper.AppHotfixResPath}", "Version.txt");

            Log.Debug("local version=" + versionPath);

            using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())//未下载的
            {
                try
                {
                    await request.DownloadAsync(versionPath);

                    streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
                }
                catch (Exception e)
                {
                    Log.Debug($"获取本地目录的Version.txt 失败! Message: {e.Message}");
                }
            }

            // 获取streaming目录的Version.txt
            if (null == streamingVersionConfig)
            {
                // string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");
                versionPath = Path.Combine($"file://{PathHelper.AppResPath}", "Version.txt");
                using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    try
                    {
                        await request.DownloadAsync(versionPath);

                        streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
                    }
                    catch (Exception e)
                    {
                        Log.Debug($"获取本地目录的Version.txt 失败! Message: {e.Message}");
                        streamingVersionConfig = new VersionConfig();
                    }
                }
                var verLocal = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");//创建路径
                if (Directory.Exists(PathHelper.AppHotfixResPath) == false)
                {
                    Directory.CreateDirectory(PathHelper.AppHotfixResPath);
                }
                using (FileStream fs = new FileStream(verLocal, FileMode.Create))//创建
                {
                    VersionConfig t = new VersionConfig()
                    {
                        Version = remoteVersionConfig.Version, TotalSize = 0, ABSecurity = remoteVersionConfig.ABSecurity, FileInfoDict = new Dictionary <string, FileVersionInfo>()
                    };
                    var    json  = JsonHelper.ToJson(t);
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(json);
                    fs.Write(bytes, 0, bytes.Length);
                }
            }

            // 删掉远程不存在的文件
            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 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
                if (fileVersionInfo.MD5 == localFileMD5 || fileVersionInfo.File == "Version.txt")//txt不去对比md5
                {
                    continue;
                }
                bundles.Enqueue(fileVersionInfo.File);
                TotalSize += fileVersionInfo.Size;
            }
        }
Beispiel #24
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();
            }
        }
        public async ETTask StartAsync()
        {
            // 获取远程的Version.txt
            string versionUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    //UnityWebRequest UnityWebRequest www下载
                    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;
            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)
                {
                    //就是远程下载下来的版本文件 json反序列化成一个实体类 FileInfoDict这个字典就包含了所有AB文件信息
                    if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name))
                    {
                        continue;
                    }

                    if (fileInfo.Name == "Version.txt")
                    {
                        continue;
                    }
                    //为什么要删除掉远程不存在的文件呢 主要是为了避免加载资源出错 资源重复 会导致到报错
                    fileInfo.Delete();
                }
            }
            //如果保存热更新资源的文件夹不存在的话 通过Create进行创建
            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;
                }
                //如果两个文件的MD5不一致 把要下载的文件 压入到队列
                this.bundles.Enqueue(fileVersionInfo.File);
                //下载的总大小也加上这个文件的大小
                this.TotalSize += fileVersionInfo.Size;
            }
        }
Beispiel #26
0
        //对比本地资源和服务器资源
        public async ETTask StartAsync(string fileFoldr = "", string versionName = "Version.txt")
        {
            downloadFoldr = fileFoldr;
            // 获取远程的Version.txt
            string versionUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    versionUrl = Path.Combine(GlobalConfigComponent.Instance.GlobalProto.GetUrl(), "StreamingAssets", downloadFoldr, versionName);
                    await webRequestAsync.DownloadAsync(versionUrl);

                    remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                }
            }
            catch (Exception e)
            {
                pLoserCall?.Invoke(e.ToString());
                throw new Exception($"url: {versionUrl}", e);
            }

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

            if (directoryInfo.Exists)
            {
                FileInfo[] fileInfos = directoryInfo.GetFiles();
                foreach (FileInfo fileInfo in fileInfos)
                {
                    if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name))
                    {
                        continue;
                    }
                    if (fileInfo.Name == versionName)
                    {
                        continue;
                    }
                    fileInfo.Delete();
                }
            }
            else
            {
                directoryInfo.Create();
            }


            // 对比MD5
            foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values)
            {
                // 对比md5
                string localFileMD5 = BundleHelper.GetBundleMD5(fileVersionInfo.File);
                if (fileVersionInfo.MD5 == localFileMD5)
                {
                    continue;
                }
                this.bundles.Enqueue(fileVersionInfo.File);
                this.TotalSize += fileVersionInfo.Size;
            }
            await DownloadAsync();
        }
        /// <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 DownloadAsync()
        {
            if (bundles.Count == 0 && downloadingBundle == "")
            {
                return;
            }

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

                    downloadingBundle = bundles.Dequeue();

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

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

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

                                if (path.Contains("Version.txt") == false)
                                {
                                    var versionLocalPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");//本地文件
                                    var versionLocalson  = JsonHelper.FromJson <VersionConfig>(File.ReadAllText(versionLocalPath));
                                    versionLocalson.FileInfoDict[downloadingBundle] = remoteVersionConfig.FileInfoDict[downloadingBundle];
                                    versionLocalson.Version    = remoteVersionConfig.Version;
                                    versionLocalson.ABSecurity = remoteVersionConfig.ABSecurity;

                                    var          content = JsonHelper.ToJson(versionLocalson);
                                    StreamWriter sw      = new StreamWriter(versionLocalPath);
                                    sw.Write(content);
                                    sw.Close();

                                    var pathPP = Path.GetDirectoryName(path);
                                    if (Directory.Exists(pathPP) == false)
                                    {
                                        Directory.CreateDirectory(pathPP);
                                    }

                                    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}");
                            continue;
                        }

                        break;
                    }
                    downloadedBundles.Add(downloadingBundle);
                    downloadingBundle = "";
                    webRequest        = null;
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Beispiel #29
0
        public async ETTask StartAsync()
        {
            // 获取远程的Version.txt
            string versionUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    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;
            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)
            {
                // 对比md5
                string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
                if (fileVersionInfo.MD5 == localFileMD5)
                {
                    continue;
                }
                this.bundles.Enqueue(fileVersionInfo.File);
                this.TotalSize += fileVersionInfo.Size;
            }
        }
Beispiel #30
0
        public async ETTask StartAsync()
        {
            // 获取远程的Version.txt
            string versionUrl = "";

            try
            {
                using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create <UnityWebRequestAsync>())
                {
                    versionUrl = PathHelper.RemoteLoadPath + "/Version.txt";
                    await webRequestAsync.DownloadAsync(versionUrl);

                    remoteVersionConfigData = webRequestAsync.Request.downloadHandler.data;
                    remoteVersionConfig     = JsonHelper.FromJson <VersionConfig>(webRequestAsync.Request.downloadHandler.text);
                }
            }
            catch (Exception e)
            {
                throw new Exception($"url: {versionUrl}", e);
            }
            try
            {
                // 获取streaming目录的Version.txt
                VersionConfig streamingVersionConfig;
                string        versionPath = PathHelper.SavePath + "Version.txt";
                if (File.Exists(versionPath))
                {
                    streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(File.ReadAllText(versionPath));
                }
                else
                {
                    streamingVersionConfig = new VersionConfig();
                }

                // 删掉远程不存在的文件
                DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.SavePath);
                if (directoryInfo.Exists)
                {
                    FileInfo[] fileInfos             = directoryInfo.GetFiles();
                    int        directoryFolderLength = directoryInfo.FullName.Length;
                    foreach (FileInfo fileInfo in fileInfos)
                    {
                        if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.FullName.Substring(directoryFolderLength)))
                        {
                            continue;
                        }

                        if (fileInfo.Name == "Version.txt")
                        {
                            continue;
                        }
                        fileInfo.Delete();
                    }
                }
                else
                {
                    directoryInfo.Create();
                }

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


                    if (fileVersionInfo.MD5 == localFileMD5)
                    {
                        hasDonwLoadedFile.Add(fileVersionInfo.File, fileVersionInfo);
                        continue;
                    }

                    this.bundles.Enqueue(fileVersionInfo.File);
                    this.TotalSize += fileVersionInfo.Size;
                }
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.LogError(ex.StackTrace);
            }
        }