public override void WriteEnum(Message msg) { m_writer.WriteLine(String.Format("-- enum_name:{0}", msg.name)); foreach(var field in msg.fields) { m_writer.WriteLine(String.Format("{0} = {1}", field.name, field.index)); } }
public override void WriteEnum(Message msg) { m_writer.WriteLine(" public enum {0}", msg.name); m_writer.WriteLine(" {"); foreach(var field in msg.fields) { m_writer.WriteLine(" {0} = {1},", field.name, field.index); } m_writer.WriteLine(" }"); }
public override void WriteStruct(Message msg) { m_writer.WriteLine(" [ProtoPacket({0})]", msg.HasID ? msg.GetMsgID(".") : ""); m_writer.WriteLine(" public struct {0}", msg.name); m_writer.WriteLine(" {"); foreach(var field in msg.fields) { if (field.deprecated) continue; // 写入tag m_writer.WriteLine(" [ProtoField({0})]", field.index); m_writer.WriteLine(" public {0} {1};", CSharpFormat.GetFieldType(field), field.name); } m_writer.WriteLine(" }"); }
public override void WriteStruct(Message msg) { m_writer.WriteLine("{0} = proto_class(\"{0}\")", msg.name); if (msg.HasID) m_writer.WriteLine("{0}.proto_id = {1}", msg.name, msg.id_name); m_writer.WriteLine("{0}.proto_desc = {{", msg.name); foreach(var field in msg.fields) { if (field.deprecated) continue; m_writer.WriteLine( " [{0,-2}] = {{ kind = {1}, ftype = {2, -9}, ktype = {3, -9}, name = \"{4}\", }},", field.index, GetContainer(field), GetLuaType(field.value), GetLuaType(field.key), field.name ); } m_writer.WriteLine("}"); }
public override void WriteStruct(Message msg) { m_writer.WriteLine(String.Format("struct {0} : public pt_message", msg.name)); m_writer.WriteLine("{"); // fields foreach (var field in msg.fields) { if (field.deprecated) continue; if(field.container == Container.NONE) {// 指针 if(field.pointer) m_writer.WriteLine(" pt_ptr<{0}> {1};", GetTypeName(field.value), field.name); else m_writer.WriteLine(" {0} {1};", GetTypeName(field.value), field.name); } else if(field.container == Container.MAP || field.container == Container.HASH_MAP) { m_writer.WriteLine(" {0}<{1}, {2}> {3};", GetContainerName(field.container), GetTypeName(field.key), GetTypeName(field.value), field.name); } else { m_writer.WriteLine(" {0}<{1}> {2};", GetContainerName(field.container), GetTypeName(field.value), field.name); } } // msgID if(!string.IsNullOrEmpty(msg.id_name)) { m_writer.WriteLine(); m_writer.WriteLine(" size_t msgid() {{ return {0}; }}", msg.id_name); } List<FieldSet> fsets = new List<FieldSet>(); ProcessFields(msg.fields, fsets); WriteEncode(msg.fields, fsets); WriteDecode(msg.fields, fsets); m_writer.WriteLine("};"); }
private void ParseFields(Message message, ProtoReader reader) { string data = reader.ReadLine(); if (data != "{") throw new Exception("message must start {"); while(true) { if (reader.EndOfStream) throw new Exception("not find }"); data = reader.ReadLine(); if (data == "}") break; Field field; switch(message.type) { case CmdType.ENUM: field = ParseEnumField(data, reader.Line); break; case CmdType.STRUCT: field = ParseStructField(data, reader.Line); break; default: throw new Exception("unknow msg type"); } message.fields.Add(field); } }
public void Parse() { ProtoReader reader = new ProtoReader(m_path); while(!reader.EndOfStream) { string cmd = reader.ReadLine(); if (cmd.Length == 0) continue; if(cmd.StartsWith("import ")) { int start = cmd.IndexOf('\"', 7); int end = cmd.LastIndexOf('\"'); if (start == -1 || end == -1) throw new Exception("bad import" + reader.Line + cmd); string file = cmd.Substring(start + 1, end - start - 1); if (!file.EndsWith(".proto")) throw new Exception(String.Format("import must end with .proto in line{0} of file{1}", reader.Line, m_path)); file = file.Remove(file.Length - 6); m_imports.Add(file); continue; } // 数据结构处理 string[] tokens = cmd.Split(new char[]{ ' '}, StringSplitOptions.RemoveEmptyEntries); CmdType type = CmdType.UNKNOWN; switch(tokens[0]) { case "enum":type = CmdType.ENUM;break; case "struct":type = CmdType.STRUCT;break; default: throw new Exception("unknow cmd line =" + reader.Line + ":cmd =" + cmd); } if (tokens.Length != 2 && tokens.Length != 4) throw new Exception("bad message" + reader.Line + cmd); Message message = new Message(); m_messages.Add(message); message.type = type; message.name = tokens[1]; if(tokens.Length == 4) { if(tokens[2] != "=") throw new Exception("bad message" + reader.Line + cmd); message.id_name = tokens[3]; } ParseFields(message, reader); message.process(); Process(); } }
public abstract void WriteEnum(Message msg);
public abstract void WriteStruct(Message msg);