CsvToLua.cs 38 KB

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