2023-04-03 11:04:31 +08:00

111 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace BFEditor.Resource
{
public struct LuaCheckResult
{
public string content;
public string path;
public string line;
}
public static class LuaCheckController
{
private static string luaDir = Application.dataPath + "/Developer/lua";
private static string excludeDir = Application.dataPath + "/Developer/lua/app/config";
private const char DOUBLE_QUOTES = '"';
private const char SINGLE_QUOTE = '\'';
public static List<LuaCheckResult> ResultList = new List<LuaCheckResult>();
// private static Regex reg = new Regex(@".*[\u4e00-\u9fa5]+"); // 汉字
private static Regex reg = new Regex(@".*[\u0391-\uffe5]+"); //双字节字符(汉字+符号)
public static void CheckAll(Action<bool> checkOverAction)
{
Clear();
DirectoryInfo dir = new DirectoryInfo(luaDir);
FileInfo[] files = dir.GetFiles("*.lua", SearchOption.AllDirectories);
for (var i = 0; i < files.Length; ++i)
{
FileInfo fileInfo = files[i];
//跳过系统文件和隐藏文件
if (((fileInfo.Attributes & FileAttributes.Hidden) > 0) || ((fileInfo.Attributes & FileAttributes.System) > 0))
{
continue;
}
if (fileInfo.DirectoryName.Contains(excludeDir))
{
continue;
}
using (StreamReader sr = new StreamReader(fileInfo.FullName))
{
string lineContent;
int lineNum = 0;
while ((lineContent = sr.ReadLine()) != null)
{
lineNum++;
var content = lineContent.Trim();
if (content.StartsWith("--"))
{
continue;
}
Match match;
int index = content.IndexOf("--");
if(index >= 0)
{
var validContent = content.Substring(0, index);
if( (GetQuoteCount(validContent) & 1) == 0) // 偶数
{
match = reg.Match(validContent);
}
else
{
match = reg.Match(content);
}
}
else
{
match = reg.Match(content);
}
if (match != Match.Empty)
{
LuaCheckResult checkResult;
checkResult.content = content;
checkResult.line = string.Format("第{0}行:", lineNum);
checkResult.path = fileInfo.FullName;
ResultList.Add(checkResult);
}
}
}
}
if (checkOverAction != null)
checkOverAction(ResultList.Count == 0);
}
public static int GetQuoteCount(string str)
{
int count = 0;
for(int i = 0; i < str.Length; i++)
{
if(str[i] == SINGLE_QUOTE || str[i] == DOUBLE_QUOTES)
{
count++;
}
}
return count;
}
public static void Clear()
{
ResultList.Clear();
}
}
}