Exemple #1
0
        /// <summary>
        /// 反序列化
        /// </summary>
        public static PatchManifest Deserialize(string jsonData)
        {
            PatchManifest patchManifest = JsonUtility.FromJson <PatchManifest>(jsonData);

            // 解析元素列表
            foreach (var element in patchManifest.ElementList)
            {
                // 解析标记位
                PatchElement.ParseFlags(element.Flags, out element.IsEncrypted, out element.IsCollected);

                // 元素集合
                patchManifest.Elements.Add(element.BundleName, element);

                // 注意:直接跳过非收集文件,因为这些文件不需要代码加载
                if (element.IsCollected == false)
                {
                    continue;
                }

                // 资源映射集合
                foreach (var assetPath in element.AssetPaths)
                {
                    // 添加原始路径
                    // 注意:我们不允许原始路径存在重名
                    if (patchManifest.AssetsMap.ContainsKey(assetPath))
                    {
                        throw new Exception($"Asset path have existed : {assetPath}");
                    }
                    patchManifest.AssetsMap.Add(assetPath, element);

                    // 添加去掉后缀名的路径
                    if (Path.HasExtension(assetPath))
                    {
                        string assetPathWithoutExtension = assetPath.RemoveExtension();
                        if (patchManifest.AssetsMap.ContainsKey(assetPathWithoutExtension))
                        {
                            MotionLog.Warning($"Asset path have existed : {assetPathWithoutExtension}");
                        }
                        else
                        {
                            patchManifest.AssetsMap.Add(assetPathWithoutExtension, element);
                        }
                    }
                }
            }

            // 解析变种列表
            foreach (var variant in patchManifest.VariantList)
            {
                patchManifest.Variants.Add(variant.BundleName, variant);
            }

            return(patchManifest);
        }
Exemple #2
0
        private WebFileRequest CreateDownloader(PatchElement element)
        {
            // 注意:资源版本号只用于确定下载路径
            string url      = _patcher.GetWebDownloadURL(element.Version.ToString(), element.Name);
            string savePath = PatchHelper.MakeSandboxCacheFilePath(element.MD5);

            FileUtility.CreateFileDirectory(savePath);

            // 创建下载器
            MotionLog.Log($"Beginning to download web file : {url}");
            WebFileRequest download = new WebFileRequest(url, savePath);

            download.UserData = element;
            download.DownLoad();
            return(download);
        }
Exemple #3
0
 public bool CheckContentIntegrity(PatchElement element)
 {
     return(CheckContentIntegrity(element.MD5, element.CRC32, element.SizeBytes));
 }
Exemple #4
0
        /// <summary>
        /// 更新下载器
        /// </summary>
        public void Update()
        {
            if (DownloadStates != EDownloaderStates.Loading)
            {
                return;
            }

            // 检测下载器结果
            for (int i = _loaders.Count - 1; i >= 0; i--)
            {
                var loader = _loaders[i];
                if (loader.IsDone() == false)
                {
                    continue;
                }

                PatchElement element = loader.UserData as PatchElement;

                // 检测是否下载失败
                if (loader.HasError())
                {
                    loader.ReportError();
                    loader.Dispose();
                    _loaders.RemoveAt(i);
                    _loadFailedList.Add(element);
                    continue;
                }

                // 验证下载文件完整性
                if (_patcher.CheckContentIntegrity(element) == false)
                {
                    MotionLog.Error($"Check download content integrity is failed : {element.Name}");
                    loader.Dispose();
                    _loaders.RemoveAt(i);
                    _checkFailedList.Add(element);
                    continue;
                }

                // 下载成功
                loader.Dispose();
                _loaders.RemoveAt(i);
                _succeedList.Add(element);
                CurrentDownloadCount++;
                CurrentDownloadBytes += element.SizeBytes;
                OnPatchFileDownloadSucceedCallback?.Invoke(TotalDownloadCount, CurrentDownloadCount, TotalDownloadBytes, CurrentDownloadBytes);
            }

            // 动态创建新的下载器到最大数量限制
            // 注意:如果期间有下载失败的文件,暂停动态创建下载器
            if (_downloadList.Count > 0 && _loadFailedList.Count == 0 && _checkFailedList.Count == 0)
            {
                if (_loaders.Count < _maxNumberOnLoad)
                {
                    int            index      = _downloadList.Count - 1;
                    WebFileRequest downloader = CreateDownloader(_downloadList[index]);
                    _loaders.Add(downloader);
                    _downloadList.RemoveAt(index);
                }
            }

            // 下载结算
            if (_loaders.Count == 0)
            {
                // 更新缓存并保存
                if (_succeedList.Count > 0)
                {
                    _patcher.CacheDownloadPatchFiles(_succeedList);
                }

                if (_loadFailedList.Count > 0)
                {
                    DownloadStates = EDownloaderStates.Failed;
                    OnPatchFileDownloadFailedCallback?.Invoke(_loadFailedList[0].Name);
                }
                else if (_checkFailedList.Count > 0)
                {
                    DownloadStates = EDownloaderStates.Failed;
                    OnPatchFileCheckFailedCallback?.Invoke(_checkFailedList[0].Name);
                }
                else
                {
                    // 结算成功
                    DownloadStates = EDownloaderStates.Succeed;
                }
            }
        }
        private void GetDownloadList()
        {
            _patcher.DownloadList.Clear();

            // 临时下载列表
            List <PatchElement> downloadList = new List <PatchElement>(1000);

            // 准备下载列表
            foreach (var pair in _patcher.WebPatchManifest.Elements)
            {
                PatchElement element = pair.Value;

                // 先检测APP里的清单
                PatchElement appElement;
                if (_patcher.AppPatchManifest.Elements.TryGetValue(element.Name, out appElement))
                {
                    if (appElement.MD5 == element.MD5)
                    {
                        continue;
                    }
                }

                // 再检测沙盒里的清单
                PatchElement sandboxElement;
                if (_patcher.SandboxPatchManifest.Elements.TryGetValue(element.Name, out sandboxElement))
                {
                    if (sandboxElement.MD5 != element.MD5)
                    {
                        downloadList.Add(element);
                    }
                }
                else
                {
                    downloadList.Add(element);
                }
            }

            // 检测已经存在的文件
            // 注意:如果玩家在加载过程中强制退出,下次再进入的时候跳过已经加载的文件
            List <string> removeList = new List <string>();

            foreach (var element in downloadList)
            {
                string filePath = AssetPathHelper.MakePersistentLoadPath(element.Name);
                if (System.IO.File.Exists(filePath))
                {
                    string md5 = HashUtility.FileMD5(filePath);
                    if (md5 == element.MD5)
                    {
                        removeList.Add(element.Name);
                    }
                }
            }
            foreach (var name in removeList)
            {
                for (int i = 0; i < downloadList.Count; i++)
                {
                    if (downloadList[i].Name == name)
                    {
                        downloadList.RemoveAt(i);
                        break;
                    }
                }
            }

            // 如果下载列表为空
            if (downloadList.Count == 0)
            {
                _patcher.SwitchNext();
            }
            else
            {
                // 最后添加到正式下载列表里
                _patcher.DownloadList.AddRange(downloadList);
                downloadList.Clear();

                // 发现新更新文件后,挂起流程系统
                int  totalDownloadCount  = _patcher.DownloadList.Count;
                long totalDownloadSizeKB = 0;
                foreach (var element in _patcher.DownloadList)
                {
                    totalDownloadSizeKB += element.SizeKB;
                }
                PatchEventDispatcher.SendFoundUpdateFilesMsg(totalDownloadCount, totalDownloadSizeKB);
            }
        }
Exemple #6
0
        private void GetDownloadList()
        {
            PatchManager.Instance.DownloadList.Clear();

            // 临时下载列表
            List <PatchElement> downloadList = new List <PatchElement>(1000);

            // 准备下载列表
            foreach (var pair in PatchManager.Instance.WebPatchFile.Elements)
            {
                PatchElement element = pair.Value;

                // 先检测APP里的资源
                PatchElement appElement;
                if (PatchManager.Instance.AppPatchFile.Elements.TryGetValue(element.Name, out appElement))
                {
                    if (appElement.MD5 == element.MD5)
                    {
                        continue;
                    }
                }

                // 再检测沙盒里的资源
                PatchElement sandboxElement;
                if (PatchManager.Instance.SandboxPatchFile.Elements.TryGetValue(element.Name, out sandboxElement))
                {
                    if (sandboxElement.MD5 != element.MD5)
                    {
                        downloadList.Add(element);
                    }
                }
                else
                {
                    downloadList.Add(element);
                }
            }

            // 检测已经存在的文件
            // 注意:如果玩家在加载过程中强制退出,下次再进入的时候跳过已经加载的文件
            List <string> removeList = new List <string>();

            foreach (var element in downloadList)
            {
                string filePath = AssetPathHelper.MakePersistentLoadPath(element.Name);
                if (System.IO.File.Exists(filePath))
                {
                    string md5 = HashUtility.FileMD5(filePath);
                    if (md5 == element.MD5)
                    {
                        removeList.Add(element.Name);
                    }
                }
            }
            foreach (var name in removeList)
            {
                for (int i = 0; i < downloadList.Count; i++)
                {
                    if (downloadList[i].Name == name)
                    {
                        downloadList.RemoveAt(i);
                        break;
                    }
                }
            }

            // 最后添加到正式下载列表里
            PatchManager.Instance.DownloadList.AddRange(downloadList);
            downloadList.Clear();
            _system.SwitchNext();
        }