EditorLanguageExport.cs 15 KB

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