123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- 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<string> nameArray = new List<string>();
- private string defaultPath;
- private bool shouldDelete = true;
- private bool isSuccess;
- private string text;
- private Dictionary<string, XmlNode> parentNodeDict;
- private string[] avaliableLan = new string[] { "English", "ChineseSimplified", "ChineseTraditional"};
-
- void Awake()
- {
- defaultPath = Application.dataPath + @"/XlsxSource/language_config.xlsx";
- }
- void OnGUI()
- {
- GUILayout.Label(new GUIContent("导出文件到xml目录"));
- shouldDelete = GUILayout.Toggle(shouldDelete, new GUIContent("覆盖已有文件"));
- if (GUILayout.Button("立即导出", GUILayout.Height(30)))
- CreateAllXml();
- }
- /// <summary>
- /// custom split string function
- /// </summary>
- private class StringExtention
- {
- public static string[] SplitWithString(string sourceString, string splitString)
- {
- // string tempSourceString = sourceString;
- List<string> arrayList = new List<string>();
- 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, XmlNode>();
- string filepath = Application.dataPath + @"/Resources/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 (!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<string, XmlNode> 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 = "<?php\n";
- FileStream stream = File.Open(Application.dataPath + @"/XlsxSource" + fileName + ".xlsx", 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
- for (int i = 0; i < rows; i++)
- {
- if (i == 0)
- {
- continue;
- }
- for (int j = 0; j < columns; j++)
- {
- string nvalue = result.Tables[0].Rows[i][j].ToString();
- if (i > 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<string, LanguageUploadData> data;
- private Thread s;
- public LanguageUploadFiles(Dictionary<string, LanguageUploadData> 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");
- }
- }
|