public List <MemberVariablesInfo> GetMemberVariableInfos() { List <List <string> > fileDataBlocks = ParseCSV(mFileContent); if (null == fileDataBlocks) { return(null); } System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < fileDataBlocks.Count; ++i) { List <string> lineDatas = fileDataBlocks [i]; for (int j = 0; j < lineDatas.Count; ++j) { sb.Append(" " + lineDatas[j]); } sb.Append("\n"); } List <MemberVariablesInfo> memberVaraiablesList = new List <MemberVariablesInfo> (); for (int i = 0; i < fileDataBlocks [(int)LineContentType.VariablesType].Count; ++i) { MemberVariablesInfo info = ParseNameAndType(fileDataBlocks[(int)LineContentType.VariablesName][i], fileDataBlocks [(int)LineContentType.VariablesType] [i]); memberVaraiablesList.Add(info); } return(memberVaraiablesList); }
public void AddChild(MemberVariablesInfo child) { if (childInfoList == null) { childInfoList = new List <MemberVariablesInfo> (); } childInfoList.Add(child); child.Parent = this; }
void GetSelfDefineClassStr(StringBuilder sb, MemberVariablesInfo variableInfo) { variableInfo.ForeachChild(delegate(MemberVariablesInfo child) { GetSelfDefineClassStr(sb, child); }); if (variableInfo.IsSelfDefineType && !mCreatedSelfDefClassSet.Contains(variableInfo.type)) { mCreatedSelfDefClassSet.Add(variableInfo.type); sb.Append("\t\tpublic class " + variableInfo.type + "\n"); sb.Append("\t\t{\n"); //成员变量 if (variableInfo.childList != null && variableInfo.childList.Count > 0) { GetVariableDefineStr(sb, variableInfo.childList, "\t\t\t"); } //对应的解析函数 sb.Append("\t\t\tpublic static " + variableInfo.type + " Parse(string content, ref int curIndex)\n"); sb.Append("\t\t\t{\n"); sb.Append("\t\t\t\t" + variableInfo.type + " ret = new " + variableInfo.type + " ();\n"); sb.Append("\t\t\t\t" + SelfClassParseBegin); bool isFirstChild = true; variableInfo.ForeachChild(delegate(MemberVariablesInfo child) { if (!isFirstChild) { sb.Append("\t\t\t\t" + VariableSeparator); } isFirstChild = false; if (child.IsArray) { sb.Append("\t\t\t\tGDSParseUtils.ParseArray(delegate() {\n"); } sb.Append("\t\t\t\tret." + child.name + " = "); if (child.IsSelfDefineType) { sb.Append(child.type + ".Parse (content, ref curIndex);\n"); } else { sb.Append(ParseSystemTypeCode(child.type) + ";\n"); } if (child.IsArray) { sb.Append("\t\t\t\t}, content, ref curIndex);\n"); } }); sb.Append("\t\t\t\t" + SelfClassParseEnd); sb.Append("\t\t\t\treturn ret;\n"); sb.Append("\t\t\t}\n"); //类的结束符号 sb.Append("\t\t}\n\n"); } }
void GetParseFuncStr(StringBuilder sb, List <MemberVariablesInfo> varialbes, string className) { sb.Append( "\t\tpublic static void Parse(string content)\n" + "\t\t{\n" + "\t\t\tClear ();\n" + "\t\t\tint headEndIndex = GDSParseUtils.GetCharIndex (content, GDSParseUtils.ObjectSeparator, GDSParseUtils.DataBeginLineIndex);\n" + "\t\t\tif (headEndIndex < 0) {\n" + "\t\t\t\tLogger.LogError (\"数据文件头缺少结束符'\\'n\");\n" + "\t\t\t\treturn;\n" + "\t\t\t}\n" + "\t\t\tint curIndex = headEndIndex + 1;\n" + "\t\t\tif (content.Length <= curIndex) {\n" + "\t\t\t\tLogger.LogWarning (\"数据内容为空,请注意\");\n" + "\t\t\t\treturn;\n" + "\t\t\t}\n" + "\t\t\tGDSParseUtils.ParseLine (delegate() {\n" + "\t\t\t\t" + className + " data = new " + className + " ();\n" ); for (int i = 0; i < varialbes.Count; ++i) { MemberVariablesInfo info = varialbes [i]; if (i != 0) { sb.Append("\t\t\t\tGDSParseUtils.MoveNextVariable (content, ref curIndex);\n"); } string parseCode = string.Empty; if (info.IsSelfDefineType) { parseCode = info.type + ".Parse (content, ref curIndex)"; } else { parseCode = ParseSystemTypeCode(info.type); } if (info.IsArray) { sb.Append("\t\t\t\tGDSParseUtils.ParseArray(delegate() {\n"); sb.Append("\t\t\t\t\tdata." + info.name + ".Add(" + parseCode + ");\n"); sb.Append("\t\t\t\t}, content, ref curIndex);\n"); } else { sb.Append("\t\t\t\tdata." + info.name + " = " + parseCode + ";\n"); } } sb.Append( "\t\t\t\tgdsDic.Add (data.id, data);\n" + "\t\t\t}, content, ref curIndex);\n" + "\t\t\tOutPut ();\n" + "\t\t}\n\n"); }
MemberVariablesInfo ParseNameAndType(string name, string type) { //{}表示为对象,[]表示为数组,|表示成员之间的分隔符 //rewards{info{type|id}|count}[] //RewardInfo{Info{int|id}|int}[] MemberVariablesInfo info = new MemberVariablesInfo(); int curIndex = 0; ParseName(info, null, name, ref curIndex); curIndex = 0; ParseType(info, null, type, ref curIndex); return(info); }
void GetVariableDefineStr(StringBuilder sb, List <MemberVariablesInfo> variables, string indentation) { sb.Append(indentation + "#region 成员变量\n"); for (int i = 0; i < variables.Count; ++i) { MemberVariablesInfo info = variables [i]; if (info.IsArray) { sb.Append(indentation + "public List<" + info.type + "> " + info.name + " = new List<" + info.type + ">();\n"); } else { sb.Append(indentation + "public " + info.type + " " + info.name + ";\n"); } } sb.Append(indentation + "#endregion\n"); }
public string GetCSFileContent() { GDSFileParser fileParser = new GDSFileParser(mFileName, mFileContent); List <MemberVariablesInfo> memberVariablesInfos = fileParser.GetMemberVariableInfos(); StringBuilder strSb = new StringBuilder(); strSb.Append(UsingStr); strSb.Append(NamespaceStr); strSb.Append("\tpublic class " + mFileName + "{\n"); //自定义类型的class的定义 strSb.Append("\t\t#region 成员自定义类型\n"); for (int i = 0; i < memberVariablesInfos.Count; ++i) { MemberVariablesInfo variableInfo = memberVariablesInfos [i]; GetSelfDefineClassStr(strSb, variableInfo); } strSb.Append("\t\t#endregion\n\n"); //成员变量的定义 GetVariableDefineStr(strSb, memberVariablesInfos, "\t\t"); //存储多个数据的字典的定义 strSb.Append("\n\n\t\tprotected static Dictionary<int, " + mFileName + "> gdsDic = new Dictionary<int, " + mFileName + ">();\n"); strSb.Append("\t\tprotected static List<" + mFileName + "> gdsList;\n\n"); //成员函数的定义 GetParseFuncStr(strSb, memberVariablesInfos, mFileName); GetInstanceFuncStr(strSb, mFileName); GetFindAllFuncStr(strSb, mFileName); GetClearFuncStr(strSb); GetOutPutFuncStr(strSb); strSb.Append("\t}\n"); strSb.Append("}"); return(strSb.ToString()); }
void ParseName(MemberVariablesInfo curInfo, MemberVariablesInfo parent, string nameStr, ref int curIndex) { bool isCloseWhile = false; while (curIndex < nameStr.Length && !isCloseWhile) { char curCh = nameStr [curIndex]; ++curIndex; switch (curCh) { case '{': //代表当前的类型为自定义类型 if (curInfo.IsSelfDefineType) { //重复设置自定义类型 throw new System.Exception("解析属性名称时,发现重复设置自定义类型,请检查{符号使用是否合理,字段为:" + nameStr); } curInfo.IsWaitSelfDefineClose = true; curInfo.IsSelfDefineType = true; MemberVariablesInfo childInfo = new MemberVariablesInfo(); curInfo.AddChild(childInfo); ParseName(childInfo, curInfo, nameStr, ref curIndex); break; case '}': //代表当前自定义类型结束 if (!parent.IsSelfDefineType) { //不合法的自定义类型结束符 throw new System.Exception("解析属性名称时,非自定义类型不应该设置符号‘}’:" + nameStr); } if (!parent.IsWaitSelfDefineClose) { // throw new System.Exception("解析属性名称时,发现多余的符号‘}’,请确保符号‘{’和‘}’是成对出现的,字段为:" + nameStr); } if (string.IsNullOrEmpty(curInfo.name)) { throw new System.Exception("解析属性名称时,发现非法的符号‘|’,请确保前一个属性名字设置后再使用符号‘|’,字段为:" + nameStr); } parent.IsWaitSelfDefineClose = false; isCloseWhile = true; break; case '[': if (curInfo.IsArray) { //当前字段已经设置过是数组了,重复设置抛出错误 throw new System.Exception("解析属性名称时,发现重复设置数组,字段为:" + nameStr); } curInfo.IsArray = true; curInfo.IsWaitCloseArray = true; break; case ']': if (!curInfo.IsWaitCloseArray) { //当前字段没有设置过数组开始,非法结束符 throw new System.Exception("解析属性名称时,发现数组结束符号前没有开始符号,字段为:" + nameStr); } char lastCh = nameStr [curIndex - 2]; if ('[' != lastCh) { //'['前面必须为'[' throw new System.Exception("解析属性名称时,发现符号']'之前不为'[',字段为:" + nameStr); } curInfo.IsWaitCloseArray = false; break; case '|': //进入下一个属性的解析 if (string.IsNullOrEmpty(curInfo.name)) { throw new System.Exception("解析属性名称时,发现非法的符号‘|’,请确保前一个属性名字设置后再使用符号‘|’,字段为:" + nameStr); } MemberVariablesInfo nextInfo = new MemberVariablesInfo(); parent.AddChild(nextInfo); ParseName(nextInfo, parent, nameStr, ref curIndex); if (!parent.IsWaitCloseArray) { isCloseWhile = true; } break; default: curInfo.name += curCh; break; } } }
void ParseType(MemberVariablesInfo curInfo, MemberVariablesInfo parent, string typeStr, ref int curIndex) { bool isCloseWhile = false; while (curIndex < typeStr.Length && !isCloseWhile) { char curCh = typeStr [curIndex]; ++curIndex; switch (curCh) { case '{': { MemberVariablesInfo child = curInfo.GetNextChild(); if (null == child) { throw new System.Exception("解析类型出错,发现额外的符号‘{’,请确保类型定义和名字定义一致,类型字符串:" + typeStr); } ParseType(child, curInfo, typeStr, ref curIndex); } break; case '}': { MemberVariablesInfo child = parent.GetNextChild(); if (null != child) { throw new System.Exception("解析类型出错,符号‘{’之前缺少了成员变量的类型定义,请确保类型定义和名字定义一致,类型字符串:" + typeStr); } isCloseWhile = true; } break; case '[': if (!curInfo.IsArray) { throw new System.Exception("解析类型出错,名称定义是是数组,但是类型定义时却不是数组,类型字符串:" + typeStr); } break; case ']': if (typeStr [curIndex - 2] != '[') { throw new System.Exception("解析类型出错,请确保符号'['和']'成对出现,类型字符串:" + typeStr); } break; case '|': if (string.IsNullOrEmpty(curInfo.type)) { throw new System.Exception("解析类型出错,成员变量定义的类型为空,类型字符串:" + typeStr); } MemberVariablesInfo nextInfo = parent.GetNextChild(); ParseType(nextInfo, parent, typeStr, ref curIndex); if (!parent.HasNextChild()) { isCloseWhile = true; } break; default: if (curInfo == null) { int a = 12; ++a; } curInfo.type += curCh; break; } } }