CsvToLua.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. if (GUILayout.Button("二进制转换"))
  141. {
  142. B2T("D:/项目/其他/Language15000-19000.csv", "D:/项目/其他/111.csv",Encoding.UTF8);
  143. }
  144. }
  145. public static string GetFilePath()
  146. {
  147. OpenDialogDir ofn2 = new OpenDialogDir();
  148. ofn2.pszDisplayName = new string(new char[2000]); ; // 存放目录路径缓冲区
  149. ofn2.lpszTitle = "Open Project";// 标题
  150. //ofn2.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // 新的样式,带编辑框
  151. IntPtr pidlPtr = DllOpenFileDialog.SHBrowseForFolder(ofn2);
  152. char[] charArray = new char[2000];
  153. for (int i = 0; i < 2000; i++)
  154. charArray[i] = '\0';
  155. DllOpenFileDialog.SHGetPathFromIDList(pidlPtr, charArray);
  156. string fullDirPath = new String(charArray);
  157. fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));
  158. return fullDirPath;
  159. }
  160. public void Change()
  161. {
  162. Debug.Log(tableDirectoryName);
  163. foreach (var item in Directory.GetFiles(tableDirectoryName))
  164. {
  165. if (item.Contains(".meta"))
  166. continue;
  167. CreatLuaTable(item);
  168. }
  169. }
  170. void CreatLuaTable(string filePath)
  171. {
  172. CreatDirectory();
  173. Save(GetSavePath(filePath), GetSaveContext(filePath));
  174. }
  175. void CreatDirectory()
  176. {
  177. string path = luaDirectoryName;
  178. if (!Directory.Exists(path))
  179. {
  180. Directory.CreateDirectory(path);
  181. AssetDatabase.Refresh();
  182. }
  183. }
  184. string GetSavePath(string filePath)
  185. {
  186. string fileName = GetFileName(filePath);
  187. return luaDirectoryName + "/" + fileName + ".lua";
  188. }
  189. string GetFileName(string filePath)
  190. {
  191. string targetPath = filePath.Replace(@"\", "/");
  192. string[] strs = targetPath.Split('/');
  193. targetPath = strs[strs.Length - 1];
  194. strs = targetPath.Split('.');
  195. string fileName = strs[0];
  196. return fileName;
  197. }
  198. string GetSaveContext(string filePath)
  199. {
  200. string fileName = GetFileName(filePath);
  201. List<string> context = GetContext(filePath);
  202. string[] propertyName = GetPropertyName(context[1]);
  203. string[] typeName = GetPropertyName(context[2]);
  204. StringBuilder sb = new StringBuilder();
  205. sb.Append("local " + fileName + " = {" + "\n");
  206. for (int i = startDataLine; i < context.Count; i++)
  207. {
  208. context[i] = context[i].Replace(" ", "");
  209. string[] details = context[i].Split(',');
  210. sb.Append("[" + details[0] + "]={" + "\n");
  211. for (int j = 0; j < propertyName.Length; j++)
  212. {
  213. sb.Append("['" + propertyName[j] + "']=");
  214. if (typeName[j].Equals("string"))
  215. sb.Append("'" + details[j] + "'," + "\n");
  216. else if (typeName[j].Equals("bool"))
  217. {
  218. sb.Append(details[j].ToLower() + "," + "\n");
  219. }
  220. else if (typeName[j].Equals("list"))
  221. {
  222. if (details[j].IndexOf(';') > 0)
  223. {
  224. ParseTable(sb, details[j], ';');
  225. }
  226. else if (details[j].IndexOf(':') > 0 && details[j].IndexOf(';') < 0)
  227. {
  228. sb.Append("{");
  229. ParseTable(sb, details[j], ':');
  230. sb.Append("}");
  231. }
  232. else
  233. {
  234. sb.Append("{");
  235. try
  236. {
  237. if (details[j].IndexOf('.') > 0)
  238. {
  239. Convert.ToDouble(details[j]);
  240. }
  241. else
  242. {
  243. Convert.ToInt32(details[j]);
  244. }
  245. sb.Append(details[j]);
  246. }
  247. catch
  248. {
  249. if (!string.IsNullOrEmpty(details[j]))
  250. sb.Append("'" + details[j] + "'");
  251. }
  252. sb.Append("}");
  253. }
  254. sb.Append(",\n");
  255. }
  256. else if (typeName[j].Equals("enum"))
  257. {
  258. sb.Append("Enum." + propertyName[j] + "." + details[j] + "," + "\n");
  259. }
  260. else
  261. {
  262. string num = "0";
  263. if (details[j] != "")
  264. {
  265. num = details[j];
  266. }
  267. sb.Append(num + "," + "\n");
  268. }
  269. }
  270. sb.Append("}," + "\n");
  271. }
  272. sb.Append("}" + "\n");
  273. sb.Append("return " + fileName);
  274. return sb.ToString();
  275. }
  276. void ParseTable(StringBuilder sb, string content, char split)
  277. {
  278. string[] strs = content.Split(split);
  279. sb.Append("{");
  280. for (int m = 0; m < strs.Length; ++m)
  281. {
  282. try
  283. {
  284. if (strs[m].IndexOf(':') > 0)
  285. {
  286. ParseTable(sb, strs[m], ':');
  287. }
  288. else
  289. {
  290. if (strs[m].IndexOf('.') > 0)
  291. {
  292. Convert.ToDouble(strs[m]);
  293. }
  294. else
  295. {
  296. Convert.ToInt32(strs[m]);
  297. }
  298. sb.Append(strs[m]);
  299. }
  300. }
  301. catch
  302. {
  303. if (!string.IsNullOrEmpty(strs[m]))
  304. sb.Append("'" + strs[m] + "'");
  305. }
  306. if (m < strs.Length - 1)
  307. {
  308. sb.Append(",");
  309. }
  310. }
  311. sb.Append("}");
  312. }
  313. List<string> GetContext(string filePath)
  314. {
  315. StreamReader sr = new StreamReader(filePath);
  316. List<string> contexts = new List<string>();
  317. string line;
  318. while ((line = sr.ReadLine()) != null)
  319. contexts.Add(line);
  320. return contexts;
  321. }
  322. string[] GetPropertyName(string line)
  323. {
  324. string[] names = line.Split(',');
  325. return names;
  326. }
  327. void Save(string path, string information)
  328. {
  329. if (File.Exists(path))
  330. File.Delete(path);
  331. FileStream aFile = new FileStream(@"" + path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  332. StreamWriter sw = new StreamWriter(aFile);
  333. sw.Write(information);
  334. sw.Close();
  335. sw.Dispose();
  336. aFile.Close();
  337. aFile.Dispose();
  338. sw = null;
  339. aFile = null;
  340. AssetDatabase.Refresh();
  341. }
  342. public static Dictionary<string, List<CfgLanguageCfg>> GetCfgLanguageCfg(string path)
  343. {
  344. Dictionary<string, List<CfgLanguageCfg>> ret = new Dictionary<string, List<CfgLanguageCfg>>();
  345. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
  346. CsvReader csvReader = new CsvReader(CfgLanguageCfg.FileName_S(), ta.bytes);
  347. CfgLanguageCfg.OnCsvLoad(csvReader);
  348. CfgLanguageCfg.Foreach(it =>
  349. {
  350. if (it.Enable == 0)
  351. {
  352. return;
  353. }
  354. if (ret.ContainsKey(it.CfgName))
  355. {
  356. ret[it.CfgName].Add(it);
  357. }
  358. else
  359. {
  360. List<CfgLanguageCfg> cfgs = new List<CfgLanguageCfg>();
  361. cfgs.Add(it);
  362. ret.Add(it.CfgName,cfgs);
  363. }
  364. //Debug.Log(it.BaseName);
  365. });
  366. CfgLanguageCfg.Clear();
  367. return ret;
  368. }
  369. public static Dictionary<string, LanguageCfg> GetLanguageCfg(string path)
  370. {
  371. Dictionary<string, LanguageCfg> ret = new Dictionary<string, LanguageCfg>();
  372. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
  373. CsvReader csvReader = new CsvReader(LanguageCfg.FileName_S(), ta.bytes,1,2,3);
  374. LanguageCfg.OnCsvLoad(csvReader);
  375. LanguageCfg.Foreach(it =>
  376. {
  377. if (!ret.ContainsKey(it.key))
  378. {
  379. ret.Add(it.key, it);
  380. }
  381. else
  382. {
  383. ret.Add(it.key+ret.Count,it);
  384. Debug.Log("重复项:" + it.key);
  385. }
  386. //Debug.Log(it.BaseName);
  387. });
  388. LanguageCfg.Clear(false);
  389. return ret;
  390. }
  391. public static void CheckCgf()
  392. {
  393. string luaPath = "Assets/Lua/Config/";
  394. string csvPath = "Assets/Content/Config/";
  395. string OutPutPath = "Build/NewCfg/";
  396. Dictionary<string, List<CfgLanguageCfg>> cfgs = GetCfgLanguageCfg("Assets/Editor/CsvToLua/CfgLanguageCfg.csv");
  397. Dictionary<string, LanguageCfg> language = GetLanguageCfg("Assets/Content/Config/LanguagePackage_cn.csv");
  398. float ckcount = 0;
  399. foreach (var item in cfgs)
  400. {
  401. ckcount+=1;
  402. List<CfgLanguageCfg> value = item.Value;
  403. EditorUtility.DisplayProgressBar("检测配置中", $"检测:{item.Key}...", ckcount/cfgs.Count);
  404. string cfgPath = "";
  405. if (value[0].CfgType == 1)
  406. {
  407. cfgPath = luaPath + item.Key + ".lua";
  408. CheckLauCfg_CS(value,cfgPath,language, $"{OutPutPath}lua/{value[0].CfgName}.lua");
  409. }
  410. else
  411. {
  412. cfgPath = csvPath + item.Key + ".csv";
  413. CheckCsvCfg(value, cfgPath, language, $"{OutPutPath}csv/{value[0].CfgName}.csv");
  414. }
  415. }
  416. EditorUtility.ClearProgressBar();
  417. CsvWriter<LanguageCfg> csvWriter = new CsvWriter<LanguageCfg>(OutPutPath+ "Language.csv", "Language.csv",language.Values.ToList(), LanguageCfg.GetFormatInfo());
  418. csvWriter.Write();
  419. }
  420. private static void CheckLauCfg(List<CfgLanguageCfg> cfg,string path, Dictionary<string, LanguageCfg> language,string outputPath)
  421. {
  422. using (LuaState runTime = new LuaState())
  423. {
  424. runTime.Start();
  425. RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, path, (id,luatable) =>
  426. {
  427. for (int i = 0; i < cfg.Count; i++)
  428. {
  429. if (cfg[i].Enable == 0)
  430. continue;
  431. string value = luatable.RawGet<string, string>(cfg[i].Field);
  432. if (string.IsNullOrEmpty(value))
  433. {
  434. continue;
  435. }
  436. KeyValuePair<string,LanguageCfg> valuePair = language.FirstOrDefault(it =>
  437. {
  438. return string.Equals(it.Value.Language, value);
  439. });
  440. if (!string.IsNullOrEmpty(valuePair.Key))
  441. {
  442. luatable.RawSet<string, string>(cfg[i].Field, valuePair.Key);
  443. }
  444. else
  445. {
  446. string key = cfg[i].CfgID + id;
  447. luatable.RawSet<string, string>(cfg[i].Field, key);
  448. if (value.Contains("\n"))
  449. {
  450. Debug.Log(value);
  451. value = value.Replace("\n","\\n");
  452. Debug.Log(value);
  453. }
  454. language.Add(key,new LanguageCfg() { key = key,Language = value});
  455. }
  456. }
  457. },(ltab)=>
  458. {
  459. bool useObj = IsObj;
  460. if (useObj)
  461. {
  462. LuaState luaState = LuaClient.Instance.luaState;
  463. luaState.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
  464. RolePreviewWindow.SaveToLua(luaState, outputPath, ltab, cfg[0].CfgName, "1");
  465. }
  466. else
  467. {
  468. LuaState luaState = runTime;
  469. luaState.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
  470. RolePreviewWindow.SaveToLua(luaState, outputPath, ltab, cfg[0].CfgName, "");
  471. }
  472. });
  473. }
  474. }
  475. private static void SetLuaLine(string[] rowDatas,int id,string key,string data,string dataF = "'{0}',")
  476. {
  477. string idkey = $"[{id}]";
  478. string keykey = $"'{key}'";
  479. string wdata = string.Format(dataF,data);//$"'{data}',";
  480. bool isFind = false;
  481. for (int line = 0; line < rowDatas.Length; line++)
  482. {
  483. if (rowDatas[line].Trim().IndexOf("--") == 0)
  484. {
  485. Debug.Log("注释跳过");
  486. }
  487. if (rowDatas[line].Contains(idkey))
  488. {
  489. isFind = true;
  490. }
  491. if (isFind && rowDatas[line].Contains(keykey))
  492. {
  493. int s = rowDatas[line].IndexOf('[');
  494. string p = "";
  495. if (s > 0)
  496. {
  497. for (int i = 0; i < s; i++)
  498. {
  499. p += " ";
  500. }
  501. }
  502. string newstr = $"{p}[{keykey}]={wdata}" ;
  503. rowDatas[line] = newstr;
  504. break;
  505. }
  506. }
  507. }
  508. private static void CheckLauCfg_CS(List<CfgLanguageCfg> cfg, string path, Dictionary<string, LanguageCfg> language, string outputPath)
  509. {
  510. string[] rowDatas = File.ReadAllLines(path);
  511. bool iswrite = true;
  512. using (LuaState runTime = new LuaState())
  513. {
  514. runTime.Start();
  515. RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, path, (id, luatable) =>
  516. {
  517. for (int i = 0; i < cfg.Count; i++)
  518. {
  519. if (cfg[i].Enable == 0)
  520. {
  521. iswrite = false;
  522. continue;
  523. }
  524. string value = luatable.RawGet<string, string>(cfg[i].Field);
  525. if (string.IsNullOrEmpty(value))
  526. {
  527. continue;
  528. }
  529. KeyValuePair<string, LanguageCfg> valuePair = language.FirstOrDefault(it =>
  530. {
  531. return string.Equals(it.Value.Language, value);
  532. });
  533. if (!string.IsNullOrEmpty(valuePair.Key))
  534. {
  535. luatable.RawSet<string, string>(cfg[i].Field, valuePair.Key);
  536. SetLuaLine(rowDatas,id, cfg[i].Field, valuePair.Key);
  537. }
  538. else
  539. {
  540. string key = cfg[i].CfgID + id;
  541. luatable.RawSet<string, string>(cfg[i].Field, key);
  542. if (value.Contains("\n"))
  543. {
  544. //Debug.Log(value);
  545. value = value.Replace("\n", "\\n");
  546. //Debug.Log(value);
  547. }
  548. if (!language.ContainsKey(key))
  549. {
  550. valuePair = language.FirstOrDefault(it =>
  551. {
  552. return string.Equals(it.Value.key, value);
  553. });
  554. if (string.IsNullOrEmpty(valuePair.Key))
  555. {
  556. SetLuaLine(rowDatas, id, cfg[i].Field, key);
  557. language.Add(key, new LanguageCfg() { key = key, Language = value });
  558. }
  559. }
  560. }
  561. }
  562. });
  563. }
  564. if (iswrite)
  565. File.WriteAllLines(outputPath,rowDatas);
  566. }
  567. private static void CheckCsvCfg(List<CfgLanguageCfg> cfg, string path, Dictionary<string, LanguageCfg> language, string outputPath)
  568. {
  569. CsvReader csvReader = new CsvReader(path,1,2,3);
  570. string[][] datas = csvReader.RowDatas;
  571. for (int i = csvReader.StartLine; i < datas.Length; i++)
  572. {
  573. string[] curLine = datas[i];
  574. for (int j = 0; j < cfg.Count; j++)
  575. {
  576. if (cfg[j].Enable == 0)
  577. continue;
  578. string value = csvReader.GetDataByFieldName(cfg[j].Field,i);
  579. if (string.IsNullOrEmpty(value))
  580. {
  581. continue;
  582. }
  583. KeyValuePair<string, LanguageCfg> valuePair = language.FirstOrDefault(it =>
  584. {
  585. return string.Equals(it.Value.Language, value);
  586. });
  587. if (!string.IsNullOrEmpty(valuePair.Key))
  588. {
  589. csvReader.SetDataByFieldName(cfg[j].Field, valuePair.Key, i);
  590. }
  591. else
  592. {
  593. string key = cfg[j].CfgID + i;
  594. csvReader.SetDataByFieldName(cfg[j].Field, key,i);
  595. language.Add(key, new LanguageCfg() { key = key, Language = value });
  596. }
  597. }
  598. }
  599. FileHelper.WirteStringToFile(outputPath,csvReader.GetString());
  600. }
  601. private static void ChangeLuaCfg(string luacfgpath,string luachagepath,string outputPath)
  602. {
  603. string[] rowDatas = File.ReadAllLines(luacfgpath);
  604. using (LuaState runTime = new LuaState())
  605. {
  606. runTime.Start();
  607. LuaTable luaTable = runTime.DoFile<LuaTable>(luachagepath);
  608. RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, luacfgpath, (id, luatable) =>
  609. {
  610. LuaFunction luaFunction = luaTable.GetLuaFunction("IsChangefg");
  611. bool ischange = luaFunction.Invoke<LuaTable,LuaTable, bool>(luaTable,luatable);
  612. if (ischange)
  613. {
  614. LuaTable fieldValue = luaTable.Invoke<LuaTable,LuaTable>("GetFieldAddValue", luaTable);
  615. string field = fieldValue.RawGet<string, string>("field");
  616. string value = fieldValue.RawGet<string, string>("value");
  617. SetLuaLine(rowDatas,id,field,value.ToString(),"{0},");
  618. }
  619. });
  620. }
  621. File.WriteAllLines(outputPath, rowDatas);
  622. }
  623. struct lgInfo
  624. {
  625. public int index;
  626. public int off;
  627. }
  628. private static void MergeLanauage(string oldPath,string newPath, string outputPath)
  629. {
  630. List<string> oldLg = File.ReadAllLines(oldPath).ToList();
  631. List<string> newLg = File.ReadAllLines(newPath).ToList();
  632. Dictionary<string, lgInfo> cfgmap = new Dictionary<string, lgInfo>();
  633. int length = oldLg.Count;
  634. int off = -1;
  635. for (int i = 0; i < length; i++)
  636. {
  637. off = oldLg[i].IndexOf(',');
  638. if (off < 0)
  639. {
  640. continue;
  641. }
  642. string key = oldLg[i].Substring(0,off);
  643. if (!cfgmap.ContainsKey(key))
  644. {
  645. lgInfo info = new lgInfo();
  646. info.index = i;
  647. info.off = off;
  648. cfgmap.Add(key, info);
  649. }
  650. }
  651. foreach (var item in newLg)
  652. {
  653. if (item == "" || item == ","||item == "key"||item == "string")
  654. continue;
  655. off = item.IndexOf(',');
  656. if (off < 0)
  657. {
  658. continue;
  659. }
  660. string key = item.Substring(0,off);
  661. if (!cfgmap.ContainsKey(key))
  662. {
  663. Debug.Log($"不包含:{key} off = {off}");
  664. oldLg.Add(item);
  665. }
  666. else
  667. {
  668. lgInfo lgIf = cfgmap[key];
  669. oldLg[lgIf.index] = item;
  670. }
  671. }
  672. string data = FileHelper.CatStringArray(oldLg,"\r\n");
  673. FileHelper.WirteStringToFile(outputPath, data);
  674. Debug.Log("合并完成");
  675. }
  676. private void B2T(string bPath,string outPath, Encoding read)
  677. {
  678. using (FileStream binaryFileStream = new FileStream(bPath, FileMode.Open, FileAccess.Read))
  679. using (StreamReader reader = new StreamReader(binaryFileStream, read)) // 使用ASCII编码读取
  680. using (StreamWriter writer = new StreamWriter(outPath))
  681. {
  682. writer.Write(reader.ReadToEnd()); // 读取并写入文本文件
  683. }
  684. }
  685. }