using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
public class CheckChineseToolEditor : EditorWindow
{
private const string findFileType = “.lua”;
private List filesPathList;//对应的脚本位置信息
private List chineseList;//对应的中文列表
Dictionary<string,List> ChineseDeatailDic; //key中文字段 vaule 具体信息
Dictionary<string,List> FileDeatailDic;//key 脚本名字 vaule 具体信息
private int startNmuber=740001;
private int countNumber = 740001;
List ignoreDirectoryList = new List (){“data”,“message”,“messageCall”};
string chkeyxmlPath = “/chinesedeatail.xml”;
string filekeyxmlpath=“/filesDeatail.xml”;
string chinesetxtPath = “/chinese.txt”;
string filesNamePath = “/files.txt”;
private string checkPath;
private bool isReplace=false;
[MenuItem(“Window/Test”)]
static void Init()
{
CheckChineseToolEditor win = (CheckChineseToolEditor)EditorWindow.GetWindow(typeof(CheckChineseToolEditor));
win.Show();
}
void OnEnable()
{
checkPath = null;
}
void OnGUI()
{
EditorGUILayout.TextField (“地址”, checkPath);
if (GUILayout.Button(“选择需要的文件夹”,GUILayout.Width(200)))
{
string dirStr = EditorUtility.OpenFolderPanel(“选择文件夹”, Application.dataPath, “”);
Debug.Log(“选择文件夹地址” + dirStr);
checkPath = dirStr;
//Check (dirStr);
}
if (GUILayout.Button("选择需要的文件",GUILayout.Width(200)))
{
string fileStr = EditorUtility.OpenFilePanel("选择文件",Application.dataPath,"");
Debug.Log("选择文件地址" + fileStr);
checkPath = fileStr;
//Check (fileStr);
}
//isReplace = GUI.Toggle(new Rect(10, 10, 100, 30), isReplace, "是否替换");
isReplace=GUILayout.Toggle (isReplace,"是否替换");
startNmuber = int.Parse (EditorGUILayout.TextField ("起始id", startNmuber.ToString ()));
if (GUILayout.Button("开始导出",GUILayout.Width(200)))
{
if (checkPath != null) {
Check (checkPath);
Debug.LogWarning ("生成成功");
} else
{
Debug.LogError ("路径没有选择");
}
}
}
//输入文件地址
private void Check(string rootPath)
{
//startNmuber =70000;
countNumber=startNmuber;
filesPathList = new List<string> ();
chineseList = new List<string> ();
ChineseDeatailDic = new Dictionary<string, List<chineseInfo>> ();
FileDeatailDic = new Dictionary<string, List<chineseInfo>> ();
if (Directory.Exists (rootPath)) {
FindFileInDirFuncTwo (rootPath, ref filesPathList);
foreach (string filepath in filesPathList) {
string fileContent = ReadFile (filepath);
Debug.Log ("读取文件内容:" + fileContent);
DealData (fileContent, filepath);
}
} else if (File.Exists (rootPath))
{
filesPathList.Add (rootPath.Replace(@"\",@"/"));//rootPath.Substring(rootPath.IndexOf("Assets")
string fileContent =ReadFile (rootPath);
Debug.Log ("读取文件内容:" + fileContent);
DealData (fileContent, rootPath);
}
Debug.Log ("总共中文个数" + chineseList.Count);
WriteListToTxtLanuage (chineseList,Application.dataPath+chinesetxtPath);
WriteListToTxtFile (filesPathList,Application.dataPath+filesNamePath);
ExportToXML (Application.dataPath+chkeyxmlPath,ChineseDeatailDic);
ExportToFilesXML (Application.dataPath+filekeyxmlpath,FileDeatailDic);
}
private void FindFileInDir(string rootdirPath,ref List<string> dirs)
{
if (Directory.Exists(rootdirPath))
{
DirectoryInfo dirinfo = new DirectoryInfo(rootdirPath);
FileInfo[] files = dirinfo.GetFiles("*",SearchOption.AllDirectories);
if (files.Length > 0)
{
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(findFileType))
{
dirs.Add(files[i].Name);
}
}
}
}
}
private void FindFileInDirFuncTwo(string dirPath,ref List<string> dirs)
{
if (Directory.Exists(dirPath))
{
foreach (string path in Directory.GetFiles (dirPath))
{
if(System.IO.Path.GetExtension(path) == findFileType)
{
string temppath=path.Replace(@"\",@"/");//Substring(path.IndexOf("Assets"))
dirs.Add(temppath);
//Debug.Log("2--" + temppath);
}
}
if (Directory.GetDirectories(dirPath).Length > 0) //遍历所有文件夹
{
foreach (string path in Directory.GetDirectories(dirPath))
{
string tempdir = path.Replace (@"\", @"/");
string[] t=tempdir.Split ('/');
if (t.Length > 0)
{
tempdir = t [t.Length - 1];
if (ignoreDirectoryList.Contains (tempdir))
{
continue;
}
}
FindFileInDirFuncTwo(path, ref dirs);
}
}
}
}
public string ReadFile(string path)
{
using (StreamReader file = new StreamReader(path))
{
string fileContents = file.ReadToEnd();
file.Close();
return fileContents;
}
}
public void DealData(string fileContents,string filepath)
{
filepath=filepath.Replace(@"\",@"/");
string[] strs = fileContents.Split('\n');
// Debug.Log("行数="+strs.Length);
Regex rg = new Regex("\"([^\"]*)\"", RegexOptions.Singleline);// new Regex("\"([^\"]*)\"", ||
Regex chrg = new Regex ("[\u4e00-\u9fa5]+");//至少有一個中文
Regex isdebug=new Regex("Log");
for (int i = 0; i < strs.Length; i++)
{
// Debug.Log("行数" + i + ",内容" + strs[i]);
MatchCollection mc = rg.Matches(strs[i]);
if (mc.Count > 0 && !isdebug.IsMatch(strs[i])) //&& chrg.IsMatch(strs[i])
{
for (int j = 0; j < mc.Count; j++)
{
if (!chrg.IsMatch (mc [j].Value))
{
continue;
}
chineseInfo temch = new chineseInfo ();
temch.line = i;
temch.fileName = filepath;
string ch = mc [j].Value;
temch.chineseStr = ch;
if (!chineseList.Contains (ch))//key chinesestr
{
temch.lanId = countNumber;
chineseList.Add (ch);
List<chineseInfo> tenpinfolist = new List<chineseInfo> ();
tenpinfolist.Add (temch);
ChineseDeatailDic.Add (ch, tenpinfolist);
countNumber++;
} else
{
List<chineseInfo> tenpinfolist=ChineseDeatailDic[ch];
temch.lanId = tenpinfolist[0].lanId;
tenpinfolist.Add (temch);
ChineseDeatailDic [ch] = tenpinfolist;
}
if (!FileDeatailDic.ContainsKey (filepath))//key filename
{
List<chineseInfo> tempchinList = new List<chineseInfo> ();
tempchinList.Add (temch);
FileDeatailDic.Add (filepath, tempchinList);
} else
{
List<chineseInfo> tempchinList = FileDeatailDic [filepath];
tempchinList.Add (temch);
FileDeatailDic [filepath] = tempchinList;
}
if (isReplace)
{
string newword=string.Format ("_localization({0})",temch.lanId );
//Regex myRegx = new Regex (ch,RegexOptions.IgnoreCase);
//fileContents=myRegx.Replace (fileContents,newword);
fileContents=fileContents.Replace(ch,newword);
}
}
}
}
//Debug.Log ("new---"+fileContents);
if (isReplace)
{
WriteFile (filepath, fileContents);
}
/*Regex rg = new Regex("\"([^\"]*)\"", RegexOptions.Singleline);
MatchCollection mc = rg.Matches(fileContents);
for (int i = 0; i < mc.Count; i++)
{
Debug.Log(mc[i].Value);
}*/
}
public void ReplaceWorld(ref string fileContents,string origword,string lanid)
{
string str = "_localization()";
string newword=string.Format (str + "{0}", lanid);
fileContents.Replace (origword, newword);
}
public void WriteFile(string path, string con)
{
string str = con;
FileStream fs=new FileStream(path,FileMode.Create,FileAccess.Write);
//若是直接在文件里添加数据,则采用重写方法,将append设置为true
var file = new StreamWriter(fs);
//var file = new StreamWriter(path);
//file.WriteLine(str);
Debug.Log("写入文件"+path);
file.Write(str);
file.Flush ();
file.Close();
fs.Close();
}
public void WriteListToTxtFile(List<string> mylist, string txtpath)
{
FileStream fs=new FileStream(txtpath,FileMode.OpenOrCreate,FileAccess.Write);
var sw = new StreamWriter(fs);
for (int i = 0; i < mylist.Count; i++) {
string str = mylist[i];
//若是直接在文件里添加数据,则采用重写方法,将append设置为true
//var file = new StreamWriter(path);
sw.WriteLine(str);
}
sw.Flush();
sw.Close();
fs.Close();
}
public void WriteListToTxtLanuage(List<string> mylist, string txtpath)
{
FileStream fs=new FileStream(txtpath,FileMode.OpenOrCreate,FileAccess.Write);
var sw = new StreamWriter(fs);
sw.WriteLine("总共数量="+mylist.Count);
for (int i = 0; i < mylist.Count; i++) {
string str = mylist[i];
string mylanuageId = "不存在";
//若是直接在文件里添加数据,则采用重写方法,将append设置为true
//var file = new StreamWriter(path);
if (ChineseDeatailDic.ContainsKey(str))
{
if (ChineseDeatailDic [str].Count > 0)
{
mylanuageId=ChineseDeatailDic [str] [0].lanId.ToString ();
}
}
sw.WriteLine("id="+mylanuageId+" content="+str);
}
sw.Flush();
sw.Close();
fs.Close();
}
public void ExportToXML(string path,Dictionary<string,List<chineseInfo>> dicList)
{
if (!File.Exists (path)) {
FileStream fs = new FileStream (path, FileMode.Create, FileAccess.ReadWrite); //创建文件
fs.Close ();
}
XmlDocument xml = new XmlDocument();
XmlElement root = xml.CreateElement("root");
foreach (string chineseitem in dicList.Keys)
{
XmlElement ele = xml.CreateElement("chineseStr");
ele.SetAttribute("chinese", chineseitem);
ele.SetAttribute("languageId", dicList [chineseitem][0].lanId.ToString());
List<chineseInfo> tempchlist = dicList [chineseitem];
foreach (chineseInfo config in tempchlist)
{
XmlElement conEle = xml.CreateElement("listinfo");
conEle.SetAttribute("line", config.line.ToString());
conEle.SetAttribute("fileName", config.fileName);
ele.AppendChild(conEle);
}
root.AppendChild(ele);
}
xml.AppendChild(root);
xml.Save(path);
}
public void ExportToFilesXML(string path,Dictionary<string,List<chineseInfo>> dicList)
{
if (!File.Exists (path)) {
FileStream fs = new FileStream (path, FileMode.Create, FileAccess.ReadWrite); //创建文件
fs.Close ();
}
XmlDocument xml = new XmlDocument();
XmlElement root = xml.CreateElement("root");
foreach (string chineseitem in dicList.Keys)
{
XmlElement ele = xml.CreateElement("script");
ele.SetAttribute("scNmae", chineseitem);
List<chineseInfo> tempchlist = dicList [chineseitem];
foreach (chineseInfo config in tempchlist)
{
XmlElement conEle = xml.CreateElement("listinfo");
conEle.SetAttribute("chinese", config.chineseStr);
conEle.SetAttribute("line", config.line.ToString());
conEle.SetAttribute("languageId", config.lanId.ToString());
ele.AppendChild(conEle);
}
root.AppendChild(ele);
}
xml.AppendChild(root);
xml.Save(path);
}
}
public class chineseInfo
{
public int line;
public string chineseStr;
public string fileName;
public int lanId;
}