import { LogicEvent } from "../event/LogicEvent" ; import { HotUpdate , AssetManagerCode , AssetManagerState , SubGameUpdateType , GameConfig } from "../base/HotUpdate" ; import { CommonEvent } from "../event/CommonEvent" ; export class GameManager { private static _instance : GameManager = null ; public static Instance ( ) { return this . _instance || ( this . _instance = new GameManager ( ) ) ; } private curGame : GameConfig = null ; private isLoading = false ; param areaType public enterGame ( config ) { if ( this . isLoading ) { cc . log ( "正在更新游戏，请稍等" ) ; return ; } this . curGame = config ; this . isLoading = true ; if ( ! HotUpdate . allGameConfig [ this . curGame . subpackageName ] ) { HotUpdate . allGameConfig [ this . curGame . subpackageName ] = config ; } let versionInfo = HotUpdate . allGameConfig [ this . curGame . subpackageName ] ; if ( HotUpdate . subGameUpdateType == SubGameUpdateType . Normal ) { if ( versionInfo . isLoaded ) { cc . log ( `游戏已经加载过了` ) ; this . onGameReady ( ) ; } else { this . checkUpdate ( versionInfo ) ; } } else { this . checkUpdate ( versionInfo ) ; } } private onGameReady ( ) { if ( this . isLoading ) { this . isLoading = false ; } dispatch ( LogicEvent . ENTER_GAME , this . curGame . subpackageName ) ; } description private checkUpdate ( versionInfo ) { let self = this ; cc . log ( `检测更新信息:${ versionInfo . gameName }(${ versionInfo . subpackageName })` ) ; HotUpdate . checkGameUpdate ( this . curGame . subpackageName , ( code , state ) => { if ( code == AssetManagerCode . NEW_VERSION_FOUND ) { HotUpdate . onDownload = this . onDownload . bind ( this ) ; cc . log ( `检测到${ versionInfo . gameName }(${ versionInfo . subpackageName })有新的版本` ) ; HotUpdate . hotUpdate ( ) ; } else if ( state == AssetManagerState . TRY_DOWNLOAD_FAILED_ASSETS ) { HotUpdate . onDownload = this . onDownload . bind ( this ) ; cc . log ( `正在尝试重新下载之前下载失败的资源` ) ; HotUpdate . downloadFailedAssets ( ) ; } else if ( code == AssetManagerCode . ALREADY_UP_TO_DATE ) { if ( versionInfo . isLoaded ) { self . onGameReady ( ) ; } else { self . loadSubpackage ( ) ; } } else if ( code == AssetManagerCode . ERROR_DOWNLOAD_MANIFEST || code == AssetManagerCode . ERROR_NO_LOCAL_MANIFEST || code == AssetManagerCode . ERROR_PARSE_MANIFEST ) { this . isLoading = false ; let content = "下载版本文件失败!" ; if ( code == AssetManagerCode . ERROR_NO_LOCAL_MANIFEST ) { content = "找不到版本文件!" ; } else if ( code == AssetManagerCode . ERROR_PARSE_MANIFEST ) { content = "版本文件解析错误!" ; } } else if ( code == AssetManagerCode . CHECKING ) { cc . log ( `正在检测更新!!` ) ; } else { this . isLoading = false ; cc . log ( `检测更新当前状态 code : ${ code } state : ${ state }` ) ; } } ) ; } private loadSubpackage ( ) { cc . log ( `updateGame : ${ this . curGame . subpackageName }` ) ; let me = this ; let versionInfo = HotUpdate . allGameConfig [ this . curGame . subpackageName ] ; cc . assetManager . loadBundle ( versionInfo . subpackageName , ( err , bundle ) => { me . isLoading = false ; if ( err ) { cc . error ( `load subpackage : ${ versionInfo . subpackageName } fail !!!` ) ; versionInfo . isLoaded = false ; } else { cc . log ( `load subpackage : ${ versionInfo . subpackageName } success !!!` ) ; versionInfo . isLoaded = true ; me . onGameReady ( ) ; } } ) ; } private onDownload ( downloadedBytes , totalBytes , downloadedFiles , totalFiles , percent , percentByFile , code , state , needRestart ) { if ( CC_DEBUG ) cc . log ( `
downloadedBytes : ${ downloadedBytes }
totalBytes : ${ totalBytes }
downloadedFiles : ${ downloadedFiles }
totalFiles : ${ totalFiles }
percent : ${ percent }
percentByFile : ${ percentByFile }
code : ${ code }
state : ${ state }
needRestart : ${ needRestart }
` ) ; let newPercent = 0 ; description description let gameConfig = HotUpdate . getGameLocalName ( this . curGame . subpackageName ) ; if ( code == AssetManagerCode . UPDATE_PROGRESSION ) { newPercent = percent == Number . NaN ? 0 : percent ; dispatch ( CommonEvent . DOWNLOAD_PROGRESS , { progress : newPercent , config : gameConfig } ) ; } else if ( code == AssetManagerCode . ALREADY_UP_TO_DATE ) { newPercent = 1 ; dispatch ( CommonEvent . DOWNLOAD_PROGRESS , { progress : newPercent , config : gameConfig } ) ; } else if ( code == AssetManagerCode . UPDATE_FINISHED ) { newPercent = 1.1 ; cc . log ( `更新${ gameConfig . gameName }成功` ) ; if ( ! needRestart ) { cc . log ( `正在加载${ gameConfig . gameName }` ) ; this . loadSubpackage ( ) ; } dispatch ( CommonEvent . DOWNLOAD_PROGRESS , { progress : newPercent , config : gameConfig } ) ; } else if ( code == AssetManagerCode . UPDATE_FAILED || code == AssetManagerCode . ERROR_NO_LOCAL_MANIFEST || code == AssetManagerCode . ERROR_DOWNLOAD_MANIFEST || code == AssetManagerCode . ERROR_PARSE_MANIFEST || code == AssetManagerCode . ERROR_DECOMPRESS ) { newPercent = - 1 ; this . isLoading = false ; cc . error ( `更新${ gameConfig . gameName }失败` ) ; dispatch ( CommonEvent . DOWNLOAD_PROGRESS , { progress : newPercent , config : gameConfig } ) ; } } }