Example #1
0
        public bool TypeMatches(ToffeeNetwork network, object o)
        {
            Type            t          = o.GetType();
            ToffeeValueType toffeeType = GetToffeeValueTypeFromType(network, t);

            if (toffeeType == BaseType)
            {
                if ((toffeeType != ToffeeValueType.Array) && (toffeeType != ToffeeValueType.Struct))
                {
                    return(true);
                }
                else
                {
                    if (t.IsArray)
                    {
                        return(SubType.TypeMatches(network, t.GetElementType()));
                    }
                    else if ((t.IsValueType) && (network.HasObject <ToffeeStruct>(t.FullName)))
                    {
                        return(StructId == network.GetObject <ToffeeStruct>(t.FullName).ObjectId);
                    }
                }
            }

            return(false);
        }
Example #2
0
        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));
            }
        }
Example #3
0
 public ToffeeType(ToffeeValueType baseType)
 {
     BaseType = baseType;
     if (BaseType == ToffeeValueType.Array)
     {
         SubType = new ToffeeType(ToffeeValueType.UInt8);
     }
     else
     {
         SubType = new ToffeeType(ToffeeValueType.None);
     }
 }
Example #4
0
        /// <summary>
        /// Write's a set of ToffeeValueType values to signify a Type.
        /// </summary>
        /// <param name="t">The type to write</param>
        public void Write(Type t)
        {
            ToffeeValueType toffeeType = ToffeeType.GetToffeeValueTypeFromType(Network, t);

            Write((byte)toffeeType);
            if (toffeeType == ToffeeValueType.Array)
            {
                Write(t.GetElementType());
            }
            if (toffeeType == ToffeeValueType.Struct)
            {
                Write(Network.GetObject <ToffeeStruct>(t.FullName).ObjectId);
            }
        }
Example #5
0
        public Array ReadArray()
        {
            // Get the properties of the array
            uint            length       = ReadUInt32();
            ToffeeValueType elementVType = ReadValueType();
            uint            structId     = 0;

            if (elementVType == ToffeeValueType.Struct)
            {
                structId = ReadUInt32();
            }
            Type elementType = ToffeeType.GetTypeFromToffeeValueType(Network, elementVType, structId: structId);

            // Create the array and fill it
            Array array = Array.CreateInstance(elementType, length);

            for (int i = 0; i < length; i++)
            {
                array.SetValue(Read(elementType), i);
            }
            return(array);
        }
Example #6
0
 public TdlType(ToffeeValueType type, TdlStruct structType, bool array = false)
     : this(type, array)
 {
     StructType = structType;
 }
Example #7
0
 public TdlType(ToffeeValueType type, bool array = false)
 {
     Type       = type;
     Array      = array;
     StructType = null;
 }
Example #8
0
 public ToffeeType(ToffeeValueType subType, bool array)
 {
     BaseType = ToffeeValueType.Array;
     SubType  = new ToffeeType(subType);
 }