private IEnumerator LoadRawFile()
    {
        GameLog.Log("加载LUA文件");
        {
            string filePath;

            var bundleInfo = PatchManager.Instance.GetAssetBundleInfo("Lua/LuaTest1.lua");
            if (bundleInfo.IsBuildinJarFile())
            {
                string hashName = Path.GetFileName(bundleInfo.LocalPath);
                filePath = $"{Application.persistentDataPath}/RawFiles/{hashName}";
                if (File.Exists(filePath) == false)
                {
                    WebFileRequest webFileRequest = new WebFileRequest(bundleInfo.LocalPath);
                    webFileRequest.SendRequest(filePath);
                    yield return(webFileRequest);
                }
            }
            else
            {
                filePath = bundleInfo.LocalPath;
            }

            var luaContent = File.ReadAllText(filePath);
            GameLog.Log(luaContent);
        }
    }
예제 #2
0
        public override void Destroy(bool force)
        {
            base.Destroy(force);

            // Check fatal
            if (RefCount > 0)
            {
                throw new Exception($"Bundle file loader ref is not zero : {BundleInfo.BundleName}");
            }
            if (IsDone() == false)
            {
                throw new Exception($"Bundle file loader is not done : {BundleInfo.BundleName}");
            }

            if (_downloader != null)
            {
                _downloader.Dispose();
                _downloader = null;
            }

            if (CacheBundle != null)
            {
                CacheBundle.Unload(force);
                CacheBundle = null;
            }

            _depends.Clear();
        }
예제 #3
0
        private WebFileRequest CreateDownloader(PatchBundle element)
        {
            // 注意:资源版本号只用于确定下载路径
            string url      = _patcher.GetWebDownloadURL(element.Version.ToString(), element.Hash);
            string savePath = PatchHelper.MakeSandboxCacheFilePath(element.Hash);

            FileUtility.CreateFileDirectory(savePath);

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

            download.UserData = element;
            download.DownLoad();
            return(download);
        }
예제 #4
0
        private WebFileRequest CreateDownloader(PatchBundle patchBundle)
        {
            // 注意:资源版本号只用于确定下载路径
            string mainURL     = _patcherMgr.GetPatchDownloadURL(patchBundle.Version, patchBundle.Hash);
            string fallbackURL = _patcherMgr.GetPatchDownloadFallbackURL(patchBundle.Version, patchBundle.Hash);
            string savePath    = PatchHelper.MakeSandboxCacheFilePath(patchBundle.Hash);

            FileUtility.CreateFileDirectory(savePath);

            // 创建下载器
            MotionLog.Log($"Beginning to download web file : {patchBundle.BundleName} URL : {mainURL}");
            WebFileRequest download = WebFileSystem.GetWebFileRequest(mainURL, fallbackURL, savePath, _failedTryAgain);

            download.UserData = patchBundle;
            return(download);
        }
        private IEnumerator Download()
        {
            // 注意:等所有文件下载完毕后,下载并替换补丁清单
            int            newResourceVersion = _patcher.RequestedResourceVersion;
            string         url      = _patcher.GetWebDownloadURL(newResourceVersion.ToString(), PatchDefine.PatchManifestFileName);
            string         savePath = AssetPathHelper.MakePersistentLoadPath(PatchDefine.PatchManifestFileName);
            WebFileRequest download = new WebFileRequest(url, savePath);

            yield return(download.DownLoad());

            if (download.States != EWebRequestStates.Success)
            {
                download.Dispose();
                PatchEventDispatcher.SendWebPatchManifestDownloadFailedMsg();
                yield break;
            }
            else
            {
                PatchHelper.Log(ELogLevel.Log, "Web patch manifest is download.");
                download.Dispose();
                _patcher.SwitchNext();
            }
        }
예제 #6
0
        public IEnumerator Download(ProcedureSystem system)
        {
            // 注意:等所有文件下载完毕后,再替换版本文件
            int            newResourceVersion = PatchManager.Instance.GameVersion.Revision;
            string         url      = PatchManager.MakeWebDownloadURL(newResourceVersion.ToString(), PatchDefine.StrPatchFileName);
            string         savePath = AssetPathHelper.MakePersistentLoadPath(PatchDefine.StrPatchFileName);
            WebFileRequest download = new WebFileRequest(url, savePath);

            yield return(download.DownLoad());

            if (download.States != EWebRequestStates.Succeed)
            {
                download.Dispose();
                system.Switch((int)EPatchStates.PatchError);
                yield break;
            }
            else
            {
                PatchManager.Log(ELogType.Log, "Download web files is finish.");
                download.Dispose();
                system.SwitchNext();
            }
        }
예제 #7
0
        private IEnumerator Download()
        {
            // 注意:开发者需要在下载前检测磁盘空间不足

            // 计算下载文件的总大小
            int  totalDownloadCount     = _patcher.DownloadList.Count;
            long totalDownloadSizeBytes = 0;

            foreach (var element in _patcher.DownloadList)
            {
                totalDownloadSizeBytes += element.SizeBytes;
            }

            // 开始下载列表里的所有资源
            PatchHelper.Log(ELogLevel.Log, $"Begine download web files : {_patcher.DownloadList.Count}");
            long currentDownloadSizeBytes = 0;
            int  currentDownloadCount     = 0;

            foreach (var element in _patcher.DownloadList)
            {
                // 注意:资源版本号只用于确定下载路径
                string url      = _patcher.GetWebDownloadURL(element.Version.ToString(), element.Name);
                string savePath = AssetPathHelper.MakePersistentLoadPath(element.Name);
                element.SavePath = savePath;
                FileUtility.CreateFileDirectory(savePath);

                // 创建下载器
                WebFileRequest download = new WebFileRequest(url, savePath);
                yield return(download.DownLoad());                //文件依次加载(在一个文件加载完毕后加载下一个)

                PatchHelper.Log(ELogLevel.Log, $"Web file is download : {savePath}");

                // 检测是否下载失败
                if (download.States != EWebRequestStates.Success)
                {
                    PatchEventDispatcher.SendWebFileDownloadFailedMsg(url, element.Name);
                    yield break;
                }

                // 立即释放加载器
                download.Dispose();
                currentDownloadCount++;
                currentDownloadSizeBytes += element.SizeBytes;
                PatchEventDispatcher.SendDownloadFilesProgressMsg(totalDownloadCount, currentDownloadCount, totalDownloadSizeBytes, currentDownloadSizeBytes);
            }

            // 验证下载文件的大小
            if (_patcher.CheckLevel == ECheckLevel.CheckSize)
            {
                foreach (var element in _patcher.DownloadList)
                {
                    long fileSize = FileUtility.GetFileSize(element.SavePath);
                    if (fileSize != element.SizeBytes)
                    {
                        PatchHelper.Log(ELogLevel.Error, $"Web file size check failed : {element.Name}");
                        PatchEventDispatcher.SendWebFileCheckFailedMsg(element.Name);
                        yield break;
                    }
                }
            }

            // 验证下载文件的MD5
            if (_patcher.CheckLevel == ECheckLevel.CheckMD5)
            {
                foreach (var element in _patcher.DownloadList)
                {
                    string md5 = HashUtility.FileMD5(element.SavePath);
                    if (md5 != element.MD5)
                    {
                        PatchHelper.Log(ELogLevel.Error, $"Web file md5 check failed : {element.Name}");
                        PatchEventDispatcher.SendWebFileCheckFailedMsg(element.Name);
                        yield break;
                    }
                }
            }

            // 最后清空下载列表
            _patcher.DownloadList.Clear();
            _patcher.SwitchNext();
        }
예제 #8
0
        public override void Update()
        {
            // 如果资源文件加载完毕
            if (States == ELoaderStates.Success || States == ELoaderStates.Fail)
            {
                UpdateAllProvider();
                return;
            }

            if (States == ELoaderStates.None)
            {
                // 检测加载地址是否为空
                if (string.IsNullOrEmpty(BundleInfo.LocalPath))
                {
                    States = ELoaderStates.Fail;
                    return;
                }

                if (string.IsNullOrEmpty(BundleInfo.RemoteURL))
                {
                    States = ELoaderStates.CheckDepends;
                }
                else
                {
                    States = ELoaderStates.Download;
                }
            }

            // 1. 从服务器下载
            if (States == ELoaderStates.Download)
            {
                _downloader = new WebFileRequest(BundleInfo.RemoteURL, BundleInfo.LocalPath);
                _downloader.DownLoad();
                States = ELoaderStates.CheckDownload;
            }

            // 2. 检测服务器下载结果
            if (States == ELoaderStates.CheckDownload)
            {
                if (_downloader.IsDone() == false)
                {
                    return;
                }

                if (_downloader.HasError())
                {
                    _downloader.ReportError();
                    States = ELoaderStates.Fail;
                }
                else
                {
                    // 校验文件完整性
                    if (AssetSystem.BundleServices.CheckContentIntegrity(BundleInfo.BundleName) == false)
                    {
                        MotionLog.Error($"Check download content integrity is failed : {BundleInfo.BundleName}");
                        States = ELoaderStates.Fail;
                    }
                    else
                    {
                        States = ELoaderStates.CheckDepends;
                    }
                }

                // 释放网络资源下载器
                if (_downloader != null)
                {
                    _downloader.Dispose();
                    _downloader = null;
                }
            }

            // 3. 检测所有依赖完成状态
            if (States == ELoaderStates.CheckDepends)
            {
                foreach (var dpLoader in _depends)
                {
                    if (dpLoader.IsDone() == false)
                    {
                        return;
                    }
                }
                States = ELoaderStates.LoadFile;
            }

            // 4. 加载AssetBundle
            if (States == ELoaderStates.LoadFile)
            {
#if UNITY_EDITOR
                // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。
                if (System.IO.File.Exists(BundleInfo.LocalPath) == false)
                {
                    MotionLog.Warning($"Not found assetBundle file : {BundleInfo.LocalPath}");
                    States = ELoaderStates.Fail;
                    return;
                }
#endif

                // Load assetBundle file
                if (BundleInfo.IsEncrypted)
                {
                    if (AssetSystem.DecryptServices == null)
                    {
                        throw new Exception($"{nameof(AssetBundleLoader)} need IDecryptServices : {BundleInfo.BundleName}");
                    }

                    EDecryptMethod decryptType = AssetSystem.DecryptServices.DecryptType;
                    if (decryptType == EDecryptMethod.GetDecryptOffset)
                    {
                        ulong offset = AssetSystem.DecryptServices.GetDecryptOffset(BundleInfo);
                        _cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath, 0, offset);
                    }
                    else if (decryptType == EDecryptMethod.GetDecryptBinary)
                    {
                        byte[] binary = AssetSystem.DecryptServices.GetDecryptBinary(BundleInfo);
                        _cacheRequest = AssetBundle.LoadFromMemoryAsync(binary);
                    }
                    else
                    {
                        throw new NotImplementedException($"{decryptType}");
                    }
                }
                else
                {
                    _cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath);
                }
                States = ELoaderStates.CheckFile;
            }

            // 5. 检测AssetBundle加载结果
            if (States == ELoaderStates.CheckFile)
            {
                if (_cacheRequest.isDone == false)
                {
                    return;
                }
                CacheBundle = _cacheRequest.assetBundle;

                // Check error
                if (CacheBundle == null)
                {
                    MotionLog.Warning($"Failed to load assetBundle file : {BundleInfo.BundleName}");
                    States = ELoaderStates.Fail;
                }
                else
                {
                    States = ELoaderStates.Success;
                }
            }
        }
예제 #9
0
        public IEnumerator Download(ProcedureSystem system)
        {
            // 检测磁盘空间不足
            // TODO 检测磁盘空间不足

            // 计算下载文件的总大小
            int  totalDownloadCount  = PatchManager.Instance.DownloadList.Count;
            long totalDownloadSizeKB = 0;

            foreach (var element in PatchManager.Instance.DownloadList)
            {
                totalDownloadSizeKB += element.SizeKB;
            }

            // 开始下载列表里的所有资源
            PatchManager.Log(ELogType.Log, $"Begine download web files : {PatchManager.Instance.DownloadList.Count}");
            long currentDownloadSizeKB = 0;
            int  currentDownloadCount  = 0;

            foreach (var element in PatchManager.Instance.DownloadList)
            {
                // 注意:资源版本号只用于确定下载路径
                string url      = PatchManager.MakeWebDownloadURL(element.Version.ToString(), element.Name);
                string savePath = AssetPathHelper.MakePersistentLoadPath(element.Name);
                element.SavePath = savePath;
                PatchManager.CreateFileDirectory(savePath);

                // 创建下载器
                WebFileRequest download = new WebFileRequest(url, savePath);
                yield return(download.DownLoad());                //文件依次加载(在一个文件加载完毕后加载下一个)

                PatchManager.Log(ELogType.Log, $"Web file is done : {url}");

                // 检测是否下载失败
                if (download.States != EWebRequestStates.Succeed)
                {
                    _system.Switch((int)EPatchStates.PatchError);
                    PatchManager.SendWebFileDownloadFailedMsg(url);
                    yield break;
                }

                // 立即释放加载器
                download.Dispose();
                currentDownloadCount++;
                currentDownloadSizeKB += element.SizeKB;
                PatchManager.SendDownloadFilesProgressMsg(totalDownloadCount, currentDownloadCount, totalDownloadSizeKB, currentDownloadSizeKB);
            }

            // 验证下载文件的MD5
            foreach (var element in PatchManager.Instance.DownloadList)
            {
                string md5 = HashUtility.FileMD5(element.SavePath);
                if (md5 != element.MD5)
                {
                    _system.Switch((int)EPatchStates.PatchError);
                    PatchManager.Log(ELogType.Error, $"Web file md5 verification error : {element.Name}");
                    PatchManager.SendWebFileMD5VerifyFailedMsg(element.Name);
                    yield break;
                }
            }

            // 最后清空下载列表
            PatchManager.Instance.DownloadList.Clear();
            _system.SwitchNext();
        }
예제 #10
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;
                }

                PatchBundle element = loader.UserData as PatchBundle;

                // 检测是否下载失败
                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.BundleName}");
                    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].BundleName);
                }
                else if (_checkFailedList.Count > 0)
                {
                    DownloadStates = EDownloaderStates.Failed;
                    OnPatchFileCheckFailedCallback?.Invoke(_checkFailedList[0].BundleName);
                }
                else
                {
                    // 结算成功
                    DownloadStates = EDownloaderStates.Succeed;
                }
            }
        }
예제 #11
0
        /// <summary>
        /// 更新下载器
        /// </summary>
        public void Update()
        {
            if (DownloadStates != EDownloaderStates.Loading)
            {
                return;
            }

            // 检测下载器结果
            _removeList.Clear();
            long downloadBytes = CurrentDownloadBytes;

            foreach (var loader in _downloaders)
            {
                downloadBytes += (long)loader.DownloadedBytes;
                if (loader.IsDone() == false)
                {
                    continue;
                }

                PatchBundle patchBundle = loader.UserData as PatchBundle;

                // 检测是否下载失败
                if (loader.HasError())
                {
                    loader.ReportError();
                    loader.Dispose();
                    _removeList.Add(loader);
                    _loadFailedList.Add(patchBundle);
                    continue;
                }

                // 验证下载文件完整性
                if (_patcherMgr.CheckContentIntegrity(patchBundle) == false)
                {
                    MotionLog.Error($"Check download content integrity is failed : {patchBundle.BundleName}");
                    loader.Dispose();
                    _removeList.Add(loader);
                    _checkFailedList.Add(patchBundle);
                    continue;
                }

                // 下载成功
                loader.Dispose();
                _removeList.Add(loader);
                _succeedList.Add(patchBundle);
                CurrentDownloadCount++;
                CurrentDownloadBytes += patchBundle.SizeBytes;
            }

            // 移除已经完成的下载器(无论成功或失败)
            foreach (var loader in _removeList)
            {
                _downloaders.Remove(loader);
            }

            // 如果下载进度发生变化
            if (_lastDownloadBytes != downloadBytes || _lastDownloadCount != CurrentDownloadCount)
            {
                _lastDownloadBytes = downloadBytes;
                _lastDownloadCount = CurrentDownloadCount;
                OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes);
            }

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

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

                if (_loadFailedList.Count > 0)
                {
                    DownloadStates = EDownloaderStates.Failed;
                    OnPatchFileDownloadFailedCallback?.Invoke(_loadFailedList[0].BundleName);
                    OnDownloadOverCallback?.Invoke(false);
                }
                else if (_checkFailedList.Count > 0)
                {
                    DownloadStates = EDownloaderStates.Failed;
                    OnPatchFileCheckFailedCallback?.Invoke(_checkFailedList[0].BundleName);
                    OnDownloadOverCallback?.Invoke(false);
                }
                else
                {
                    // 结算成功
                    DownloadStates = EDownloaderStates.Succeed;
                    OnDownloadOverCallback?.Invoke(true);
                }
            }
        }