CsvToLua.cs 31 KB

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