public void TestFileRead1() { string path = Environment.CurrentDirectory + "/config-1.csv"; if (!File.Exists(path)) { throw new Exception("file not found"); } CsvConfigReader reader = new CsvConfigReader(); reader.ReadFile(path); CsvValue value = reader[1]["key12"].Value; string[] stringArray = value; Assert.AreEqual(5, stringArray.Length); Assert.AreEqual("hello,001", stringArray[0]); Assert.AreEqual("hello,002", stringArray[1]); Assert.AreEqual("hello,003", stringArray[2]); Assert.AreEqual("hello,004", stringArray[3]); Assert.AreEqual("hello,005", stringArray[4]); bool[] boolArray = reader[1]["key9"].Value; foreach (bool b in boolArray) { Assert.IsTrue(b); } }
public void Test2() { StringBuilder sb = new StringBuilder(); sb.AppendLine("KI,B,I,L,F,D,S"); //7types sb.AppendLine("key1,key2,key3,key4,key5,key6,key7"); sb.AppendLine("1, true, 101, 1001,1.1, 2.1, k1"); sb.AppendLine("2,false, 102, 1002,1.2, 2.2, k2"); sb.AppendLine("3, true, 103, 1003,1.3, 2.3, k3"); sb.AppendLine("4,false, 104, 1004,1.4, 2.4, k4"); sb.AppendLine("5, true, 105, 1005,1.5, 2.5, k5"); CsvConfigReader reader = new CsvConfigReader(); reader.Read(new StringReader(sb.ToString())); //key type is string (KS in first line) //so keye is k1,k2,k3,k4,k5,5 elements for (int i = 1; i < 6; i++) { int key3Value = reader[i]["key3"].Value; Assert.AreEqual(100 + i, key3Value); long key4Value = reader[i]["key4"].Value; Assert.AreEqual(1000 + i, key4Value); string key7Value = reader[i]["key7"].Value; Assert.AreEqual("k" + i, key7Value); } }
public void Test1() { StringBuilder sb = new StringBuilder(); sb.AppendLine("I,B,I,L,F,D,KS"); //7types sb.AppendLine("key1,key2,key3,key4,key5,key6,key7"); sb.AppendLine("1, true, 101, 1001,1.1, 2.1, k1"); sb.AppendLine("2,false, 102, 1002,1.2, 2.2, k2"); sb.AppendLine("3, true, 103, 1003,1.3, 2.3, k3"); sb.AppendLine("4,false, 104, 1004,1.4, 2.4, k4"); sb.AppendLine("5, true, 105, 1005,1.5, 2.5, k5"); CsvConfigReader reader = new CsvConfigReader(); reader.Read(new StringReader(sb.ToString())); //key type is string (KS in first line) //so keye is k1,k2,k3,k4,k5,5 elements //get 3th config CsvConfig config = reader["k3"]; //get field key4 1003 int value = config["key4"].Value; Assert.AreEqual(1003, value); value = config[3].Value; Assert.AreEqual(1003, value); }
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"); }
public void Test3() { StringBuilder sb = new StringBuilder(); sb.AppendLine("KI,B,I,L,F,D,S"); //7types sb.AppendLine("key1,key2,key3,key4,key5,key6,key7"); sb.AppendLine("1, true, 101, 1001,1.1, 2.1, \"v1,v1;v1,v1\""); sb.AppendLine("2,false, 102, 1002,1.2, 2.2, \"v2,v2;v2,v2\""); CsvConfigReader reader = new CsvConfigReader(); reader.Read(new StringReader(sb.ToString())); CsvConfig config = reader[1]; CsvValue value = config["key7"].Value; string strValue = value; Console.WriteLine(strValue); }
public void TestFileRead() { string path = Environment.CurrentDirectory + "/config.csv"; if (!File.Exists(path)) { throw new Exception("file not found"); } CsvConfigReader reader = new CsvConfigReader(); reader.ReadFile(path); Assert.AreEqual(7, reader.FieldNames.Count); Assert.AreEqual("key", reader.FieldNames[0]); Assert.AreEqual("nameBool", reader.FieldNames[1]); Assert.AreEqual("nameInt", reader.FieldNames[2]); Assert.AreEqual("nameLong", reader.FieldNames[3]); Assert.AreEqual("nameFloat", reader.FieldNames[4]); Assert.AreEqual("nameDouble", reader.FieldNames[5]); Assert.AreEqual("nameString", reader.FieldNames[6]); Assert.AreEqual(100, reader.Keys.Count); Assert.AreEqual(CsvValueType.Int, reader.KeyType); for (int i = 1; i <= 100; i++) { if (i < 26 || (i > 50 && i < 76)) { Assert.True(reader[i]["nameBool"].Value); } else { Assert.False(reader[i]["nameBool"].Value); } Assert.AreEqual(100 + i, reader[i]["nameInt"].Value); Assert.AreEqual(1000 + i, reader[i]["nameLong"].Value); Assert.AreEqual(0 + 0.01f * i, reader[i]["nameFloat"].Value, 0.00001f); Assert.AreEqual(1 + 0.01f * i, reader[i]["nameDouble"].Value, 0.00001f); string s = i < 10 ? "a0" + i : "a" + i; Assert.AreEqual(s, reader[i]["nameString"].Value.Value);//need implicit to string } }
public void TestDoubleKeyType() { StringBuilder sb = new StringBuilder(); sb.AppendLine("KI,B,I,L,F,D,S,KL");//7types CsvConfigReader reader = new CsvConfigReader(); bool hasException = false; Exception e = null; try { reader.Read(new StringReader(sb.ToString())); } catch (Exception exception) { Console.WriteLine(exception.Message); hasException = true; e = exception; } Assert.True(hasException); Assert.True(e is CsvException); }
public void TestTypeNotMatchError() { StringBuilder sb = new StringBuilder(); sb.AppendLine("KI,B,I,L,F,D,S");//7types sb.AppendLine("key1,key2,key3,key4,key5,key6,key7"); sb.AppendLine("1, 112, 101, 1001,1.1, 2.1, hello world 1"); CsvConfigReader reader = new CsvConfigReader(); bool hasException = false; Exception e = null; try { reader.Read(new StringReader(sb.ToString())); } catch (Exception exception) { Console.WriteLine(exception.Message); hasException = true; e = exception; } Assert.True(hasException); Assert.True(e is CsvException); }
public void TestKeyIsString() { StringBuilder sb = new StringBuilder(); sb.AppendLine("I,B,I,L,F,D,KS");//7types sb.AppendLine("key1,key2,key3,key4,key5,key6,key7"); sb.AppendLine("1, YES, 101, 1001,1.1, 2.1, k1"); sb.AppendLine("2,NO, 102, 1002,1.2, 2.2, k2"); sb.AppendLine("3, YES, 103, 1003,1.3, 2.3, k3"); sb.AppendLine("4,NO, 104, 1004,1.4, 2.4, k4"); sb.AppendLine("5, YES, 105, 1005,1.5, 2.5, k5"); CsvConfigReader reader = new CsvConfigReader(); reader.Read(new StringReader(sb.ToString())); Assert.AreEqual(CsvValueType.String, reader.KeyType); Assert.AreEqual(5, reader[reader.Keys[4]]["key1"].Value); Assert.AreEqual(5, reader.Keys.Count); string key; key = reader.Keys[0]; Assert.AreEqual("k1", key); key = reader.Keys[1]; Assert.AreEqual("k2", key); key = reader.Keys[2]; Assert.AreEqual("k3", key); key = reader.Keys[3]; Assert.AreEqual("k4", key); key = reader.Keys[4]; Assert.AreEqual("k5", key); Assert.AreEqual(7, reader.FieldNames.Count); Assert.AreEqual("key1", reader.FieldNames[0]); Assert.AreEqual("key2", reader.FieldNames[1]); Assert.AreEqual("key3", reader.FieldNames[2]); Assert.AreEqual("key4", reader.FieldNames[3]); Assert.AreEqual("key5", reader.FieldNames[4]); Assert.AreEqual("key6", reader.FieldNames[5]); Assert.AreEqual("key7", reader.FieldNames[6]); CsvConfig config = reader[reader.Keys[0]]; Assert.AreEqual(0, config[0].Index); Assert.AreEqual(false, config[0].IsKey); Assert.AreEqual("key1", config[0].Name); Assert.AreEqual(1, config[0].Value); Assert.AreEqual(1, config[1].Index); Assert.AreEqual(false, config[1].IsKey); Assert.AreEqual("key2", config[1].Name); bool b = config[1].Value; Assert.AreEqual(true, b); Assert.AreEqual(2, config[2].Index); Assert.AreEqual(false, config[2].IsKey); Assert.AreEqual("key3", config[2].Name); Assert.AreEqual(101, config[2].Value); Assert.AreEqual(3, config[3].Index); Assert.AreEqual(false, config[3].IsKey); Assert.AreEqual("key4", config[3].Name); Assert.AreEqual(1001, config[3].Value); Assert.AreEqual(4, config[4].Index); Assert.AreEqual(false, config[4].IsKey); Assert.AreEqual("key5", config[4].Name); float f = config[4].Value; Assert.AreEqual(float.Parse("1.1"), f); Assert.AreEqual(5, config[5].Index); Assert.AreEqual(false, config[5].IsKey); Assert.AreEqual("key6", config[5].Name); double d = config[5].Value; Assert.AreEqual(double.Parse("2.1"), d); Assert.AreEqual(6, config[6].Index); Assert.AreEqual(true, config[6].IsKey); Assert.AreEqual("key7", config[6].Name); string s = config[6].Value; Assert.AreEqual("k1", s); }
public void TestNormal() { StringBuilder sb = new StringBuilder(); sb.AppendLine("KI,B,I,L,F,D,S");//7types sb.AppendLine("key1,key2,key3,key4,key5,key6,key7"); sb.AppendLine("1, true, 101, 1001,1.1, 2.1, hello world 1"); sb.AppendLine("2,false, 102, 1002,1.2, 2.2, hello world 2"); sb.AppendLine("3, true, 103, 1003,1.3, 2.3, hello world 3"); sb.AppendLine("4,false, 104, 1004,1.4, 2.4, hello world 4"); sb.AppendLine("5, true, 105, 1005,1.5, 2.5, hello world 5"); CsvConfigReader reader = new CsvConfigReader(); reader.Read(new StringReader(sb.ToString())); Assert.AreEqual(CsvValueType.Int, reader.KeyType); Assert.AreEqual(5, reader.Keys.Count); Assert.AreEqual(1, reader.Keys[0]); Assert.AreEqual(2, reader.Keys[1]); Assert.AreEqual(3, reader.Keys[2]); Assert.AreEqual(4, reader.Keys[3]); Assert.AreEqual(5, reader.Keys[4]); Assert.AreEqual(7, reader.FieldNames.Count); Assert.AreEqual("key1", reader.FieldNames[0]); Assert.AreEqual("key2", reader.FieldNames[1]); Assert.AreEqual("key3", reader.FieldNames[2]); Assert.AreEqual("key4", reader.FieldNames[3]); Assert.AreEqual("key5", reader.FieldNames[4]); Assert.AreEqual("key6", reader.FieldNames[5]); Assert.AreEqual("key7", reader.FieldNames[6]); CsvConfig config = reader[reader.Keys[0]]; Assert.AreEqual(0, config[0].Index); Assert.AreEqual(true, config[0].IsKey); string key = config[0].Name; Assert.AreEqual("key1", key); Assert.AreEqual(1, config[0].Value); Assert.AreEqual(1, config[1].Index); Assert.AreEqual(false, config[1].IsKey); Assert.AreEqual("key2", config[1].Name); bool b = config[1].Value; Assert.AreEqual(true, b); Assert.AreEqual(2, config[2].Index); Assert.AreEqual(false, config[2].IsKey); Assert.AreEqual("key3", config[2].Name); Assert.AreEqual(101, config[2].Value); Assert.AreEqual(3, config[3].Index); Assert.AreEqual(false, config[3].IsKey); Assert.AreEqual("key4", config[3].Name); Assert.AreEqual(1001, config[3].Value); Assert.AreEqual(4, config[4].Index); Assert.AreEqual(false, config[4].IsKey); Assert.AreEqual("key5", config[4].Name); float f = config[4].Value; Assert.AreEqual(float.Parse("1.1"), f); Assert.AreEqual(5, config[5].Index); Assert.AreEqual(false, config[5].IsKey); Assert.AreEqual("key6", config[5].Name); double d = config[5].Value; Assert.AreEqual(double.Parse("2.1"), d); Assert.AreEqual(6, config[6].Index); Assert.AreEqual(false, config[6].IsKey); Assert.AreEqual("key7", config[6].Name); string s = config[6].Value; Assert.AreEqual("hello world 1", s); }
/// <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); }
string ConvertCsvTypeToCsType(string csvType) { CsvValue.CsvValueType type = CsvConfigReader.GetCsvValueType(csvType); string ret = string.Empty; switch (type) { case CsvValue.CsvValueType.None: ret = "None"; break; case CsvValue.CsvValueType.Bool: ret = "bool"; break; case CsvValue.CsvValueType.Int: ret = "int"; break; case CsvValue.CsvValueType.Long: ret = "long"; break; case CsvValue.CsvValueType.Float: ret = "float"; break; case CsvValue.CsvValueType.Double: ret = "double"; break; case CsvValue.CsvValueType.String: ret = "string"; break; case CsvValue.CsvValueType.VFixedPoint: ret = "VFixedPoint"; break; case CsvValue.CsvValueType.ArrayInt: ret = "int[]"; break; case CsvValue.CsvValueType.ArrayLong: ret = "long[]"; break; case CsvValue.CsvValueType.ArrayFloat: ret = "float[]"; break; case CsvValue.CsvValueType.ArrayString: ret = "string[]"; break; case CsvValue.CsvValueType.ArrayVFixedPoint: ret = "VFixedPoint[]"; break; case CsvValue.CsvValueType.Tuple: break; default: throw new ArgumentOutOfRangeException(); } if (string.IsNullOrEmpty(ret)) { Debug.LogError(string.Format("not support type convert: source[{0}]==> target[{1}] ", csvType, ret)); throw new ArgumentOutOfRangeException(); } return(ret); }