예제 #1
0
        void DownloadServerResource()
        {
            if (_currentDownloader != null)
            {
                _currentDownloader.PauseDownload(false);
                GameClient.Instance.StartCoroutine(CheckDownloadingNetWork());
                return;
            }

            _currentProgress.Progreess = 0f;
            _onShowUpdateProgress(_currentProgress);

            string newResourceListPath = Application.persistentDataPath + "/ResourceList.ab";

            if (File.Exists(newResourceListPath))
            {
                _finishDatas.Clear();
                var itor = ResourceManager.Instance.ResourceList.Resources.GetEnumerator();
                while (itor.MoveNext())
                {
                    _currentResourclist.Resources.Add(itor.Current.Key, itor.Current.Value);
                }
            }

            int toltalSize = 0;
            List <DownloadFile> downloadfiles = new List <DownloadFile>(_needUpdateResources.Count);
            var a = new TraverseInThread <string>(_needUpdateResources, _needUpdateResources.Count,
                                                  (arg) =>
            {
                string key                = arg as string;
                ResourceData rd           = _newResources.Resources[key];
                string filename           = ResourceManager.GetResourceFileName(key);
                string saveName           = rd.IsOptional() ? ResourceManager.OptionalPath + filename : ResourceManager.DataPath + filename;
                DownloadFile downloadFile = new DownloadFile(ResourceManager.ResourceUrl + key + ".ab", saveName, rd.Size, rd.Crc);
                toltalSize               += rd.Size;
                lock (_threadLock)
                {
                    downloadfiles.Add(downloadFile);
                }

                if (rd.Path.Contains("Install/Unpackage/GameLogic.bytes") || rd.Path.Contains("Scenes/Install/UI"))
                {
                    _needRestart = true;
                }
                else if (rd.Path.Contains("Install/Unpackage/Data/"))
                {
                    _needReload = true;
                }
            },
                                                  () =>
            {
                _currentProgress.Progreess = 0f;
                _currentProgress.TotalSize = toltalSize;
                _onShowUpdateProgress(_currentProgress);

                _lastSaveTime = DateTime.Now;

                _currentDownloader = Downloader.DownloadFiles(downloadfiles,
                                                              (arg) => //onFinish
                {
                    _currentDownloader = null;
                    _currentResourclist.Resources.Clear();
                    _finishDatas.Clear();
                    if (_needUpdateResources.Count > 0)
                    {
                        _onShowUpdateStepFail(0);
                        return;
                    }

                    if (File.Exists(newResourceListPath))
                    {
                        try
                        {
                            lock (_threadLock)
                            {
                                File.Copy(newResourceListPath, ResourceManager.DataPath + "ResourceList.ab", true);
                            }
                            File.Delete(newResourceListPath);
                        }
                        catch (Exception ex)
                        {
                            Debugger.LogException(ex);
                        }
                        ResourceManager.Instance.LoadResourceList();
                    }

                    if (_needRestart)
                    {
                        ChangeCurrentUpdateState(UpdateState.RestartClient, false);
                    }
                    else
                    {
                        _newResources.Resources.Clear();
                        _newResources = null;
                        if (_needReload)
                        {
                            ChangeCurrentUpdateState(UpdateState.ReloadConfigs);
                        }
                        else
                        {
                            ChangeCurrentUpdateState(UpdateState.UpdateFinish);
                        }
                    }
                },
                                                              (arg) => //onProgress;
                {
                    object[] args = arg as object[];
                    _currentProgress.Progreess = (float)args[0];
                    _currentProgress.TotalSize = toltalSize;
                    _onShowUpdateProgress(_currentProgress);
                },
                                                              (arg) => //onSingleFileFinish
                {
                    if (!File.Exists(newResourceListPath))
                    {
                        return;
                    }

                    DownloadFile file = arg as DownloadFile;
                    string key        = Path.GetFileNameWithoutExtension(file.savePath);
                    if (File.Exists(file.savePath))
                    {
                        ResourceData rd = _newResources.Resources[key];
                        KeyValuePair <string, ResourceData> pair = new KeyValuePair <string, ResourceData>(key, rd);
                        lock (_saveThreadLock)
                        {
                            _finishDatas.Add(pair);
                        }
                        _needUpdateResources.Remove(key);
                    }

                    if (_currentDownloader != null && !_isSaving && (_lastSaveTime - DateTime.Now).Seconds > 20)
                    {
                        _isSaving     = true;
                        _lastSaveTime = DateTime.Now;
                        Thread thread = new Thread(SaveResourceList);
                        thread.Start();
                        _isSaving = false;
                    }
                }
                                                              );
                GameClient.Instance.StartCoroutine(CheckDownloadingNetWork());
            });
        }
예제 #2
0
        void NeedDownloadResource()
        {
            _needUpdateResources.Clear();
            int totalSize = 0;

#if UNITY_ANDROID && !UNITY_EDITOR
            ResourceDatas streamingResources = ResourceManager.LoadResourceDatas(ResourceManager.GetStreamingFile("ResourceList.ab"));
#else
            ResourceDatas streamingResources = ResourceManager.LoadResourceDatas(ResourceManager.StreamingPath + "ResourceList.ab");
#endif
            var a = new TraverseInThread <KeyValuePair <string, ResourceData> >(_newResources.Resources, _newResources.Resources.Count,
                                                                                (obj) =>
            {
                KeyValuePair <string, ResourceData> pair = (KeyValuePair <string, ResourceData>)obj;
                string key      = pair.Key;
                ResourceData rd = pair.Value;
                string filename = ResourceManager.GetResourceFileName(key);
                if (!rd.IsOptional())
                {
                    bool streaminghas = false;
                    if (streamingResources.Resources.ContainsKey(key))
                    {
                        ResourceData srd = streamingResources.Resources[key];
                        if (rd.Crc == srd.Crc)
                        {
                            streaminghas = true;
                        }
                    }

                    if (FileHelper.GetFileCrc(ResourceManager.DataPath + filename) != rd.Crc && !streaminghas)
                    {
                        lock (_threadLock)
                        {
                            _needUpdateResources.Add(key);
                        }
                        totalSize += rd.Size;
                    }
                }
                else
                {
                    if (File.Exists(ResourceManager.OptionalPath + filename))
                    {
                        if (FileHelper.GetFileCrc(ResourceManager.OptionalPath + filename) != rd.Crc)
                        {
                            lock (_threadLock)
                            {
                                _needUpdateResources.Add(key);
                            }
                            totalSize += rd.Size;
                        }
                    }
                }
            },
                                                                                () =>
            {
                if (_needUpdateResources.Count == 0)
                {
                    if (File.Exists(Application.persistentDataPath + "/ResourceList.ab"))
                    {
                        try
                        {
                            File.Copy(Application.persistentDataPath + "/ResourceList.ab", ResourceManager.DataPath + "ResourceList.ab", true);
                            File.Delete(Application.persistentDataPath + "/ResourceList.ab");
                        }
                        catch (Exception ex)
                        {
                            Debugger.LogException(ex);
                        }
                    }
                    ChangeCurrentUpdateState(UpdateState.UpdateFinish);
                }
                else
                {
                    _currentProgress.TotalSize = totalSize;
                    object[] obj = new object[2];
                    obj[0]       = totalSize;
                    obj[1]       = _resourceUpdateTips;
                    ChangeCurrentUpdateState(UpdateState.DownloadServerResource, false, obj);
                }
            });
        }