public T ReadStruct <T>() where T : new() { object o = new T(); ToffeeStruct definition = Network.GetObject <ToffeeStruct>(o.GetType().FullName); if (definition == null) { throw new Exception(string.Format("Cannot read Type '{0}' from ToffeePacket", o.GetType().Name)); } if (Position + definition.MinimumSize > Data.Length) { throw new IndexOutOfRangeException(); } if (!definition.ImplementsInterface) { foreach (ToffeeProperty property in definition.Properties) { property.Set(o, Read(property.PropertyInfo.PropertyType)); } } else { ((IToffeeStructure)o).ReadFrom(this); } return((T)o); }
public static Type GetTypeFromToffeeValueType(ToffeeNetwork network, ToffeeValueType type, ToffeeValueType subType = ToffeeValueType.UInt8, uint structId = 0, uint length = 0) { switch (type) { case ToffeeValueType.Bool: return(typeof(bool)); case ToffeeValueType.Char: return(typeof(char)); case ToffeeValueType.Int8: return(typeof(sbyte)); case ToffeeValueType.Int16: return(typeof(short)); case ToffeeValueType.Int32: return(typeof(int)); case ToffeeValueType.Int64: return(typeof(long)); case ToffeeValueType.UInt8: return(typeof(byte)); case ToffeeValueType.UInt16: return(typeof(ushort)); case ToffeeValueType.UInt32: return(typeof(uint)); case ToffeeValueType.UInt64: return(typeof(ulong)); case ToffeeValueType.String: return(typeof(string)); case ToffeeValueType.Float32: return(typeof(float)); case ToffeeValueType.Float64: return(typeof(double)); case ToffeeValueType.Array: return(Array.CreateInstance(GetTypeFromToffeeValueType(network, subType, structId: structId), length).GetType()); case ToffeeValueType.Struct: ToffeeStruct definition = network.GetObject <ToffeeStruct>(structId); if (definition == null) { throw new Exception(string.Format("Cannot get a Type instance from invalid structId: {0}", structId)); } return(definition.Type); default: throw new Exception(string.Format("Cannot get a Type instance from {0}", type)); } }
/// <summary> /// Write's a ToffeeStructure that is part of the sender's network. /// </summary> /// <typeparam name="T">The type of structure to write</typeparam> /// <param name="o">An instance of the type of structure</param> public void WriteStruct <T>(T o) { Type oT = typeof(T); if (!Network.HasObject <ToffeeStruct>(oT.FullName)) { throw new Exception(string.Format("Cannot add Type '{0}' to ToffeePacket", oT)); } ToffeeStruct definition = Network.GetObject <ToffeeStruct>(oT.FullName); if (!definition.ImplementsInterface) { foreach (var property in definition.Properties) { Write(property.Get(o)); } } else { ((IToffeeStructure)o).WriteTo(this); } }