CsvToLua.cs 37 KB

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