CsvToLua.cs 34 KB

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