Main/Assets/Editor/DIY/FormTools/ModifyUIEventDefine.cs

105 lines
3.7 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
using System.Collections.Generic;
using System.IO;
namespace Thousandto.DIY
{
/// <summary>
/// 修改UIEventDefine.cs文件添加open和close的事件定义
/// </summary>
public static class ModifyUIEventDefine
{
public const string UIEVENT_FILE_PATH = "Assets/Code/Global/Event/UIEventDefine.cs";
public static void ModifyFile(string formName, int uiconfigID)
{
string openEventDefine = string.Format("{0}_OPEN", formName);
string openEventValue = string.Format("{0} + EventConstDefine.EVENT_UI_BASE_ID,", uiconfigID * 10);
string closeEventDefine = string.Format("{0}_CLOSE", formName);
string closeEventValue = string.Format("{0} + EventConstDefine.EVENT_UI_BASE_ID,", uiconfigID * 10 + 9);
List<string> lineList = new List<string>();
List<string> endSignList = new List<string>();
StreamReader sr = new StreamReader(UIEVENT_FILE_PATH);
int changed = 0;
string line = "";
while ((line = sr.ReadLine()) != null)
{
if (line.IndexOf("}") >= 0 && line.IndexOf("//") == -1)
{
endSignList.Add(line);
continue;
}
int checkValue = checkLine(ref line, openEventDefine, openEventValue);
if (checkValue > changed)
changed = checkValue;
checkValue = checkLine(ref line, closeEventDefine, closeEventValue);
if (checkValue > changed)
changed = checkValue;
lineList.Add(line);
}
sr.Close();
//不存在,需要新增
if (changed == 0)
{
string newOpenDefine = string.Format(" {0} = {1}", openEventDefine, openEventValue);
string newCloseDefine = string.Format(" {0} = {1}", closeEventDefine, closeEventValue);
lineList.Add(newOpenDefine);
lineList.Add(newCloseDefine);
}
//不存在或者做了修改
if (changed == 0 || changed == 2)
{
lineList.AddRange(endSignList);
WriteToFile(lineList);
}
else
UnityEngine.Debug.LogError("UIEventDefine已经定义窗体Open和Close事件 " + formName);
}
/// <summary>
/// 检查form的事件定义是否存在如果存在则判断是否需要修改
/// </summary>
/// <param name="line">已知行</param>
/// <param name="define">事件定义</param>
/// <param name="value">事件值</param>
/// <returns>0不存在 1 存在但相同; 2存在且不同</returns>
private static int checkLine(ref string line, string define, string value)
{
int ret = 0;
if (line.IndexOf(define) >= 0)
{
if (line.IndexOf(value) == -1)
{
line = string.Format(" {0} = {1}", define, value);
ret = 2;
}
else
ret = 1;
}
return ret;
}
private static void WriteToFile(List<string> contentList)
{
if (contentList == null || contentList.Count == 0) return;
StreamWriter sw = new StreamWriter(UIEVENT_FILE_PATH);
for (int i = 0; i < contentList.Count; ++i)
{
sw.WriteLine(contentList[i]);
}
sw.Close();
UnityEngine.Debug.LogError("保存到UIEventDefine成功");
}
}
}