CsvToLua.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using UnityEditor;
  6. using System.Text;
  7. using System;
  8. public class CsvToLua : EditorWindow
  9. {
  10. public static string CSV_PATH = "csv path";
  11. public static string LUA_PATH = "lua path";
  12. public static string START_LINE = "start line";
  13. public string tableDirectoryName;
  14. public string luaDirectoryName;
  15. public int startDataLine = 1;
  16. [MenuItem("Tools/CsvToLua")]
  17. static void Init()
  18. {
  19. CsvToLua window = (CsvToLua)GetWindow(typeof(CsvToLua));
  20. window.Show();
  21. }
  22. void OnEnable()
  23. {
  24. tableDirectoryName = EditorPrefs.GetString(CSV_PATH);
  25. luaDirectoryName = EditorPrefs.GetString(LUA_PATH);
  26. startDataLine = EditorPrefs.GetInt(START_LINE);
  27. }
  28. void OnDisable()
  29. {
  30. EditorPrefs.SetString(CSV_PATH, tableDirectoryName);
  31. EditorPrefs.SetString(LUA_PATH, luaDirectoryName);
  32. EditorPrefs.SetInt(START_LINE, startDataLine);
  33. }
  34. private void OnGUI()
  35. {
  36. GUILayout.Label("Base Settings", EditorStyles.boldLabel);
  37. EditorGUILayout.BeginHorizontal();
  38. tableDirectoryName = EditorGUILayout.TextField("TableDirectory Name", tableDirectoryName);
  39. if (GUILayout.Button("选择csv路径", GUILayout.Width(200)))
  40. {
  41. tableDirectoryName = GetFilePath();
  42. }
  43. EditorGUILayout.EndHorizontal();
  44. EditorGUILayout.BeginHorizontal();
  45. luaDirectoryName = EditorGUILayout.TextField("LuaDirectory Name", luaDirectoryName);
  46. if (GUILayout.Button("选择到处table路径", GUILayout.Width(200)))
  47. {
  48. luaDirectoryName = GetFilePath();
  49. }
  50. EditorGUILayout.EndHorizontal();
  51. startDataLine = EditorGUILayout.IntField("StartDataLine", startDataLine);
  52. if (GUILayout.Button("CsvToLua"))
  53. Change();
  54. }
  55. public static string GetFilePath()
  56. {
  57. OpenDialogDir ofn2 = new OpenDialogDir();
  58. ofn2.pszDisplayName = new string(new char[2000]); ; // 存放目录路径缓冲区
  59. ofn2.lpszTitle = "Open Project";// 标题
  60. //ofn2.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // 新的样式,带编辑框
  61. IntPtr pidlPtr = DllOpenFileDialog.SHBrowseForFolder(ofn2);
  62. char[] charArray = new char[2000];
  63. for (int i = 0; i < 2000; i++)
  64. charArray[i] = '\0';
  65. DllOpenFileDialog.SHGetPathFromIDList(pidlPtr, charArray);
  66. string fullDirPath = new String(charArray);
  67. fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));
  68. return fullDirPath;
  69. }
  70. public void Change()
  71. {
  72. Debug.Log(tableDirectoryName);
  73. foreach (var item in Directory.GetFiles(tableDirectoryName))
  74. {
  75. if (item.Contains(".meta"))
  76. continue;
  77. CreatLuaTable(item);
  78. }
  79. }
  80. void CreatLuaTable(string filePath)
  81. {
  82. CreatDirectory();
  83. Save(GetSavePath(filePath), GetSaveContext(filePath));
  84. }
  85. void CreatDirectory()
  86. {
  87. string path = luaDirectoryName;
  88. if (!Directory.Exists(path))
  89. {
  90. Directory.CreateDirectory(path);
  91. AssetDatabase.Refresh();
  92. }
  93. }
  94. string GetSavePath(string filePath)
  95. {
  96. string fileName = GetFileName(filePath);
  97. return luaDirectoryName + "/" + fileName + ".lua";
  98. }
  99. string GetFileName(string filePath)
  100. {
  101. string targetPath = filePath.Replace(@"\", "/");
  102. string[] strs = targetPath.Split('/');
  103. targetPath = strs[strs.Length - 1];
  104. strs = targetPath.Split('.');
  105. string fileName = strs[0];
  106. return fileName;
  107. }
  108. string GetSaveContext(string filePath)
  109. {
  110. string fileName = GetFileName(filePath);
  111. List<string> context = GetContext(filePath);
  112. string[] propertyName = GetPropertyName(context[1]);
  113. string[] typeName = GetPropertyName(context[2]);
  114. StringBuilder sb = new StringBuilder();
  115. sb.Append("local " + fileName + " = {" + "\n");
  116. for (int i = startDataLine; i < context.Count; i++)
  117. {
  118. context[i] = context[i].Replace(" ", "");
  119. string[] details = context[i].Split(',');
  120. sb.Append("[" + details[0] + "]={" + "\n");
  121. for (int j = 0; j < propertyName.Length; j++)
  122. {
  123. sb.Append("['" + propertyName[j] + "']=");
  124. if (typeName[j].Equals("string"))
  125. sb.Append("'" + details[j] + "'," + "\n");
  126. else if (typeName[j].Equals("bool"))
  127. {
  128. sb.Append(details[j].ToLower() + "," + "\n");
  129. }
  130. else if (typeName[j].Equals("list"))
  131. {
  132. if (details[j].IndexOf(';') > 0)
  133. {
  134. ParseTable(sb, details[j], ';');
  135. }
  136. else if (details[j].IndexOf(':') > 0 && details[j].IndexOf(';') < 0)
  137. {
  138. sb.Append("{");
  139. ParseTable(sb, details[j], ':');
  140. sb.Append("}");
  141. }
  142. else
  143. {
  144. sb.Append("{");
  145. try
  146. {
  147. if (details[j].IndexOf('.') > 0)
  148. {
  149. Convert.ToDouble(details[j]);
  150. }
  151. else
  152. {
  153. Convert.ToInt32(details[j]);
  154. }
  155. sb.Append(details[j]);
  156. }
  157. catch
  158. {
  159. if (!string.IsNullOrEmpty(details[j]))
  160. sb.Append("'" + details[j] + "'");
  161. }
  162. sb.Append("}");
  163. }
  164. sb.Append(",\n");
  165. }
  166. else if (typeName[j].Equals("enum"))
  167. {
  168. sb.Append("Enum." + propertyName[j] + "." + details[j] + "," + "\n");
  169. }
  170. else
  171. {
  172. string num = "0";
  173. if (details[j] != "")
  174. {
  175. num = details[j];
  176. }
  177. sb.Append(num + "," + "\n");
  178. }
  179. }
  180. sb.Append("}," + "\n");
  181. }
  182. sb.Append("}" + "\n");
  183. sb.Append("return " + fileName);
  184. return sb.ToString();
  185. }
  186. void ParseTable(StringBuilder sb, string content, char split)
  187. {
  188. string[] strs = content.Split(split);
  189. sb.Append("{");
  190. for (int m = 0; m < strs.Length; ++m)
  191. {
  192. try
  193. {
  194. if (strs[m].IndexOf(':') > 0)
  195. {
  196. ParseTable(sb, strs[m], ':');
  197. }
  198. else
  199. {
  200. if (strs[m].IndexOf('.') > 0)
  201. {
  202. Convert.ToDouble(strs[m]);
  203. }
  204. else
  205. {
  206. Convert.ToInt32(strs[m]);
  207. }
  208. sb.Append(strs[m]);
  209. }
  210. }
  211. catch
  212. {
  213. if (!string.IsNullOrEmpty(strs[m]))
  214. sb.Append("'" + strs[m] + "'");
  215. }
  216. if (m < strs.Length - 1)
  217. {
  218. sb.Append(",");
  219. }
  220. }
  221. sb.Append("}");
  222. }
  223. List<string> GetContext(string filePath)
  224. {
  225. StreamReader sr = new StreamReader(filePath);
  226. List<string> contexts = new List<string>();
  227. string line;
  228. while ((line = sr.ReadLine()) != null)
  229. contexts.Add(line);
  230. return contexts;
  231. }
  232. string[] GetPropertyName(string line)
  233. {
  234. string[] names = line.Split(',');
  235. return names;
  236. }
  237. void Save(string path, string information)
  238. {
  239. if (File.Exists(path))
  240. File.Delete(path);
  241. FileStream aFile = new FileStream(@"" + path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  242. StreamWriter sw = new StreamWriter(aFile);
  243. sw.Write(information);
  244. sw.Close();
  245. sw.Dispose();
  246. aFile.Close();
  247. aFile.Dispose();
  248. sw = null;
  249. aFile = null;
  250. AssetDatabase.Refresh();
  251. }
  252. }