private void btnAnalyze_Click(object sender, EventArgs e) { string inputFormatString = tbFormat.Text.Trim(); string inputDataString = tbData.Text; if (string.IsNullOrEmpty(inputFormatString)) { MessageBox.Show("未输入格式定义", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(inputDataString)) { MessageBox.Show("未输入数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 解析格式定义 string errorString = null; TableStringFormatDefine formatDefine = TableAnalyzeHelper.GetTableStringFormatDefine(inputFormatString, out errorString); if (errorString != null) { tbOutput.Text = "格式声明错误," + errorString; return; } // 生成lua table string exportString = TableExportToLuaHelper.GetTableStringValue(formatDefine, inputDataString, 0, out errorString); if (errorString != null) { tbOutput.Text = "输入数据错误," + errorString; } else { tbOutput.Text = exportString; } }
public static TableStringFormatDefine GetTableStringFormatDefine(string dataTypeString, out string errorString) { TableStringFormatDefine formatDefine = new TableStringFormatDefine(); // 必须在tableString[]中声明格式 string defineStartString = "tableString["; if (!(dataTypeString.StartsWith(defineStartString, StringComparison.CurrentCultureIgnoreCase) && dataTypeString.EndsWith("]"))) { errorString = "必须在tableString[]中声明,即以\"tableString[\"开头,以\"]\"结尾"; return(formatDefine); } // 去掉外面的tableString[],取得中间定义内容 int startIndex = defineStartString.Length; string formatString = dataTypeString.Substring(startIndex, dataTypeString.Length - startIndex - 1).Trim(); // 通过|分离key和value的声明 string[] keyAndValueFormatString = formatString.Split(new char[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries); if (keyAndValueFormatString.Length != 2) { errorString = "必须用|分隔key和value"; return(formatDefine); } // 解析key声明 string keyFormatString = keyAndValueFormatString[0].Trim(); string keyStartString = "k:"; if (!keyFormatString.StartsWith(keyStartString, StringComparison.CurrentCultureIgnoreCase)) { errorString = "key的声明必须以k:开头"; return(formatDefine); } else { // 去除开头的k: keyFormatString = keyFormatString.Substring(keyStartString.Length).Trim(); // 按数据顺序自动编号 if (keyFormatString.Equals("#seq", StringComparison.CurrentCultureIgnoreCase)) { formatDefine.KeyDefine.KeyType = TABLE_STRING_KEY_TYPE.SEQ; } // 以数据组中指定索引位置的数据为key else if (keyFormatString.StartsWith("#")) { formatDefine.KeyDefine.KeyType = TABLE_STRING_KEY_TYPE.DATA_IN_INDEX; formatDefine.KeyDefine.DataInIndexDefine = _GetDataInIndexDefine(keyFormatString, out errorString); if (errorString != null) { errorString = "key的声明未符合形如#1(int)\n" + errorString; return(formatDefine); } // 只有int型或string型数据才能作为key if (!(formatDefine.KeyDefine.DataInIndexDefine.DataType == DataType.Int || formatDefine.KeyDefine.DataInIndexDefine.DataType == DataType.String)) { errorString = string.Format("key只允许为int或string型,你定义的类型为{0}\n", formatDefine.KeyDefine.DataInIndexDefine.DataType.ToString()); return(formatDefine); } } else { errorString = "key声明非法"; return(formatDefine); } } // 解析value声明 string valueFormatString = keyAndValueFormatString[1].Trim(); string valueStartString = "v:"; if (!valueFormatString.StartsWith(valueStartString, StringComparison.CurrentCultureIgnoreCase)) { errorString = "value的声明必须以v:开头"; return(formatDefine); } else { // 去除开头的v: valueFormatString = valueFormatString.Substring(valueStartString.Length).Trim(); // value始终为true if (valueFormatString.Equals("#true", StringComparison.CurrentCultureIgnoreCase)) { formatDefine.ValueDefine.ValueType = TABLE_STRING_VALUE_TYPE.TRUE; } // value为table类型 else if (valueFormatString.StartsWith("#table", StringComparison.CurrentCultureIgnoreCase)) { formatDefine.ValueDefine.ValueType = TABLE_STRING_VALUE_TYPE.TABLE; // 判断是否形如#table(xxx) int leftBracketIndex = valueFormatString.IndexOf('('); int rightBracketIndex = valueFormatString.LastIndexOf(')'); if (leftBracketIndex == -1 || rightBracketIndex != valueFormatString.Length - 1) { errorString = "table类型value格式声明错误,必须形如#table(xxx)"; return(formatDefine); } else { // 去掉#table(xxx)外面,只保留括号中的内容 string tableDefineString = valueFormatString.Substring(leftBracketIndex + 1, rightBracketIndex - leftBracketIndex - 1).Trim(); // table中每个键值对的声明用英文逗号隔开 string[] tableElementDefine = tableDefineString.Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries); // 解析每个键值对的定义,形如type=#1(int) formatDefine.ValueDefine.TableValueDefineList = new List <TableElementDefine>(); // 记录每个键值对的key,不允许重复(key:key名, value:第几组键值对,从0开始记) Dictionary <string, int> tableKeys = new Dictionary <string, int>(); for (int i = 0; i < tableElementDefine.Length; ++i) { TableElementDefine oneTableElementDefine = _GetTablelementDefine(tableElementDefine[i].Trim(), out errorString); if (errorString != null) { errorString = string.Format("table类型值声明错误,无法解析{0},", tableElementDefine[i].Trim()) + errorString; return(formatDefine); } else { // 检查定义的key是否重复 if (tableKeys.ContainsKey(oneTableElementDefine.KeyName)) { errorString = string.Format("table类型的第{0}个与第{1}个子元素均为相同的key({2})", tableKeys[oneTableElementDefine.KeyName] + 1, i + 1, oneTableElementDefine.KeyName); return(formatDefine); } else { tableKeys.Add(oneTableElementDefine.KeyName, i + 1); formatDefine.ValueDefine.TableValueDefineList.Add(oneTableElementDefine); } } } } } // 以数据组中指定索引位置的数据为value else if (valueFormatString.StartsWith("#")) { formatDefine.ValueDefine.ValueType = TABLE_STRING_VALUE_TYPE.DATA_IN_INDEX; formatDefine.ValueDefine.DataInIndexDefine = _GetDataInIndexDefine(valueFormatString, out errorString); if (errorString != null) { errorString = "value的声明未符合形如#1(int)\n" + errorString; return(formatDefine); } } else { errorString = "value声明非法"; return(formatDefine); } } errorString = null; return(formatDefine); }
public static string GetTableStringValue(TableStringFormatDefine formatDefine, string inputData, int level, out string errorString) { StringBuilder content = new StringBuilder(); errorString = null; // tableString字符串中不允许出现英文引号、斜杠 if (inputData.Contains("\"") || inputData.Contains("\\") || inputData.Contains("/")) { errorString = "tableString字符串中不允许出现英文引号、斜杠"; return(null); } // 包裹tableString所生成table的左括号 content.AppendLine("{"); ++level; // 每组数据间用英文分号分隔,最终每组数据会生成一个lua table string[] allDataString = inputData.Split(new char[] { ';' }, System.StringSplitOptions.RemoveEmptyEntries); // 记录每组数据中的key值(转为字符串后的),不允许出现相同的key(key:每组数据中的key值, value:第几组数据,从0开始记) Dictionary <string, int> stringKeys = new Dictionary <string, int>(); for (int i = 0; i < allDataString.Length; ++i) { content.Append(_GetLuaTableIndentation(level)); // 根据key的格式定义生成key switch (formatDefine.KeyDefine.KeyType) { case TABLE_STRING_KEY_TYPE.SEQ: { content.AppendFormat("[{0}]", i + 1); break; } case TABLE_STRING_KEY_TYPE.DATA_IN_INDEX: { string value = _GetDataInIndexType(formatDefine.KeyDefine.DataInIndexDefine, allDataString[i], out errorString); if (errorString == null) { if (formatDefine.KeyDefine.DataInIndexDefine.DataType == DataType.Int) { // 检查key是否在该组数据中重复 if (stringKeys.ContainsKey(value)) { errorString = string.Format("第{0}组数据与第{1}组数据均为相同的key({2})", stringKeys[value] + 1, i + 1, value); } else { stringKeys.Add(value, i); content.AppendFormat("[{0}]", value); } } else if (formatDefine.KeyDefine.DataInIndexDefine.DataType == DataType.String) { // string型的key不允许为空或纯空格且必须符合变量名的规范 value = value.Trim(); if (TableCheckHelper.CheckFieldName(value, out errorString)) { // 检查key是否在该组数据中重复 if (stringKeys.ContainsKey(value)) { errorString = string.Format("第{0}组数据与第{1}组数据均为相同的key({2})", stringKeys[value] + 1, i + 1, value); } else { stringKeys.Add(value, i); content.Append(value); } } else { errorString = "string型的key不符合变量名定义规范," + errorString; } } else { errorString = "错误:用_WriteTableStringValue函数导出非int或string型的key值"; return(null); } } break; } default: { errorString = "错误:用_WriteTableStringValue函数导出未知类型的key"; return(null); } } if (errorString != null) { errorString = string.Format("tableString中第{0}组数据({1})的key数据存在错误,", i + 1, allDataString[i]) + errorString; return(null); } content.Append(" = "); // 根据value的格式定义生成value switch (formatDefine.ValueDefine.ValueType) { case TABLE_STRING_VALUE_TYPE.TRUE: { content.Append("true"); break; } case TABLE_STRING_VALUE_TYPE.DATA_IN_INDEX: { string value = _GetDataInIndexType(formatDefine.ValueDefine.DataInIndexDefine, allDataString[i], out errorString); if (errorString == null) { DataType dataType = formatDefine.ValueDefine.DataInIndexDefine.DataType; if (dataType == DataType.String || dataType == DataType.Lang) { content.AppendFormat("\"{0}\"", value); } else { content.Append(value); } } break; } case TABLE_STRING_VALUE_TYPE.TABLE: { content.AppendLine("{"); ++level; // 依次输出table中定义的子元素 foreach (TableElementDefine elementDefine in formatDefine.ValueDefine.TableValueDefineList) { content.Append(_GetLuaTableIndentation(level)); content.Append(elementDefine.KeyName); content.Append(" = "); string value = _GetDataInIndexType(elementDefine.DataInIndexDefine, allDataString[i], out errorString); if (errorString == null) { if (elementDefine.DataInIndexDefine.DataType == DataType.String || elementDefine.DataInIndexDefine.DataType == DataType.Lang) { content.AppendFormat("\"{0}\"", value); } else { content.Append(value); } } content.AppendLine(","); } --level; content.Append(_GetLuaTableIndentation(level)); content.Append("}"); break; } default: { errorString = "错误:用_WriteTableStringValue函数导出未知类型的value"; return(null); } } if (errorString != null) { errorString = string.Format("tableString中第{0}组数据({1})的value数据存在错误,", i + 1, allDataString[i]) + errorString; return(null); } // 每组数据生成完毕后加逗号并换行 content.AppendLine(","); } // 包裹tableString所生成table的右括号 --level; content.Append(_GetLuaTableIndentation(level)); content.Append("}"); return(content.ToString()); }