NLDClient-yudde/ProjectNLD/Assets/Editor/Tools/AutoConfig/MixKeyTool/GenMixKeyTool.cs

299 lines
8.9 KiB
C#
Raw Normal View History

2025-04-02 19:48:05 +08:00
using Cysharp.Threading.Tasks;
2024-04-23 14:43:56 +08:00
using System;
using System.Collections.Generic;
using System.Globalization;
2024-04-23 14:43:56 +08:00
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace AutoConfig
{
public class GenMixKeyTool
{
2025-04-02 19:48:05 +08:00
public static readonly string CfgPath = Application.dataPath + "/../../../NLDMisc/config/Datas/";
public static readonly string StringConfigPath = "StringConfig.xlsx";//"StringLocalizeConfig.xlsx";
2024-04-23 16:10:10 +08:00
private static string Config_MixKey_Path = @"Assets/Code/Scripts/Framework/Data/MixKey/MixKeyConfig.cs";
2024-04-23 14:43:56 +08:00
private const string Config_MixKey_Name = "MixKeyConfig.cs";
static string _cfgName = "";
static List<string> _keyList = new List<string>(); //Key字段所有Key的列表
static List<string> _keyIdxList = new List<string>(); //Value字段的索引列表
2024-04-23 14:43:56 +08:00
static string _param = "";
private static int _listIdx = 0; //如果值是一个
2024-04-23 14:43:56 +08:00
private static bool _isPer;
private static int _process;
2024-04-23 14:43:56 +08:00
private static List<string> _stringCfgMixValueList;
public static int LoadCfgProcess => _process;
2024-04-23 14:43:56 +08:00
//配置内容分段枚举
private enum KeyPart
{
Cfg,
Key,
Value,
Param, //参数字段名
ListIdx, //参数若为List取第几位从0开始
2024-04-23 14:43:56 +08:00
IsPer, //是否百分比
Max //无意义,仅作为上限值
}
private Dictionary<string, string> _tempDic = new()
{
["cfg_key0/key1_value0/value1_paramName_listIdx_isPer(0/1)"] = "1",
2024-04-23 14:43:56 +08:00
};
private static bool IsFileExit()
{
return File.Exists(Config_MixKey_Path);
}
#region 获取Cell内对应配置内容
//获取Key对应Value
public static string GetValue(string fullKey)
{
ParseContent(fullKey);
2024-10-22 12:35:38 +08:00
var val = GetValueString(fullKey);
2024-04-23 14:43:56 +08:00
return val;
}
/// <summary>
/// 解析配置的Key内容
/// </summary>
/// <param name="fullContent"></param>
private static void ParseContent(string fullContent)
{
var strList = fullContent.Split('_');
if (strList.Length != (int)KeyPart.Max)
{
DebugUtil.LogError($"StringConfig中配置的索引参数内容有误, Key:{fullContent} ");
return;
}
Release();
for (var i = KeyPart.Cfg; i < KeyPart.Max; i++)
{
var str = strList[(int)i];
switch (i)
{
case KeyPart.Cfg:
{
_cfgName = str;
break;
}
case KeyPart.Key:
{
var keyStrL = str.Split('/');
_keyList = keyStrL.ToList();
break;
}
case KeyPart.Value:
{
var keyStrL = str.Split('/');
_keyIdxList = keyStrL.ToList();
break;
}
case KeyPart.Param:
{
_param = str;
break;
}
case KeyPart.ListIdx:
{
_listIdx = int.Parse(str);
break;
}
2024-04-23 14:43:56 +08:00
case KeyPart.IsPer:
{
_isPer = str == "1";
break;
}
default:
break;
}
}
}
2024-10-22 12:35:38 +08:00
private static string GetValueString(string fullKey)
2024-04-23 14:43:56 +08:00
{
var keyLineIdxList = ExcelRead.GetValueIndexList(_cfgName, _keyList);
2024-10-22 12:35:38 +08:00
try
2024-04-23 14:43:56 +08:00
{
2024-10-22 12:35:38 +08:00
var val = ExcelRead.GetStringValByIndex(_cfgName, _param, keyLineIdxList, _keyIdxList, _listIdx);
if (val == null)
{
DebugUtil.LogError($"FullKey:{fullKey}, cfgName:{_cfgName}, param:{_param}");
return "";
}
if (_isPer)
{
float value = Math.Abs(float.Parse(val));
2024-10-22 12:35:38 +08:00
val = $"{value:P}";
val = val.Replace(".00", "");
}
else
{
if(int.TryParse(val, out var valueInt))
{
val = valueInt.ToString();
}
else if(float.TryParse(val, out var valueFloat))
{
val = valueFloat.ToString(CultureInfo.InvariantCulture);
}
}
2024-10-22 12:35:38 +08:00
if (val.Contains(","))
{
val = val.Replace(",", "");
}
if (val.Contains(""))
{
val = val.Replace("", "");
}
2024-10-22 12:35:38 +08:00
return val;
2024-04-23 14:43:56 +08:00
}
2024-10-22 12:35:38 +08:00
catch (Exception e)
{
DebugUtil.LogError($"FullKey:{fullKey}, cfgName:{_cfgName}, param:{_param}");
DebugUtil.LogError(e);
return "";
}
2024-04-23 14:43:56 +08:00
}
private static async UniTask LoadMixValue()
2024-04-23 14:43:56 +08:00
{
2024-07-29 14:31:10 +08:00
ExcelRead.InitAll();
2024-04-23 14:43:56 +08:00
ExcelRead.LoadStrCfgMixKeyList();
var keyList = ExcelRead.StringCfgMixKeyList;
ExcelRead.StringCfgMixValueList.Clear();
_process = 0;
2024-04-23 14:43:56 +08:00
for (int i = 0 ; i < keyList.Count; i++)
{
await UniTask.Delay(1);
_process++;
2024-04-23 14:43:56 +08:00
var key = keyList[i];
var val = GetValue(key);
ExcelRead.StringCfgMixValueList.Add(val);
_ShowGenMixKeyProcess();
2024-04-23 14:43:56 +08:00
}
2024-07-29 14:31:10 +08:00
Release();
EditorUtility.ClearProgressBar();
}
private static void _ShowGenMixKeyProcess()
{
var strInfo = LoadCfgProcess + "/" + ExcelRead.StringCfgMixKeyList.Count;
float process = (float)LoadCfgProcess / ExcelRead.StringCfgMixKeyList.Count;
EditorUtility.DisplayProgressBar("MixKey生成中", strInfo, process);
2024-04-23 14:43:56 +08:00
}
#endregion
/// <summary>
/// 生成文件内容
/// </summary>
/// <returns></returns>
private static bool WriteFileStr()
{
StreamWriter fWriter = null;
try
{
fWriter = File.CreateText(Config_MixKey_Path);
}
catch (Exception e)
{
Debug.LogException(e);
}
if (fWriter == null)
{
Debug.LogError($"不存在文件{Config_MixKey_Name}, 路径:{Config_MixKey_Path}");
return false;
}
string startStr = "";
startStr += "using System.Collections.Generic;\n";
startStr += "public class MixKeyConfig\n{\n\t";
startStr += "public static Dictionary<string, string> MixKeyDic = new()\n\t{\n\t\t";
fWriter.Write(startStr);
var keyList = ExcelRead.StringCfgMixKeyList;
var valList = _stringCfgMixValueList;
if (keyList.Count != valList.Count)
{
DebugUtil.LogError("Key, Value长度不等请检查");
}
for (int i = 0; i < keyList.Count; i++)
{
string contentStr = "";
var keyContent = keyList[i];
var valContent = valList[i];
contentStr += "[\""+ keyContent + "\"] = \"" + valContent + "\",";
if (i < keyList.Count - 1)
{
contentStr += "\n\t\t";
}
fWriter.Write(contentStr);
}
string endStr = "\n\t};\n}";
fWriter.Write(endStr);
fWriter.Close();
return true;
}
private static void ClearFile()
{
FileStream stream = File.Open(Config_MixKey_Path, FileMode.OpenOrCreate, FileAccess.Write);
stream.Seek(0, SeekOrigin.Begin);
stream.SetLength(0);
stream.Close();
}
private static void DetectFile()
{
ClearFile();
AssetDatabase.Refresh();
}
public static void GenMixKeyFile()
{
LoadMixValue();
DetectFile();
if (WriteFileStr())
{
2024-04-23 16:10:10 +08:00
DebugUtil.Log("生成MixKey配置成功");
2024-04-23 14:43:56 +08:00
return;
}
2024-04-23 16:10:10 +08:00
DebugUtil.Log("生成MixKey配置失败");
2024-04-23 14:43:56 +08:00
}
public static async void GenMixKeyJson()
{
await LoadMixValue();
MixKeyGenJson.GenMixKeyJsonFile();
}
2024-04-23 14:43:56 +08:00
public static void Release()
{
_cfgName = "";
_keyList.Clear();
_keyIdxList.Clear();
_param = "";
_isPer = false;
2024-07-29 14:31:10 +08:00
ExcelRead.ReleaseAll();
2024-04-23 14:43:56 +08:00
}
}
}