Esempio n. 1
0
    private async void UpdateAsync()
    {
        try
        {
            while (true)
            {
                if (this.needDownLoadBundles.Count == 0)
                {
                    TagDownloadFinish();
                    break;
                }

                this.downloadingBundle = this.needDownLoadBundles.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.remoteVersionConfig));
                }

            this.Tcs?.SetResult(true);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }
Esempio n. 2
0
    /// <summary>
    /// 返回local version config
    /// </summary>
    private async Task <VersionConfig> GetLocalVersionConfig()
    {
        VersionConfig localVersionConfig;
        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");
            UnityWebRequestAsync request = MonoBehaviourHelper.CreateTempComponent <UnityWebRequestAsync>();
            try
            {
                await request.DownloadAsync(versionPath);

                localVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
            }
            catch (System.Exception e)
            {
                Debug.Log(e.ToString());
                localVersionConfig = null;
            }
            finally
            {
                Destroy(request.gameObject);
                request = null;
            }
        }
        return(localVersionConfig);
    }
Esempio n. 3
0
    public static async Task <bool> IsGameVersionCodeEqual()
    {
        UnityWebRequestAsync webRequestAsync = MonoBehaviourHelper.CreateTempComponent <UnityWebRequestAsync>();

        try
        {
            GlobalProto.VersionCodeInfo NowLineCodeInfo  = null;
            GlobalProto.VersionCodeInfo UpdatingCodeInfo = null;
            string PathNowLineCodeInfo = GlobalConfigComponent.intance.GlobalProto.GetNowLineCodeInfoPath();
            //下载PathNowLineCodeInfo
            await webRequestAsync.DownloadAsync(PathNowLineCodeInfo);

            NowLineCodeInfo = GlobalConfigComponent.intance.GlobalProto.GetNowLineCodeInfo(webRequestAsync.Request.downloadHandler.text);
            string PathUpdatingCodeInfo = GlobalConfigComponent.intance.GlobalProto.GetUpdatingCodeInfoPath();
            //下载PathUpdatingCodeInfo
            await webRequestAsync.DownloadAsync(PathUpdatingCodeInfo);

            UpdatingCodeInfo = GlobalConfigComponent.intance.GlobalProto.GetUpdatingCodeInfo(webRequestAsync.Request.downloadHandler.text);
            webRequestAsync.Dispose();
            //比较VersionCode
            StaticData.DebugGreen($"local versionGameCode:{StaticData.localVersionGameCode},remote NowLineCodeInfo GameVersionCode:{NowLineCodeInfo.GameVersionCode} remote NowLineCodeInfo ResVersionCode:{NowLineCodeInfo.ResVersionCode},remote UpdatingCodeInfo GameVersionCode:{UpdatingCodeInfo.GameVersionCode} remote UpdatingCodeInfo ResVersionCode:{UpdatingCodeInfo.ResVersionCode}");
            if (StaticData.localVersionGameCode < NowLineCodeInfo.GameVersionCode)
            {
                //强更
                TipsDifferentVersion();
                StaticData.intParentResABDirectory      = NowLineCodeInfo.ResVersionCode;
                StaticData.isUsePlatformUpdatingGateWay = false;
                return(false);
            }
            else if (StaticData.localVersionGameCode == NowLineCodeInfo.GameVersionCode)
            {
                //热更本地资源
                StaticData.intParentResABDirectory      = NowLineCodeInfo.ResVersionCode;
                StaticData.isUsePlatformUpdatingGateWay = false;
                return(true);
            }
            else
            {
                //已经是最新包,只需要更新最新包资源即可
                StaticData.intParentResABDirectory      = UpdatingCodeInfo.ResVersionCode;
                StaticData.isUsePlatformUpdatingGateWay = true;
                return(true);
            }
        }
        catch (Exception e)
        {
            if (e.Message.Contains("request error"))
            {
                Debug.Log($"load VersionGameCode error:'{e.Message}'");
                return(true);
            }
        }
        finally
        {
            GameObject.Destroy(webRequestAsync.gameObject);
        }
        return(false);
    }
Esempio n. 4
0
        public static async Task Req(string url)
        {
            using (UnityWebRequestAsync webRequestAsync = ETModel.ComponentFactory.Create <UnityWebRequestAsync>())
            {
                await webRequestAsync.DownloadAsync(url);

                InitWords(webRequestAsync.Request.downloadHandler.text);
            }
        }
Esempio n. 5
0
    //获得VersionConfig对象
    public static VersionConfig GetVersionConfig()
    {
        // 获取streaming目录的Version.txt
        VersionConfig streamingVersionConfig;
        string        versionPath = PathHelper.AppHotfixResPath + "/Version.txt";

        using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>())
        {
            request.DownloadAsync(versionPath);
            streamingVersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text);
            //Log.Debug("本地Version.txt" + JsonHelper.ToJson(streamingVersionConfig));
        }
        return(streamingVersionConfig);
    }
Esempio n. 6
0
    /// <summary>
    /// 返回是否需要下载
    /// </summary>
    /// <returns></returns>
    public async Task <bool> LoadInfo()
    {
        UnityWebRequestAsync webRequestAsync = MonoBehaviourHelper.CreateTempComponent <UnityWebRequestAsync>();
        string remoteVersionText             = string.Empty;

        try
        {
            //下载remote version.txt
            string versionUrl = LoadBundlePathRoot() + "StreamingAssets/Version.txt";
            await webRequestAsync.DownloadAsync(versionUrl);

            remoteVersionText = webRequestAsync.Request.downloadHandler.text;
        }
        catch (Exception e)
        {
            if (e.Message.Contains("request error"))
            {
                webRequestAsync.Dispose();
                Debug.Log($"load VersionText error:'{e.Message}'");
                StaticData.isUseStreamingAssetRes = true;
                OnFileServerNotReach(e.Message);
                return(false);
            }
        }
        finally
        {
            Destroy(webRequestAsync.gameObject);
        }
        Debug.Log($"remoteVersionText:{remoteVersionText}");
        if (!remoteVersionText.StartsWith("{"))
        {
            Debug.Log("remote version text is not a correct json");
            this.remoteVersionConfig = null;
            return(false);
        }
        this.remoteVersionConfig = JsonHelper.FromJson <VersionConfig>(remoteVersionText);
        var needDown = await AnalyseVersionConfig();

        if (needDown == false)
        {
            return(false);
        }
        return(true);
    }
Esempio n. 7
0
    private async Task DownServerBundle()
    {
        while (true)
        {
            try
            {
                this.webRequest      = MonoBehaviourHelper.CreateTempComponent <UnityWebRequestAsync>();
                DownloadInfo.IsStart = true;
                var bundlePath = LoadBundlePathRoot() + "StreamingAssets/" + this.downloadingBundle;
                await this.webRequest.DownloadAsync(bundlePath);

                byte[] data          = this.webRequest.Request.downloadHandler.data;
                string path          = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
                string directoryName = Path.GetDirectoryName(path);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    fs.Write(data, 0, data.Length);
                    Debug.Log($"更新Bundle:{path} 完成");
                }
                var p = this.Progress;
                BundleRealProgress?.Invoke(p);
                Destroy(this.webRequest.gameObject);
                this.webRequest = null;
            }
            catch (Exception e)
            {
                Debug.LogError($"download bundle error: {this.downloadingBundle}\n{e}");
                //如果报错了,等1秒
                await UniTask.Delay(1);

                continue;
            }

            break;
        }
    }