CsvToLua.cs 34 KB

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