using PublicTools.XMLDataBase;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEngine;
using XRTool.UI;
namespace XRTool.Util
{
///
/// 语言包的配置
///
public class LanguagePackConf
{
private string language = "简体中文";
private string packagePath;
/// 语言包的名称
///
public string Language { get => language; set => language = value; }
///
/// 语言包的路径
///
public string PackagePath { get => packagePath; set => packagePath = value; }
public Dictionary LanguageMap
{
get
{
if (languageMap == null)
{
languageMap = new Dictionary();
}
return languageMap;
}
}
public Dictionary ChineseMap
{
get
{
if (chineseMap == null)
{
chineseMap = new Dictionary();
}
return chineseMap;
}
}
///
/// 语言文本映射
///
private Dictionary languageMap;
///
/// 语言文本映射
///
private Dictionary chineseMap;
public event Action OnReadComplete;
///
/// 新旧编号
///
public static Dictionary numMap;
public static string NULL = "null";
///
/// 读取语言包,异步操作,读完后有对应的数据
///
public void ReadLanguageMap()
{
LanguageMap.Clear();
ChineseMap.Clear();
string path = Path.Combine(Application.streamingAssetsPath, "Language", packagePath);
DataConfMgr.Instance.OpenData(path, true);
DataConfMgr.Instance.TableHelper.readConfComplete -= OnReadConfComplete;
DataConfMgr.Instance.TableHelper.readConfComplete += OnReadConfComplete;
DataConfMgr.Instance.OpenTable("PriKey");
}
private void OnReadConfComplete()
{
InitLanguage(DataConfMgr.Instance.TableHelper.DataList);
OnReadComplete?.Invoke(this);
}
public void InitLanguage(List list)
{
LanguageMap.Clear();
ChineseMap.Clear();
bool isInitNum = (numMap == null);
if (isInitNum)
{
numMap = new Dictionary();
}
if (list != null)
{
for (int i = 0; i < list.Count; i++)
{
if (!LanguageMap.ContainsKey(list[i].PriKey))
{
LanguageMap.Add(list[i].PriKey, list[i]);
string[] strs = list[i].AudioPath.Split(';');
if (isInitNum && !string.IsNullOrEmpty(list[i].AudioPath) && list[i].AudioPath != NULL)
{
for (int j = 0; j < strs.Length; j++)
{
if (strs[j].Contains("T"))
{
if (!numMap.ContainsKey(strs[j]))
{
numMap.Add(strs[j], list[i].PriKey);
}
}
else
{
if (!numMap.ContainsKey("T" + strs[j]))
{
numMap.Add("T"+strs[j], list[i].PriKey);
}
}
}
}
}
if (!ChineseMap.ContainsKey(list[i].Message))
{
ChineseMap.Add(list[i].Message, list[i]);
}
}
}
}
///
/// 获取对应的语言文本的配置
///
///
///
public LanguageConf GetLanguage(string prikey)
{
if (!string.IsNullOrEmpty(prikey))
{
if (LanguageMap.ContainsKey(prikey))
{
return LanguageMap[prikey];
}
else if (ChineseMap.ContainsKey(prikey))
{
return ChineseMap[prikey];
}
else if (numMap != null && numMap.ContainsKey(prikey))
{
return GetLanguage(numMap[prikey]);
}
}
//UnityLog.LogError("GetLanguage=>Error:" + prikey);
return null;
}
}
}