string GetKeyValue(string[] variable, string[] type, string[] values, string[] _Summary) { for (int i = 0; i < variable.Length; i++) { if (type[i] == IgnoreType) { Debug.Log("Ignore variable name:" + variable[i]); continue; } if (CsvConfigReader.IsKeyType(_Summary[i])) { return(values[i]); } } return("xxxx"); }
/// <summary> /// 讀取GameTable(.txt) /// </summary> public bool LoadGameTable(Object obj, bool exportCSharp, bool exportServer, bool exportLua) { _config = ClientDataBaseManager.Instance.m_config; _tableName = obj.name; //-string strTemp; TextAsset data = (TextAsset)obj; TextReader reader = null; string[] _Summary = null; string[] _Variable = null; string[] _ErlangVariable = null; string[] _LuaVariable = null; string[] _Type = null; int index = 0; if (data == null) { Debug.LogError("GameTable is null."); return(false); } if (data.text == string.Empty) { Debug.LogError("GameTable is empty."); return(false); } reader = new StringReader(data.text); if (reader != null) { int keyCol = -1; //while ((strTemp = reader.ReadLine()) != null) CsvReader csvReader = new CsvReader(reader, false); foreach (string[] splitStr in csvReader) { if (index == 0) { _Type = splitStr.Select(a => a.Trim()).ToArray(); _Summary = _Type.ToArray(); // convert to real type for (var i = 0; i < _Type.Length; i++) { //检查多个主键 if (CsvConfigReader.IsKeyType(_Type[i])) { if (keyCol >= 0) { Debug.LogError(string.Format("Duplicate key type conflict in table:[{0}] col:[{1}] with col[{2}]", _tableName, keyCol, index)); return(false); } keyCol = i; } _Type[i] = ConvertCsvTypeToCsType(_Type[i]); } index++; continue; } if (index == 1) { _Variable = splitStr.Select(a => a.Trim()).ToArray(); _ErlangVariable = splitStr.ToArray(); // 解决字段有空格的问题 for (int i = 0; i < _Variable.Length; i++) { _ErlangVariable[i] = Utility.TypeRelate.CorrectFieldName(_ErlangVariable[i], "_").ToLower(); _Variable[i] = Utility.TypeRelate.CorrectFieldName(_Variable[i], ""); } _LuaVariable = _Variable.Select(FirstCharToUpper).ToArray(); //1.判斷是否是 GameTable(txt),檔案的開始字串是否包含 識別字 if (!string.IsNullOrEmpty(_config.m_gameTableCheck) && _Summary[0].IndexOf(_config.m_gameTableCheck) < 0) { Debug.LogError("GameTable is not a table. Please Check txt file start string is [" + _config.m_gameTableCheck + "]"); return(false); } //2.判斷欄位數量是否一致 int count = _Summary.Length; if (count != _Variable.Length || count != _Type.Length) { Debug.LogError("GameTable column not same."); return(false); } if (!exportCSharp) { break; } Dictionary <string, TableData> datamap = CreateTableScript(_Summary, _Variable, _Type); if (datamap == null) { return(false); } if (CreateScriptableScript(_Variable, datamap, keyCol) == false) { return(false); } if (CreateScriptableScriptEditor() == false) { return(false); } break; } } reader.Close(); } if (exportServer) { ExportServerConfig(_Summary, _ErlangVariable, _Type, data.text); } if (exportLua) { ExportLuaConfig(_Summary, _LuaVariable, _Type, data.text); } return(true); }
private StringBuilder BuildSearchMethods(string[] _Summary, string[] variable, string[] type, string[] values, Dictionary <string, Dictionary <string, List <string> > > indexStats, List <string> keyList, bool isLua, out string keyVal, out string keyType) { StringBuilder sb = new StringBuilder(); string fmt = " {0} = {1},\n"; string lastlineFmt = " {0} = {1}"; //string[] values = line.Split(","[0]).Select(a => a.Trim()).ToArray(); keyVal = ""; keyType = "None"; int validCount = 0; for (int i = 0; i < variable.Length; i++) { if (type[i] == IgnoreType) { continue; } validCount++; } for (int i = 0, validIndex = 0; i < variable.Length; i++) { if (type[i] == IgnoreType) { //Debug.Log("Ignore variable name:" + variable[i]); continue; } string typeVal = type[i]; // 字段名 string fieldName = variable[i]; string value = values[i]; if (CsvConfigReader.IsKeyType(_Summary[i].ToLower())) { keyVal = value; keyType = typeVal; keyList.Add(GetScriptValue(keyType, keyVal, isLua)); } else if (CsvConfigReader.IsIndexType(_Summary[i].ToLower())) { Dictionary <string, List <string> > fieldIndexDict = null; if (!indexStats.TryGetValue(fieldName, out fieldIndexDict)) { fieldIndexDict = new Dictionary <string, List <string> >(); indexStats.Add(fieldName, fieldIndexDict); } List <string> keys; if (!fieldIndexDict.TryGetValue(value, out keys)) { keys = new List <string>(); fieldIndexDict.Add(value, keys); } keys.Add(string.IsNullOrEmpty(keyVal) ? GetKeyValue(variable, type, values, _Summary) : keyVal); } if (isLua) { sb.AppendFormat(fmt, fieldName, GetScriptValue(typeVal, value, true)); } else { sb.AppendFormat(validIndex == validCount - 1 ? lastlineFmt : fmt, fieldName.ToLower(), GetScriptValue(typeVal, value, false)); } validIndex++; } return(sb); }