EditorLanguageExport.cs 15 KB

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