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

299 lines
8.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Cysharp.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace AutoConfig
{
public class GenMixKeyTool
{
public static readonly string CfgPath = Application.dataPath + "/../../../NLDMisc/config/Datas/";
public static readonly string StringConfigPath = "StringConfig.xlsx";//"StringLocalizeConfig.xlsx";
private static string Config_MixKey_Path = @"Assets/Code/Scripts/Framework/Data/MixKey/MixKeyConfig.cs";
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字段的索引列表
static string _param = "";
private static int _listIdx = 0; //如果值是一个
private static bool _isPer;
private static int _process;
private static List<string> _stringCfgMixValueList;
public static int LoadCfgProcess => _process;
//配置内容分段枚举
private enum KeyPart
{
Cfg,
Key,
Value,
Param, //参数字段名
ListIdx, //参数若为List取第几位从0开始
IsPer, //是否百分比
Max //无意义,仅作为上限值
}
private Dictionary<string, string> _tempDic = new()
{
["cfg_key0/key1_value0/value1_paramName_listIdx_isPer(0/1)"] = "1",
};
private static bool IsFileExit()
{
return File.Exists(Config_MixKey_Path);
}
#region 获取Cell内对应配置内容
//获取Key对应Value
public static string GetValue(string fullKey)
{
ParseContent(fullKey);
var val = GetValueString(fullKey);
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;
}
case KeyPart.IsPer:
{
_isPer = str == "1";
break;
}
default:
break;
}
}
}
private static string GetValueString(string fullKey)
{
var keyLineIdxList = ExcelRead.GetValueIndexList(_cfgName, _keyList);
try
{
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));
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);
}
}
if (val.Contains(","))
{
val = val.Replace(",", "");
}
if (val.Contains(""))
{
val = val.Replace("", "");
}
return val;
}
catch (Exception e)
{
DebugUtil.LogError($"FullKey:{fullKey}, cfgName:{_cfgName}, param:{_param}");
DebugUtil.LogError(e);
return "";
}
}
private static async UniTask LoadMixValue()
{
ExcelRead.InitAll();
ExcelRead.LoadStrCfgMixKeyList();
var keyList = ExcelRead.StringCfgMixKeyList;
ExcelRead.StringCfgMixValueList.Clear();
_process = 0;
for (int i = 0 ; i < keyList.Count; i++)
{
await UniTask.Delay(1);
_process++;
var key = keyList[i];
var val = GetValue(key);
ExcelRead.StringCfgMixValueList.Add(val);
_ShowGenMixKeyProcess();
}
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);
}
#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())
{
DebugUtil.Log("生成MixKey配置成功");
return;
}
DebugUtil.Log("生成MixKey配置失败");
}
public static async void GenMixKeyJson()
{
await LoadMixValue();
MixKeyGenJson.GenMixKeyJsonFile();
}
public static void Release()
{
_cfgName = "";
_keyList.Clear();
_keyIdxList.Clear();
_param = "";
_isPer = false;
ExcelRead.ReleaseAll();
}
}
}