private static void LoadDefRawData() { string p = arrCsvName[0]; CsvRawData rd = CsvReader.Load(CsvPath + "/" + p); dictRawData.Add(rd.name, rd); }
// csvName 仅用于打印 static void AnalyzeCsv(CsvRawData rd, string csvName) { string defaultStructName = string.Empty; for (int c = 0; c <= rd.maxC + 1; c++) { for (int r = 0; r <= rd.maxR + 1; r++) { string cell = rd.get(r, c); if (cell.StartsWith(TABLEID)) { string structName = cell.Substring(TABLEID.Length); if (!string.IsNullOrEmpty(structName) && string.IsNullOrEmpty(defaultStructName)) { defaultStructName = structName; } if (string.IsNullOrEmpty(structName)) { structName = defaultStructName; } if (string.IsNullOrEmpty(structName)) { throw new Exception("In \"" + csvName + "\": structName missing."); } AnalyzeATable(rd, r, c, structName); } } } }
public static CsvRawData Load(string csvName, string content) { CsvRawData data = new CsvRawData(csvName); string[] lines = content.Split(LINE_SERERATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int row = 0; row < lines.Length; row++) { string line = lines[row]; int col = 0; string[] strs = line.Split(SEPERATOR); string s = string.Empty; foreach (var str in strs) { s += str; string cellString = formatCellString(s); if (cellString != null) { data.set(row, col++, cellString); s = string.Empty; } else { // 如果不符合,把后面的也串起来 s += SEPERATOR; } } if (s.Length != 0) { throw new Exception("\"" + csvName + "\" has error!"); //return null; } } return(data); }
// 加载所有csv的文本 private static void LoadAllRawDataExceptDef() { CsvRawData rd = null; for (int i = 1 /* 跳过 def.csv */; i < arrCsvName.Length; i++) { string p = arrCsvName[i]; rd = CsvReader.Load(CsvPath + "/" + p); dictRawData.Add(rd.name, rd); } }
static void AnalyzeAllCsvsExceptDef() { for (int i = 1 /* 跳过 def.csv */; i < arrCsvName.Length; i++) { parsingCsvName = arrCsvName[i]; Console.WriteLine(" loading csv: " + parsingCsvName); CsvRawData rd = getCsvRawData(parsingCsvName); AnalyzeCsv(rd, parsingCsvName); } }
// 分析一个.csv 里的一个部分,这个部分组成一个表 // (一个.csv里可以写上很多个表,只要按照特定格式组织就可以) // csvName csv文件名 // rd 整个csv的数据 // or/oc 子表的表头位置 // structName 这个子表所使用的结构体名字 static void AnalyzeATable(CsvRawData rd, int or, int oc, string structName) { // od下一行是字段名定义 // 可以考虑改成往下一直找,找到不空为止,现在是必须紧接着才行 int field_r = or + 1; int c = oc; List <string> fieldNames = new List <string>(); // 字段名字 List <string> fieldTypes = new List <string>(); // 不为null则表示枚举名 // 先查找一共有几列 // 往右一直找,找到空的就表示结束了 while (true) { string cell = rd.get(field_r, c++); if (string.IsNullOrEmpty(cell)) { break; } string[] name_type = cell.Split(':'); fieldNames.Add(name_type[0]); fieldTypes.Add(name_type.Length > 1 ? name_type[1] : null); } // 再下一行就是表数据了 int r = or + 2; while (true) { string firstCell = rd.get(r, oc); if (string.IsNullOrEmpty(firstCell)) { // 空行就结束 break; } if (!firstCell.StartsWith("#")) // 跳过以#开始的行(这一行现在写的是中文的列名,用来看的) { int tid = int.Parse(firstCell); // 注意,如果这个对象已经存在了,接下去可能就覆盖他的字段值 object obj = TryGetObject(structName, tid); bool shouldAdd = false; if (obj == null) { shouldAdd = true; obj = CsvStructures.CreateByName(structName); } if (obj == null) { throw new Exception("In " + parsingCsvName + ": Table structure not found: " + structName); } //Type type = obj.GetType(); for (c = oc; c < oc + fieldNames.Count; c++) { // tid又会取一遍,没关系 string cell = rd.get(r, c); string fieldName = fieldNames[c - oc]; string typeStr = fieldTypes[c - oc]; SetFieldValue(obj, fieldName, typeStr, cell); } if (shouldAdd) { AddObject(structName, tid, obj); } } // 下一行 r++; } }
// 解析def表 // 现在只存储枚举 public static void AnalyzeDefCsv() { parsingCsvName = arrCsvName[0]; CsvRawData rd = getCsvRawData(arrCsvName[0]); rd.clearMarked(); string enumName = null; Dictionary <string, int> enumValues = null; bool specifyValue = false; // 用另一列指定枚举值 int n = 0; // 没指定枚举值时,递增 // 逐列解析 // 为啥要+1?+1才可以到后来取到一个空串,得知已结束。 for (int c = 0; c <= rd.maxC + 1; c++) { for (int r = 0; r <= rd.maxR + 1; r++) { string cell = rd.get(r, c); bool empty = string.IsNullOrEmpty(cell); bool isEnumID = !empty && cell.StartsWith(ENUMID); if (empty || isEnumID) { if (!string.IsNullOrEmpty(enumName) && enumValues != null && enumValues.Count > 0) { dictEnum.Add(enumName, enumValues); } enumName = null; enumValues = null; specifyValue = false; n = 0; if (isEnumID) { enumName = cell.Substring(ENUMID.Length); enumValues = new Dictionary <string, int>(); specifyValue = rd.get(r, c + 1).StartsWith(VALUEID); if (specifyValue) { rd.mark(r, c + 1); } } } else { if (string.IsNullOrEmpty(enumName) || enumValues == null) { if (!rd.isMarked(r, c)) { throw new Exception("def.csv 配置有误"); } } else { int N = specifyValue ? int.Parse(rd.get(r, c + 1)) : n++; enumValues.Add(cell, N); if (specifyValue) { rd.mark(r, c + 1); } } } } } }