using BeinLab.Util;
using PublicTools.XMLDataBase;
using System;
using System.Collections.Generic;
using System.IO;
namespace XRTool.Util
{
///
/// 语言控制器
/// 提供语言切换时事件
/// 提供语言包读取,解析
/// 语言包放到stream包中
///
public class LanguageMgr : Singleton
{
///
/// 改变语言的事件
///
public event Action changeLanuage;
///
/// 当前的语言
///
private LanguagePackConf curLanguage;
///
/// 语言包列表
///
private List languagePackList;
private string tableName;
private TableInterface table;
public int LanguageCount
{
get
{
if (languagePackList != null)
{
return languagePackList.Count;
}
return 0;
}
}
//private int languageIndex = 0;
///
/// 当开始引用的时候创建实例
/// 读取配置表
///
public LanguageMgr()
{
GameSession.OnFocusGame += OnFocusGame;
InitLanguage();
}
private void InitLanguage()
{
if (BuildConfigMgr.Instance.IsInit)
{
if (TimerMgr.Instance)
{
TimerMgr.Instance.CreateTimer(() =>
{
ReadLanguage(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.languagePath));
}, 0.5f);
}
else
{
TimerMgr.InitComplte += () =>
{
TimerMgr.Instance.CreateTimer(() =>
{
ReadLanguage(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.languagePath));
}, 0.5f);
};
}
}
else
{
UnityLog.Instance.LogError("配置文件丢失!");
}
}
private void OnFocusGame()
{
if (!IsInit)
{
if (TimerMgr.Instance)
{
TimerMgr.Instance.CreateTimer(OnFocusGame, 1f);
}
else
{
TimerMgr.InitComplte += () =>
{
TimerMgr.Instance.CreateTimer(OnFocusGame, 1f);
};
}
return;
}
if (CurLanguage == null)
{
if (languagePackList == null || languagePackList.Count < SystemSettingMgr.Instance.settings.Language + 1)
{
UnityLog.Instance.LogError("没有找到语言包");
InitLanguage();
return;
}
else
{
CurLanguage = languagePackList[SystemSettingMgr.Instance.settings.Language];
}
}
if (CurLanguage != null)
{
if (CurLanguage.LanguageMap == null)
{
CurLanguage.ReadLanguageMap();
}
changeLanuage?.Invoke(CurLanguage);
IsInit = true;
}
}
private bool isInit = false;
public LanguagePackConf CurLanguage { get => curLanguage; set => curLanguage = value; }
public bool IsInit { get => isInit; set => isInit = value; }
///
/// 读取语言包
///
public void ReadLanguage(string path)
{
//languageIndex = SystemSettingMgr.Instance.settings.Language;
tableName = typeof(LanguagePackConf).Name;
#if UNITY_EDITOR || !UNITY_ANDROID
table = XSql.Instance.OpenTable(path, tableName, ".xml", true);
OpenTable();
#else
string fullPath = Path.Combine(path, tableName + ".xml");
if (GameSession.Instance)
{
GameSession.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List packList) =>
{
InitPackage(packList);
}));
}
else
{
GameSession.InitComplte += () =>
{
GameSession.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List packList) =>
{
InitPackage(packList);
}));
};
}
#endif
}
///
/// 打开语言包配置表
///
public void OpenTable()
{
if (!table.Open())
{
table.Create(tableName);
UnityLog.Instance.Log("create table" + tableName);
}
//languagePackList = table.FindAllData();
InitPackage(table.FindAllData());
}
public void InitPackage(List packList)
{
languagePackList = packList;
if (languagePackList == null || languagePackList.Count < SystemSettingMgr.Instance.settings.Language + 1)
{
UnityLog.Instance.LogError("没有找到语言包");
}
else
{
CurLanguage = languagePackList[SystemSettingMgr.Instance.settings.Language];
if (CurLanguage.LanguageMap == null)
{
CurLanguage.ReadLanguageMap();
}
changeLanuage?.Invoke(CurLanguage);
IsInit = true;
}
}
///
/// 选中某一个语言时,默认是中文
///
///
public void SelectLanguage(int index)
{
if (index >= 0 && index != SystemSettingMgr.Instance.settings.Language && index < languagePackList.Count)
{
SystemSettingMgr.Instance.settings.Language = index;
CurLanguage = languagePackList[index];
if (CurLanguage.LanguageMap == null)
{
CurLanguage.ReadLanguageMap();
}
changeLanuage?.Invoke(CurLanguage);
SystemSettingMgr.Instance.SaveSettings();
}
else
{
UnityLog.Instance.Log("未切换语言" + index);
}
}
///
/// 通过Key值获取对应的语言
///
///
///
public LanguageConf GetMessage(string prikey)
{
if (!string.IsNullOrEmpty(prikey))
{
prikey = prikey.Trim();
if (!prikey.StartsWith(BuildConfig.Instance.prefix))
{
prikey = BuildConfig.Instance.prefix + prikey;
}
if (CurLanguage != null)
{
return CurLanguage.GetLanguage(prikey);
}
}
return null;
}
}
}