EditorLanguageExport.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 + @"/XlsxSource/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 + @"/Resources/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 (!parentNodeDict.ContainsKey(nvalue))
  141. {
  142. XmlNode node = xmlDoc.CreateElement(nvalue);
  143. parentNodeDict.Add(nvalue, node);
  144. }
  145. parentNodeName = nvalue;
  146. // name
  147. nvalue = result.Tables[0].Rows[i][nameindex].ToString();
  148. currentChildNode = xmlDoc.CreateElement(nvalue);
  149. parentNodeDict[parentNodeName].AppendChild(currentChildNode);
  150. // content
  151. nvalue = result.Tables[0].Rows[i][contentindex].ToString();
  152. XmlCDataSection cData = xmlDoc.CreateCDataSection(nvalue);
  153. currentChildNode.AppendChild(cData);
  154. // desc
  155. nvalue = result.Tables[0].Rows[i][descindex].ToString();
  156. XmlAttribute comment = xmlDoc.CreateAttribute("desc");
  157. comment.Value = nvalue;
  158. currentChildNode.Attributes.Append(comment);
  159. // for (int j = 0; j < columns; j++)
  160. // {
  161. // string nvalue = result.Tables[0].Rows[i][j].ToString();
  162. //
  163. // switch (j)
  164. // {
  165. // case 0:
  166. // if (!parentNodeDict.ContainsKey(nvalue))
  167. // {
  168. // XmlNode node = xmlDoc.CreateElement(nvalue);
  169. // parentNodeDict.Add(nvalue, node);
  170. // }
  171. // parentNodeName = nvalue;
  172. // break;
  173. // case 1:
  174. // currentChildNode = xmlDoc.CreateElement(nvalue);
  175. // parentNodeDict[parentNodeName].AppendChild(currentChildNode);
  176. // break;
  177. // case 2:
  178. // XmlCDataSection cData = xmlDoc.CreateCDataSection(nvalue);
  179. // currentChildNode.AppendChild(cData);
  180. // break;
  181. // case 3:
  182. // XmlAttribute comment = xmlDoc.CreateAttribute("desc");
  183. // comment.Value = nvalue;
  184. // currentChildNode.Attributes.Append(comment);
  185. // break;
  186. // }
  187. //
  188. // //Debug.Log("labels[j] is " + labels[j] + ", nvalue is " + nvalue);
  189. // //Debug.Log(nvalue);
  190. // }
  191. }
  192. foreach(KeyValuePair<string, XmlNode> keyValue in parentNodeDict)
  193. root.AppendChild(keyValue.Value);
  194. xmlDoc.AppendChild(root);
  195. xmlDoc.Save(filepath);
  196. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  197. // AssetDatabase.ImportAsset(@"Assets/Resources/XML/Config" + fileName + ".xml", ImportAssetOptions.ForceUpdate);
  198. //Debug.Log(fileName + ".xml is saved to " + filepath + ", count is " + idArr.Count);
  199. isSuccess = true;
  200. }
  201. catch (Exception e)
  202. {
  203. text += "Exception " + e.Message;
  204. Debug.Log("Exception " + e.Message);
  205. }
  206. }
  207. }
  208. private void CreateAllPhp()
  209. {
  210. foreach (string str in nameArray)
  211. {
  212. if (!str.Contains("language"))
  213. continue;
  214. CreatePhp(str);
  215. }
  216. if (isSuccess)
  217. {
  218. ShowNotification(new GUIContent("已成功导出!"));
  219. //DirectoryInfo di = new DirectoryInfo(defaultPath);
  220. //DirectoryInfo[] diArr = di.GetDirectories();
  221. isSuccess = false;
  222. }
  223. else
  224. ShowNotification(new GUIContent("文件有错误!"));
  225. }
  226. private void CreatePhp(string fileName)
  227. {
  228. string realName = StringExtention.SplitWithString(fileName, "\\")[1];
  229. string filepath = defaultPath + "/" + realName + ".php";
  230. text = "";
  231. filepath = filepath.Replace("\\", "/");
  232. string[] pathArr = filepath.Split('/');
  233. string tempPath = "";
  234. for (int i = 0; i < pathArr.Length; i++)
  235. {
  236. tempPath += pathArr[i] + "/";
  237. if (i > 1 && i < pathArr.Length - 1)
  238. {
  239. if (!Directory.Exists(tempPath))
  240. {
  241. Directory.CreateDirectory(tempPath);
  242. }
  243. }
  244. }
  245. if ((!File.Exists(filepath) && !shouldDelete) || shouldDelete)
  246. {
  247. try
  248. {
  249. string fullText = "<?php\n";
  250. FileStream stream = File.Open(Application.dataPath + @"/XlsxSource" + fileName + ".xlsx", FileMode.Open, FileAccess.Read);
  251. IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  252. text += "\nexcelReader.ResultsCount is" + excelReader.ResultsCount + "\n";
  253. text += "start excelReader. \n";
  254. DataSet result = excelReader.AsDataSet();
  255. text += "get result successful? result[" + result + "]";
  256. text += "result columns count is " + result.Tables[0].Columns.Count;
  257. int columns = result.Tables[0].Columns.Count;
  258. int rows = result.Tables[0].Rows.Count;
  259. //start create xml
  260. for (int i = 0; i < rows; i++)
  261. {
  262. if (i == 0)
  263. {
  264. continue;
  265. }
  266. for (int j = 0; j < columns; j++)
  267. {
  268. string nvalue = result.Tables[0].Rows[i][j].ToString();
  269. if (i > 2)
  270. {
  271. switch (j)
  272. {
  273. case 0:
  274. fullText += "$LANG['" + nvalue + "|";
  275. break;
  276. case 1:
  277. fullText += nvalue + "'] = '";
  278. break;
  279. case 2:
  280. nvalue = nvalue.Replace("\'", "\\\'");
  281. fullText += nvalue + "\';\n";
  282. break;
  283. }
  284. }
  285. }
  286. }
  287. fullText += "?>";
  288. StreamWriter sw = new StreamWriter(filepath);
  289. string w = fullText;
  290. sw.Write(w);
  291. sw.Close();
  292. AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
  293. isSuccess = true;
  294. }
  295. catch (Exception e)
  296. {
  297. text += "Exception " + e.Message;
  298. Debug.Log("Exception " + e.Message);
  299. }
  300. }
  301. }
  302. private void HideNotification()
  303. {
  304. this.RemoveNotification();
  305. }
  306. void OnInspectorUpdate()
  307. {
  308. this.Repaint();
  309. }
  310. void OnDestory()
  311. {
  312. EditorUtility.UnloadUnusedAssetsImmediate();
  313. }
  314. }
  315. public class LanguageUploadData
  316. {
  317. public string sourcePath;
  318. public string targetUrl;
  319. public string GetCmd()
  320. {
  321. string command = "@echo off\r\n" +
  322. "echo open " + targetUrl + ">ftp.up\r\n" +
  323. "echo gaoyuqin>>ftp.up\r\n" +
  324. "echo Gaoyuqin123654>>ftp.up\r\n" +
  325. //"echo Cd .\\User >>ftp.up\r\n" +
  326. "echo binary>>ftp.up\r\n" +
  327. "echo lcd \"" + sourcePath + "\">>ftp.up\r\n" +
  328. "echo prompt>>ftp.up\r\n" +
  329. "echo mkdir qwsk/lang/>>ftp.up\r\n" +
  330. "echo cd qwsk/lang/>>ftp.up\r\n" +
  331. "echo mput *.php>>ftp.up\r\n" +
  332. "echo bye>>ftp.up\r\n" +
  333. "FTP -s:ftp.up\r\n" +
  334. "del ftp.up /q\r\n" +
  335. "pause\r\n";
  336. return command;
  337. }
  338. }
  339. public class LanguageUploadFiles
  340. {
  341. private Dictionary<string, LanguageUploadData> data;
  342. private Thread s;
  343. public LanguageUploadFiles(Dictionary<string, LanguageUploadData> data)
  344. {
  345. this.data = data;
  346. s = new Thread(Run);
  347. s.Start();
  348. }
  349. private void Run()
  350. {
  351. foreach (string key in data.Keys)
  352. {
  353. LanguageUploadData uploadData = data[key];
  354. RunCmd(key, uploadData.GetCmd());
  355. Debug.Log("【RunCmd】" + uploadData.sourcePath + " upload to---- 【 " + uploadData.targetUrl + " 】");
  356. }
  357. s.Abort();
  358. }
  359. private void RunCmd(string key, string command)
  360. {
  361. Debug.Log(command);
  362. File.WriteAllText(key + "-upload-language.bat", command, System.Text.Encoding.GetEncoding(936));
  363. System.Diagnostics.Process.Start(key + "-upload-language.bat");
  364. }
  365. }