Ejemplo n.º 1
0
        private void LoadDependInfo(out AssetBundleReader reader, string fileName)
        {
            reader = null;
            string     depFilePath = Path.Combine(_pathResolver.bundleDir, fileName);
            FileStream fs          = new FileStream(depFilePath, FileMode.Open, FileAccess.Read);

            if (fs.Length > 4)
            {
                BinaryReader br = new BinaryReader(fs);
                if (br.ReadChar() == 'A' && br.ReadChar() == 'B' && br.ReadChar() == 'D')
                {
                    if (br.ReadChar() == 'T')
                    {
                        reader = new AssetBundleReaderText();
                    }
                    else
                    {
                        reader = new AssetBundleReaderBinary();
                    }

                    fs.Position = 0;
                    reader.Read(fs);
                }
            }
            fs.Close();
        }
Ejemplo n.º 2
0
        private IEnumerator CheckUpdate()
        {
            if (!Directory.Exists(_pathResolver.bundleDir))
            {
                Directory.CreateDirectory(_pathResolver.bundleDir);
            }

            // 下载bundle.info文件至
            Debug.Log("Download bundle.info...");
            WWW www = new WWW(string.Format("{0}/{1}", _pathResolver.bundleURL, _pathResolver.dependInfoFileName));

            yield return(www);

            if (!string.IsNullOrEmpty(www.error))
            {
                Debug.LogError(www.error);
                www.Dispose();
                //TODO:下载失败处理
                yield break;
            }

            // 写入persist目录并命名为bundle.info.new
            File.WriteAllBytes(string.Format("{0}/{1}", _pathResolver.bundleDir, _pathResolver.dependInfoFileNameNew), www.bytes);
            www.Dispose();
            yield return(null);

            // 下载完成,解析bundle.info.new文件
            LoadDependInfo(out _dependInfoReaderNew, _pathResolver.dependInfoFileNameNew);

            // 比较版本号,当前是最新版本的话跳过更新
            if (_dependInfoReader.version >= _dependInfoReaderNew.version)
            {
                File.Delete(Path.Combine(_pathResolver.bundleDir, _pathResolver.dependInfoFileNameNew));

                if (onInited != null)
                {
                    onInited();
                }
                //Util.EventDispatcher.TriggerEvent(Global.UpdateFinish);

                yield break;
            }

            // 删除最新版已经删除但是persist下还存在的ab
            foreach (var info in _dependInfoReader.infoMap)
            {
                if (!_dependInfoReaderNew.infoMap.ContainsKey(info.Key))
                {
                    File.Delete(string.Format("{0}/{1}.ab", _pathResolver.bundleDir, info.Key));
                }
            }

            // 统计需要更新的文件及大小
            long needUpdateSize = 0;
            Dictionary <string, long> needUpdateABs = new Dictionary <string, long>();

            foreach (var info in _dependInfoReaderNew.infoMap)
            {
                string          abName = info.Key;
                AssetBundleData abData = info.Value;
                if (!_dependInfoReader.infoMap.ContainsKey(abName) || _dependInfoReader.infoMap[abName].hash != abData.hash)
                {
                    needUpdateABs.Add(abName, abData.size);
                    needUpdateSize += abData.size;
                }
            }

            // 更新
            long updatedSize = 0;

            foreach (var ab in needUpdateABs)
            {
                string abName = ab.Key;
                Debug.Log(string.Format("Update {0}...", abName));

                www = new WWW(Path.Combine(_pathResolver.bundleURL, abName));
                yield return(www);

                if (!string.IsNullOrEmpty(www.error))
                {
                    Debug.LogError(www.error);
                    www.Dispose();
                    continue;
                }

                string abPath = Path.Combine(_pathResolver.bundleDir, abName);
                File.WriteAllBytes(abPath, www.bytes);
                www.Dispose();

                // 解压
                string decompressPath = string.Format("{0}.tmp", abPath);
                LZMAUtil.DecompressFile(abPath, decompressPath);
                File.Delete(abPath);
                yield return(null);

                File.Move(decompressPath, abPath);

                updatedSize += ab.Value;
            }

            // 覆盖bundle.info文件
            string dependInfoFilePath    = Path.Combine(_pathResolver.bundleDir, _pathResolver.dependInfoFileName);
            string newDependInfoFilePath = Path.Combine(_pathResolver.bundleDir, _pathResolver.dependInfoFileNameNew);

            File.Delete(dependInfoFilePath);
            File.Move(newDependInfoFilePath, dependInfoFilePath);

            _dependInfoReader    = _dependInfoReaderNew;
            _dependInfoReaderNew = null;

            // 初始化完成
            if (onInited != null)
            {
                onInited();
            }
        }