static void SaveResourceDatas(string path, ResourceDatas datas)
        {
            FileStream fs = new FileStream(path, FileMode.Create);

            datas.WriteTo(fs);
            fs.Flush();
            fs.Close();
        }
Esempio n. 2
0
        void ComparServerResource()
        {
            _newResources = ResourceManager.LoadResourceDatas(Application.persistentDataPath + "/ResourceList.ab");
            if (_newResources == null || _newResources.Resources.Count == 0)
            {
                _onShowUpdateStepFail(0);
                return;
            }

            Debugger.Log("new resource count : " + _newResources.Resources.Count);
            NeedDownloadResource();
        }
        public static ResourceDatas LoadResourceDatas(string path)
        {
            if (!File.Exists(path))
            {
                return(null);
            }

            try
            {
                FileStream    fs  = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                ResourceDatas rds = LoadResourceDatas(fs);
                fs.Close();
                return(rds);
            }
            catch (Exception ex)
            {
                Debugger.LogException(ex);
                return(null);
            }
        }
        public void LoadResourceList()
        {
#if UNITY_EDITOR
#if RECOURCE_CLIENT
            _resourceList = LoadResourceDatas(_dataPath + "_ResourceList_1.ab");
#else
            _resourceList = LoadResourceDatas(_dataPath + "_ResourceList_2.ab");
#endif
#else
#if ILRUNTIME_DEBUG && UNITY_STANDALONE_WIN
            if (ResourceManager.IsILRuntimeDebug)
            {
                _resourceList = LoadResourceDatas(_dataPath + "_ResourceList.ab");
            }
            else
            {
#endif
            _resourceList = LoadResourceDatas(_dataPath + "ResourceList.ab");
#if ILRUNTIME_DEBUG && UNITY_STANDALONE_WIN
        }
#endif
            if (_resourceList == null || _resourceList.Resources.Count == 0)
            {
                CopyStreamingFile("ResourceList.ab", _dataPath + "ResourceList.ab");
                _resourceList = LoadResourceDatas(_dataPath + "ResourceList.ab");
            }
#endif

            if (_resourceList != null)
            {
                Debugger.Log("Resource count : " + _resourceList.Resources.Count, true);
            }
#if !UNITY_EDITOR
            else
            {
                Debugger.LogError("ResourceList load fail!!!");
            }
#endif
        }
        public void Init()
        {
#if !UNITY_EDITOR
            bool newVersion = PlayerPrefs.GetFloat("CLIENT_CODE_VERSION", -1) != CodeVersion;
#if UNITY_ANDROID
            int nativeVersionCode = GoogleObbPath.GetApkVerCode();
#elif UNITY_IPHONE
            int nativeVersionCode = IOSVersionCode.GetIOSVersionCode();
#else
            int nativeVersionCode = 0;
#endif

            if (PlayerPrefs.GetInt("NATIVE_VERSION_CODE", -1) != nativeVersionCode)
            {
                newVersion = true;
            }

            Debugger.Log("newVersion : " + newVersion, true);
            if (newVersion)
            {
                if (!GameClient.Instance.BuildSettings.MiniBuild)
                {
                    if (Directory.Exists(_dataPath))
                    {
                        Directory.Delete(_dataPath, true);
                    }
                    Directory.CreateDirectory(_dataPath);

                    CopyStreamingFile("ResourceList.ab", _dataPath + "ResourceList.ab");
                }
                else
                {
                    if (!File.Exists(_dataPath + "ResourceList.ab"))
                    {
                        if (Directory.Exists(_dataPath))
                        {
                            Directory.Delete(_dataPath, true);
                        }
                        Directory.CreateDirectory(_dataPath);

                        CopyStreamingFile("ResourceList.ab", _dataPath + "ResourceList.ab");
                    }
                    else
                    {
#if UNITY_ANDROID
                        ResourceDatas newDatas = LoadResourceDatas(GetStreamingFile("ResourceList.ab"));
#else
                        ResourceDatas newDatas = LoadResourceDatas(_streamingPath + "ResourceList.ab");
#endif
                        if (newDatas != null)
                        {
                            ResourceDatas datas = LoadResourceDatas(_dataPath + "ResourceList.ab");
                            var           e     = newDatas.Resources.GetEnumerator();
                            while (e.MoveNext())
                            {
                                string filename = _dataPath + GetResourceFileName(e.Current.Key);
                                if (File.Exists(filename))
                                {
                                    File.Delete(filename);
                                }

                                if (datas != null)
                                {
                                    if (!datas.Resources.ContainsKey(e.Current.Key))
                                    {
                                        datas.Resources.Add(e.Current.Key, e.Current.Value);
                                    }
                                    else
                                    {
                                        datas.Resources[e.Current.Key] = e.Current.Value;
                                    }
                                }
                            }

                            if (datas != null)
                            {
                                SaveResourceDatas(_dataPath + "ResourceList.ab", datas);
                            }
                        }
                    }
                }

                PlayerPrefs.SetFloat("CLIENT_CODE_VERSION", CodeVersion);
                PlayerPrefs.SetInt("NATIVE_VERSION_CODE", nativeVersionCode);
                PlayerPrefs.Save();
            }
#endif

            LoadResourceList();
        }
Esempio n. 6
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());
            });
        }
Esempio n. 7
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);
                }
            });
        }
Esempio n. 8
0
 void CheckLoacalResource()
 {
     _newResources = ResourceManager.LoadResourceDatas(ResourceManager.DataPath + "ResourceList.ab");
     NeedDownloadResource();
 }