public static ProtobufMessage Copy(ProtobufMessage origin) { ProtobufMessage newProtobufMessage = new ProtobufMessage { Name = origin.Name, EnumItems = new List <EnumItem>(), MessageItems = new List <MessageItem>() }; origin.EnumItems.ForEach(i => newProtobufMessage.EnumItems.Add(EnumItem.Copy(i))); origin.MessageItems.ForEach(i => newProtobufMessage.MessageItems.Add(MessageItem.Copy(i))); return(newProtobufMessage); }
public static EnumItem Copy(EnumItem origin) { EnumItem newEnumItem = new EnumItem { Name = origin.Name, EnumItems = new Dictionary <string, string>() }; foreach (var originEnumItem in origin.EnumItems) { newEnumItem.EnumItems.Add(originEnumItem.Key, originEnumItem.Value); } return(newEnumItem); }
private void AddEnumKey(EnumItem enumItem) { if (!enumItem.EnumItems.ContainsKey("new_key")) { enumItem.EnumItems.Add("new_key", ""); } else { var id = 1; while (enumItem.EnumItems.ContainsKey("new_key_" + id)) { id++; } enumItem.EnumItems.Add("new_key_" + id, ""); } }
private void LoadAndDeserializeFile() { List <string> lines = new List <string>(); m_Message = new ProtobufMessage { MessageItems = new List <MessageItem>(), EnumItems = new List <EnumItem>() }; StreamReader file = new StreamReader(m_MessageFilePaths[m_SelectedFile]); var line = file.ReadLine(); while (line != null) { lines.Add(line); line = file.ReadLine(); } file.Close(); bool inItem = false; bool inEnum = false; EnumItem enumItem = null; foreach (var line1 in lines) { if (string.IsNullOrWhiteSpace(line1)) { continue; } var words = line1.Trim().Split(' '); if (inItem) { if (words[0] == "enum") { inEnum = true; enumItem = new EnumItem { EnumItems = new Dictionary <string, string>() }; } if (inEnum) { if (words[0] == "enum") { enumItem.Name = words[1]; } else if (words[0] == "}") { inEnum = false; m_Message.EnumItems.Add(enumItem); } else { enumItem.EnumItems[words[0]] = words[2].Substring(0, words[2].Length - 1); } continue; } if (words[0] == "}") { continue; } var messageItem = new MessageItem { Repeated = false, Optional = false }; bool hasType = false; bool hasName = false; foreach (var word in words) { switch (word) { case "repeated": messageItem.Repeated = true; break; case "optional": messageItem.Optional = true; break; default: if (!hasType) { messageItem.Type = word; hasType = true; break; } else if (!hasName) { messageItem.Name = word; hasName = true; break; } else if (word.EndsWith(";")) { var numStr = word.Substring(0, word.Length - 1); messageItem.Id = numStr; break; } else { break; } } } m_Message.MessageItems.Add(messageItem); } else { switch (words[0]) { case "message": m_Message.Name = words[1]; inItem = true; break; default: break; } } } m_MessageItems = m_Message.MessageItems; m_EnumItems = m_Message.EnumItems; GetAllTypes(); m_OriginMessage = ProtobufMessage.Copy(m_Message); }