/// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            MapBlock temp = (MapBlock)obj;

            if (KeywordPosition > temp.KeywordPosition)
            {
                return(1);
            }
            else
            {
                if (temp.KeywordPosition == KeywordPosition)
                {
                    return(0);
                }
                else
                {
                    return(-1);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapFile"></param>
		private void LoadMapFile(string mapFile)
		{
			FileStream map;

			// Load the protocol map file
			try
			{
				map = new FileStream(mapFile, FileMode.Open, FileAccess.Read); 
			}
			catch(Exception e) 
			{
				throw new Exception("Map file error", e);
			}

			try
			{
				StreamReader r = new StreamReader(map);        
				r.BaseStream.Seek(0, SeekOrigin.Begin);
				string newline;
				string trimmedline;
				bool inPacket = false;
				bool inBlock = false;
				MapPacket currentPacket = null;
				MapBlock currentBlock = null;
				char[] trimArray = new char[] {' ', '\t'};

				// While not at the end of the file
				while (r.Peek() > -1) 
				{
					#region ParseMap

					newline = r.ReadLine();
					trimmedline = System.Text.RegularExpressions.Regex.Replace(newline, @"\s+", " ");
					trimmedline = trimmedline.Trim(trimArray);

					if (!inPacket)
					{
						// Outside of all packet blocks

						if (trimmedline == "{")
						{
							inPacket = true;
						}
					}
					else
					{
						// Inside of a packet block

						if (!inBlock)
						{
							// Inside a packet block, outside of the blocks

							if (trimmedline == "{")
							{
								inBlock = true;
							}
							else if (trimmedline == "}")
							{
								// Reached the end of the packet
								// currentPacket.Blocks.Sort();
								inPacket = false;
							}
							else 
							{
								// The packet header
								#region ParsePacketHeader

								// Splice the string in to tokens
								string[] tokens = trimmedline.Split(new char[] {' ', '\t'});

								if (tokens.Length > 3)
								{
                                    //Hash packet name to insure correct keyword ordering
                                    KeywordPosition(tokens[0]);

									uint packetID;										

									// Remove the leading "0x"
									if (tokens[2].Length > 2 && tokens[2].Substring(0, 2) == "0x")
									{
										tokens[2] = tokens[2].Substring(2, tokens[2].Length - 2);
										packetID = UInt32.Parse(tokens[2], System.Globalization.NumberStyles.HexNumber);
									} else {
										packetID = UInt32.Parse(tokens[2]);	
									}
										

									if (tokens[1] == "Fixed")
									{
										
										// Truncate the id to a short
										packetID &= 0xFFFF;
										LowMaps[packetID] = new MapPacket();
										LowMaps[packetID].ID = (ushort)packetID;
										LowMaps[packetID].Frequency = PacketFrequency.Low;
										LowMaps[packetID].Name = tokens[0];
										LowMaps[packetID].Trusted = (tokens[3] == "Trusted");
										LowMaps[packetID].Encoded = (tokens[4] == "Zerocoded");
										LowMaps[packetID].Blocks = new List<MapBlock>();

										currentPacket = LowMaps[packetID];
									}
									else if (tokens[1] == "Low")
									{
										LowMaps[packetID] = new MapPacket();
										LowMaps[packetID].ID = (ushort)packetID;
										LowMaps[packetID].Frequency = PacketFrequency.Low;
										LowMaps[packetID].Name = tokens[0];
										LowMaps[packetID].Trusted = (tokens[2] == "Trusted");
										LowMaps[packetID].Encoded = (tokens[3] == "Zerocoded");
										LowMaps[packetID].Blocks = new List<MapBlock>();

										currentPacket = LowMaps[packetID];

									}
									else if (tokens[1] == "Medium")
									{
										MediumMaps[packetID] = new MapPacket();
										MediumMaps[packetID].ID = (ushort)packetID;
										MediumMaps[packetID].Frequency = PacketFrequency.Medium;
										MediumMaps[packetID].Name = tokens[0];
										MediumMaps[packetID].Trusted = (tokens[2] == "Trusted");
										MediumMaps[packetID].Encoded = (tokens[3] == "Zerocoded");
										MediumMaps[packetID].Blocks = new List<MapBlock>();

										currentPacket = MediumMaps[packetID];

									}
									else if (tokens[1] == "High")
									{
										HighMaps[packetID] = new MapPacket();
										HighMaps[packetID].ID = (ushort)packetID;
										HighMaps[packetID].Frequency = PacketFrequency.High;
										HighMaps[packetID].Name = tokens[0];
										HighMaps[packetID].Trusted = (tokens[2] == "Trusted");
										HighMaps[packetID].Encoded = (tokens[3] == "Zerocoded");
										HighMaps[packetID].Blocks = new List<MapBlock>();

										currentPacket = HighMaps[packetID];

									}
									else
									{
										//Client.Log("Unknown packet frequency", Helpers.LogLevel.Error);
                                        throw new Exception("Unknown packet frequency");
									}
								}

								#endregion
							}
						}
						else
						{
							if (trimmedline.Length > 0 && trimmedline.Substring(0, 1) == "{")
							{
								// A field
								#region ParseField

								MapField field = new MapField();

								// Splice the string in to tokens
								string[] tokens = trimmedline.Split(new char[] {' ', '\t'});

								field.Name = tokens[1];
								field.KeywordPosition = KeywordPosition(field.Name);
								field.Type = (FieldType)Enum.Parse(typeof(FieldType), tokens[2], true);

								if (tokens[3] != "}")
								{
									field.Count = Int32.Parse(tokens[3]);
								}
								else
								{
									field.Count = 1;
								}

								// Save this field to the current block
								currentBlock.Fields.Add(field);

								#endregion
							}
							else if (trimmedline == "}")
							{
								// currentBlock.Fields.Sort();
								inBlock = false;
							}
							else if (trimmedline.Length != 0 && trimmedline.Substring(0, 2) != "//")
							{
								// The block header
								#region ParseBlockHeader

								currentBlock = new MapBlock();

								// Splice the string in to tokens
								string[] tokens = trimmedline.Split(new char[] {' ', '\t'});

								currentBlock.Name = tokens[0];
								currentBlock.KeywordPosition = KeywordPosition(currentBlock.Name);
								currentBlock.Fields = new List<MapField>();
								currentPacket.Blocks.Add(currentBlock);

								if (tokens[1] == "Single")
								{
									currentBlock.Count = 1;
								}
								else if (tokens[1] == "Multiple")
								{
									currentBlock.Count = Int32.Parse(tokens[2]);
								}
								else if (tokens[1] == "Variable")
								{
									currentBlock.Count = -1;
								}
								else
								{
									//Client.Log("Unknown block frequency", Helpers.LogLevel.Error);
                                    throw new Exception("Unknown block frequency");
								}

								#endregion
							}
						}
					}

					#endregion
				}

				r.Close();
				map.Close();
			}
			catch (Exception e)
			{
                throw e;
			}
		}
        /// <summary>
        ///
        /// </summary>
        /// <param name="mapFile"></param>
        private void LoadMapFile(string mapFile)
        {
            FileStream map;

            // Load the protocol map file
            try
            {
                map = new FileStream(mapFile, FileMode.Open, FileAccess.Read);
            }
            catch (Exception e)
            {
                throw new Exception("Map file error", e);
            }

            try
            {
                StreamReader r = new StreamReader(map);
                r.BaseStream.Seek(0, SeekOrigin.Begin);
                string    newline;
                string    trimmedline;
                bool      inPacket      = false;
                bool      inBlock       = false;
                MapPacket currentPacket = null;
                MapBlock  currentBlock  = null;
                char[]    trimArray     = new char[] { ' ', '\t' };

                // While not at the end of the file
                while (r.Peek() > -1)
                {
                    #region ParseMap

                    newline     = r.ReadLine();
                    trimmedline = System.Text.RegularExpressions.Regex.Replace(newline, @"\s+", " ");
                    trimmedline = trimmedline.Trim(trimArray);

                    if (!inPacket)
                    {
                        // Outside of all packet blocks

                        if (trimmedline == "{")
                        {
                            inPacket = true;
                        }
                    }
                    else
                    {
                        // Inside of a packet block

                        if (!inBlock)
                        {
                            // Inside a packet block, outside of the blocks

                            if (trimmedline == "{")
                            {
                                inBlock = true;
                            }
                            else if (trimmedline == "}")
                            {
                                // Reached the end of the packet
                                // currentPacket.Blocks.Sort();
                                inPacket = false;
                            }
                            else
                            {
                                // Skip comments
                                if (trimmedline.StartsWith("//"))
                                {
                                    continue;
                                }

                                // The packet header
                                #region ParsePacketHeader

                                // Splice the string in to tokens
                                string[] tokens = trimmedline.Split(new char[] { ' ', '\t' });

                                if (tokens.Length > 3)
                                {
                                    //Hash packet name to insure correct keyword ordering
                                    KeywordPosition(tokens[0]);

                                    uint packetID;

                                    // Remove the leading "0x"
                                    if (tokens[2].Length > 2 && tokens[2].Substring(0, 2) == "0x")
                                    {
                                        tokens[2] = tokens[2].Substring(2, tokens[2].Length - 2);
                                        packetID  = UInt32.Parse(tokens[2], System.Globalization.NumberStyles.HexNumber);
                                    }
                                    else
                                    {
                                        packetID = UInt32.Parse(tokens[2]);
                                    }


                                    if (tokens[1] == "Fixed")
                                    {
                                        // Truncate the id to a short
                                        packetID         &= 0xFFFF;
                                        LowMaps[packetID] = new MapPacket
                                        {
                                            ID        = (ushort)packetID,
                                            Frequency = PacketFrequency.Low,
                                            Name      = tokens[0],
                                            Trusted   = (tokens[3] == "Trusted"),
                                            Encoded   = (tokens[4] == "Zerocoded"),
                                            Blocks    = new List <MapBlock>()
                                        };

                                        currentPacket = LowMaps[packetID];
                                    }
                                    else if (tokens[1] == "Low")
                                    {
                                        LowMaps[packetID] = new MapPacket
                                        {
                                            ID        = (ushort)packetID,
                                            Frequency = PacketFrequency.Low,
                                            Name      = tokens[0],
                                            Trusted   = (tokens[2] == "Trusted"),
                                            Encoded   = (tokens[4] == "Zerocoded"),
                                            Blocks    = new List <MapBlock>()
                                        };

                                        currentPacket = LowMaps[packetID];
                                    }
                                    else if (tokens[1] == "Medium")
                                    {
                                        MediumMaps[packetID] = new MapPacket
                                        {
                                            ID        = (ushort)packetID,
                                            Frequency = PacketFrequency.Medium,
                                            Name      = tokens[0],
                                            Trusted   = (tokens[2] == "Trusted"),
                                            Encoded   = (tokens[4] == "Zerocoded"),
                                            Blocks    = new List <MapBlock>()
                                        };

                                        currentPacket = MediumMaps[packetID];
                                    }
                                    else if (tokens[1] == "High")
                                    {
                                        HighMaps[packetID] = new MapPacket
                                        {
                                            ID        = (ushort)packetID,
                                            Frequency = PacketFrequency.High,
                                            Name      = tokens[0],
                                            Trusted   = (tokens[2] == "Trusted"),
                                            Encoded   = (tokens[4] == "Zerocoded"),
                                            Blocks    = new List <MapBlock>()
                                        };

                                        currentPacket = HighMaps[packetID];
                                    }
                                    else
                                    {
                                        //Client.Log("Unknown packet frequency", Helpers.LogLevel.Error);
                                        throw new Exception("Unknown packet frequency");
                                    }
                                }

                                #endregion
                            }
                        }
                        else
                        {
                            if (trimmedline.Length > 0 && trimmedline.Substring(0, 1) == "{")
                            {
                                // A field
                                #region ParseField

                                MapField field = new MapField();

                                // Splice the string in to tokens
                                string[] tokens = trimmedline.Split(new char[] { ' ', '\t' });

                                field.Name            = tokens[1];
                                field.KeywordPosition = KeywordPosition(field.Name);
                                field.Type            = (FieldType)Enum.Parse(typeof(FieldType), tokens[2], true);

                                field.Count = tokens[3] != "}" ? Int32.Parse(tokens[3]) : 1;

                                // Save this field to the current block
                                currentBlock.Fields.Add(field);

                                #endregion
                            }
                            else if (trimmedline == "}")
                            {
                                // currentBlock.Fields.Sort();
                                inBlock = false;
                            }
                            else if (trimmedline.Length != 0 && trimmedline.Substring(0, 2) != "//")
                            {
                                // The block header
                                #region ParseBlockHeader

                                currentBlock = new MapBlock();

                                // Splice the string in to tokens
                                string[] tokens = trimmedline.Split(new char[] { ' ', '\t' });

                                currentBlock.Name            = tokens[0];
                                currentBlock.KeywordPosition = KeywordPosition(currentBlock.Name);
                                currentBlock.Fields          = new List <MapField>();
                                currentPacket.Blocks.Add(currentBlock);

                                if (tokens[1] == "Single")
                                {
                                    currentBlock.Count = 1;
                                }
                                else if (tokens[1] == "Multiple")
                                {
                                    currentBlock.Count = Int32.Parse(tokens[2]);
                                }
                                else if (tokens[1] == "Variable")
                                {
                                    currentBlock.Count = -1;
                                }
                                else
                                {
                                    //Client.Log("Unknown block frequency", Helpers.LogLevel.Error);
                                    throw new Exception("Unknown block frequency");
                                }

                                #endregion
                            }
                        }
                    }

                    #endregion
                }

                r.Close();
                map.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 4
0
        static void WriteBlockClass(TextWriter writer, MapBlock block, MapPacket packet)
        {
            int variableFieldCountBytes = 0;

            //writer.WriteLine("        /// <summary>" + block.Name + " block</summary>");
            writer.WriteLine("        /// <exclude/>");
            writer.WriteLine("        public sealed class " + block.Name + "Block : PacketBlock" + Environment.NewLine + "        {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldMember(writer, field);
                if (field.Type == FieldType.Variable) { variableFieldCountBytes += field.Count; }
            }

            // Length property
            writer.WriteLine("");
            //writer.WriteLine("            /// <summary>Length of this block serialized in bytes</summary>");
            writer.WriteLine("            public override int Length" + Environment.NewLine +
                             "            {" + Environment.NewLine +
                             "                get" + Environment.NewLine +
                             "                {");
            int length = variableFieldCountBytes;

            // Figure out the length of this block
            foreach (MapField field in block.Fields)
            {
                length += GetFieldLength(writer, field);
            }

            if (variableFieldCountBytes == 0)
            {
                writer.WriteLine("                    return " + length + ";");
            }
            else
            {
                writer.WriteLine("                    int length = " + length + ";");

                foreach (MapField field in block.Fields)
                {
                    if (field.Type == FieldType.Variable)
                    {
                        writer.WriteLine("                    if (" + field.Name +
                            " != null) { length += " + field.Name + ".Length; }");
                    }
                }

                writer.WriteLine("                    return length;");
            }

            writer.WriteLine("                }" + Environment.NewLine + "            }" + Environment.NewLine);

            // Default constructor
            //writer.WriteLine("            /// <summary>Default constructor</summary>");
            writer.WriteLine("            public " + block.Name + "Block() { }");

            // Constructor for building the class from bytes
            //writer.WriteLine("            /// <summary>Constructor for building the block from a byte array</summary>");
            writer.WriteLine("            public " + block.Name + "Block(byte[] bytes, ref int i)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                FromBytes(bytes, ref i);" + Environment.NewLine +
                "            }" + Environment.NewLine);

            // Initiates instance variables from a byte message
            writer.WriteLine("            public override void FromBytes(byte[] bytes, ref int i)" + Environment.NewLine +
                "            {");

            // Declare a length variable if we need it for variable fields in this constructor
            if (variableFieldCountBytes > 0) { writer.WriteLine("                int length;"); }

            // Start of the try catch block
            writer.WriteLine("                try" + Environment.NewLine + "                {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldFromBytes(writer, field);
            }

            writer.WriteLine("                }" + Environment.NewLine +
                "                catch (Exception)" + Environment.NewLine +
                "                {" + Environment.NewLine +
                "                    throw new MalformedDataException();" + Environment.NewLine +
                "                }" + Environment.NewLine + "            }" + Environment.NewLine);

            // ToBytes() function
            //writer.WriteLine("            /// <summary>Serialize this block to a byte array</summary>");
            writer.WriteLine("            public override void ToBytes(byte[] bytes, ref int i)" + Environment.NewLine +
                "            {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldToBytes(writer, field);
            }

            writer.WriteLine("            }" + Environment.NewLine);
            writer.WriteLine("        }" + Environment.NewLine);
        }
Esempio n. 5
0
        static void WriteBlockClass(TextWriter writer, MapBlock block, MapPacket packet)
        {
            bool variableFields = false;

            //writer.WriteLine("        /// <summary>" + block.Name + " block</summary>");
            writer.WriteLine("        /// <exclude/>");
            writer.WriteLine("        public class " + block.Name + "Block : PacketBlock" + Environment.NewLine + "        {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldMember(writer, field);
                if (field.Type == FieldType.Variable) { variableFields = true; }
            }

            // Length property
            writer.WriteLine("");
            //writer.WriteLine("            /// <summary>Length of this block serialized in bytes</summary>");
            writer.WriteLine("            public override int Length" + Environment.NewLine +
                             "            {" + Environment.NewLine +
                             "                get" + Environment.NewLine +
                             "                {");
            int length = 0;

            // Figure out the length of this block
            foreach (MapField field in block.Fields)
            {
                length += GetFieldLength(writer, field);
            }

            if (!variableFields)
            {
                writer.WriteLine("                    return " + length + ";");
            }
            else
            {
                writer.WriteLine("                    int length = " + length + ";");

                foreach (MapField field in block.Fields)
                {
                    if (field.Type == FieldType.Variable)
                    {
                        writer.WriteLine("                    if (" + field.Name +
                            " != null) { length += " + field.Count + " + " + field.Name + ".Length; }");
                    }
                }

                writer.WriteLine("                    return length;");
            }

            writer.WriteLine("                }" + Environment.NewLine + "            }" + Environment.NewLine);

            // Default constructor
            //writer.WriteLine("            /// <summary>Default constructor</summary>");
            writer.WriteLine("            public " + block.Name + "Block() { }");

            // Constructor for building the class from bytes
            //writer.WriteLine("            /// <summary>Constructor for building the block from a byte array</summary>");
            writer.WriteLine("            public " + block.Name + "Block(byte[] bytes, ref int i)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "                FromBytes(bytes, ref i);" + Environment.NewLine +
                "            }" + Environment.NewLine);

            // Initiates instance variables from a byte message
            writer.WriteLine("            public override void FromBytes(byte[] bytes, ref int i)" + Environment.NewLine +
                "            {");

            // Declare a length variable if we need it for variable fields in this constructor
            if (variableFields) { writer.WriteLine("                int length;"); }

            // Start of the try catch block
            writer.WriteLine("                try" + Environment.NewLine + "                {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldFromBytes(writer, field);
            }

            writer.WriteLine("                }" + Environment.NewLine +
                "                catch (Exception)" + Environment.NewLine +
                "                {" + Environment.NewLine +
                "                    throw new MalformedDataException();" + Environment.NewLine +
                "                }" + Environment.NewLine + "            }" + Environment.NewLine);

            // ToBytes() function
            //writer.WriteLine("            /// <summary>Serialize this block to a byte array</summary>");
            writer.WriteLine("            public override void ToBytes(byte[] bytes, ref int i)" + Environment.NewLine +
                "            {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldToBytes(writer, field);
            }

            writer.WriteLine("            }" + Environment.NewLine);

            // ToString() function
            writer.WriteLine("            public override string ToString()" + Environment.NewLine + "            {");
            writer.WriteLine("                StringBuilder output = new StringBuilder();");
            writer.WriteLine("                output.AppendLine(\"-- " + block.Name + " --\");");

            for (int i = 0; i < block.Fields.Count; i++)
            {
                MapField field = block.Fields[i];

                if (field.Type == FieldType.Variable || field.Type == FieldType.Fixed)
                {
                    writer.WriteLine("                Helpers.FieldToString(output, " + field.Name + ", \"" + field.Name + "\");");
                    if (i != block.Fields.Count - 1) writer.WriteLine("                output.Append(Environment.NewLine);");
                }
                else
                {
                    if (i != block.Fields.Count - 1) writer.WriteLine("                output.AppendLine(String.Format(\"" + field.Name + ": {0}\", " + field.Name + "));");
                    else writer.WriteLine("                output.Append(String.Format(\"" + field.Name + ": {0}\", " + field.Name + "));");
                }
            }

            writer.WriteLine("                return output.ToString();" + Environment.NewLine + "            }");
            writer.WriteLine("        }" + Environment.NewLine);
        }
Esempio n. 6
0
        static void WriteBlockClass(TextWriter writer, MapBlock block, MapPacket packet)
        {
            bool variableFields = false;

            //writer.WriteLine("        /// <summary>" + block.Name + " block</summary>");
            writer.WriteLine("        /// <exclude/>");
            writer.WriteLine("        public class " + block.Name + "Block : PacketBlock" + Environment.NewLine + "        {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldMember(writer, field);
                if (field.Type == FieldType.Variable)
                {
                    variableFields = true;
                }
            }

            // Length property
            writer.WriteLine("");
            //writer.WriteLine("            /// <summary>Length of this block serialized in bytes</summary>");
            writer.WriteLine("            public override int Length" + Environment.NewLine +
                             "            {" + Environment.NewLine +
                             "                get" + Environment.NewLine +
                             "                {");
            int length = 0;

            // Figure out the length of this block
            foreach (MapField field in block.Fields)
            {
                length += GetFieldLength(writer, field);
            }

            if (!variableFields)
            {
                writer.WriteLine("                    return " + length + ";");
            }
            else
            {
                writer.WriteLine("                    int length = " + length + ";");

                foreach (MapField field in block.Fields)
                {
                    if (field.Type == FieldType.Variable)
                    {
                        writer.WriteLine("                    if (" + field.Name +
                                         " != null) { length += " + field.Count + " + " + field.Name + ".Length; }");
                    }
                }

                writer.WriteLine("                    return length;");
            }

            writer.WriteLine("                }" + Environment.NewLine + "            }" + Environment.NewLine);

            // Default constructor
            //writer.WriteLine("            /// <summary>Default constructor</summary>");
            writer.WriteLine("            public " + block.Name + "Block() { }");

            // Constructor for building the class from bytes
            //writer.WriteLine("            /// <summary>Constructor for building the block from a byte array</summary>");
            writer.WriteLine("            public " + block.Name + "Block(byte[] bytes, ref int i)" + Environment.NewLine +
                             "            {" + Environment.NewLine +
                             "                FromBytes(bytes, ref i);" + Environment.NewLine +
                             "            }" + Environment.NewLine);

            // Initiates instance variables from a byte message
            writer.WriteLine("            public override void FromBytes(byte[] bytes, ref int i)" + Environment.NewLine +
                             "            {");

            // Declare a length variable if we need it for variable fields in this constructor
            if (variableFields)
            {
                writer.WriteLine("                int length;");
            }

            // Start of the try catch block
            writer.WriteLine("                try" + Environment.NewLine + "                {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldFromBytes(writer, field);
            }

            writer.WriteLine("                }" + Environment.NewLine +
                             "                catch (Exception)" + Environment.NewLine +
                             "                {" + Environment.NewLine +
                             "                    throw new MalformedDataException();" + Environment.NewLine +
                             "                }" + Environment.NewLine + "            }" + Environment.NewLine);

            // ToBytes() function
            //writer.WriteLine("            /// <summary>Serialize this block to a byte array</summary>");
            writer.WriteLine("            public override void ToBytes(byte[] bytes, ref int i)" + Environment.NewLine +
                             "            {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldToBytes(writer, field);
            }

            writer.WriteLine("            }" + Environment.NewLine);

            // ToString() function
            writer.WriteLine("            public override string ToString()" + Environment.NewLine + "            {");
            writer.WriteLine("                StringBuilder output = new StringBuilder();");
            writer.WriteLine("                output.AppendLine(\"-- " + block.Name + " --\");");

            for (int i = 0; i < block.Fields.Count; i++)
            {
                MapField field = block.Fields[i];

                if (field.Type == FieldType.Variable || field.Type == FieldType.Fixed)
                {
                    writer.WriteLine("                Helpers.FieldToString(output, " + field.Name + ", \"" + field.Name + "\");");
                    if (i != block.Fields.Count - 1)
                    {
                        writer.WriteLine("                output.Append(Environment.NewLine);");
                    }
                }
                else
                {
                    if (i != block.Fields.Count - 1)
                    {
                        writer.WriteLine("                output.AppendLine(String.Format(\"" + field.Name + ": {0}\", " + field.Name + "));");
                    }
                    else
                    {
                        writer.WriteLine("                output.Append(String.Format(\"" + field.Name + ": {0}\", " + field.Name + "));");
                    }
                }
            }

            writer.WriteLine("                return output.ToString();" + Environment.NewLine + "            }");
            writer.WriteLine("        }" + Environment.NewLine);
        }
Esempio n. 7
0
        static void WriteBlockClass(TextWriter writer, MapBlock block, MapPacket packet)
        {
            bool variableFields = false;
            bool floatFields    = false;

            //writer.WriteLine("        /// <summary>" + block.Name + " block</summary>");
            writer.WriteLine("        /// <exclude/>");
            writer.WriteLine("        [XmlType(\"" + packet.Name.ToLower() + "_" + block.Name.ToLower() + "\")]");
            writer.WriteLine("        public class " + block.Name + "Block" + Environment.NewLine + "        {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldMember(writer, field);

                if (field.Type == FieldType.Variable)
                {
                    variableFields = true;
                }
                if (field.Type == FieldType.F32 || field.Type == FieldType.F64)
                {
                    floatFields = true;
                }
            }

            // Length property
            writer.WriteLine("");
            //writer.WriteLine("            /// <summary>Length of this block serialized in bytes</summary>");
            writer.WriteLine("            [XmlIgnore]" + Environment.NewLine +
                             "            public int Length" + Environment.NewLine +
                             "            {" + Environment.NewLine +
                             "                get" + Environment.NewLine +
                             "                {");
            int length = 0;

            // Figure out the length of this block
            foreach (MapField field in block.Fields)
            {
                length += GetFieldLength(writer, field);
            }

            if (!variableFields)
            {
                writer.WriteLine("                    return " + length + ";");
            }
            else
            {
                writer.WriteLine("                    int length = " + length + ";");

                foreach (MapField field in block.Fields)
                {
                    if (field.Type == FieldType.Variable)
                    {
                        writer.WriteLine("                    if (" + field.Name +
                                         " != null) { length += " + field.Count + " + " + field.Name + ".Length; }");
                    }
                }

                writer.WriteLine("                    return length;");
            }

            writer.WriteLine("                }" + Environment.NewLine + "            }" + Environment.NewLine);

            // Default constructor
            //writer.WriteLine("            /// <summary>Default constructor</summary>");
            writer.WriteLine("            public " + block.Name + "Block() { }");

            // Constructor for building the class from bytes
            //writer.WriteLine("            /// <summary>Constructor for building the block from a byte array</summary>");
            writer.WriteLine("            public " + block.Name + "Block(byte[] bytes, ref int i)" + Environment.NewLine +
                             "            {");

            // Declare a length variable if we need it for variable fields in this constructor
            if (variableFields)
            {
                writer.WriteLine("                int length;");
            }

            // Start of the try catch block
            writer.WriteLine("                try" + Environment.NewLine + "                {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldFromBytes(writer, field);
            }

            writer.WriteLine("                }" + Environment.NewLine +
                             "                catch (Exception)" + Environment.NewLine +
                             "                {" + Environment.NewLine +
                             "                    throw new MalformedDataException();" + Environment.NewLine +
                             "                }" + Environment.NewLine + "            }" + Environment.NewLine);

            // ToBytes() function
            //writer.WriteLine("            /// <summary>Serialize this block to a byte array</summary>");
            writer.WriteLine("            public void ToBytes(byte[] bytes, ref int i)" + Environment.NewLine +
                             "            {");

            // Declare a byte[] variable if we need it for floating point field conversions
            if (floatFields)
            {
                writer.WriteLine("                byte[] ba;");
            }

            foreach (MapField field in block.Fields)
            {
                WriteFieldToBytes(writer, field);
            }

            writer.WriteLine("            }" + Environment.NewLine);

            // ToString() function
            //writer.WriteLine("            /// <summary>Serialize this block to a string</summary><returns>A string containing the serialized block</returns>");
            writer.WriteLine("            public override string ToString()" + Environment.NewLine + "            {");
            writer.WriteLine("                string output = \"-- " + block.Name + " --\" + Environment.NewLine;");

            foreach (MapField field in block.Fields)
            {
                if (field.Type == FieldType.Variable || field.Type == FieldType.Fixed)
                {
                    writer.WriteLine("                output += Helpers.FieldToString(" + field.Name + ", \"" + field.Name + "\") + \"\" + Environment.NewLine;");
                }
                else
                {
                    writer.WriteLine("                output += \"" + field.Name + ": \" + " + field.Name + ".ToString() + \"\" + Environment.NewLine;");
                }
            }

            writer.WriteLine("                output = output.Trim();" + Environment.NewLine +
                             "                return output;" + Environment.NewLine + "            }");

            writer.WriteLine("        }" + Environment.NewLine);
        }
Esempio n. 8
0
        static void WriteBlockClass(TextWriter writer, MapBlock block)
        {
            bool variableFields = false;
            bool floatFields    = false;

            writer.WriteLine("        /// <summary>" + block.Name + " block</summary>");
            writer.WriteLine("        public class " + block.Name + "Block\n        {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldMember(writer, field);

                if (field.Type == FieldType.Variable)
                {
                    variableFields = true;
                }
                if (field.Type == FieldType.F32 || field.Type == FieldType.F64)
                {
                    floatFields = true;
                }
            }

            // Length property
            writer.WriteLine("");
            writer.WriteLine("            /// <summary>Length of this block serialized in bytes</summary>");
            writer.WriteLine("            public int Length\n            {\n                get\n" +
                             "                {");
            int length = 0;

            foreach (MapField field in block.Fields)
            {
                length += GetFieldLength(writer, field);
            }

            if (!variableFields)
            {
                writer.WriteLine("                    return " + length + ";");
            }
            else
            {
                writer.WriteLine("                    int length = " + length + ";");

                foreach (MapField field in block.Fields)
                {
                    if (field.Type == FieldType.Variable)
                    {
                        writer.WriteLine("                    if (" + field.Name +
                                         " != null) { length += " + field.Count + " + " + field.Name + ".Length; }");
                    }
                }

                writer.WriteLine("                    return length;");
            }

            writer.WriteLine("                }\n            }\n");

            // Default constructor
            writer.WriteLine("            /// <summary>Default constructor</summary>");
            writer.WriteLine("            public " + block.Name + "Block() { }");

            // Constructor for building the class from bytes
            writer.WriteLine("            /// <summary>Constructor for building the block from a byte array</summary>");
            writer.WriteLine("            public " + block.Name + "Block(byte[] bytes, ref int i)" +
                             "\n            {");

            // Declare a length variable if we need it for variable fields in this constructor
            if (variableFields)
            {
                writer.WriteLine("                int length;");
            }

            // Start of the try catch block
            writer.WriteLine("                try\n                {");

            foreach (MapField field in block.Fields)
            {
                WriteFieldFromBytes(writer, field);
            }

            writer.WriteLine("                }\n                catch (Exception)\n" +
                             "                {\n                    throw new MalformedDataException();\n" +
                             "                }\n            }\n");

            // ToBytes() function
            writer.WriteLine("            /// <summary>Serialize this block to a byte array</summary>");
            writer.WriteLine("            public void ToBytes(byte[] bytes, ref int i)\n            {");

            // Declare a byte[] variable if we need it for floating point field conversions
            if (floatFields)
            {
                writer.WriteLine("                byte[] ba;");
            }

            foreach (MapField field in block.Fields)
            {
                WriteFieldToBytes(writer, field);
            }

            writer.WriteLine("            }\n");

            // ToString() function
            writer.WriteLine("            /// <summary>Serialize this block to a string</summary><returns>A string containing the serialized block</returns>");
            writer.WriteLine("            public override string ToString()\n            {");
            writer.WriteLine("                string output = \"-- " + block.Name + " --\\n\";");

            foreach (MapField field in block.Fields)
            {
                if (field.Type == FieldType.Variable || field.Type == FieldType.Fixed)
                {
                    writer.WriteLine("                output += Helpers.FieldToString(" + field.Name + ", \"" + field.Name + "\") + \"\\n\";");
                }
                else
                {
                    writer.WriteLine("                output += \"" + field.Name + ": \" + " + field.Name + ".ToString() + \"\\n\";");
                }
            }

            writer.WriteLine("                output = output.Trim();\n                return output;\n            }");

            writer.WriteLine("        }\n");
        }