void IFsmNode.OnEnter()
        {
            PatchEventDispatcher.SendPatchStatesChangeMsg(EPatchStates.ParseSandboxPatchManifest);

            // 读取并解析沙盒内的补丁清单
            if (PatchHelper.CheckSandboxPatchManifestFileExist())
            {
                string filePath    = AssetPathHelper.MakePersistentLoadPath(PatchDefine.PatchManifestFileName);
                string fileContent = PatchHelper.ReadFile(filePath);

                PatchHelper.Log(ELogLevel.Log, $"Parse sandbox patch file.");
                _patcher.ParseSandboxPatchManifest(fileContent);
            }
            else
            {
                _patcher.ParseSandboxPatchManifest(_patcher.AppPatchManifest);
            }

            _patcher.SwitchNext();
        }
        /// <summary>
        /// 异步初始化
        /// </summary>
        public IEnumerator InitializeAync(PatchManagerImpl patcher)
        {
            // 处理沙盒被污染
            ProcessSandboxDirty();

            // 分析APP内的补丁清单
            {
                string         filePath   = AssetPathHelper.MakeStreamingLoadPath(PatchDefine.PatchManifestBytesFileName);
                string         url        = AssetPathHelper.ConvertToWWWPath(filePath);
                WebDataRequest downloader = new WebDataRequest(url);
                yield return(downloader.DownLoad());

                if (downloader.States == EWebRequestStates.Success)
                {
                    MotionLog.Log("Parse app patch manifest.");
                    patcher.ParseAppPatchManifest(downloader.GetData());
                    downloader.Dispose();
                }
                else
                {
                    throw new System.Exception($"Fatal error : Failed download file : {url}");
                }
            }

            // 分析沙盒内的补丁清单
            if (PatchHelper.CheckSandboxPatchManifestFileExist())
            {
                string filePath = AssetPathHelper.MakePersistentLoadPath(PatchDefine.PatchManifestBytesFileName);
                byte[] fileData = File.ReadAllBytes(filePath);
                MotionLog.Log($"Parse sandbox patch file.");
                patcher.ParseSandboxPatchManifest(fileData);
            }
            else
            {
                patcher.ParseSandboxPatchManifest(patcher.AppPatchManifest);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 异步初始化
        /// </summary>
        public IEnumerator InitializeAsync()
        {
            MotionLog.Log($"Beginning to initialize patch manager.");

            // 加载缓存
            _cache = PatchCache.LoadCache();

            // 检测沙盒被污染
            // 注意:在覆盖安装的时候,会保留沙盒目录里的文件,所以需要强制清空
            {
                // 如果是首次打开,记录APP版本号
                if (PatchHelper.CheckSandboxCacheFileExist() == false)
                {
                    _cache.CacheAppVersion = Application.version;
                    _cache.SaveCache();
                }
                else
                {
                    // 每次启动时比对APP版本号是否一致
                    if (_cache.CacheAppVersion != Application.version)
                    {
                        MotionLog.Warning($"Cache is dirty ! Cache version is {_cache.CacheAppVersion}, APP version is {Application.version}");
                        ClearCache();

                        // 重新写入最新的APP版本号
                        _cache.CacheAppVersion = Application.version;
                        _cache.SaveCache();
                    }
                }
            }

            // 加载APP内的补丁清单
            MotionLog.Log($"Load app patch manifest.");
            {
                string        filePath   = AssetPathHelper.MakeStreamingLoadPath(PatchDefine.PatchManifestFileName);
                string        url        = AssetPathHelper.ConvertToWWWPath(filePath);
                WebGetRequest downloader = new WebGetRequest(url);
                downloader.DownLoad();
                yield return(downloader);

                if (downloader.HasError())
                {
                    downloader.ReportError();
                    downloader.Dispose();
                    throw new System.Exception($"Fatal error : Failed download file : {url}");
                }

                // 解析补丁清单
                string jsonData = downloader.GetText();
                _appPatchManifest = PatchManifest.Deserialize(jsonData);
                downloader.Dispose();
            }

            // 加载沙盒内的补丁清单
            MotionLog.Log($"Load sandbox patch manifest.");
            if (PatchHelper.CheckSandboxPatchManifestFileExist())
            {
                string filePath = AssetPathHelper.MakePersistentLoadPath(PatchDefine.PatchManifestFileName);
                string jsonData = File.ReadAllText(filePath);
                _localPatchManifest = PatchManifest.Deserialize(jsonData);
            }
            else
            {
                _localPatchManifest = _appPatchManifest;
            }
        }
        private IEnumerator Download()
        {
            // 如果忽略资源版本,那么每次启动都会下载补丁清单
            bool ignoreResourceVersion = _patcher.IgnoreResourceVersion;

            // 新安装的用户首次启动游戏(包括覆盖安装的用户)
            // 注意:请求的补丁清单会在下载流程结束的时候,自动保存在沙盒里。
            bool firstStartGame = PatchHelper.CheckSandboxPatchManifestFileExist() == false;

            // 检测资源版本是否变化
            int newResourceVersion = _patcher.RequestedResourceVersion;
            int oldResourceVersion = _patcher.LocalResourceVersion;

            if (ignoreResourceVersion == false && firstStartGame == false && newResourceVersion == oldResourceVersion)
            {
                MotionLog.Log($"Resource version is not change.");
                _patcher.Switch(EPatchStates.PatchDone);
            }
            else
            {
                // 从远端请求补丁清单
                _requestCount++;
                string        url      = GetRequestURL(ignoreResourceVersion, newResourceVersion, PatchDefine.PatchManifestFileName);
                WebGetRequest download = new WebGetRequest(url);
                download.SendRequest();
                yield return(download);

                // Check fatal
                if (download.HasError())
                {
                    download.ReportError();
                    download.Dispose();
                    PatchEventDispatcher.SendPatchManifestRequestFailedMsg();
                    yield break;
                }

                // 解析补丁清单
                _patcher.ParseRemotePatchManifest(download.GetText());
                download.Dispose();

                // 如果发现了新的安装包
                if (_patcher.FoundNewApp)
                {
                    string requestedGameVersion = _patcher.RequestedGameVersion.ToString();
                    MotionLog.Log($"Found new APP can be install : {requestedGameVersion}");
                    PatchEventDispatcher.SendFoundNewAppMsg(_patcher.ForceInstall, _patcher.AppURL, requestedGameVersion);
                }
                else
                {
                    if (firstStartGame)
                    {
                        MotionLog.Log("First start game.");
                    }
                    if (newResourceVersion != oldResourceVersion)
                    {
                        MotionLog.Log($"Resource version is change : {oldResourceVersion} -> {newResourceVersion}");
                    }
                    _patcher.SwitchNext();
                }
            }
        }