using GameFramework.Event; using GameFramework.Resource; using System.Collections.Generic; using SepCore.Definition; using SepCore.UI; using SepCore.Utility; using TMPro; using UnityEngine; using UnityGameFramework.Runtime; using ProcedureOwner = GameFramework.Fsm.IFsm; namespace SepCore.Procedure { public class ProcedurePreload : ProcedureBase { private const string LubanTablesFlag = "LubanTables"; private Dictionary _loadedFlag = new Dictionary(); protected override void OnEnter(ProcedureOwner procedureOwner) { base.OnEnter(procedureOwner); GameEntry.Event.Subscribe(LoadConfigSuccessEventArgs.EventId, OnLoadConfigSuccess); GameEntry.Event.Subscribe(LoadConfigFailureEventArgs.EventId, OnLoadConfigFailure); GameEntry.Event.Subscribe(LoadDictionarySuccessEventArgs.EventId, OnLoadDictionarySuccess); GameEntry.Event.Subscribe(LoadDictionaryFailureEventArgs.EventId, OnLoadDictionaryFailure); _loadedFlag.Clear(); PreloadResources(); } protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown) { GameEntry.Event.Unsubscribe(LoadConfigSuccessEventArgs.EventId, OnLoadConfigSuccess); GameEntry.Event.Unsubscribe(LoadConfigFailureEventArgs.EventId, OnLoadConfigFailure); GameEntry.Event.Unsubscribe(LoadDictionarySuccessEventArgs.EventId, OnLoadDictionarySuccess); GameEntry.Event.Unsubscribe(LoadDictionaryFailureEventArgs.EventId, OnLoadDictionaryFailure); base.OnLeave(procedureOwner, isShutdown); } protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds) { base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds); foreach (KeyValuePair loadedFlag in _loadedFlag) { if (!loadedFlag.Value) { return; } } procedureOwner.SetData("NextSceneId", (int)SceneType.Menu); ChangeState(procedureOwner); } private void PreloadResources() { // Preload configs LoadConfig("DefaultConfig"); // Preload Luban data tables _loadedFlag.Add(LubanTablesFlag, false); GameEntry.Luban.LoadTables( () => { _loadedFlag[LubanTablesFlag] = true; // Load save data; create a new game if no save exists GameEntry.Save.Load(); if (!GameEntry.Save.HasSave && !GameEntry.Save.IsReady) { GameEntry.Save.CreateNewGame(); } Log.Info("Load Luban data tables OK."); }, errorMessage => { Log.Error("Load Luban data tables failure with error message '{0}'.", errorMessage); }); // Preload dictionaries LoadDictionary("Default"); // Preload fonts LoadFont("MainFont"); LoadTMPFont("MainTMPFont"); } private void LoadConfig(string configName) { string configAssetName = AssetUtility.GetConfigAsset(configName, false); _loadedFlag.Add(configAssetName, false); GameEntry.Config.ReadData(configAssetName, this); } private void LoadDictionary(string dictionaryName) { string dictionaryAssetName = AssetUtility.GetDictionaryAsset(GameEntry.Localization.Language.ToString(), dictionaryName, false); _loadedFlag.Add(dictionaryAssetName, false); GameEntry.Localization.ReadData(dictionaryAssetName, this); } private void LoadFont(string fontName) { _loadedFlag.Add(GameFramework.Utility.Text.Format("Font.{0}", fontName), false); GameEntry.Resource.LoadAsset(AssetUtility.GetFontAsset(fontName), Constant.AssetPriority.FontAsset, new LoadAssetCallbacks( (assetName, asset, duration, userData) => { _loadedFlag[GameFramework.Utility.Text.Format("Font.{0}", fontName)] = true; UGuiForm.SetMainFont((Font)asset); Log.Info("Load font '{0}' OK.", fontName); }, (assetName, status, errorMessage, userData) => { Log.Error("Can not load font '{0}' from '{1}' with error message '{2}'.", fontName, assetName, errorMessage); })); } private void LoadTMPFont(string fontName) { _loadedFlag.Add(GameFramework.Utility.Text.Format("Font.{0}", fontName), false); GameEntry.Resource.LoadAsset(AssetUtility.GetTMPFontAsset(fontName), Constant.AssetPriority.FontAsset, new LoadAssetCallbacks( (assetName, asset, duration, userData) => { _loadedFlag[GameFramework.Utility.Text.Format("Font.{0}", fontName)] = true; UGuiForm.SetMainTMPFont((TMP_FontAsset)asset); Log.Info("Load font '{0}' OK.", fontName); }, (assetName, status, errorMessage, userData) => { Log.Error("Can not load font '{0}' from '{1}' with error message '{2}'.", fontName, assetName, errorMessage); })); } private void OnLoadConfigSuccess(object sender, GameEventArgs e) { LoadConfigSuccessEventArgs ne = (LoadConfigSuccessEventArgs)e; if (ne.UserData != this) { return; } _loadedFlag[ne.ConfigAssetName] = true; Log.Info("Load config '{0}' OK.", ne.ConfigAssetName); } private void OnLoadConfigFailure(object sender, GameEventArgs e) { LoadConfigFailureEventArgs ne = (LoadConfigFailureEventArgs)e; if (ne.UserData != this) { return; } Log.Error("Can not load config '{0}' from '{1}' with error message '{2}'.", ne.ConfigAssetName, ne.ConfigAssetName, ne.ErrorMessage); } private void OnLoadDictionarySuccess(object sender, GameEventArgs e) { LoadDictionarySuccessEventArgs ne = (LoadDictionarySuccessEventArgs)e; if (ne.UserData != this) { return; } _loadedFlag[ne.DictionaryAssetName] = true; Log.Info("Load dictionary '{0}' OK.", ne.DictionaryAssetName); } private void OnLoadDictionaryFailure(object sender, GameEventArgs e) { LoadDictionaryFailureEventArgs ne = (LoadDictionaryFailureEventArgs)e; if (ne.UserData != this) { return; } Log.Error("Can not load dictionary '{0}' from '{1}' with error message '{2}'.", ne.DictionaryAssetName, ne.DictionaryAssetName, ne.ErrorMessage); } } }