CsvToLua.cs 28 KB

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