CsvToLua.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 CfgLanguageCfg()
  21. {
  22. CfgName = "";
  23. Field = "";
  24. CfgID = "";
  25. CfgType = 0;
  26. Enable = 0;
  27. }
  28. public static void OnCsvLoad(CsvReader csvReader)
  29. {
  30. CfgLanguageCfg.Onload(csvReader);
  31. }
  32. public static string FileName_S()
  33. {
  34. return "";
  35. }
  36. }
  37. public class LanguageCfg : GameData<LanguageCfg>
  38. {
  39. public string key;
  40. public string Language;
  41. public LanguageCfg()
  42. {
  43. key = "";
  44. Language = "";
  45. }
  46. public static void OnCsvLoad(CsvReader csvReader)
  47. {
  48. LanguageCfg.Onload(csvReader);
  49. }
  50. public static string FileName_S()
  51. {
  52. return "";
  53. }
  54. }
  55. public class CsvToLua : EditorWindow
  56. {
  57. public static string CSV_PATH = "csv path";
  58. public static string LUA_PATH = "lua path";
  59. public static string START_LINE = "start line";
  60. public string tableDirectoryName;
  61. public string luaDirectoryName;
  62. public int startDataLine = 1;
  63. public static bool IsObj;
  64. [MenuItem("Tools/CsvToLua")]
  65. static void Init()
  66. {
  67. CsvToLua window = (CsvToLua)GetWindow(typeof(CsvToLua));
  68. window.Show();
  69. }
  70. void OnEnable()
  71. {
  72. tableDirectoryName = EditorPrefs.GetString(CSV_PATH);
  73. luaDirectoryName = EditorPrefs.GetString(LUA_PATH);
  74. startDataLine = EditorPrefs.GetInt(START_LINE);
  75. }
  76. void OnDisable()
  77. {
  78. EditorPrefs.SetString(CSV_PATH, tableDirectoryName);
  79. EditorPrefs.SetString(LUA_PATH, luaDirectoryName);
  80. EditorPrefs.SetInt(START_LINE, startDataLine);
  81. }
  82. private void OnGUI()
  83. {
  84. GUILayout.Label("Base Settings", EditorStyles.boldLabel);
  85. EditorGUILayout.BeginHorizontal();
  86. tableDirectoryName = EditorGUILayout.TextField("TableDirectory Name", tableDirectoryName);
  87. if (GUILayout.Button("选择csv路径", GUILayout.Width(200)))
  88. {
  89. tableDirectoryName = GetFilePath();
  90. }
  91. EditorGUILayout.EndHorizontal();
  92. EditorGUILayout.BeginHorizontal();
  93. luaDirectoryName = EditorGUILayout.TextField("LuaDirectory Name", luaDirectoryName);
  94. if (GUILayout.Button("选择到处table路径", GUILayout.Width(200)))
  95. {
  96. luaDirectoryName = GetFilePath();
  97. }
  98. EditorGUILayout.EndHorizontal();
  99. startDataLine = EditorGUILayout.IntField("StartDataLine", startDataLine);
  100. if (GUILayout.Button("CsvToLua"))
  101. Change();
  102. if (GUILayout.Button("测试"))
  103. {
  104. string lua = @"local aaa =
  105. {
  106. [1] =
  107. {
  108. ['a'] = 1,
  109. ['b'] = '2',
  110. ['c'] = 'asd' ,
  111. ['d'] = {1,2,3,{{1,2},{3,4},{1.2,0},{0}}},
  112. ['aId'] = 1,
  113. } ,
  114. }
  115. return aaa";
  116. using (LuaState runTime = new LuaState())
  117. {
  118. runTime.Start();
  119. //LuaState runTime = LuaClient.Instance.luaState;
  120. runTime.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
  121. LuaTable luaTable = runTime.DoString<LuaTable>(lua);
  122. RolePreviewWindow.SaveToLua(runTime, "aaa.lua", luaTable, "AAA","");
  123. }
  124. }
  125. if (GUILayout.Button("检查"))
  126. {
  127. CheckCgf();
  128. }
  129. IsObj = GUILayout.Toggle(IsObj,"运行时启动");
  130. if (GUILayout.Button("更改CardCfg"))
  131. {
  132. ChangeLuaCfg("Assets/Lua/Config/CardCfg.lua", "Assets\\Editor\\CsvToLua\\LuaCfg.lua", "Build/NewCfg/CardCfg.lua");
  133. }
  134. if (GUILayout.Button("合并Language"))
  135. {
  136. //MergeLanauage("Assets/Content/Config/LanguagePackage_cn.csv", "Build/NewCfg/ROLanguagePackage_cn.csv", "Build/NewCfg/Language_merge.csv");
  137. MergeLanauage("Assets/Content/Config/LanguagePackage_cn.csv", "D:/项目/其他/RO11.csv", "Build/NewCfg/Language_merge.csv");
  138. ///MergeLanauage("Build/NewCfg/Language.csv", "Assets/Content/Config/LanguagePackage_cn.csv", "Build/NewCfg/Language_merge_cn.csv");
  139. }
  140. }
  141. public static string GetFilePath()
  142. {
  143. OpenDialogDir ofn2 = new OpenDialogDir();
  144. ofn2.pszDisplayName = new string(new char[2000]); ; // 存放目录路径缓冲区
  145. ofn2.lpszTitle = "Open Project";// 标题
  146. //ofn2.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // 新的样式,带编辑框
  147. IntPtr pidlPtr = DllOpenFileDialog.SHBrowseForFolder(ofn2);
  148. char[] charArray = new char[2000];
  149. for (int i = 0; i < 2000; i++)
  150. charArray[i] = '\0';
  151. DllOpenFileDialog.SHGetPathFromIDList(pidlPtr, charArray);
  152. string fullDirPath = new String(charArray);
  153. fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));
  154. return fullDirPath;
  155. }
  156. public void Change()
  157. {
  158. Debug.Log(tableDirectoryName);
  159. foreach (var item in Directory.GetFiles(tableDirectoryName))
  160. {
  161. if (item.Contains(".meta"))
  162. continue;
  163. CreatLuaTable(item);
  164. }
  165. }
  166. void CreatLuaTable(string filePath)
  167. {
  168. CreatDirectory();
  169. Save(GetSavePath(filePath), GetSaveContext(filePath));
  170. }
  171. void CreatDirectory()
  172. {
  173. string path = luaDirectoryName;
  174. if (!Directory.Exists(path))
  175. {
  176. Directory.CreateDirectory(path);
  177. AssetDatabase.Refresh();
  178. }
  179. }
  180. string GetSavePath(string filePath)
  181. {
  182. string fileName = GetFileName(filePath);
  183. return luaDirectoryName + "/" + fileName + ".lua";
  184. }
  185. string GetFileName(string filePath)
  186. {
  187. string targetPath = filePath.Replace(@"\", "/");
  188. string[] strs = targetPath.Split('/');
  189. targetPath = strs[strs.Length - 1];
  190. strs = targetPath.Split('.');
  191. string fileName = strs[0];
  192. return fileName;
  193. }
  194. string GetSaveContext(string filePath)
  195. {
  196. string fileName = GetFileName(filePath);
  197. List<string> context = GetContext(filePath);
  198. string[] propertyName = GetPropertyName(context[1]);
  199. string[] typeName = GetPropertyName(context[2]);
  200. StringBuilder sb = new StringBuilder();
  201. sb.Append("local " + fileName + " = {" + "\n");
  202. for (int i = startDataLine; i < context.Count; i++)
  203. {
  204. context[i] = context[i].Replace(" ", "");
  205. string[] details = context[i].Split(',');
  206. sb.Append("[" + details[0] + "]={" + "\n");
  207. for (int j = 0; j < propertyName.Length; j++)
  208. {
  209. sb.Append("['" + propertyName[j] + "']=");
  210. if (typeName[j].Equals("string"))
  211. sb.Append("'" + details[j] + "'," + "\n");
  212. else if (typeName[j].Equals("bool"))
  213. {
  214. sb.Append(details[j].ToLower() + "," + "\n");
  215. }
  216. else if (typeName[j].Equals("list"))
  217. {
  218. if (details[j].IndexOf(';') > 0)
  219. {
  220. ParseTable(sb, details[j], ';');
  221. }
  222. else if (details[j].IndexOf(':') > 0 && details[j].IndexOf(';') < 0)
  223. {
  224. sb.Append("{");
  225. ParseTable(sb, details[j], ':');
  226. sb.Append("}");
  227. }
  228. else
  229. {
  230. sb.Append("{");
  231. try
  232. {
  233. if (details[j].IndexOf('.') > 0)
  234. {
  235. Convert.ToDouble(details[j]);
  236. }
  237. else
  238. {
  239. Convert.ToInt32(details[j]);
  240. }
  241. sb.Append(details[j]);
  242. }
  243. catch
  244. {
  245. if (!string.IsNullOrEmpty(details[j]))
  246. sb.Append("'" + details[j] + "'");
  247. }
  248. sb.Append("}");
  249. }
  250. sb.Append(",\n");
  251. }
  252. else if (typeName[j].Equals("enum"))
  253. {
  254. sb.Append("Enum." + propertyName[j] + "." + details[j] + "," + "\n");
  255. }
  256. else
  257. {
  258. string num = "0";
  259. if (details[j] != "")
  260. {
  261. num = details[j];
  262. }
  263. sb.Append(num + "," + "\n");
  264. }
  265. }
  266. sb.Append("}," + "\n");
  267. }
  268. sb.Append("}" + "\n");
  269. sb.Append("return " + fileName);
  270. return sb.ToString();
  271. }
  272. void ParseTable(StringBuilder sb, string content, char split)
  273. {
  274. string[] strs = content.Split(split);
  275. sb.Append("{");
  276. for (int m = 0; m < strs.Length; ++m)
  277. {
  278. try
  279. {
  280. if (strs[m].IndexOf(':') > 0)
  281. {
  282. ParseTable(sb, strs[m], ':');
  283. }
  284. else
  285. {
  286. if (strs[m].IndexOf('.') > 0)
  287. {
  288. Convert.ToDouble(strs[m]);
  289. }
  290. else
  291. {
  292. Convert.ToInt32(strs[m]);
  293. }
  294. sb.Append(strs[m]);
  295. }
  296. }
  297. catch
  298. {
  299. if (!string.IsNullOrEmpty(strs[m]))
  300. sb.Append("'" + strs[m] + "'");
  301. }
  302. if (m < strs.Length - 1)
  303. {
  304. sb.Append(",");
  305. }
  306. }
  307. sb.Append("}");
  308. }
  309. List<string> GetContext(string filePath)
  310. {
  311. StreamReader sr = new StreamReader(filePath);
  312. List<string> contexts = new List<string>();
  313. string line;
  314. while ((line = sr.ReadLine()) != null)
  315. contexts.Add(line);
  316. return contexts;
  317. }
  318. string[] GetPropertyName(string line)
  319. {
  320. string[] names = line.Split(',');
  321. return names;
  322. }
  323. void Save(string path, string information)
  324. {
  325. if (File.Exists(path))
  326. File.Delete(path);
  327. FileStream aFile = new FileStream(@"" + path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  328. StreamWriter sw = new StreamWriter(aFile);
  329. sw.Write(information);
  330. sw.Close();
  331. sw.Dispose();
  332. aFile.Close();
  333. aFile.Dispose();
  334. sw = null;
  335. aFile = null;
  336. AssetDatabase.Refresh();
  337. }
  338. public static Dictionary<string, List<CfgLanguageCfg>> GetCfgLanguageCfg(string path)
  339. {
  340. Dictionary<string, List<CfgLanguageCfg>> ret = new Dictionary<string, List<CfgLanguageCfg>>();
  341. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
  342. CsvReader csvReader = new CsvReader(CfgLanguageCfg.FileName_S(), ta.bytes);
  343. CfgLanguageCfg.OnCsvLoad(csvReader);
  344. CfgLanguageCfg.Foreach(it =>
  345. {
  346. if (it.Enable == 0)
  347. {
  348. return;
  349. }
  350. if (ret.ContainsKey(it.CfgName))
  351. {
  352. ret[it.CfgName].Add(it);
  353. }
  354. else
  355. {
  356. List<CfgLanguageCfg> cfgs = new List<CfgLanguageCfg>();
  357. cfgs.Add(it);
  358. ret.Add(it.CfgName,cfgs);
  359. }
  360. //Debug.Log(it.BaseName);
  361. });
  362. CfgLanguageCfg.Clear();
  363. return ret;
  364. }
  365. public static Dictionary<string, LanguageCfg> GetLanguageCfg(string path)
  366. {
  367. Dictionary<string, LanguageCfg> ret = new Dictionary<string, LanguageCfg>();
  368. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
  369. CsvReader csvReader = new CsvReader(LanguageCfg.FileName_S(), ta.bytes,1,2,3);
  370. LanguageCfg.OnCsvLoad(csvReader);
  371. LanguageCfg.Foreach(it =>
  372. {
  373. if (!ret.ContainsKey(it.key))
  374. {
  375. ret.Add(it.key, it);
  376. }
  377. else
  378. {
  379. ret.Add(it.key+ret.Count,it);
  380. Debug.Log("重复项:" + it.key);
  381. }
  382. //Debug.Log(it.BaseName);
  383. });
  384. LanguageCfg.Clear(false);
  385. return ret;
  386. }
  387. public static void CheckCgf()
  388. {
  389. string luaPath = "Assets/Lua/Config/";
  390. string csvPath = "Assets/Content/Config/";
  391. string OutPutPath = "Build/NewCfg/";
  392. Dictionary<string, List<CfgLanguageCfg>> cfgs = GetCfgLanguageCfg("Assets/Editor/CsvToLua/CfgLanguageCfg.csv");
  393. Dictionary<string, LanguageCfg> language = GetLanguageCfg("Assets/Content/Config/LanguagePackage_cn.csv");
  394. float ckcount = 0;
  395. foreach (var item in cfgs)
  396. {
  397. ckcount+=1;
  398. List<CfgLanguageCfg> value = item.Value;
  399. EditorUtility.DisplayProgressBar("检测配置中", $"检测:{item.Key}...", ckcount/cfgs.Count);
  400. string cfgPath = "";
  401. if (value[0].CfgType == 1)
  402. {
  403. cfgPath = luaPath + item.Key + ".lua";
  404. CheckLauCfg_CS(value,cfgPath,language, $"{OutPutPath}lua/{value[0].CfgName}.lua");
  405. }
  406. else
  407. {
  408. cfgPath = csvPath + item.Key + ".csv";
  409. CheckCsvCfg(value, cfgPath, language, $"{OutPutPath}csv/{value[0].CfgName}.csv");
  410. }
  411. }
  412. EditorUtility.ClearProgressBar();
  413. CsvWriter<LanguageCfg> csvWriter = new CsvWriter<LanguageCfg>(OutPutPath+ "Language.csv", "Language.csv",language.Values.ToList(), LanguageCfg.GetFormatInfo());
  414. csvWriter.Write();
  415. }
  416. private static void CheckLauCfg(List<CfgLanguageCfg> cfg,string path, Dictionary<string, LanguageCfg> language,string outputPath)
  417. {
  418. using (LuaState runTime = new LuaState())
  419. {
  420. runTime.Start();
  421. RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, path, (id,luatable) =>
  422. {
  423. for (int i = 0; i < cfg.Count; i++)
  424. {
  425. if (cfg[i].Enable == 0)
  426. continue;
  427. string value = luatable.RawGet<string, string>(cfg[i].Field);
  428. if (string.IsNullOrEmpty(value))
  429. {
  430. continue;
  431. }
  432. KeyValuePair<string,LanguageCfg> valuePair = language.FirstOrDefault(it =>
  433. {
  434. return string.Equals(it.Value.Language, value);
  435. });
  436. if (!string.IsNullOrEmpty(valuePair.Key))
  437. {
  438. luatable.RawSet<string, string>(cfg[i].Field, valuePair.Key);
  439. }
  440. else
  441. {
  442. string key = cfg[i].CfgID + id;
  443. luatable.RawSet<string, string>(cfg[i].Field, key);
  444. if (value.Contains("\n"))
  445. {
  446. Debug.Log(value);
  447. value = value.Replace("\n","\\n");
  448. Debug.Log(value);
  449. }
  450. language.Add(key,new LanguageCfg() { key = key,Language = value});
  451. }
  452. }
  453. },(ltab)=>
  454. {
  455. bool useObj = IsObj;
  456. if (useObj)
  457. {
  458. LuaState luaState = LuaClient.Instance.luaState;
  459. luaState.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
  460. RolePreviewWindow.SaveToLua(luaState, outputPath, ltab, cfg[0].CfgName, "1");
  461. }
  462. else
  463. {
  464. LuaState luaState = runTime;
  465. luaState.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
  466. RolePreviewWindow.SaveToLua(luaState, outputPath, ltab, cfg[0].CfgName, "");
  467. }
  468. });
  469. }
  470. }
  471. private static void SetLuaLine(string[] rowDatas,int id,string key,string data,string dataF = "'{0}',")
  472. {
  473. string idkey = $"[{id}]";
  474. string keykey = $"'{key}'";
  475. string wdata = string.Format(dataF,data);//$"'{data}',";
  476. bool isFind = false;
  477. for (int line = 0; line < rowDatas.Length; line++)
  478. {
  479. if (rowDatas[line].Trim().IndexOf("--") == 0)
  480. {
  481. Debug.Log("注释跳过");
  482. }
  483. if (rowDatas[line].Contains(idkey))
  484. {
  485. isFind = true;
  486. }
  487. if (isFind && rowDatas[line].Contains(keykey))
  488. {
  489. int s = rowDatas[line].IndexOf('[');
  490. string p = "";
  491. if (s > 0)
  492. {
  493. for (int i = 0; i < s; i++)
  494. {
  495. p += " ";
  496. }
  497. }
  498. string newstr = $"{p}[{keykey}]={wdata}" ;
  499. rowDatas[line] = newstr;
  500. break;
  501. }
  502. }
  503. }
  504. private static void CheckLauCfg_CS(List<CfgLanguageCfg> cfg, string path, Dictionary<string, LanguageCfg> language, string outputPath)
  505. {
  506. string[] rowDatas = File.ReadAllLines(path);
  507. bool iswrite = true;
  508. using (LuaState runTime = new LuaState())
  509. {
  510. runTime.Start();
  511. RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, path, (id, luatable) =>
  512. {
  513. for (int i = 0; i < cfg.Count; i++)
  514. {
  515. if (cfg[i].Enable == 0)
  516. {
  517. iswrite = false;
  518. continue;
  519. }
  520. string value = luatable.RawGet<string, string>(cfg[i].Field);
  521. if (string.IsNullOrEmpty(value))
  522. {
  523. continue;
  524. }
  525. KeyValuePair<string, LanguageCfg> valuePair = language.FirstOrDefault(it =>
  526. {
  527. return string.Equals(it.Value.Language, value);
  528. });
  529. if (!string.IsNullOrEmpty(valuePair.Key))
  530. {
  531. luatable.RawSet<string, string>(cfg[i].Field, valuePair.Key);
  532. SetLuaLine(rowDatas,id, cfg[i].Field, valuePair.Key);
  533. }
  534. else
  535. {
  536. string key = cfg[i].CfgID + id;
  537. luatable.RawSet<string, string>(cfg[i].Field, key);
  538. if (value.Contains("\n"))
  539. {
  540. //Debug.Log(value);
  541. value = value.Replace("\n", "\\n");
  542. //Debug.Log(value);
  543. }
  544. if (!language.ContainsKey(key))
  545. {
  546. valuePair = language.FirstOrDefault(it =>
  547. {
  548. return string.Equals(it.Value.key, value);
  549. });
  550. if (string.IsNullOrEmpty(valuePair.Key))
  551. {
  552. SetLuaLine(rowDatas, id, cfg[i].Field, key);
  553. language.Add(key, new LanguageCfg() { key = key, Language = value });
  554. }
  555. }
  556. }
  557. }
  558. });
  559. }
  560. if (iswrite)
  561. File.WriteAllLines(outputPath,rowDatas);
  562. }
  563. private static void CheckCsvCfg(List<CfgLanguageCfg> cfg, string path, Dictionary<string, LanguageCfg> language, string outputPath)
  564. {
  565. CsvReader csvReader = new CsvReader(path,1,2,3);
  566. string[][] datas = csvReader.RowDatas;
  567. for (int i = csvReader.StartLine; i < datas.Length; i++)
  568. {
  569. string[] curLine = datas[i];
  570. for (int j = 0; j < cfg.Count; j++)
  571. {
  572. if (cfg[j].Enable == 0)
  573. continue;
  574. string value = csvReader.GetDataByFieldName(cfg[j].Field,i);
  575. if (string.IsNullOrEmpty(value))
  576. {
  577. continue;
  578. }
  579. KeyValuePair<string, LanguageCfg> valuePair = language.FirstOrDefault(it =>
  580. {
  581. return string.Equals(it.Value.Language, value);
  582. });
  583. if (!string.IsNullOrEmpty(valuePair.Key))
  584. {
  585. csvReader.SetDataByFieldName(cfg[j].Field, valuePair.Key, i);
  586. }
  587. else
  588. {
  589. string key = cfg[j].CfgID + i;
  590. csvReader.SetDataByFieldName(cfg[j].Field, key,i);
  591. language.Add(key, new LanguageCfg() { key = key, Language = value });
  592. }
  593. }
  594. }
  595. FileHelper.WirteStringToFile(outputPath,csvReader.GetString());
  596. }
  597. private static void ChangeLuaCfg(string luacfgpath,string luachagepath,string outputPath)
  598. {
  599. string[] rowDatas = File.ReadAllLines(luacfgpath);
  600. using (LuaState runTime = new LuaState())
  601. {
  602. runTime.Start();
  603. LuaTable luaTable = runTime.DoFile<LuaTable>(luachagepath);
  604. RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, luacfgpath, (id, luatable) =>
  605. {
  606. LuaFunction luaFunction = luaTable.GetLuaFunction("IsChangefg");
  607. bool ischange = luaFunction.Invoke<LuaTable,LuaTable, bool>(luaTable,luatable);
  608. if (ischange)
  609. {
  610. LuaTable fieldValue = luaTable.Invoke<LuaTable,LuaTable>("GetFieldAddValue", luaTable);
  611. string field = fieldValue.RawGet<string, string>("field");
  612. string value = fieldValue.RawGet<string, string>("value");
  613. SetLuaLine(rowDatas,id,field,value.ToString(),"{0},");
  614. }
  615. });
  616. }
  617. File.WriteAllLines(outputPath, rowDatas);
  618. }
  619. struct lgInfo
  620. {
  621. public int index;
  622. public int off;
  623. }
  624. private static void MergeLanauage(string oldPath,string newPath, string outputPath)
  625. {
  626. List<string> oldLg = File.ReadAllLines(oldPath).ToList();
  627. List<string> newLg = File.ReadAllLines(newPath).ToList();
  628. Dictionary<string, lgInfo> cfgmap = new Dictionary<string, lgInfo>();
  629. int length = oldLg.Count;
  630. int off = -1;
  631. for (int i = 0; i < length; i++)
  632. {
  633. off = oldLg[i].IndexOf(',');
  634. if (off < 0)
  635. {
  636. continue;
  637. }
  638. string key = oldLg[i].Substring(0,off);
  639. if (!cfgmap.ContainsKey(key))
  640. {
  641. lgInfo info = new lgInfo();
  642. info.index = i;
  643. info.off = off;
  644. cfgmap.Add(key, info);
  645. }
  646. }
  647. foreach (var item in newLg)
  648. {
  649. if (item == "" || item == ","||item == "key"||item == "string")
  650. continue;
  651. off = item.IndexOf(',');
  652. if (off < 0)
  653. {
  654. continue;
  655. }
  656. string key = item.Substring(0,off);
  657. if (!cfgmap.ContainsKey(key))
  658. {
  659. Debug.Log($"不包含:{key} off = {off}");
  660. oldLg.Add(item);
  661. }
  662. else
  663. {
  664. lgInfo lgIf = cfgmap[key];
  665. oldLg[lgIf.index] = item;
  666. }
  667. }
  668. string data = FileHelper.CatStringArray(oldLg,"\r\n");
  669. FileHelper.WirteStringToFile(outputPath, data);
  670. Debug.Log("合并完成");
  671. }
  672. }