using UnityEditor; using UnityEngine; using System; using System.Collections.Generic; using System.IO; using Excel; using System.Data; using System.Xml; using System.Threading; public class EditorLanguageExport : EditorWindow { [MenuItem("DashGame/Design Tools/Export Language")] public static void AddWindow() { EditorLanguageExport window = (EditorLanguageExport)EditorWindow.GetWindow(typeof(EditorLanguageExport), false, "Export Language"); window.Show(); } public List nameArray = new List(); private string defaultPath; private bool shouldDelete = true; private bool isSuccess; private string text; private Dictionary parentNodeDict; private string[] avaliableLan = new string[] { "English", "ChineseSimplified", "ChineseTraditional"}; void Awake() { defaultPath = Application.dataPath + @"/Resource/Xlsx/language_config.xlsx"; } void OnGUI() { GUILayout.Label(new GUIContent("导出文件到xml目录")); shouldDelete = GUILayout.Toggle(shouldDelete, new GUIContent("覆盖已有文件")); if (GUILayout.Button("立即导出", GUILayout.Height(30))) CreateAllXml(); } /// /// custom split string function /// private class StringExtention { public static string[] SplitWithString(string sourceString, string splitString) { // string tempSourceString = sourceString; List arrayList = new List(); string s = string.Empty; while (sourceString.IndexOf(splitString) > -1) //split { s = sourceString.Substring(0, sourceString.IndexOf(splitString)); sourceString = sourceString.Substring(sourceString.IndexOf(splitString) + splitString.Length); arrayList.Add(s); } arrayList.Add(sourceString); return arrayList.ToArray(); } } private void CreateAllXml() { // foreach (string str in nameArray) // { // if (!str.Contains("language")) // continue; // // CreateXml(str); // } foreach( string s in avaliableLan) { CreateXml(s); } if (isSuccess) { ShowNotification(new GUIContent( "已成功导出!")); //DirectoryInfo di = new DirectoryInfo(defaultPath); //DirectoryInfo[] diArr = di.GetDirectories(); isSuccess = false; AssetDatabase.Refresh(); } else ShowNotification(new GUIContent("文件有错误!")); } private void CreateXml(string lan) { parentNodeDict = new Dictionary(); string filepath = Application.dataPath + @"/Resource/xml/lan/" + lan + ".xml"; text = ""; filepath = filepath.Replace("\\", "/"); string[] pathArr = filepath.Split('/'); string tempPath = ""; for(int i = 0; i < pathArr.Length; i++) { tempPath += pathArr[i] + "/"; if (i > 1 && i < pathArr.Length - 1) { if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } } } if ((!File.Exists(filepath) && !shouldDelete) || shouldDelete) { XmlDocument xmlDoc = new XmlDocument(); //try //{ FileStream stream = File.Open(defaultPath, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); text += "\nexcelReader.ResultsCount is" + excelReader.ResultsCount + "\n"; text += "start excelReader. \n"; DataSet result = excelReader.AsDataSet(); text += "get result successful? result[" + result + "]"; text += "result columns count is " + result.Tables[0].Columns.Count; int columns = result.Tables[0].Columns.Count; int rows = result.Tables[0].Rows.Count; //start create xml XmlElement root = xmlDoc.CreateElement("lan"); XmlNode currentChildNode = null; string parentNodeName = ""; int parentidex = -1; int nameindex = -1; int contentindex = -1; int descindex = -1; // get column index for (int j = 0; j < columns; ++j) { string nvalue = result.Tables[0].Rows[1][j].ToString(); if (string.Equals(nvalue, "parent")) { parentidex = j; } else if (string.Equals(nvalue, "desc")) { descindex = j; } else if (string.Equals(nvalue, "name")) { nameindex = j; } else if (string.Equals(nvalue, lan)) { contentindex = j; } } if (contentindex < 0) { throw new Exception("language " + lan + " is not exist in the excel"); } for (int i = 3; i < rows; i++) { // parent string nvalue = result.Tables[0].Rows[i][parentidex].ToString(); if (string.IsNullOrEmpty(nvalue)) { break; } if (!parentNodeDict.ContainsKey(nvalue)) { XmlNode node = xmlDoc.CreateElement(nvalue); parentNodeDict.Add(nvalue, node); } parentNodeName = nvalue; // name nvalue = result.Tables[0].Rows[i][nameindex].ToString(); currentChildNode = xmlDoc.CreateElement(nvalue); parentNodeDict[parentNodeName].AppendChild(currentChildNode); // content nvalue = result.Tables[0].Rows[i][contentindex].ToString(); XmlCDataSection cData = xmlDoc.CreateCDataSection(nvalue); currentChildNode.AppendChild(cData); // desc nvalue = result.Tables[0].Rows[i][descindex].ToString(); XmlAttribute comment = xmlDoc.CreateAttribute("desc"); comment.Value = nvalue; currentChildNode.Attributes.Append(comment); // for (int j = 0; j < columns; j++) // { // string nvalue = result.Tables[0].Rows[i][j].ToString(); // // switch (j) // { // case 0: // if (!parentNodeDict.ContainsKey(nvalue)) // { // XmlNode node = xmlDoc.CreateElement(nvalue); // parentNodeDict.Add(nvalue, node); // } // parentNodeName = nvalue; // break; // case 1: // currentChildNode = xmlDoc.CreateElement(nvalue); // parentNodeDict[parentNodeName].AppendChild(currentChildNode); // break; // case 2: // XmlCDataSection cData = xmlDoc.CreateCDataSection(nvalue); // currentChildNode.AppendChild(cData); // break; // case 3: // XmlAttribute comment = xmlDoc.CreateAttribute("desc"); // comment.Value = nvalue; // currentChildNode.Attributes.Append(comment); // break; // } // // //Debug.Log("labels[j] is " + labels[j] + ", nvalue is " + nvalue); // //Debug.Log(nvalue); // } } foreach(KeyValuePair keyValue in parentNodeDict) root.AppendChild(keyValue.Value); xmlDoc.AppendChild(root); xmlDoc.Save(filepath); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); // AssetDatabase.ImportAsset(@"Assets/Resources/XML/Config" + fileName + ".xml", ImportAssetOptions.ForceUpdate); //Debug.Log(fileName + ".xml is saved to " + filepath + ", count is " + idArr.Count); isSuccess = true; //} //catch (Exception e) //{ // text += "Exception " + e.Message; // Debug.Log("Exception " + e.Message); //} } } private void CreateAllPhp() { foreach (string str in nameArray) { if (!str.Contains("language")) continue; CreatePhp(str); } if (isSuccess) { ShowNotification(new GUIContent("已成功导出!")); //DirectoryInfo di = new DirectoryInfo(defaultPath); //DirectoryInfo[] diArr = di.GetDirectories(); isSuccess = false; } else ShowNotification(new GUIContent("文件有错误!")); } private void CreatePhp(string fileName) { string realName = StringExtention.SplitWithString(fileName, "\\")[1]; string filepath = defaultPath + "/" + realName + ".php"; text = ""; filepath = filepath.Replace("\\", "/"); string[] pathArr = filepath.Split('/'); string tempPath = ""; for (int i = 0; i < pathArr.Length; i++) { tempPath += pathArr[i] + "/"; if (i > 1 && i < pathArr.Length - 1) { if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } } } if ((!File.Exists(filepath) && !shouldDelete) || shouldDelete) { try { string fullText = " 2) { switch (j) { case 0: fullText += "$LANG['" + nvalue + "|"; break; case 1: fullText += nvalue + "'] = '"; break; case 2: nvalue = nvalue.Replace("\'", "\\\'"); fullText += nvalue + "\';\n"; break; } } } } fullText += "?>"; StreamWriter sw = new StreamWriter(filepath); string w = fullText; sw.Write(w); sw.Close(); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); isSuccess = true; } catch (Exception e) { text += "Exception " + e.Message; Debug.Log("Exception " + e.Message); } } } private void HideNotification() { this.RemoveNotification(); } void OnInspectorUpdate() { this.Repaint(); } void OnDestory() { EditorUtility.UnloadUnusedAssetsImmediate(); } } public class LanguageUploadData { public string sourcePath; public string targetUrl; public string GetCmd() { string command = "@echo off\r\n" + "echo open " + targetUrl + ">ftp.up\r\n" + "echo gaoyuqin>>ftp.up\r\n" + "echo Gaoyuqin123654>>ftp.up\r\n" + //"echo Cd .\\User >>ftp.up\r\n" + "echo binary>>ftp.up\r\n" + "echo lcd \"" + sourcePath + "\">>ftp.up\r\n" + "echo prompt>>ftp.up\r\n" + "echo mkdir qwsk/lang/>>ftp.up\r\n" + "echo cd qwsk/lang/>>ftp.up\r\n" + "echo mput *.php>>ftp.up\r\n" + "echo bye>>ftp.up\r\n" + "FTP -s:ftp.up\r\n" + "del ftp.up /q\r\n" + "pause\r\n"; return command; } } public class LanguageUploadFiles { private Dictionary data; private Thread s; public LanguageUploadFiles(Dictionary data) { this.data = data; s = new Thread(Run); s.Start(); } private void Run() { foreach (string key in data.Keys) { LanguageUploadData uploadData = data[key]; RunCmd(key, uploadData.GetCmd()); Debug.Log("【RunCmd】" + uploadData.sourcePath + " upload to---- 【 " + uploadData.targetUrl + " 】"); } s.Abort(); } private void RunCmd(string key, string command) { Debug.Log(command); File.WriteAllText(key + "-upload-language.bat", command, System.Text.Encoding.GetEncoding(936)); System.Diagnostics.Process.Start(key + "-upload-language.bat"); } }