CsvToLua.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. using Game.Config;
  9. using RO.Editor;
  10. using LuaInterface;
  11. using System.Linq;
  12. using System.Text.RegularExpressions;
  13. public class CfgLanguageCfg : GameData<CfgLanguageCfg>
  14. {
  15. public string CfgName;
  16. public string Field;
  17. public string CfgID;
  18. public int CfgType;
  19. public int Enable;
  20. public string AssetPath;
  21. public string AssetName;
  22. public CfgLanguageCfg()
  23. {
  24. CfgName = "";
  25. Field = "";
  26. CfgID = "";
  27. CfgType = 0;
  28. Enable = 0;
  29. AssetPath = "";
  30. AssetName = "";
  31. }
  32. public static void OnCsvLoad(CsvReader csvReader)
  33. {
  34. CfgLanguageCfg.Onload(csvReader);
  35. }
  36. public static string FileName_S()
  37. {
  38. return "";
  39. }
  40. }
  41. public class LanguageCfg : GameData<LanguageCfg>
  42. {
  43. public string key;
  44. public string Language;
  45. public LanguageCfg()
  46. {
  47. key = "";
  48. Language = "";
  49. }
  50. public static void OnCsvLoad(CsvReader csvReader)
  51. {
  52. LanguageCfg.Onload(csvReader);
  53. }
  54. public static string FileName_S()
  55. {
  56. return "";
  57. }
  58. }
  59. public class LocalizedTextureCfg :GameData<LocalizedTextureCfg>
  60. {
  61. public string key;
  62. public string Language;
  63. public string AssetPath;
  64. public string OtherSetting;
  65. public string SetNativeSize;
  66. public LocalizedTextureCfg()
  67. {
  68. key = "";
  69. Language = "";
  70. AssetPath = "";
  71. OtherSetting = "";
  72. SetNativeSize = "0";
  73. }
  74. public static void OnCsvLoad(CsvReader csvReader)
  75. {
  76. Onload(csvReader);
  77. }
  78. public static string FileName_S()
  79. {
  80. return "";
  81. }
  82. }
  83. public class CsvToLua : EditorWindow
  84. {
  85. public static string CSV_PATH = "csv path";
  86. public static string LUA_PATH = "lua path";
  87. public static string START_LINE = "start line";
  88. public string tableDirectoryName;
  89. public string luaDirectoryName;
  90. public int startDataLine = 1;
  91. public static bool IsObj;
  92. [MenuItem("Tools/CsvToLua")]
  93. static void Init()
  94. {
  95. CsvToLua window = (CsvToLua)GetWindow(typeof(CsvToLua));
  96. window.Show();
  97. }
  98. void OnEnable()
  99. {
  100. tableDirectoryName = EditorPrefs.GetString(CSV_PATH);
  101. luaDirectoryName = EditorPrefs.GetString(LUA_PATH);
  102. startDataLine = EditorPrefs.GetInt(START_LINE);
  103. }
  104. void OnDisable()
  105. {
  106. EditorPrefs.SetString(CSV_PATH, tableDirectoryName);
  107. EditorPrefs.SetString(LUA_PATH, luaDirectoryName);
  108. EditorPrefs.SetInt(START_LINE, startDataLine);
  109. }
  110. private void OnGUI()
  111. {
  112. GUILayout.Label("Base Settings", EditorStyles.boldLabel);
  113. EditorGUILayout.BeginHorizontal();
  114. tableDirectoryName = EditorGUILayout.TextField("TableDirectory Name", tableDirectoryName);
  115. if (GUILayout.Button("选择csv路径", GUILayout.Width(200)))
  116. {
  117. tableDirectoryName = GetFilePath();
  118. }
  119. EditorGUILayout.EndHorizontal();
  120. EditorGUILayout.BeginHorizontal();
  121. luaDirectoryName = EditorGUILayout.TextField("LuaDirectory Name", luaDirectoryName);
  122. if (GUILayout.Button("选择到处table路径", GUILayout.Width(200)))
  123. {
  124. luaDirectoryName = GetFilePath();
  125. }
  126. EditorGUILayout.EndHorizontal();
  127. startDataLine = EditorGUILayout.IntField("StartDataLine", startDataLine);
  128. if (GUILayout.Button("CsvToLua"))
  129. Change();
  130. if (GUILayout.Button("测试"))
  131. {
  132. string lua = @"local aaa =
  133. {
  134. [1] =
  135. {
  136. ['a'] = 1,
  137. ['b'] = '2',
  138. ['c'] = 'asd' ,
  139. ['d'] = {1,2,3,{{1,2},{3,4},{1.2,0},{0}}},
  140. ['aId'] = 1,
  141. } ,
  142. }
  143. return aaa";
  144. using (LuaState runTime = new LuaState())
  145. {
  146. runTime.Start();
  147. //LuaState runTime = LuaClient.Instance.luaState;
  148. runTime.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
  149. LuaTable luaTable = runTime.DoString<LuaTable>(lua);
  150. RolePreviewWindow.SaveToLua(runTime, "aaa.lua", luaTable, "AAA","");
  151. }
  152. }
  153. if (GUILayout.Button("提取语言配置"))
  154. {
  155. CheckCgf();
  156. }
  157. if (GUILayout.Button("提取图片配置"))
  158. {
  159. CheckTextureCgf();
  160. }
  161. IsObj = GUILayout.Toggle(IsObj,"运行时启动");
  162. if (GUILayout.Button("更改CardCfg"))
  163. {
  164. ChangeLuaCfg("Assets/Lua/Config/CardCfg.lua", "Assets\\Editor\\CsvToLua\\LuaCfg.lua", "Build/NewCfg/CardCfg.lua");
  165. }
  166. if (GUILayout.Button("合并Language"))
  167. {
  168. //MergeLanauage("Assets/Content/Config/LanguagePackage_cn.csv", "Build/NewCfg/ROLanguagePackage_cn.csv", "Build/NewCfg/Language_merge.csv");
  169. MergeLanauage("Assets/Content/Config/LanguagePackage_cn.csv", "D:/项目/其他/RO11.csv", "Build/NewCfg/Language_merge.csv");
  170. ///MergeLanauage("Build/NewCfg/Language.csv", "Assets/Content/Config/LanguagePackage_cn.csv", "Build/NewCfg/Language_merge_cn.csv");
  171. }
  172. if (GUILayout.Button("二进制转换"))
  173. {
  174. B2T("D:/项目/其他/Language15000-19000.csv", "D:/项目/其他/111.csv",Encoding.UTF8);
  175. }
  176. if (GUILayout.Button("复制luacfg文件"))
  177. {
  178. CopyFleByLuaCfg("Assets\\Editor\\CsvToLua\\luaCopyFileCfg.lua", "", "D:/项目/图/仙境/cn/","_cn");
  179. }
  180. }
  181. public static string GetFilePath()
  182. {
  183. OpenDialogDir ofn2 = new OpenDialogDir();
  184. ofn2.pszDisplayName = new string(new char[2000]); ; // 存放目录路径缓冲区
  185. ofn2.lpszTitle = "Open Project";// 标题
  186. //ofn2.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // 新的样式,带编辑框
  187. IntPtr pidlPtr = DllOpenFileDialog.SHBrowseForFolder(ofn2);
  188. char[] charArray = new char[2000];
  189. for (int i = 0; i < 2000; i++)
  190. charArray[i] = '\0';
  191. DllOpenFileDialog.SHGetPathFromIDList(pidlPtr, charArray);
  192. string fullDirPath = new String(charArray);
  193. fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));
  194. return fullDirPath;
  195. }
  196. public void Change()
  197. {
  198. Debug.Log(tableDirectoryName);
  199. foreach (var item in Directory.GetFiles(tableDirectoryName))
  200. {
  201. if (item.Contains(".meta"))
  202. continue;
  203. CreatLuaTable(item);
  204. }
  205. }
  206. void CreatLuaTable(string filePath)
  207. {
  208. CreatDirectory();
  209. Save(GetSavePath(filePath), GetSaveContext(filePath));
  210. }
  211. void CreatDirectory()
  212. {
  213. string path = luaDirectoryName;
  214. if (!Directory.Exists(path))
  215. {
  216. Directory.CreateDirectory(path);
  217. AssetDatabase.Refresh();
  218. }
  219. }
  220. string GetSavePath(string filePath)
  221. {
  222. string fileName = GetFileName(filePath);
  223. return luaDirectoryName + "/" + fileName + ".lua";
  224. }
  225. string GetFileName(string filePath)
  226. {
  227. string targetPath = filePath.Replace(@"\", "/");
  228. string[] strs = targetPath.Split('/');
  229. targetPath = strs[strs.Length - 1];
  230. strs = targetPath.Split('.');
  231. string fileName = strs[0];
  232. return fileName;
  233. }
  234. string GetSaveContext(string filePath)
  235. {
  236. string fileName = GetFileName(filePath);
  237. List<string> context = GetContext(filePath);
  238. string[] propertyName = GetPropertyName(context[1]);
  239. string[] typeName = GetPropertyName(context[2]);
  240. StringBuilder sb = new StringBuilder();
  241. sb.Append("local " + fileName + " = {" + "\n");
  242. for (int i = startDataLine; i < context.Count; i++)
  243. {
  244. context[i] = context[i].Replace(" ", "");
  245. string[] details = context[i].Split(',');
  246. sb.Append("[" + details[0] + "]={" + "\n");
  247. for (int j = 0; j < propertyName.Length; j++)
  248. {
  249. sb.Append("['" + propertyName[j] + "']=");
  250. if (typeName[j].Equals("string"))
  251. sb.Append("'" + details[j] + "'," + "\n");
  252. else if (typeName[j].Equals("bool"))
  253. {
  254. sb.Append(details[j].ToLower() + "," + "\n");
  255. }
  256. else if (typeName[j].Equals("list"))
  257. {
  258. if (details[j].IndexOf(';') > 0)
  259. {
  260. ParseTable(sb, details[j], ';');
  261. }
  262. else if (details[j].IndexOf(':') > 0 && details[j].IndexOf(';') < 0)
  263. {
  264. sb.Append("{");
  265. ParseTable(sb, details[j], ':');
  266. sb.Append("}");
  267. }
  268. else
  269. {
  270. sb.Append("{");
  271. try
  272. {
  273. if (details[j].IndexOf('.') > 0)
  274. {
  275. Convert.ToDouble(details[j]);
  276. }
  277. else
  278. {
  279. Convert.ToInt32(details[j]);
  280. }
  281. sb.Append(details[j]);
  282. }
  283. catch
  284. {
  285. if (!string.IsNullOrEmpty(details[j]))
  286. sb.Append("'" + details[j] + "'");
  287. }
  288. sb.Append("}");
  289. }
  290. sb.Append(",\n");
  291. }
  292. else if (typeName[j].Equals("enum"))
  293. {
  294. sb.Append("Enum." + propertyName[j] + "." + details[j] + "," + "\n");
  295. }
  296. else
  297. {
  298. string num = "0";
  299. if (details[j] != "")
  300. {
  301. num = details[j];
  302. }
  303. sb.Append(num + "," + "\n");
  304. }
  305. }
  306. sb.Append("}," + "\n");
  307. }
  308. sb.Append("}" + "\n");
  309. sb.Append("return " + fileName);
  310. return sb.ToString();
  311. }
  312. void ParseTable(StringBuilder sb, string content, char split)
  313. {
  314. string[] strs = content.Split(split);
  315. sb.Append("{");
  316. for (int m = 0; m < strs.Length; ++m)
  317. {
  318. try
  319. {
  320. if (strs[m].IndexOf(':') > 0)
  321. {
  322. ParseTable(sb, strs[m], ':');
  323. }
  324. else
  325. {
  326. if (strs[m].IndexOf('.') > 0)
  327. {
  328. Convert.ToDouble(strs[m]);
  329. }
  330. else
  331. {
  332. Convert.ToInt32(strs[m]);
  333. }
  334. sb.Append(strs[m]);
  335. }
  336. }
  337. catch
  338. {
  339. if (!string.IsNullOrEmpty(strs[m]))
  340. sb.Append("'" + strs[m] + "'");
  341. }
  342. if (m < strs.Length - 1)
  343. {
  344. sb.Append(",");
  345. }
  346. }
  347. sb.Append("}");
  348. }
  349. List<string> GetContext(string filePath)
  350. {
  351. StreamReader sr = new StreamReader(filePath);
  352. List<string> contexts = new List<string>();
  353. string line;
  354. while ((line = sr.ReadLine()) != null)
  355. contexts.Add(line);
  356. return contexts;
  357. }
  358. string[] GetPropertyName(string line)
  359. {
  360. string[] names = line.Split(',');
  361. return names;
  362. }
  363. void Save(string path, string information)
  364. {
  365. if (File.Exists(path))
  366. File.Delete(path);
  367. FileStream aFile = new FileStream(@"" + path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  368. StreamWriter sw = new StreamWriter(aFile);
  369. sw.Write(information);
  370. sw.Close();
  371. sw.Dispose();
  372. aFile.Close();
  373. aFile.Dispose();
  374. sw = null;
  375. aFile = null;
  376. AssetDatabase.Refresh();
  377. }
  378. public static Dictionary<string, List<CfgLanguageCfg>> GetCfgLanguageCfg(string path)
  379. {
  380. Dictionary<string, List<CfgLanguageCfg>> ret = new Dictionary<string, List<CfgLanguageCfg>>();
  381. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
  382. CsvReader csvReader = new CsvReader(CfgLanguageCfg.FileName_S(), ta.bytes);
  383. CfgLanguageCfg.OnCsvLoad(csvReader);
  384. CfgLanguageCfg.Foreach(it =>
  385. {
  386. if (it.Enable == 0)
  387. {
  388. return;
  389. }
  390. if (ret.ContainsKey(it.CfgName))
  391. {
  392. ret[it.CfgName].Add(it);
  393. }
  394. else
  395. {
  396. List<CfgLanguageCfg> cfgs = new List<CfgLanguageCfg>();
  397. cfgs.Add(it);
  398. ret.Add(it.CfgName,cfgs);
  399. }
  400. //Debug.Log(it.BaseName);
  401. });
  402. CfgLanguageCfg.Clear();
  403. return ret;
  404. }
  405. public static Dictionary<string, LanguageCfg> GetLanguageCfg(string path)
  406. {
  407. Dictionary<string, LanguageCfg> ret = new Dictionary<string, LanguageCfg>();
  408. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
  409. CsvReader csvReader = new CsvReader(LanguageCfg.FileName_S(), ta.bytes,1,2,3);
  410. LanguageCfg.OnCsvLoad(csvReader);
  411. LanguageCfg.Foreach(it =>
  412. {
  413. if (!ret.ContainsKey(it.key))
  414. {
  415. ret.Add(it.key, it);
  416. }
  417. else
  418. {
  419. ret.Add(it.key+ret.Count,it);
  420. Debug.Log("重复项:" + it.key);
  421. }
  422. //Debug.Log(it.BaseName);
  423. });
  424. LanguageCfg.Clear(false);
  425. return ret;
  426. }
  427. public static Dictionary<string, LocalizedTextureCfg> GetLanguageTextureCfg(string path)
  428. {
  429. Dictionary<string, LocalizedTextureCfg> ret = new Dictionary<string, LocalizedTextureCfg>();
  430. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
  431. CsvReader csvReader = new CsvReader(LanguageCfg.FileName_S(), ta.bytes, 1, 2, 3);
  432. LocalizedTextureCfg.OnCsvLoad(csvReader);
  433. LocalizedTextureCfg.Foreach(it =>
  434. {
  435. if (!ret.ContainsKey(it.key))
  436. {
  437. ret.Add(it.key, it);
  438. }
  439. else
  440. {
  441. ret.Add(it.key + ret.Count, it);
  442. Debug.Log("重复项:" + it.key);
  443. }
  444. //Debug.Log(it.BaseName);
  445. });
  446. LocalizedTextureCfg.Clear(false);
  447. return ret;
  448. }
  449. public static void CheckCgf()
  450. {
  451. string luaPath = "Assets/Lua/Config/";
  452. string csvPath = "Assets/Content/Config/";
  453. string OutPutPath = "Build/NewCfg/";
  454. Dictionary<string, List<CfgLanguageCfg>> cfgs = GetCfgLanguageCfg("Assets/Editor/CsvToLua/CfgLanguageCfg.csv");
  455. Dictionary<string, LanguageCfg> language = GetLanguageCfg("Assets/Content/Config/LanguagePackage_cn.csv");
  456. float ckcount = 0;
  457. foreach (var item in cfgs)
  458. {
  459. ckcount+=1;
  460. List<CfgLanguageCfg> value = item.Value;
  461. EditorUtility.DisplayProgressBar("检测配置中", $"检测:{item.Key}...", ckcount/cfgs.Count);
  462. string cfgPath = "";
  463. if (value[0].CfgType == 1)
  464. {
  465. cfgPath = luaPath + item.Key + ".lua";
  466. CheckLauCfg_CS(value,cfgPath,language, $"{OutPutPath}lua/{value[0].CfgName}.lua",(it,_v) => string.Equals(it.Value.Language, _v),(obj,key,_value,cfg)=>
  467. {
  468. obj.key = key;
  469. obj.Language = _value;
  470. });
  471. }
  472. else
  473. {
  474. cfgPath = csvPath + item.Key + ".csv";
  475. CheckCsvCfg(value, cfgPath, language, $"{OutPutPath}csv/{value[0].CfgName}.csv", (it, _v) => string.Equals(it.Value.Language, _v), (obj, key, _value, cfg) =>
  476. {
  477. obj.key = key;
  478. obj.Language = _value;
  479. });
  480. }
  481. }
  482. EditorUtility.ClearProgressBar();
  483. CsvWriter<LanguageCfg> csvWriter = new CsvWriter<LanguageCfg>(OutPutPath+ "Language.csv", "Language.csv",language.Values.ToList(), LanguageCfg.GetFormatInfo());
  484. csvWriter.Write();
  485. }
  486. public static void CheckTextureCgf()
  487. {
  488. string luaPath = "Assets/Lua/Config/";
  489. string csvPath = "Assets/Content/Config/";
  490. string OutPutPath = "Build/NewCfg/";
  491. Dictionary<string, List<CfgLanguageCfg>> cfgs = GetCfgLanguageCfg("Assets/Editor/CsvToLua/CfgLanguageTextureCfg.csv");
  492. Dictionary<string, LocalizedTextureCfg> language = GetLanguageTextureCfg("Assets/Content/Config/LocalizeTextureCfg_base.csv");
  493. float ckcount = 0;
  494. foreach (var item in cfgs)
  495. {
  496. ckcount += 1;
  497. List<CfgLanguageCfg> value = item.Value;
  498. EditorUtility.DisplayProgressBar("检测配置中", $"检测:{item.Key}...", ckcount / cfgs.Count);
  499. string cfgPath = "";
  500. if (value[0].CfgType == 1)
  501. {
  502. cfgPath = luaPath + item.Key + ".lua";
  503. CheckLauCfg_CS(value, cfgPath, language, $"{OutPutPath}lua/{value[0].CfgName}.lua", (it, _v) => string.Equals(it.Value.Language, _v), (obj, key, _value, cfg) =>
  504. {
  505. obj.key = key;
  506. obj.Language = _value;
  507. obj.AssetPath = cfg.AssetPath;
  508. });
  509. }
  510. else
  511. {
  512. cfgPath = csvPath + item.Key + ".csv";
  513. CheckCsvCfg(value, cfgPath, language, $"{OutPutPath}csv/{value[0].CfgName}.csv", (it, _v) => string.Equals(it.Value.Language, _v), (obj, key, _value, cfg) =>
  514. {
  515. obj.key = key;
  516. obj.Language = _value;
  517. obj.AssetPath = cfg.AssetPath;
  518. });
  519. }
  520. }
  521. EditorUtility.ClearProgressBar();
  522. CsvWriter<LocalizedTextureCfg> csvWriter = new CsvWriter<LocalizedTextureCfg>(OutPutPath + "LocalizedTextureCfg.csv", "LocalizedTextureCfg.csv", language.Values.ToList(), LocalizedTextureCfg.GetFormatInfo());
  523. csvWriter.Write();
  524. }
  525. private static void CheckLauCfg(List<CfgLanguageCfg> cfg,string path, Dictionary<string, LanguageCfg> language,string outputPath)
  526. {
  527. using (LuaState runTime = new LuaState())
  528. {
  529. runTime.Start();
  530. RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, path, (id,luatable) =>
  531. {
  532. for (int i = 0; i < cfg.Count; i++)
  533. {
  534. if (cfg[i].Enable == 0)
  535. continue;
  536. string value = luatable.RawGet<string, string>(cfg[i].Field);
  537. if (string.IsNullOrEmpty(value))
  538. {
  539. continue;
  540. }
  541. KeyValuePair<string,LanguageCfg> valuePair = language.FirstOrDefault(it =>
  542. {
  543. return string.Equals(it.Value.Language, value);
  544. });
  545. if (!string.IsNullOrEmpty(valuePair.Key))
  546. {
  547. luatable.RawSet<string, string>(cfg[i].Field, valuePair.Key);
  548. }
  549. else
  550. {
  551. string key = cfg[i].CfgID + id;
  552. luatable.RawSet<string, string>(cfg[i].Field, key);
  553. if (value.Contains("\n"))
  554. {
  555. Debug.Log(value);
  556. value = value.Replace("\n","\\n");
  557. Debug.Log(value);
  558. }
  559. language.Add(key,new LanguageCfg() { key = key,Language = value});
  560. }
  561. }
  562. },(ltab)=>
  563. {
  564. bool useObj = IsObj;
  565. if (useObj)
  566. {
  567. LuaState luaState = LuaClient.Instance.luaState;
  568. luaState.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
  569. RolePreviewWindow.SaveToLua(luaState, outputPath, ltab, cfg[0].CfgName, "1");
  570. }
  571. else
  572. {
  573. LuaState luaState = runTime;
  574. luaState.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
  575. RolePreviewWindow.SaveToLua(luaState, outputPath, ltab, cfg[0].CfgName, "");
  576. }
  577. });
  578. }
  579. }
  580. private static void SetLuaLine(string[] rowDatas,int id,string key,string data,string dataF = "'{0}',")
  581. {
  582. string idkey = $"[{id}]";
  583. string keykey = $"'{key}'";
  584. string wdata = string.Format(dataF,data);//$"'{data}',";
  585. bool isFind = false;
  586. for (int line = 0; line < rowDatas.Length; line++)
  587. {
  588. if (rowDatas[line].Trim().IndexOf("--") == 0)
  589. {
  590. //Debug.Log("注释跳过");
  591. }
  592. if (rowDatas[line].Contains(idkey))
  593. {
  594. isFind = true;
  595. }
  596. if (isFind && rowDatas[line].Contains(keykey))
  597. {
  598. int s = rowDatas[line].IndexOf('[');
  599. string p = "";
  600. if (s > 0)
  601. {
  602. for (int i = 0; i < s; i++)
  603. {
  604. p += " ";
  605. }
  606. }
  607. string newstr = $"{p}[{keykey}]={wdata}" ;
  608. rowDatas[line] = newstr;
  609. break;
  610. }
  611. }
  612. }
  613. private static void CheckLauCfg_CS<T>(List<CfgLanguageCfg> cfg, string path, Dictionary<string, T> language, string outputPath,Func<KeyValuePair<string,T>,string,bool> findcb, Action<T,string,string, CfgLanguageCfg> cb) where T :GameData
  614. {
  615. string[] rowDatas = File.ReadAllLines(path);
  616. bool iswrite = true;
  617. using (LuaState runTime = new LuaState())
  618. {
  619. runTime.Start();
  620. RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, path, (id, luatable) =>
  621. {
  622. for (int i = 0; i < cfg.Count; i++)
  623. {
  624. if (cfg[i].Enable == 0)
  625. {
  626. iswrite = false;
  627. continue;
  628. }
  629. string value = luatable.RawGet<string, string>(cfg[i].Field);
  630. if (string.IsNullOrEmpty(value))
  631. {
  632. continue;
  633. }
  634. KeyValuePair<string, T> valuePair = language.FirstOrDefault(it=>
  635. {
  636. return findcb(it,value);
  637. });
  638. if (!string.IsNullOrEmpty(valuePair.Key))
  639. {
  640. luatable.RawSet<string, string>(cfg[i].Field, valuePair.Key);
  641. SetLuaLine(rowDatas,id, cfg[i].Field, valuePair.Key);
  642. }
  643. else
  644. {
  645. string key = cfg[i].CfgID + id;
  646. luatable.RawSet<string, string>(cfg[i].Field, key);
  647. SetLuaLine(rowDatas, id, cfg[i].Field, key);
  648. if (value.Contains("\n"))
  649. {
  650. //Debug.Log(value);
  651. value = value.Replace("\n", "\\n");
  652. //Debug.Log(value);
  653. }
  654. if (!language.ContainsKey(key))
  655. {
  656. Type type = typeof(T);
  657. T obj = Activator.CreateInstance<T>();
  658. cb?.Invoke(obj,key,value, cfg[i]);
  659. language.Add(key, obj);
  660. }
  661. }
  662. }
  663. });
  664. }
  665. if (iswrite)
  666. File.WriteAllLines(outputPath,rowDatas);
  667. }
  668. private static void CheckCsvCfg<T>(List<CfgLanguageCfg> cfg, string path, Dictionary<string, T> language, string outputPath, Func<KeyValuePair<string, T>,string, bool> findcb, Action<T, string, string, CfgLanguageCfg> cb) where T : GameData
  669. {
  670. CsvReader csvReader = new CsvReader(path,1,2,3);
  671. string[][] datas = csvReader.RowDatas;
  672. for (int i = csvReader.StartLine; i < datas.Length; i++)
  673. {
  674. string[] curLine = datas[i];
  675. for (int j = 0; j < cfg.Count; j++)
  676. {
  677. if (cfg[j].Enable == 0)
  678. continue;
  679. string value = csvReader.GetDataByFieldName(cfg[j].Field,i);
  680. if (string.IsNullOrEmpty(value))
  681. {
  682. continue;
  683. }
  684. KeyValuePair<string, T> valuePair = language.FirstOrDefault(it =>
  685. {
  686. return findcb(it, value);
  687. });
  688. if (!string.IsNullOrEmpty(valuePair.Key))
  689. {
  690. csvReader.SetDataByFieldName(cfg[j].Field, valuePair.Key, i);
  691. }
  692. else
  693. {
  694. string key = cfg[j].CfgID + i;
  695. csvReader.SetDataByFieldName(cfg[j].Field, key,i);
  696. Type type = typeof(T);
  697. T obj = Activator.CreateInstance<T>();
  698. cb?.Invoke(obj, key, value, cfg[i]);
  699. language.Add(key,obj);
  700. }
  701. }
  702. }
  703. FileHelper.WirteStringToFile(outputPath,csvReader.GetString());
  704. }
  705. private static void ChangeLuaCfg(string luacfgpath,string luachagepath,string outputPath)
  706. {
  707. string[] rowDatas = File.ReadAllLines(luacfgpath);
  708. using (LuaState runTime = new LuaState())
  709. {
  710. runTime.Start();
  711. LuaTable luaTable = runTime.DoFile<LuaTable>(luachagepath);
  712. RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, luacfgpath, (id, luatable) =>
  713. {
  714. LuaFunction luaFunction = luaTable.GetLuaFunction("IsChangefg");
  715. bool ischange = luaFunction.Invoke<LuaTable,LuaTable, bool>(luaTable,luatable);
  716. if (ischange)
  717. {
  718. LuaTable fieldValue = luaTable.Invoke<LuaTable,LuaTable>("GetFieldAddValue", luaTable);
  719. string field = fieldValue.RawGet<string, string>("field");
  720. string value = fieldValue.RawGet<string, string>("value");
  721. SetLuaLine(rowDatas,id,field,value.ToString(),"{0},");
  722. }
  723. });
  724. }
  725. File.WriteAllLines(outputPath, rowDatas);
  726. }
  727. private static void CopyFleByLuaCfg(string luacfgpath, string fileRootPath,string outputPath,string output)
  728. {
  729. using (LuaState runTime = new LuaState())
  730. {
  731. runTime.Start();
  732. LuaTable luaTable = runTime.DoFile<LuaTable>(luacfgpath);
  733. LuaFunction init_fun = luaTable.GetLuaFunction("Init");
  734. LuaFunction SetOutput_fun = luaTable.GetLuaFunction("SetOutput");
  735. LuaFunction GetNextCfg_fun = luaTable.GetLuaFunction("GetNextCfg");
  736. LuaFunction GetLen_fun = luaTable.GetLuaFunction("GetLen");
  737. init_fun.Call<LuaTable>(luaTable);
  738. SetOutput_fun.Call<LuaTable, string>(luaTable, output);
  739. int len = GetLen_fun.Invoke<LuaTable,int>(luaTable);
  740. int curindex = 1;
  741. EditorUtility.DisplayProgressBar("复制中", $"开始复制", 0);
  742. string metastr = ".meta";
  743. while (true)
  744. {
  745. EditorUtility.DisplayProgressBar("复制中", $"复制{curindex}/{len}", curindex/len);
  746. curindex++;
  747. LuaTable cfg = GetNextCfg_fun.Invoke<LuaTable, LuaTable>(luaTable);
  748. //{result = false,path = path,outputPath = outputPath}
  749. bool result = cfg.RawGet<string, bool>("result");
  750. if (result)
  751. {
  752. string path = cfg.RawGet<string, string>("path");
  753. string _outputPath = cfg.RawGet<string, string>("outputPath");
  754. string filePath = fileRootPath + path;
  755. string fileOutputPath = outputPath + _outputPath;
  756. if (path.Contains(".psd"))
  757. {
  758. Debug.Log($"不复制psd文件:[{path}]");
  759. continue;
  760. }
  761. FileHelper.CreateDir(FileHelper.PathRemoveBack(fileOutputPath));
  762. if (File.Exists(filePath))
  763. {
  764. File.Copy(filePath, fileOutputPath, true);
  765. if (!path.Contains(metastr))
  766. {
  767. File.Copy(filePath+ metastr, fileOutputPath+ metastr, true);
  768. }
  769. }
  770. else
  771. {
  772. Debug.Log($"不存在文件:[{filePath}]");
  773. }
  774. }
  775. else
  776. {
  777. break;
  778. }
  779. }
  780. EditorUtility.ClearProgressBar();
  781. }
  782. }
  783. struct lgInfo
  784. {
  785. public int index;
  786. public int off;
  787. }
  788. private static void MergeLanauage(string oldPath,string newPath, string outputPath)
  789. {
  790. List<string> oldLg = File.ReadAllLines(oldPath).ToList();
  791. List<string> newLg = File.ReadAllLines(newPath).ToList();
  792. Dictionary<string, lgInfo> cfgmap = new Dictionary<string, lgInfo>();
  793. int length = oldLg.Count;
  794. int off = -1;
  795. for (int i = 0; i < length; i++)
  796. {
  797. off = oldLg[i].IndexOf(',');
  798. if (off < 0)
  799. {
  800. continue;
  801. }
  802. string key = oldLg[i].Substring(0,off);
  803. if (!cfgmap.ContainsKey(key))
  804. {
  805. lgInfo info = new lgInfo();
  806. info.index = i;
  807. info.off = off;
  808. cfgmap.Add(key, info);
  809. }
  810. }
  811. foreach (var item in newLg)
  812. {
  813. if (item == "" || item == ","||item == "key"||item == "string")
  814. continue;
  815. off = item.IndexOf(',');
  816. if (off < 0)
  817. {
  818. continue;
  819. }
  820. string key = item.Substring(0,off);
  821. if (!cfgmap.ContainsKey(key))
  822. {
  823. Debug.Log($"不包含:{key} off = {off}");
  824. oldLg.Add(item);
  825. }
  826. else
  827. {
  828. lgInfo lgIf = cfgmap[key];
  829. oldLg[lgIf.index] = item;
  830. }
  831. }
  832. string data = FileHelper.CatStringArray(oldLg,"\r\n");
  833. FileHelper.WirteStringToFile(outputPath, data);
  834. Debug.Log("合并完成");
  835. }
  836. private void B2T(string bPath,string outPath, Encoding read)
  837. {
  838. using (FileStream binaryFileStream = new FileStream(bPath, FileMode.Open, FileAccess.Read))
  839. using (StreamReader reader = new StreamReader(binaryFileStream, read)) // 使用ASCII编码读取
  840. using (StreamWriter writer = new StreamWriter(outPath))
  841. {
  842. writer.Write(reader.ReadToEnd()); // 读取并写入文本文件
  843. }
  844. }
  845. }