Files
Main/Assets/Code/Logic/LanguageSystem/LanguageConvertSystem.cs
2025-01-25 04:38:09 +08:00

105 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using Thousandto.Core.Base;
using UnityEngine;
using PathUtils = UnityEngine.Gonbest.MagicCube.PathUtils;
using FFileReader = UnityEngine.Gonbest.MagicCube.FFileReader;
namespace Thousandto.Code.Logic
{
/// <summary>
/// 首领功能数据类
/// </summary>
public class LanguageConvertSystem
{
private bool _inited = false;
private Dictionary<string, string> _dict;
public string ConvertLan(string lanKey)
{
if (string.IsNullOrEmpty(lanKey))
return string.Empty;
if (!_inited)
Initialize();
string sep = "2&_";
//存在多个的情况要遍历下
string[] lanKeys = lanKey.Split(new string[] { sep }, StringSplitOptions.None);
//最终找到的翻译结果
string translateVal = "";
for (int i = 0; i < lanKeys.Length; i++)
{
string tempKey = lanKeys[i];
if (lanKeys[i].IndexOf(sep) == 0)
{
tempKey = lanKeys[i].Replace(sep, "");
}
if (_dict != null)
{
string key = lanKeys[i].Trim();
if (_dict.ContainsKey(key))
{
tempKey = _dict[key];
}
else
{
if (_dict.ContainsKey(tempKey))
tempKey = _dict[tempKey];
}
}
translateVal += tempKey;
}
return translateVal;
}
public void Initialize()
{
string lan = UnityEngine.Gonbest.MagicCube.FLanguage.Default;
string fileName = string.Format("Lan_{0}.bytes", string.IsNullOrEmpty(lan)?"CH":lan);
var fullPath = PathUtils.GetConfigFilePath(fileName);
FFileReader.ReadBytesAsync(fullPath, bytes => {
if (bytes != null)
_dict = ReadLanConfig(bytes);
});
_inited = true;
}
public static Dictionary<string, string> ReadLanConfig(byte[] bytes)
{
int readLen = 0;
int dataLen = bytes.Length;
//先读取总数量
int allCount = BitConverter.ToInt32(bytes, readLen);
readLen += 4;
Dictionary<string, string> result = new Dictionary<string, string>(allCount);
while (readLen < dataLen)
{
string key = ParseString(bytes, ref readLen);
string value = ParseString(bytes, ref readLen);
if (!result.ContainsKey(key))
result.Add(key, value);
else if(!string.IsNullOrEmpty(value.Trim()))
result[key] = value;
}
return result;
}
private static string ParseString(byte[] bytes, ref int readLen)
{
var len = BitConverter.ToInt32(bytes, readLen);
readLen += 4;
string value = System.Text.Encoding.Unicode.GetString(bytes, readLen, len);
readLen += len;
return value;
}
}
}