| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- using UnityEditor;
- using System.Text;
- using System;
- using Game.Config;
- using RO.Editor;
- using LuaInterface;
- using System.Linq;
- using System.Text.RegularExpressions;
- public class CfgLanguageCfg : GameData<CfgLanguageCfg>
- {
- public string CfgName;
- public string Field;
- public string CfgID;
- public int CfgType;
- public int Enable;
- public string AssetPath;
- public string AssetName;
- public CfgLanguageCfg()
- {
- CfgName = "";
- Field = "";
- CfgID = "";
- CfgType = 0;
- Enable = 0;
- AssetPath = "";
- AssetName = "";
- }
- public static void OnCsvLoad(CsvReader csvReader)
- {
- CfgLanguageCfg.Onload(csvReader);
- }
- public static string FileName_S()
- {
- return "";
- }
- }
- public class LanguageCfg : GameData<LanguageCfg>
- {
- public string key;
- public string Language;
-
- public LanguageCfg()
- {
- key = "";
- Language = "";
- }
- public static void OnCsvLoad(CsvReader csvReader)
- {
- LanguageCfg.Onload(csvReader);
- }
- public static string FileName_S()
- {
- return "";
- }
- }
- public class LocalizedTextureCfg :GameData<LocalizedTextureCfg>
- {
- public string key;
- public string Language;
- public string AssetPath;
- public string OtherSetting;
- public string SetNativeSize;
- public LocalizedTextureCfg()
- {
- key = "";
- Language = "";
- AssetPath = "";
- OtherSetting = "";
- SetNativeSize = "0";
- }
- public static void OnCsvLoad(CsvReader csvReader)
- {
- Onload(csvReader);
- }
- public static string FileName_S()
- {
- return "";
- }
- }
- public class CsvToLua : EditorWindow
- {
- public static string CSV_PATH = "csv path";
- public static string LUA_PATH = "lua path";
- public static string START_LINE = "start line";
- public string tableDirectoryName;
- public string luaDirectoryName;
- public int startDataLine = 1;
- public static bool IsObj;
- [MenuItem("Tools/CsvToLua")]
- static void Init()
- {
- CsvToLua window = (CsvToLua)GetWindow(typeof(CsvToLua));
- window.Show();
- }
- void OnEnable()
- {
- tableDirectoryName = EditorPrefs.GetString(CSV_PATH);
- luaDirectoryName = EditorPrefs.GetString(LUA_PATH);
- startDataLine = EditorPrefs.GetInt(START_LINE);
- }
- void OnDisable()
- {
- EditorPrefs.SetString(CSV_PATH, tableDirectoryName);
- EditorPrefs.SetString(LUA_PATH, luaDirectoryName);
- EditorPrefs.SetInt(START_LINE, startDataLine);
- }
- private void OnGUI()
- {
- GUILayout.Label("Base Settings", EditorStyles.boldLabel);
- EditorGUILayout.BeginHorizontal();
- tableDirectoryName = EditorGUILayout.TextField("TableDirectory Name", tableDirectoryName);
- if (GUILayout.Button("选择csv路径", GUILayout.Width(200)))
- {
- tableDirectoryName = GetFilePath();
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.BeginHorizontal();
- luaDirectoryName = EditorGUILayout.TextField("LuaDirectory Name", luaDirectoryName);
- if (GUILayout.Button("选择到处table路径", GUILayout.Width(200)))
- {
- luaDirectoryName = GetFilePath();
- }
- EditorGUILayout.EndHorizontal();
- startDataLine = EditorGUILayout.IntField("StartDataLine", startDataLine);
- if (GUILayout.Button("CsvToLua"))
- Change();
- if (GUILayout.Button("测试"))
- {
- string lua = @"local aaa =
- {
- [1] =
- {
- ['a'] = 1,
- ['b'] = '2',
- ['c'] = 'asd' ,
- ['d'] = {1,2,3,{{1,2},{3,4},{1.2,0},{0}}},
- ['aId'] = 1,
- } ,
- }
- return aaa";
- using (LuaState runTime = new LuaState())
- {
- runTime.Start();
- //LuaState runTime = LuaClient.Instance.luaState;
- runTime.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
- LuaTable luaTable = runTime.DoString<LuaTable>(lua);
- RolePreviewWindow.SaveToLua(runTime, "aaa.lua", luaTable, "AAA","");
- }
- }
- if (GUILayout.Button("提取语言配置"))
- {
- CheckCgf();
- }
- if (GUILayout.Button("提取图片配置"))
- {
- CheckTextureCgf();
- }
- IsObj = GUILayout.Toggle(IsObj,"运行时启动");
- if (GUILayout.Button("更改CardCfg"))
- {
- ChangeLuaCfg("Assets/Lua/Config/CardCfg.lua", "Assets\\Editor\\CsvToLua\\LuaCfg.lua", "Build/NewCfg/CardCfg.lua");
- }
- if (GUILayout.Button("合并Language"))
- {
- MergeLanauage("Assets/Content/Config/LanguagePackage_cn.csv", "Build/NewCfg/ROLanguagePackage_cn.csv", "Build/NewCfg/Language_merge.csv");
- }
- if (GUILayout.Button("二进制转换"))
- {
- B2T("D:/项目/其他/Language15000-19000.csv", "D:/项目/其他/111.csv",Encoding.UTF8);
- }
- if (GUILayout.Button("复制luacfg文件"))
- {
- CopyFleByLuaCfg("Assets\\Editor\\CsvToLua\\luaCopyFileCfg.lua", "", "D:/项目/图/仙境/cn/","_cn");
- }
- if (GUILayout.Button("添加图片配置"))
- {
- AddLocalizTextureCfgByFileCfg("Assets\\Editor\\CsvToLua\\luaCopyFileCfg.lua");
- }
- }
- public static string GetFilePath()
- {
- OpenDialogDir ofn2 = new OpenDialogDir();
- ofn2.pszDisplayName = new string(new char[2000]); ; // 存放目录路径缓冲区
- ofn2.lpszTitle = "Open Project";// 标题
- //ofn2.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // 新的样式,带编辑框
- IntPtr pidlPtr = DllOpenFileDialog.SHBrowseForFolder(ofn2);
- char[] charArray = new char[2000];
- for (int i = 0; i < 2000; i++)
- charArray[i] = '\0';
- DllOpenFileDialog.SHGetPathFromIDList(pidlPtr, charArray);
- string fullDirPath = new String(charArray);
- fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));
- return fullDirPath;
- }
- public void Change()
- {
- Debug.Log(tableDirectoryName);
- foreach (var item in Directory.GetFiles(tableDirectoryName))
- {
- if (item.Contains(".meta"))
- continue;
- CreatLuaTable(item);
- }
- }
- void CreatLuaTable(string filePath)
- {
- CreatDirectory();
- Save(GetSavePath(filePath), GetSaveContext(filePath));
- }
- void CreatDirectory()
- {
- string path = luaDirectoryName;
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- AssetDatabase.Refresh();
- }
- }
- string GetSavePath(string filePath)
- {
- string fileName = GetFileName(filePath);
- return luaDirectoryName + "/" + fileName + ".lua";
- }
- string GetFileName(string filePath)
- {
- string targetPath = filePath.Replace(@"\", "/");
- string[] strs = targetPath.Split('/');
- targetPath = strs[strs.Length - 1];
- strs = targetPath.Split('.');
- string fileName = strs[0];
- return fileName;
- }
- string GetSaveContext(string filePath)
- {
- string fileName = GetFileName(filePath);
- List<string> context = GetContext(filePath);
- string[] propertyName = GetPropertyName(context[1]);
- string[] typeName = GetPropertyName(context[2]);
- StringBuilder sb = new StringBuilder();
- sb.Append("local " + fileName + " = {" + "\n");
- for (int i = startDataLine; i < context.Count; i++)
- {
- context[i] = context[i].Replace(" ", "");
- string[] details = context[i].Split(',');
- sb.Append("[" + details[0] + "]={" + "\n");
- for (int j = 0; j < propertyName.Length; j++)
- {
- sb.Append("['" + propertyName[j] + "']=");
- if (typeName[j].Equals("string"))
- sb.Append("'" + details[j] + "'," + "\n");
- else if (typeName[j].Equals("bool"))
- {
- sb.Append(details[j].ToLower() + "," + "\n");
- }
- else if (typeName[j].Equals("list"))
- {
- if (details[j].IndexOf(';') > 0)
- {
- ParseTable(sb, details[j], ';');
- }
- else if (details[j].IndexOf(':') > 0 && details[j].IndexOf(';') < 0)
- {
- sb.Append("{");
- ParseTable(sb, details[j], ':');
- sb.Append("}");
- }
- else
- {
- sb.Append("{");
- try
- {
- if (details[j].IndexOf('.') > 0)
- {
- Convert.ToDouble(details[j]);
- }
- else
- {
- Convert.ToInt32(details[j]);
- }
- sb.Append(details[j]);
- }
- catch
- {
- if (!string.IsNullOrEmpty(details[j]))
- sb.Append("'" + details[j] + "'");
- }
- sb.Append("}");
- }
- sb.Append(",\n");
- }
- else if (typeName[j].Equals("enum"))
- {
- sb.Append("Enum." + propertyName[j] + "." + details[j] + "," + "\n");
- }
- else
- {
- string num = "0";
- if (details[j] != "")
- {
- num = details[j];
- }
- sb.Append(num + "," + "\n");
- }
- }
- sb.Append("}," + "\n");
- }
- sb.Append("}" + "\n");
- sb.Append("return " + fileName);
- return sb.ToString();
- }
- void ParseTable(StringBuilder sb, string content, char split)
- {
- string[] strs = content.Split(split);
- sb.Append("{");
- for (int m = 0; m < strs.Length; ++m)
- {
- try
- {
- if (strs[m].IndexOf(':') > 0)
- {
- ParseTable(sb, strs[m], ':');
- }
- else
- {
- if (strs[m].IndexOf('.') > 0)
- {
- Convert.ToDouble(strs[m]);
- }
- else
- {
- Convert.ToInt32(strs[m]);
- }
- sb.Append(strs[m]);
- }
- }
- catch
- {
- if (!string.IsNullOrEmpty(strs[m]))
- sb.Append("'" + strs[m] + "'");
- }
- if (m < strs.Length - 1)
- {
- sb.Append(",");
- }
- }
- sb.Append("}");
- }
- List<string> GetContext(string filePath)
- {
- StreamReader sr = new StreamReader(filePath);
- List<string> contexts = new List<string>();
- string line;
- while ((line = sr.ReadLine()) != null)
- contexts.Add(line);
- return contexts;
- }
- string[] GetPropertyName(string line)
- {
- string[] names = line.Split(',');
- return names;
- }
- void Save(string path, string information)
- {
- if (File.Exists(path))
- File.Delete(path);
- FileStream aFile = new FileStream(@"" + path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
- StreamWriter sw = new StreamWriter(aFile);
- sw.Write(information);
- sw.Close();
- sw.Dispose();
- aFile.Close();
- aFile.Dispose();
- sw = null;
- aFile = null;
- AssetDatabase.Refresh();
- }
- public static Dictionary<string, List<CfgLanguageCfg>> GetCfgLanguageCfg(string path)
- {
- Dictionary<string, List<CfgLanguageCfg>> ret = new Dictionary<string, List<CfgLanguageCfg>>();
-
- TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
- CsvReader csvReader = new CsvReader(CfgLanguageCfg.FileName_S(), ta.bytes);
- CfgLanguageCfg.OnCsvLoad(csvReader);
- CfgLanguageCfg.Foreach(it =>
- {
- if (ret.ContainsKey(it.CfgName))
- {
- ret[it.CfgName].Add(it);
- }
- else
- {
- List<CfgLanguageCfg> cfgs = new List<CfgLanguageCfg>();
- cfgs.Add(it);
- ret.Add(it.CfgName,cfgs);
- }
- //Debug.Log(it.BaseName);
- });
- CfgLanguageCfg.Clear();
- return ret;
- }
- public static Dictionary<string, LanguageCfg> GetLanguageCfg(string path)
- {
- Dictionary<string, LanguageCfg> ret = new Dictionary<string, LanguageCfg>();
-
- TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
- CsvReader csvReader = new CsvReader(LanguageCfg.FileName_S(), ta.bytes,1,2,3);
- LanguageCfg.OnCsvLoad(csvReader);
- LanguageCfg.Foreach(it =>
- {
- if (!ret.ContainsKey(it.key))
- {
- ret.Add(it.key, it);
- }
- else
- {
- ret.Add(it.key+ret.Count,it);
- Debug.Log("重复项:" + it.key);
- }
-
- //Debug.Log(it.BaseName);
- });
- LanguageCfg.Clear(false);
- return ret;
- }
- public static Dictionary<string, LocalizedTextureCfg> GetLanguageTextureCfg(string path)
- {
- Dictionary<string, LocalizedTextureCfg> ret = new Dictionary<string, LocalizedTextureCfg>();
- TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
- CsvReader csvReader = new CsvReader(LanguageCfg.FileName_S(), ta.bytes, 1, 2, 3);
- LocalizedTextureCfg.OnCsvLoad(csvReader);
- LocalizedTextureCfg.Foreach(it =>
- {
- if (!ret.ContainsKey(it.key))
- {
- ret.Add(it.key, it);
- }
- else
- {
- ret.Add(it.key + ret.Count, it);
- Debug.Log("重复项:" + it.key);
- }
- //Debug.Log(it.BaseName);
- });
- LocalizedTextureCfg.Clear(false);
- return ret;
- }
- public static void CheckCgf()
- {
- string luaPath = "Assets/Lua/Config/";
- string csvPath = "Assets/Content/Config/";
- string OutPutPath = "Build/NewCfg/";
- Dictionary<string, List<CfgLanguageCfg>> cfgs = GetCfgLanguageCfg("Assets/Editor/CsvToLua/CfgLanguageCfg.csv");
- Dictionary<string, LanguageCfg> language = GetLanguageCfg("Assets/Content/Config/LanguagePackage_cn.csv");
- float ckcount = 0;
- foreach (var item in cfgs)
- {
- ckcount+=1;
- List<CfgLanguageCfg> value = item.Value;
-
- EditorUtility.DisplayProgressBar("检测配置中", $"检测:{item.Key}...", ckcount/cfgs.Count);
- string cfgPath = "";
- if (value[0].CfgType == 1)
- {
- cfgPath = luaPath + item.Key + ".lua";
- CheckLauCfg_CS(value,cfgPath,language, $"{OutPutPath}lua/{value[0].CfgName}.lua",(it,_v) => string.Equals(it.Value.Language, _v),(obj,key,_value,cfg)=>
- {
- obj.key = key;
- obj.Language = _value;
- });
- }
- else
- {
- cfgPath = csvPath + item.Key + ".csv";
- CheckCsvCfg(value, cfgPath, language, $"{OutPutPath}csv/{value[0].CfgName}.csv", (it, _v) => string.Equals(it.Value.Language, _v), (obj, key, _value, cfg) =>
- {
- obj.key = key;
- obj.Language = _value;
- });
- }
- }
- EditorUtility.ClearProgressBar();
- CsvWriter<LanguageCfg> csvWriter = new CsvWriter<LanguageCfg>(OutPutPath+ "Language.csv", "Language.csv",language.Values.ToList(), LanguageCfg.GetFormatInfo());
- csvWriter.Write();
- }
- public static void CheckTextureCgf()
- {
- string luaPath = "Assets/Lua/Config/";
- string csvPath = "Assets/Content/Config/";
- string OutPutPath = "Build/NewCfg/";
- Dictionary<string, List<CfgLanguageCfg>> cfgs = GetCfgLanguageCfg("Assets/Editor/CsvToLua/CfgLanguageTextureCfg.csv");
- Dictionary<string, LocalizedTextureCfg> language = GetLanguageTextureCfg("Assets/Content/Config/LocalizeTextureCfg_base.csv");
- float ckcount = 0;
- foreach (var item in cfgs)
- {
- ckcount += 1;
- List<CfgLanguageCfg> value = item.Value;
- EditorUtility.DisplayProgressBar("检测配置中", $"检测:{item.Key}...", ckcount / cfgs.Count);
- string cfgPath = "";
- if (value[0].CfgType == 1)
- {
- cfgPath = luaPath + item.Key + ".lua";
- CheckLauCfg_CS(value, cfgPath, language, $"{OutPutPath}lua/{value[0].CfgName}.lua", (it, _v) => string.Equals(it.Value.Language, _v), (obj, key, _value, cfg) =>
- {
- obj.key = key;
- obj.Language = _value;
- obj.AssetPath = cfg.AssetPath;
- });
- }
- else
- {
- cfgPath = csvPath + item.Key + ".csv";
- CheckCsvCfg(value, cfgPath, language, $"{OutPutPath}csv/{value[0].CfgName}.csv", (it, _v) => string.Equals(it.Value.Language, _v), (obj, key, _value, cfg) =>
- {
- obj.key = key;
- obj.Language = _value;
- obj.AssetPath = cfg.AssetPath;
- });
- }
- }
- EditorUtility.ClearProgressBar();
- CsvWriter<LocalizedTextureCfg> csvWriter = new CsvWriter<LocalizedTextureCfg>(OutPutPath + "LocalizedTextureCfg.csv", "LocalizedTextureCfg.csv", language.Values.ToList(), LocalizedTextureCfg.GetFormatInfo());
- csvWriter.Write();
- }
- private static void CheckLauCfg(List<CfgLanguageCfg> cfg,string path, Dictionary<string, LanguageCfg> language,string outputPath)
- {
- using (LuaState runTime = new LuaState())
- {
- runTime.Start();
- RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, path, (id,luatable) =>
- {
- for (int i = 0; i < cfg.Count; i++)
- {
- if (cfg[i].Enable == 0)
- continue;
- string value = luatable.RawGet<string, string>(cfg[i].Field);
- if (string.IsNullOrEmpty(value))
- {
- continue;
- }
- KeyValuePair<string,LanguageCfg> valuePair = language.FirstOrDefault(it =>
- {
- return string.Equals(it.Value.Language, value);
- });
- if (!string.IsNullOrEmpty(valuePair.Key))
- {
- luatable.RawSet<string, string>(cfg[i].Field, valuePair.Key);
- }
- else
- {
- string key = cfg[i].CfgID + id;
- luatable.RawSet<string, string>(cfg[i].Field, key);
- if (value.Contains("\n"))
- {
- Debug.Log(value);
- value = value.Replace("\n","\\n");
- Debug.Log(value);
- }
- language.Add(key,new LanguageCfg() { key = key,Language = value});
- }
- }
- },(ltab)=>
- {
- bool useObj = IsObj;
- if (useObj)
- {
- LuaState luaState = LuaClient.Instance.luaState;
- luaState.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
- RolePreviewWindow.SaveToLua(luaState, outputPath, ltab, cfg[0].CfgName, "1");
- }
- else
- {
- LuaState luaState = runTime;
- luaState.DoFile("Assets\\Editor\\CsvToLua\\table-save.lua");
- RolePreviewWindow.SaveToLua(luaState, outputPath, ltab, cfg[0].CfgName, "");
- }
-
- });
- }
-
- }
- private static void SetLuaLine(string[] rowDatas,int id,string key,string data,string dataF = "'{0}',")
- {
- string idkey = $"[{id}]";
- string keykey = $"'{key}'";
- string wdata = string.Format(dataF,data);//$"'{data}',";
- bool isFind = false;
- for (int line = 0; line < rowDatas.Length; line++)
- {
- if (rowDatas[line].Trim().IndexOf("--") == 0)
- {
- //Debug.Log("注释跳过");
- }
-
- if (rowDatas[line].Contains(idkey))
- {
- isFind = true;
- }
- if (isFind && rowDatas[line].Contains(keykey))
- {
- int s = rowDatas[line].IndexOf('[');
- string p = "";
- if (s > 0)
- {
- for (int i = 0; i < s; i++)
- {
- p += " ";
- }
- }
- string newstr = $"{p}[{keykey}]={wdata}" ;
- rowDatas[line] = newstr;
- break;
- }
- }
- }
- 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
- {
- string[] rowDatas = File.ReadAllLines(path);
- bool iswrite = true;
- using (LuaState runTime = new LuaState())
- {
- runTime.Start();
- RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, path, (id, luatable) =>
- {
- for (int i = 0; i < cfg.Count; i++)
- {
- if (cfg[i].Enable == 0)
- {
- iswrite = false;
- continue;
- }
- string value = luatable.RawGet<string, string>(cfg[i].Field);
- if (string.IsNullOrEmpty(value))
- {
- continue;
- }
- KeyValuePair<string, T> valuePair = language.FirstOrDefault(it=>
- {
- return findcb(it,value);
- });
- if (!string.IsNullOrEmpty(valuePair.Key))
- {
- luatable.RawSet<string, string>(cfg[i].Field, valuePair.Key);
- SetLuaLine(rowDatas,id, cfg[i].Field, valuePair.Key);
- }
- else
- {
- string key = cfg[i].CfgID + id;
- luatable.RawSet<string, string>(cfg[i].Field, key);
- SetLuaLine(rowDatas, id, cfg[i].Field, key);
- if (value.Contains("\n"))
- {
- //Debug.Log(value);
- value = value.Replace("\n", "\\n");
- //Debug.Log(value);
- }
- if (!language.ContainsKey(key))
- {
- Type type = typeof(T);
- T obj = Activator.CreateInstance<T>();
- cb?.Invoke(obj,key,value, cfg[i]);
- language.Add(key, obj);
- }
- }
- }
- });
- }
- if (iswrite)
- File.WriteAllLines(outputPath,rowDatas);
- }
- 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
- {
- CsvReader csvReader = new CsvReader(path,1,2,3);
- string[][] datas = csvReader.RowDatas;
- for (int i = csvReader.StartLine; i < datas.Length; i++)
- {
-
- string[] curLine = datas[i];
- for (int j = 0; j < cfg.Count; j++)
- {
- if (cfg[j].Enable == 0)
- continue;
- string value = csvReader.GetDataByFieldName(cfg[j].Field,i);
- if (string.IsNullOrEmpty(value))
- {
- continue;
- }
- KeyValuePair<string, T> valuePair = language.FirstOrDefault(it =>
- {
- return findcb(it, value);
- });
- if (!string.IsNullOrEmpty(valuePair.Key))
- {
- csvReader.SetDataByFieldName(cfg[j].Field, valuePair.Key, i);
- }
- else
- {
- string key = cfg[j].CfgID + i;
- csvReader.SetDataByFieldName(cfg[j].Field, key,i);
- Type type = typeof(T);
- T obj = Activator.CreateInstance<T>();
- cb?.Invoke(obj, key, value, cfg[i]);
- language.Add(key,obj);
- }
- }
- }
- FileHelper.WirteStringToFile(outputPath,csvReader.GetString());
- }
- private static void ChangeLuaCfg(string luacfgpath,string luachagepath,string outputPath)
- {
- string[] rowDatas = File.ReadAllLines(luacfgpath);
- using (LuaState runTime = new LuaState())
- {
- runTime.Start();
- LuaTable luaTable = runTime.DoFile<LuaTable>(luachagepath);
- RolePreviewWindow.LuaForEach<int, LuaTable>(runTime, luacfgpath, (id, luatable) =>
- {
- LuaFunction luaFunction = luaTable.GetLuaFunction("IsChangefg");
- bool ischange = luaFunction.Invoke<LuaTable,LuaTable, bool>(luaTable,luatable);
- if (ischange)
- {
- LuaTable fieldValue = luaTable.Invoke<LuaTable,LuaTable>("GetFieldAddValue", luaTable);
- string field = fieldValue.RawGet<string, string>("field");
- string value = fieldValue.RawGet<string, string>("value");
-
- SetLuaLine(rowDatas,id,field,value.ToString(),"{0},");
-
- }
- });
-
- }
- File.WriteAllLines(outputPath, rowDatas);
- }
- private static void CopyFleByLuaCfg(string luacfgpath, string fileRootPath,string outputPath,string output)
- {
- using (LuaState runTime = new LuaState())
- {
- runTime.Start();
- LuaTable luaTable = runTime.DoFile<LuaTable>(luacfgpath);
- LuaFunction init_fun = luaTable.GetLuaFunction("Init");
- LuaFunction SetOutput_fun = luaTable.GetLuaFunction("SetOutput");
- LuaFunction GetNextCfg_fun = luaTable.GetLuaFunction("GetNextCfg");
- LuaFunction GetLen_fun = luaTable.GetLuaFunction("GetLen");
-
- init_fun.Call<LuaTable>(luaTable);
- SetOutput_fun.Call<LuaTable, string>(luaTable, output);
- int len = GetLen_fun.Invoke<LuaTable,int>(luaTable);
- int curindex = 1;
- EditorUtility.DisplayProgressBar("复制中", $"开始复制", 0);
- string metastr = ".meta";
- while (true)
- {
- EditorUtility.DisplayProgressBar("复制中", $"复制{curindex}/{len}", curindex/len);
- curindex++;
- LuaTable cfg = GetNextCfg_fun.Invoke<LuaTable, LuaTable>(luaTable);
- //{result = false,path = path,outputPath = outputPath}
- bool result = cfg.RawGet<string, bool>("result");
- if (result)
- {
- string path = cfg.RawGet<string, string>("path");
- string _outputPath = cfg.RawGet<string, string>("outputPath");
- string filePath = fileRootPath + path;
- string fileOutputPath = outputPath + _outputPath;
- if (path.Contains(".psd"))
- {
- Debug.Log($"不复制psd文件:[{path}]");
- continue;
- }
-
- FileHelper.CreateDir(FileHelper.PathRemoveBack(fileOutputPath));
- if (File.Exists(filePath))
- {
- File.Copy(filePath, fileOutputPath, true);
-
- if (!path.Contains(metastr))
- {
- File.Copy(filePath+ metastr, fileOutputPath+ metastr, true);
- }
- }
-
- else
- {
- Debug.Log($"不存在文件:[{filePath}]");
- }
- }
- else
- {
- break;
- }
- }
- EditorUtility.ClearProgressBar();
- }
- }
- struct lgInfo
- {
- public int index;
- public int off;
- }
- private static void MergeLanauage(string oldPath,string newPath, string outputPath)
- {
- List<string> oldLg = File.ReadAllLines(oldPath).ToList();
- List<string> newLg = File.ReadAllLines(newPath).ToList();
- Dictionary<string, lgInfo> cfgmap = new Dictionary<string, lgInfo>();
- int length = oldLg.Count;
- int off = -1;
- for (int i = 0; i < length; i++)
- {
- off = oldLg[i].IndexOf(',');
- if (off < 0)
- {
- continue;
- }
- string key = oldLg[i].Substring(0,off);
- if (!cfgmap.ContainsKey(key))
- {
- lgInfo info = new lgInfo();
- info.index = i;
- info.off = off;
- cfgmap.Add(key, info);
- }
- }
- foreach (var item in newLg)
- {
- if (item == "" || item == ","||item == "key"||item == "string")
- continue;
- off = item.IndexOf(',');
- if (off < 0)
- {
- continue;
- }
- string key = item.Substring(0,off);
- if (!cfgmap.ContainsKey(key))
- {
- oldLg.Add(item);
- }
- else
- {
- lgInfo lgIf = cfgmap[key];
- oldLg[lgIf.index] = item;
- }
- }
- string data = FileHelper.CatStringArray(oldLg);
- FileHelper.WirteStringToFile(outputPath, data);
- }
- private void B2T(string bPath,string outPath, Encoding read)
- {
- using (FileStream binaryFileStream = new FileStream(bPath, FileMode.Open, FileAccess.Read))
- using (StreamReader reader = new StreamReader(binaryFileStream, read)) // 使用ASCII编码读取
- using (StreamWriter writer = new StreamWriter(outPath))
- {
- writer.Write(reader.ReadToEnd()); // 读取并写入文本文件
- }
- }
- public static void AddLocalizTextureCfgByFileCfg(string luacfgpath)
- {
- string luaPath = "Assets/Lua/Config/";
- string csvPath = "Assets/Content/Config/";
- string OutPutPath = "Build/NewCfg/";
-
- Dictionary<string, LocalizedTextureCfg> language = GetLanguageTextureCfg("Assets/Content/Config/LocalizeTextureCfg_base.csv");
- using (LuaState runTime = new LuaState())
- {
- runTime.Start();
- LuaTable luaTable = runTime.DoFile<LuaTable>(luacfgpath);
- LuaFunction init_fun = luaTable.GetLuaFunction("Init");
- LuaFunction SetOutput_fun = luaTable.GetLuaFunction("SetOutput");
- LuaFunction GetNextCfg_fun = luaTable.GetLuaFunction("GetNextCfg");
- LuaFunction GetLen_fun = luaTable.GetLuaFunction("GetLen");
- init_fun.Call<LuaTable>(luaTable);
- //SetOutput_fun.Call<LuaTable, string>(luaTable, output);
- int len = GetLen_fun.Invoke<LuaTable, int>(luaTable);
- int curindex = 1;
- EditorUtility.DisplayProgressBar("复制中", $"开始复制", 0);
- string metastr = ".meta";
- while (true)
- {
- EditorUtility.DisplayProgressBar("复制中", $"复制{curindex}/{len}", curindex / len);
- curindex++;
- LuaTable cfg = GetNextCfg_fun.Invoke<LuaTable, LuaTable>(luaTable);
- //{result = false,path = path,outputPath = outputPath}
- int result = cfg.RawGet<string, int>("result");
- if (result ==1)
- {
- string key = cfg.RawGet<string, string>("key");
- string Languages = cfg.RawGet<string, string>("Language");
- string AssetPath = cfg.RawGet<string, string>("AssetPath");
- string other = cfg.RawGet<string, string>("other");
- string setN = cfg.RawGet<string, string>("setN");
- if (!language.ContainsKey(key))
- {
- language.Add(key,new LocalizedTextureCfg() {key = key,Language = Languages, AssetPath = AssetPath ,OtherSetting = other,SetNativeSize = setN});
- }
- else
- {
- Debug.Log("key重复:"+key);
- }
- }
- else if (result == 0)
- {
- break;
- }
- }
- EditorUtility.ClearProgressBar();
- }
- Debug.Log("完成检查");
- CsvWriter<LocalizedTextureCfg> csvWriter = new CsvWriter<LocalizedTextureCfg>(OutPutPath + "LocalizedTextureCfg.csv", "LocalizedTextureCfg.csv", language.Values.ToList(), LocalizedTextureCfg.GetFormatInfo());
- csvWriter.Write();
- }
- }
|