Packet Structures From XML

10/10/2011 10:23 BaussHacker#1
Not sure, if it works nor if I have done it right, but it looks correct to me.

However would it be a good thing to read packet structures from a xml file like this or is there better ways?

The reason I'm interested in it, it's because you do not need to open source to edit offsets nor build, if an offset is wrong or something.

The wrapper:
Code:
    public class PacketStructure
    {
        public int Size;
        private XmlTextReader Reader;
        public Dictionary<int, PacketData> Data;

        public static Func<string, int> ToInt32 = new Func<string, int>(int.Parse);
        public static Func<string, uint> ToUInt32 = new Func<string, uint>(uint.Parse);
        public static Func<string, short> ToInt16 = new Func<string, short>(short.Parse);
        public static Func<string, ushort> ToUInt16 = new Func<string, ushort>(ushort.Parse);
        public static Func<string, sbyte> ToInt8 = new Func<string, sbyte>(sbyte.Parse);
        public static Func<string, byte> ToUInt8 = new Func<string, byte>(byte.Parse);
        public static Func<string, bool> ToBool = new Func<string, bool>(bool.Parse);
        public static Func<string, double> ToDouble = new Func<string, double>(double.Parse);
        public static Func<string, long> ToInt64 = new Func<string, long>(long.Parse);
        public static Func<string, ulong> ToUInt64 = new Func<string, ulong>(ulong.Parse);
        public static Func<string, float> ToFloat = new Func<string, float>(float.Parse);

        public PacketStructure(string XmlLoc)
        {
            if (!XmlLoc.EndsWith(".xml"))
                XmlLoc += ".xml";

            if (!System.IO.File.Exists(XmlLoc))
                throw new System.IO.FileLoadException("There was a problem locating the xml file ('" + XmlLoc + "')");

            Reader = new XmlTextReader(XmlLoc);
            Data = new Dictionary<int, PacketData>();
        }

        public void Read()
        {
            while (Reader.Read())
            {
                string DataType = Reader.Name;

                if (DataType == "Size")
                {
                    Size = ToInt32(Reader.GetAttribute(0));
                }
                else
                {

                    PacketData Data = new PacketData();
                    Data.Offset = ToInt32(Reader.GetAttribute(0));
                    switch (DataType)
                    {
                        #region SByte
                        case "SByte":
                            Data.Default = ToInt8(Reader.Value);
                            break;
                        #endregion

                        #region Int16
                        case "Int16":
                            Data.Default = ToInt16(Reader.Value);
                            break;
                        #endregion

                        #region Int32
                        case "Int32":
                            Data.Default = ToInt32(Reader.Value);
                            break;
                        #endregion

                        #region Int64
                        case "Int64":
                            Data.Default = ToInt64(Reader.Value);
                            break;
                        #endregion

                        #region Byte
                        case "Byte":
                            Data.Default = ToUInt8(Reader.Value);
                            break;
                        #endregion

                        #region UInt16
                        case "UInt16":
                            Data.Default = ToUInt16(Reader.Value);
                            break;
                        #endregion

                        #region UInt32
                        case "UInt32":
                            Data.Default = ToUInt32(Reader.Value);
                            break;
                        #endregion

                        #region UInt64
                        case "UInt64":
                            Data.Default = ToUInt64(Reader.Value);
                            break;
                        #endregion

                        #region Double
                        case "Double":
                            Data.Default = ToDouble(Reader.Value);
                            break;
                        #endregion

                        #region Float
                        case "Float":
                            Data.Default = ToFloat(Reader.Value);
                            break;
                        #endregion

                        #region Bool
                        case "Bool":
                            Data.Default = ToBool(Reader.Value);
                            break;
                        #endregion
                    }
                    Data.Value = Data.Default;
                    this.Data.Add(Data.Offset, Data);
                }
            }
        }

        public static implicit operator PacketData[](PacketStructure Structure)
        {
            return Structure.Data.Values.ToArray();
        }
    }
    public struct PacketData
    {
        public object Value;
        public object Default;
        public int Offset;
    }
10/10/2011 10:25 Korvacs#2
Simply put, whats the point? 90% of the time if you have to change a packet you have to change logic aswell, and if an offset is wrong you will naturally open the source to breakpoint test the problem before fixing it, so the source is open anyway =x
10/10/2011 10:26 BaussHacker#3
Quote:
Originally Posted by Korvacs View Post
Simply put, whats the point? 90% of the time if you have to change a packet you have to change logic aswell.
That you do not need to build your server to edit offsets.

True t that tho, but under development it could be nice, because you don't have to rebuild the whole server, because you have one wrong offset.
10/10/2011 10:33 Korvacs#4
Sorry just edited the post :p
10/10/2011 10:34 BaussHacker#5
So it would be rather useless rofl?
10/10/2011 10:43 Korvacs#6
Personally i would never use such a thing for packets unless i knew they would never change ever, and even then there would be increased loading time at runtime due to having to grab these using io.
10/10/2011 10:44 BaussHacker#7
Quote:
Originally Posted by Korvacs View Post
Personally i would never use such a thing for packets unless i knew they would never change ever, and even then there would be increased loading time at runtime due to having to grab these using io.
Well you grab it at load time not while using the packetstructures. It's one time load. But I get what you mean, thanks for clearing it up. :)