using System; using System.Collections; using System.Collections.Generic; using RenderHeads.Media.AVProVideo; using UniRx; using UnityEngine; namespace MoleMole { public class MonoGameEntry : BaseMonoCanvas { private enum Status { Default = 0, WaitingRetryLoadDataAsset = 1, WaitingRetryLoadEventAsset = 2 } public const int MAX_ASSET_BUNDLE_RETRY_TIMES = 4; public const float RETRY_ASSET_BUNDLE_WAIT_SECONDS = 2f; public GameObject GameEntryPage; public int assetbundleRetryTimes; public Avatar3dModelContext avatar3dModelContext; private GameEntryPageContext _pageContext; private ElevatorModelContext _elevatorModelContext; private SpaceShipModelContext _spaceshipModelContext; private Coroutine _waitLoadSpaceshipCoroutine; private Coroutine _waitAvatarModelCoroutine; private Coroutine _waitSplashFadeoutCoroutine; private Coroutine _waitBeforeLoginCoroutine; private Status _status; private float _retryAssetBundleWaitTimer; private MediaPlayer _videoPlayer; private DisplayIMGUI _videoDisplay; private bool _startFirstLevel; public GameObject spaceshipGO { get; set; } public float warshipDefaultYPos { get; set; } public void Awake() { GraphicsSettingUtil.SetTargetFrameRate(60); GeneralLogicManager.InitOnGameStart(); Singleton.Instance.SetMainCanvas(this); assetbundleRetryTimes = 0; _status = Status.Default; _retryAssetBundleWaitTimer = 0f; _videoPlayer = base.transform.Find("Video/VideoPlayer").GetComponent(); _videoPlayer.Events.AddListener(OnVideoEvent); _videoPlayer.gameObject.SetActive(false); _videoDisplay = base.transform.Find("Video/VideoDisplay").GetComponent(); _videoDisplay.gameObject.SetActive(false); base.transform.Find("Video/BlackPanel").gameObject.SetActive(false); base.transform.Find("Video").gameObject.SetActive(false); } public override void Start() { GameObject view = GameObject.Find("StartLoading_Model"); _elevatorModelContext = new ElevatorModelContext(view); Singleton.Instance.ShowWidget(_elevatorModelContext); avatar3dModelContext = new Avatar3dModelContext(); Singleton.Instance.ShowWidget(avatar3dModelContext, UIType.Root); _pageContext = new GameEntryPageContext(GameEntryPage); Singleton.Instance.ShowPage(_pageContext); Singleton.Instance.FireNotify(new Notify(NotifyTypes.FadeOutGameEntrySplash)); Singleton.Instance.PushSoundBankScale(new string[3] { "All_In_One_Bank", "BK_Global", "BK_Events" }); Singleton.Instance.ManualPrepareBank("BK_GameEntry"); Singleton.Instance.Post("GameEntry_Elevator_Start_Alarm"); string text = LocalizationGeneralLogic.GetText("Menu_ConnectGlobalDispatch"); Singleton.Instance.FireNotify(new Notify(NotifyTypes.SetLoadAssetText, new Tuple(true, text, false, 0f))); _waitSplashFadeoutCoroutine = StartCoroutine(WaitSplashFadeout()); PostStartHandleBenchmark(); AudioSettingData.ApplySettingConfig(); if (Singleton.Instance == null) { Singleton.Create(); Singleton.Instance.InitAtAwake(); Singleton.Instance.InitAtStart(); } base.Start(); } public override void Update() { base.Update(); if (_status == Status.WaitingRetryLoadDataAsset) { _retryAssetBundleWaitTimer += Time.fixedDeltaTime; if (_retryAssetBundleWaitTimer >= 2f) { _status = Status.Default; _retryAssetBundleWaitTimer = 0f; RetryCheckAndLoadDataAsset(); } } else if (_status == Status.WaitingRetryLoadEventAsset) { _retryAssetBundleWaitTimer += Time.fixedDeltaTime; if (_retryAssetBundleWaitTimer >= 2f) { _status = Status.Default; _retryAssetBundleWaitTimer = 0f; RetryCheckAndLoadEventAsset(); } } } public override void OnDestroy() { StopWaitLoadSpaceship(); StopWaitAvatarModel(); StopWaitSplashFadeout(); StopWaitBeforeLogin(); if (_startFirstLevel) { if (_spaceshipModelContext != null && _spaceshipModelContext.view != null) { UnityEngine.Object.Destroy(_spaceshipModelContext.view); } if (avatar3dModelContext != null && avatar3dModelContext.view != null) { UnityEngine.Object.Destroy(avatar3dModelContext.view); } } base.OnDestroy(); } public void OnRestartGame() { StopWaitLoadSpaceship(); StopWaitAvatarModel(); StopWaitSplashFadeout(); StopWaitBeforeLogin(); if (_spaceshipModelContext != null && _spaceshipModelContext.view != null) { UnityEngine.Object.Destroy(_spaceshipModelContext.view); } if (avatar3dModelContext != null && avatar3dModelContext.view != null) { UnityEngine.Object.Destroy(avatar3dModelContext.view); } } public void ConnentGlobalDispatch() { StartCoroutine(Singleton.Instance.ConnectGlobalDispatchServer(ConnectDispatch)); } public void ConnectDispatch() { StartCoroutine(Singleton.Instance.ConnectDispatchServer(DispatchConnectCallback)); } private void DispatchConnectCallback() { Singleton.Instance.manager.SetupByDispatchServerData(); if (GlobalVars.DataUseAssetBundle) { CheckAndLoadDataAsset(); } else { OnDataAssetReady(); } if (Singleton.Instance.DispatchSeverData.showVersionText) { Singleton.Instance.FireNotify(new Notify(NotifyTypes.ShowVersionText)); } } private void CheckAndLoadDataAsset() { Singleton.Instance.Loader.LoadVersionFile(BundleType.DATA_FILE, OnLoadingDataVersionProgress, delegate(bool success) { if (success) { StartCoroutine(Singleton.Instance.Loader.StartDownloadAssetBundle(BundleType.DATA_FILE, OnLoadingDataAssetBundleProgress, null, OnLoadingDataAssetBundleComplete)); } else { OnLoadingDataAssetBundleComplete(false); } }); } private void OnDataAssetReady() { GeneralLogicManager.InitOnDataAssetReady(true, AfterDataAssetReady); } private void AfterDataAssetReady() { if (GlobalVars.ResourceUseAssetBundle) { CheckAndLoadEventAsset(); } else { OnEventAssetReady(); } } private void OnLoadingDataVersionProgress(long current, long total, long delta, float speed) { string text = LocalizationGeneralLogic.GetText("Menu_CheckDataAsset"); Singleton.Instance.FireNotify(new Notify(NotifyTypes.SetLoadAssetText, new Tuple(true, text, false, 0f))); } private void OnLoadingDataAssetBundleProgress(long current, long total, long delta, float speed) { float item = (float)current / (float)total; string text = LocalizationGeneralLogic.GetText("Menu_DownloadDataAsset"); Singleton.Instance.FireNotify(new Notify(NotifyTypes.SetLoadAssetText, new Tuple(true, text, true, item))); } private void OnLoadingDataAssetBundleComplete(bool result) { if (result) { assetbundleRetryTimes = 0; Singleton.Instance.FireNotify(new Notify(NotifyTypes.ShowLoadAssetText, false)); OnDataAssetReady(); } else { TriggerRetryCheckAndLoadDataAsset(); } } private void TriggerRetryCheckAndLoadDataAsset() { _status = Status.WaitingRetryLoadDataAsset; _retryAssetBundleWaitTimer = 0f; } private void RetryCheckAndLoadDataAsset() { assetbundleRetryTimes++; if (assetbundleRetryTimes > 4) { Singleton.Instance.ShowDialog(new GeneralDialogContext { type = GeneralDialogContext.ButtonType.SingleButton, title = LocalizationGeneralLogic.GetText("Menu_NetError"), desc = LocalizationGeneralLogic.GetText("Menu_Desc_DownloadDataAssetErr"), notDestroyAfterTouchBG = true, buttonCallBack = delegate { CheckAndLoadDataAsset(); TriggerRetryCheckAndLoadDataAsset(); } }); } else { CheckAndLoadDataAsset(); } } private void CheckAndLoadEventAsset() { Singleton.Instance.Loader.LoadVersionFile(BundleType.RESOURCE_FILE, OnLoadingEventVersionProgress, delegate(bool success) { if (success) { StartCoroutine(Singleton.Instance.Loader.StartDownloadAssetBundle(BundleType.RESOURCE_FILE, OnLoadingEventAssetBundleProgress, null, OnLoadingEventAssetBundleComplete)); } else { OnLoadingEventAssetBundleComplete(false); } }); } private void OnEventAssetReady() { Singleton.Instance.UpdateEventSVNVersion(); _waitBeforeLoginCoroutine = StartCoroutine(WaitBeforeLogin()); } private void OnLoadingEventVersionProgress(long current, long total, long delta, float speed) { string text = LocalizationGeneralLogic.GetText("Menu_CheckEventAsset"); Singleton.Instance.FireNotify(new Notify(NotifyTypes.SetLoadAssetText, new Tuple(true, text, false, 0f))); } private void OnLoadingEventAssetBundleProgress(long current, long total, long delta, float speed) { float item = (float)current / (float)total; string text = LocalizationGeneralLogic.GetText("Menu_DownloadEventAsset"); Singleton.Instance.FireNotify(new Notify(NotifyTypes.SetLoadAssetText, new Tuple(true, text, true, item))); } private void OnLoadingEventAssetBundleComplete(bool result) { if (result) { assetbundleRetryTimes = 0; _status = Status.Default; _retryAssetBundleWaitTimer = 0f; Singleton.Instance.FireNotify(new Notify(NotifyTypes.ShowLoadAssetText, false)); OnEventAssetReady(); } else { TriggerRetryCheckAndLoadEventAsset(); } } private void TriggerRetryCheckAndLoadEventAsset() { _status = Status.WaitingRetryLoadEventAsset; _retryAssetBundleWaitTimer = 0f; } private void RetryCheckAndLoadEventAsset() { assetbundleRetryTimes++; if (assetbundleRetryTimes > 4) { Singleton.Instance.ShowDialog(new GeneralDialogContext { type = GeneralDialogContext.ButtonType.SingleButton, title = LocalizationGeneralLogic.GetText("Menu_NetError"), desc = LocalizationGeneralLogic.GetText("Menu_Desc_DownloadEventAssetErr"), notDestroyAfterTouchBG = true, buttonCallBack = delegate { CheckAndLoadEventAsset(); TriggerRetryCheckAndLoadEventAsset(); } }); } else { CheckAndLoadEventAsset(); } } public void OnPlayerLogin(bool isFirstTime) { Singleton.Instance.FireNotify(new Notify(NotifyTypes.SetLoadAssetText, new Tuple(false, string.Empty, false, 0f))); UIUtil.SpaceshipCheckWeather(); Singleton.Instance.InitTechTree(); if (isFirstTime) { PlayVideo(); } else { _waitAvatarModelCoroutine = StartCoroutine(WaitCreateAvatarModel()); DisableVideo(); } if (Singleton.Instance.Available) { ProcessRealtimeWeatherUpdate(); } Singleton.Instance.GeneralLocalData.ReportUIStatistics(); } public void OnElevatorFloorAnimEvent(int phase) { if (phase == 1) { GraphicsSettingUtil.EnableUIAvatarsDynamicBone(false); } } public void OnElevatorFloorPhase1AnimOver() { _elevatorModelContext.HideSomeParts(); Animation component = spaceshipGO.transform.Find("Warship").GetComponent(); component.Play("WarshipFall"); _elevatorModelContext.PlayFloorPhase2Animation(); SetUIAvatarStandOnSpaceship(); } public void OnElevatorFloorPhase2AnimOver() { GraphicsSettingUtil.EnableUIAvatarsDynamicBone(true); Camera main = Camera.main; Animation component = main.gameObject.GetComponent(); component.Play("BeforeEnterSpaceship"); } public void OnElevatorDoorAnimOver() { Camera main = Camera.main; Animation component = main.gameObject.GetComponent(); component.Play("EnterSpaceship"); _elevatorModelContext.PlayBackAnimation(); } public void OnCameraBeforeEnterSpaceshipAnimOver() { _elevatorModelContext.SetDescImage(ElevatorModelContext.DescImageType.Confirmed); _elevatorModelContext.PlayDoorAnimation(); Singleton.Instance.Post("GameEntry_Elevator_Door_Open"); } public void OnCameraEnterSpaceshipAnimEvent(int phase) { if (phase == 1) { Singleton.Instance.Post("VO_M_Con_12_CPT_On_Bridge"); } switch (phase) { case 2: TriggerAvatarModelTurnAround(); break; case 3: Singleton.Instance.ClearManualPrepareBank(); Singleton.Instance.TryEnterMainMenu(); Singleton.Instance.MoveToNextScene("MainMenuWithoutSpaceship"); break; } } private IEnumerator WaitSplashFadeout() { while (!_pageContext.IsSplashFadeOut) { yield return null; } _waitSplashFadeoutCoroutine = null; if (!GlobalVars.DISABLE_NETWORK_DEBUG) { ConnentGlobalDispatch(); yield break; } FakePacketHelper.FakeConnectDispatch(); OnDataAssetReady(); } private IEnumerator WaitBeforeLogin() { _waitLoadSpaceshipCoroutine = StartCoroutine(WaitLoadSpaceShip()); yield return _waitLoadSpaceshipCoroutine; bool hasLastLoginUser = Singleton.Instance.GeneralLocalData.LastLoginUserId != 0; bool isLoginByAccount = !string.IsNullOrEmpty(Singleton.Instance.manager.AccountUid); string text = ((hasLastLoginUser || isLoginByAccount) ? "ENTRY_PREPARED" : "ENTRY_PREPARED_LOGIN"); Singleton.Instance.FireNotify(new Notify(NotifyTypes.SetLoadAssetText, new Tuple(true, LocalizationGeneralLogic.GetText(text), true, 1f))); _elevatorModelContext.SetDescImage(ElevatorModelContext.DescImageType.Identifying); Singleton.Instance.manager.LoginUI(); _waitBeforeLoginCoroutine = null; } private IEnumerator WaitLoadSpaceShip() { AsyncAssetRequst resReq = Miscs.LoadResourceAsync("Stage/MainMenu_SpaceShip/MainMenu_SpaceShip"); yield return resReq.operation; spaceshipGO = UnityEngine.Object.Instantiate((GameObject)resReq.asset); spaceshipGO.name = "MainMenu_SpaceShip"; warshipDefaultYPos = spaceshipGO.transform.Find("Warship").position.y; spaceshipGO.transform.Find("Warship").localPosition = new Vector3(0f, 10f, 0f); UnityEngine.Object.DontDestroyOnLoad(spaceshipGO); _spaceshipModelContext = new SpaceShipModelContext(uiMainCamera: Camera.main.gameObject, view: spaceshipGO); Singleton.Instance.ShowWidget(_spaceshipModelContext); _waitLoadSpaceshipCoroutine = null; } private IEnumerator WaitCreateAvatarModel() { List avatars = avatar3dModelContext.GetAllAvatars(); while (avatars.Count == 0) { yield return null; avatars = avatar3dModelContext.GetAllAvatars(); } float timer = 0f; while (Singleton.Instance == null || !Singleton.Instance.missionDataReceived) { yield return null; timer += Time.deltaTime; if (timer > 3f) { Singleton.Instance.RequestGetMissionData(); timer = 0f; } } _elevatorModelContext.PlayFloorPhase1Animation(); Singleton.Instance.Post("GameEntry_Elevator_End"); _waitAvatarModelCoroutine = null; } private void SetUIAvatarStandOnSpaceship() { int readyToTouchAvatarID = Singleton.Instance.GetReadyToTouchAvatarID(); AvatarDataItem avatarByID = Singleton.Instance.GetAvatarByID(readyToTouchAvatarID); avatar3dModelContext.SetStandOnSpaceship(avatarByID.avatarID); } private void TriggerAvatarModelTurnAround() { int readyToTouchAvatarID = Singleton.Instance.GetReadyToTouchAvatarID(); AvatarDataItem avatarByID = Singleton.Instance.GetAvatarByID(readyToTouchAvatarID); avatar3dModelContext.TriggerAvatarTurnAround(avatarByID.avatarID); } private void StopWaitAvatarModel() { if (_waitAvatarModelCoroutine != null) { StopCoroutine(_waitAvatarModelCoroutine); _waitAvatarModelCoroutine = null; } } private void StopWaitSplashFadeout() { if (_waitSplashFadeoutCoroutine != null) { StopCoroutine(_waitSplashFadeoutCoroutine); _waitSplashFadeoutCoroutine = null; } } private void StopWaitLoadSpaceship() { if (_waitLoadSpaceshipCoroutine != null) { StopCoroutine(_waitLoadSpaceshipCoroutine); _waitLoadSpaceshipCoroutine = null; } } private void StopWaitBeforeLogin() { if (_waitBeforeLoginCoroutine != null) { StopCoroutine(_waitBeforeLoginCoroutine); _waitBeforeLoginCoroutine = null; } } private void PostStartHandleBenchmark() { if (GlobalVars.IS_BENCHMARK) { Screen.sleepTimeout = -1; SuperDebug.CloseAllDebugs(); GameObject gameObject = new GameObject(); UnityEngine.Object.DontDestroyOnLoad(gameObject); gameObject.name = "__Benchmark"; gameObject.AddComponent(); } } private void PlayVideo() { _pageContext.FadeOutVideo(); base.transform.Find("Video").gameObject.SetActive(true); _videoPlayer.gameObject.SetActive(true); try { CgDataItem cgDataItem = Singleton.Instance.GetCgDataItemList()[0]; string text = string.Format("Video/{0}.mp4", cgDataItem.cgPath); if (_videoPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, text, false)) { Singleton.Instance.InvokeNextFrame(delegate { Singleton.Instance.LockUI(true, float.MaxValue); }); } else { SuperDebug.VeryImportantError("open video failed! path=" + text); OnVideoFinished(); } } catch (Exception ex) { SuperDebug.VeryImportantError(ex.ToString()); OnVideoFinished(); } } private void StartFirstLevel() { try { int cgID = Singleton.Instance.GetCgDataItemList()[0].cgID; Singleton.Instance.MarkCGIDFinish(cgID); } catch (Exception ex) { SuperDebug.VeryImportantError(ex.ToString()); } _startFirstLevel = _pageContext.StartFirstLevel(); if (!_startFirstLevel) { DisableVideo(); _pageContext.DisableVideoFadeOut(); _waitAvatarModelCoroutine = StartCoroutine(WaitCreateAvatarModel()); } } private void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, RenderHeads.Media.AVProVideo.ErrorCode ec) { switch (et) { case MediaPlayerEvent.EventType.ReadyToPlay: OnVideoReady(); break; case MediaPlayerEvent.EventType.FirstFrameReady: OnVideoStarted(); break; case MediaPlayerEvent.EventType.FinishedPlaying: case MediaPlayerEvent.EventType.Error: OnVideoFinished(); break; case MediaPlayerEvent.EventType.Started: case MediaPlayerEvent.EventType.Closing: break; } } private void OnVideoReady() { try { AudioVolumeParamSetting(0f); Singleton.Instance.Post("GameEntry_Elevator_End"); _videoPlayer.Play(); Singleton.Instance.LockUI(true, float.MaxValue); Screen.sleepTimeout = -1; } catch (Exception ex) { SuperDebug.VeryImportantError(ex.ToString()); OnVideoFinished(); } } private void OnVideoStarted() { _videoDisplay.gameObject.SetActive(true); base.transform.Find("Video/BlackPanel").gameObject.SetActive(true); } private void OnVideoFinished() { try { AudioVolumeParamSetting(3f); DisableVideo(); Screen.sleepTimeout = -2; } catch (Exception ex) { SuperDebug.VeryImportantError(ex.ToString()); } finally { StartFirstLevel(); } } private void DisableVideo() { _videoPlayer.Events.RemoveAllListeners(); _videoPlayer.gameObject.SetActive(false); _videoDisplay.gameObject.SetActive(false); base.transform.Find("Video").gameObject.SetActive(false); } private void AudioVolumeParamSetting(float vol) { Singleton.Instance.SetParam("Vol_BGM", vol); Singleton.Instance.SetParam("Vol_SE", vol); Singleton.Instance.SetParam("Vol_Voice", vol); } private void ProcessRealtimeWeatherUpdate() { if (Singleton.Instance.IsWeatherInfoExpired()) { Singleton.Instance.QueryWeatherInfo(); } } } }