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;
}






