CsvToLua.cs 28 KB

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