public static void ParseStructure(PacketsRegistry packetsRegistry, string structure) { var reader = new StringReader(structure.Trim()); var head = reader.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var type = head[0]; var id = head[1]; if (id.EndsWith(":")) { id = id.Substring(0, id.Length - 1); } var declaration = new StructureDeclaration(); while (true) { var line = reader.ReadLine(); if (string.IsNullOrWhiteSpace(line)) { break; } var field = Ext.ParseArgs(line); var fieldType = field[0]; var fieldName = field[1]; if (fieldName.EndsWith(";")) { fieldName = fieldName.Substring(0, fieldName.Length - 1); } declaration.Fields.Add(new KeyValuePair <string, string>(fieldName, fieldType)); } switch (type) { case "packet": packetsRegistry.Register(id, declaration); break; case "struct": case "type": packetsRegistry.Serializer.Register(id, declaration); break; } }
private StructureDeclaration GetStructDeclaration(AstToken token) { StructureDeclaration structure = new StructureDeclaration(); if (token.properties[0] == "union") { structure.isUnion = true; } else if (token.properties[0] == "class") { structure.isClass = true; } else if (token.properties[0] != "struct") { throw new ArgumentException(); } if (token.properties.Length > 1) { structure.name = token.properties[1]; } foreach (var childToken in token.children) { switch (childToken.Type) { case AstType.DefinitionData: // almost no idea how to parse it // TODO break; case AstType.CXXRecordDecl: structure.subStructures.Add(GetStructDeclaration(childToken)); break; case AstType.FieldDecl: structure.properties.Add(new StructureDeclaration.Property() { name = childToken.properties[0], type = childToken.properties[1] }); break; case AstType.Public: // TODO break; case AstType.FullComment: // ignore this. or TODO if you wish break; case AstType.AccessSpecDecl: // TODO local modificator based on properties[0] break; case AstType.CXXConstructorDecl: // TODO break; case AstType.CXXDestructorDecl: // TODO break; case AstType.CXXMethodDecl: // TODO structure.others.Add(childToken.properties[0] + " " + childToken.properties[1]); break; default: throw new Exception(childToken.unknownName); } } return(structure); }