EditorLanguageExport.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Excel;
  7. using System.Data;
  8. using System.Xml;
  9. using System.Threading;
  10. public class EditorLanguageExport : EditorWindow
  11. {
  12. [MenuItem("DashGame/Design Tools/Export Language")]
  13. public static void AddWindow()
  14. {
  15. EditorLanguageExport window = (EditorLanguageExport)EditorWindow.GetWindow(typeof(EditorLanguageExport), false, "Export Language");
  16. window.Show();
  17. }
  18. public List<string> nameArray = new List<string>();
  19. private List<string> defaultPathes;
  20. private List<string> exportPathes;
  21. private bool shouldDelete = true;
  22. private bool isSuccess;
  23. private string text;
  24. private Dictionary<string, XmlNode> parentNodeDict;
  25. private string[] avaliableLan = new string[] { "English", "ChineseSimplified", "ChineseTraditional"};
  26. void Awake()
  27. {
  28. defaultPathes = new List<string>();
  29. defaultPathes.Add(Application.dataPath + @"/Resource/Xlsx/language_config.xlsx");
  30. defaultPathes.Add(Application.dataPath + @"/Resource/Xlsx/new_language_config.xlsx");
  31. exportPathes = new List<string>();
  32. exportPathes.Add(Application.dataPath + @"/Resource/xml/lan/" + "#NAME" + ".xml");
  33. exportPathes.Add(Application.dataPath + @"/Resource/xml/lan/" + "new_#NAME" + ".xml");
  34. }
  35. void OnGUI()
  36. {
  37. GUILayout.Label(new GUIContent("导出文件到xml目录"));
  38. shouldDelete = GUILayout.Toggle(shouldDelete, new GUIContent("覆盖已有文件"));
  39. if (GUILayout.Button("立即导出", GUILayout.Height(30)))
  40. CreateAllXml();
  41. }
  42. /// <summary>
  43. /// custom split string function
  44. /// </summary>
  45. private class StringExtention
  46. {
  47. public static string[] SplitWithString(string sourceString, string splitString)
  48. {
  49. // string tempSourceString = sourceString;
  50. List<string> arrayList = new List<string>();
  51. string s = string.Empty;
  52. while (sourceString.IndexOf(splitString) > -1) //split
  53. {
  54. s = sourceString.Substring(0, sourceString.IndexOf(splitString));
  55. sourceString = sourceString.Substring(sourceString.IndexOf(splitString) + splitString.Length);
  56. arrayList.Add(s);
  57. }
  58. arrayList.Add(sourceString);
  59. return arrayList.ToArray();
  60. }
  61. }
  62. private void CreateAllXml()
  63. {
  64. // foreach (string str in nameArray)
  65. // {
  66. // if (!str.Contains("language"))
  67. // continue;
  68. //
  69. // CreateXml(str);
  70. // }
  71. foreach( string s in avaliableLan) {
  72. CreateXml(defaultPathes[0], exportPathes[0], s);
  73. CreateXml(defaultPathes[1], exportPathes[1], s);
  74. }
  75. if (isSuccess)
  76. {
  77. ShowNotification(new GUIContent( "已成功导出!"));
  78. //DirectoryInfo di = new DirectoryInfo(defaultPath);
  79. //DirectoryInfo[] diArr = di.GetDirectories();
  80. isSuccess = false;
  81. AssetDatabase.Refresh();
  82. }
  83. else
  84. ShowNotification(new GUIContent("文件有错误!"));
  85. }
  86. private void CreateXml(string xlsxPath, string xmlPath, string lan)
  87. {
  88. parentNodeDict = new Dictionary<string, XmlNode>();
  89. string filepath = xmlPath.Replace("#NAME", lan);
  90. //string filepath = Application.dataPath + @"/Resource/xml/lan/" + lan + ".xml";
  91. text = "";
  92. filepath = filepath.Replace("\\", "/");
  93. string[] pathArr = filepath.Split('/');
  94. string tempPath = "";
  95. for(int i = 0; i < pathArr.Length; i++)
  96. {
  97. tempPath += pathArr[i] + "/";
  98. if (i > 1 && i < pathArr.Length - 1)
  99. {
  100. if (!Directory.Exists(tempPath))
  101. {
  102. Directory.CreateDirectory(tempPath);
  103. }
  104. }
  105. }
  106. if ((!File.Exists(filepath) && !shouldDelete) || shouldDelete)
  107. {
  108. XmlDocument xmlDoc = new XmlDocument();
  109. //try
  110. //{
  111. FileStream stream = File.Open(xlsxPath, FileMode.Open, FileAccess.Read);
  112. IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  113. text += "\nexcelReader.ResultsCount is" + excelReader.ResultsCount + "\n";
  114. text += "start excelReader. \n";
  115. DataSet result = excelReader.AsDataSet();
  116. text += "get result successful? result[" + result + "]";
  117. text += "result columns count is " + result.Tables[0].Columns.Count;
  118. int columns = result.Tables[0].Columns.Count;
  119. int rows = result.Tables[0].Rows.Count;
  120. //start create xml
  121. XmlElement root = xmlDoc.CreateElement("lan");
  122. XmlNode currentChildNode = null;
  123. string parentNodeName = "";
  124. int parentidex = -1;
  125. int nameindex = -1;
  126. int contentindex = -1;
  127. int descindex = -1;
  128. // get column index
  129. for (int j = 0; j < columns; ++j) {
  130. string nvalue = result.Tables[0].Rows[1][j].ToString();
  131. if (string.Equals(nvalue, "parent")) {
  132. parentidex = j;
  133. } else if (string.Equals(nvalue, "desc")) {
  134. descindex = j;
  135. } else if (string.Equals(nvalue, "name")) {
  136. nameindex = j;
  137. } else if (string.Equals(nvalue, lan)) {
  138. contentindex = j;
  139. }
  140. }
  141. if (contentindex < 0) {
  142. throw new Exception("language " + lan + " is not exist in the excel");
  143. }
  144. for (int i = 3; i < rows; i++)
  145. {
  146. // parent
  147. string nvalue = result.Tables[0].Rows[i][parentidex].ToString();
  148. if (string.IsNullOrEmpty(nvalue))
  149. {
  150. break;
  151. }
  152. if (!parentNodeDict.ContainsKey(nvalue))
  153. {
  154. XmlNode node = xmlDoc.CreateElement(nvalue);
  155. parentNodeDict.Add(nvalue, node);
  156. }
  157. parentNodeName = nvalue;
  158. // name
  159. nvalue = result.Tables[0].Rows[i][nameindex].ToString();
  160. currentChildNode = xmlDoc.CreateElement(nvalue);
  161. parentNodeDict[parentNodeName].AppendChild(currentChildNode);
  162. // content
  163. nvalue = result.Tables[0].Rows[i][contentindex].ToString();
  164. XmlCDataSection cData = xmlDoc.CreateCDataSection(nvalue);
  165. currentChildNode.AppendChild(cData);
  166. // desc
  167. nvalue = result.Tables[0].Rows[i][descindex].ToString();
  168. XmlAttribute comment = xmlDoc.CreateAttribute("desc");
  169. comment.Value = nvalue;
  170. currentChildNode.Attributes.Append(comment);
  171. // for (int j = 0; j < columns; j++)
  172. // {
  173. // string nvalue = result.Tables[0].Rows[i][j].ToString();
  174. //
  175. // switch (j)
  176. // {
  177. // case 0:
  178. // if (!parentNodeDict.ContainsKey(nvalue))
  179. // {
  180. // XmlNode node = xmlDoc.CreateElement(nvalue);
  181. // parentNodeDict.Add(nvalue, node);
  182. // }
  183. // parentNodeName = nvalue;
  184. // break;
  185. // case 1:
  186. // currentChildNode = xmlDoc.CreateElement(nvalue);
  187. // parentNodeDict[parentNodeName].AppendChild(currentChildNode);
  188. // break;
  189. // case 2:
  190. // XmlCDataSection cData = xmlDoc.CreateCDataSection(nvalue);
  191. // currentChildNode.AppendChild(cData);
  192. // break;
  193. // case 3:
  194. // XmlAttribute comment = xmlDoc.CreateAttribute("desc");
  195. // comment.Value = nvalue;
  196. // currentChildNode.Attributes.Append(comment);
  197. // break;
  198. // }
  199. //
  200. // //Debug.Log("labels[j] is " + labels[j] + ", nvalue is " + nvalue);
  201. // //Debug.Log(nvalue);
  202. // }
  203. }
  204. foreach(KeyValuePair<string, XmlNode> keyValue in parentNodeDict)
  205. root.AppendChild(keyValue.Value);
  206. xmlDoc.AppendChild(root);
  207. xmlDoc.Save(filepath);
  208. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  209. // AssetDatabase.ImportAsset(@"Assets/Resources/XML/Config" + fileName + ".xml", ImportAssetOptions.ForceUpdate);
  210. //Debug.Log(fileName + ".xml is saved to " + filepath + ", count is " + idArr.Count);
  211. isSuccess = true;
  212. //}
  213. //catch (Exception e)
  214. //{
  215. // text += "Exception " + e.Message;
  216. // Debug.Log("Exception " + e.Message);
  217. //}
  218. }
  219. }
  220. private void CreateAllPhp()
  221. {
  222. foreach (string str in nameArray)
  223. {
  224. if (!str.Contains("language"))
  225. continue;
  226. CreatePhp(str);
  227. }
  228. if (isSuccess)
  229. {
  230. ShowNotification(new GUIContent("已成功导出!"));
  231. //DirectoryInfo di = new DirectoryInfo(defaultPath);
  232. //DirectoryInfo[] diArr = di.GetDirectories();
  233. isSuccess = false;
  234. }
  235. else
  236. ShowNotification(new GUIContent("文件有错误!"));
  237. }
  238. private void CreatePhp(string fileName)
  239. {
  240. string realName = StringExtention.SplitWithString(fileName, "\\")[1];
  241. string filepath = defaultPathes + "/" + realName + ".php";
  242. text = "";
  243. filepath = filepath.Replace("\\", "/");
  244. string[] pathArr = filepath.Split('/');
  245. string tempPath = "";
  246. for (int i = 0; i < pathArr.Length; i++)
  247. {
  248. tempPath += pathArr[i] + "/";
  249. if (i > 1 && i < pathArr.Length - 1)
  250. {
  251. if (!Directory.Exists(tempPath))
  252. {
  253. Directory.CreateDirectory(tempPath);
  254. }
  255. }
  256. }
  257. if ((!File.Exists(filepath) && !shouldDelete) || shouldDelete)
  258. {
  259. try
  260. {
  261. string fullText = "<?php\n";
  262. FileStream stream = File.Open(Application.dataPath + @"/Resource/Xlsx" + fileName + ".xlsx", FileMode.Open, FileAccess.Read);
  263. IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  264. text += "\nexcelReader.ResultsCount is" + excelReader.ResultsCount + "\n";
  265. text += "start excelReader. \n";
  266. DataSet result = excelReader.AsDataSet();
  267. text += "get result successful? result[" + result + "]";
  268. text += "result columns count is " + result.Tables[0].Columns.Count;
  269. int columns = result.Tables[0].Columns.Count;
  270. int rows = result.Tables[0].Rows.Count;
  271. //start create xml
  272. for (int i = 0; i < rows; i++)
  273. {
  274. if (i == 0)
  275. {
  276. continue;
  277. }
  278. for (int j = 0; j < columns; j++)
  279. {
  280. string nvalue = result.Tables[0].Rows[i][j].ToString();
  281. if (i > 2)
  282. {
  283. switch (j)
  284. {
  285. case 0:
  286. fullText += "$LANG['" + nvalue + "|";
  287. break;
  288. case 1:
  289. fullText += nvalue + "'] = '";
  290. break;
  291. case 2:
  292. nvalue = nvalue.Replace("\'", "\\\'");
  293. fullText += nvalue + "\';\n";
  294. break;
  295. }
  296. }
  297. }
  298. }
  299. fullText += "?>";
  300. StreamWriter sw = new StreamWriter(filepath);
  301. string w = fullText;
  302. sw.Write(w);
  303. sw.Close();
  304. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  305. isSuccess = true;
  306. }
  307. catch (Exception e)
  308. {
  309. text += "Exception " + e.Message;
  310. Debug.Log("Exception " + e.Message);
  311. }
  312. }
  313. }
  314. private void HideNotification()
  315. {
  316. this.RemoveNotification();
  317. }
  318. void OnInspectorUpdate()
  319. {
  320. this.Repaint();
  321. }
  322. void OnDestory()
  323. {
  324. EditorUtility.UnloadUnusedAssetsImmediate();
  325. }
  326. }
  327. public class LanguageUploadData
  328. {
  329. public string sourcePath;
  330. public string targetUrl;
  331. public string GetCmd()
  332. {
  333. string command = "@echo off\r\n" +
  334. "echo open " + targetUrl + ">ftp.up\r\n" +
  335. "echo gaoyuqin>>ftp.up\r\n" +
  336. "echo Gaoyuqin123654>>ftp.up\r\n" +
  337. //"echo Cd .\\User >>ftp.up\r\n" +
  338. "echo binary>>ftp.up\r\n" +
  339. "echo lcd \"" + sourcePath + "\">>ftp.up\r\n" +
  340. "echo prompt>>ftp.up\r\n" +
  341. "echo mkdir qwsk/lang/>>ftp.up\r\n" +
  342. "echo cd qwsk/lang/>>ftp.up\r\n" +
  343. "echo mput *.php>>ftp.up\r\n" +
  344. "echo bye>>ftp.up\r\n" +
  345. "FTP -s:ftp.up\r\n" +
  346. "del ftp.up /q\r\n" +
  347. "pause\r\n";
  348. return command;
  349. }
  350. }
  351. public class LanguageUploadFiles
  352. {
  353. private Dictionary<string, LanguageUploadData> data;
  354. private Thread s;
  355. public LanguageUploadFiles(Dictionary<string, LanguageUploadData> data)
  356. {
  357. this.data = data;
  358. s = new Thread(Run);
  359. s.Start();
  360. }
  361. private void Run()
  362. {
  363. foreach (string key in data.Keys)
  364. {
  365. LanguageUploadData uploadData = data[key];
  366. RunCmd(key, uploadData.GetCmd());
  367. Debug.Log("【RunCmd】" + uploadData.sourcePath + " upload to---- 【 " + uploadData.targetUrl + " 】");
  368. }
  369. s.Abort();
  370. }
  371. private void RunCmd(string key, string command)
  372. {
  373. Debug.Log(command);
  374. File.WriteAllText(key + "-upload-language.bat", command, System.Text.Encoding.GetEncoding(936));
  375. System.Diagnostics.Process.Start(key + "-upload-language.bat");
  376. }
  377. }