public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         floatHandler = floatHandler ?? context.TypeHandlers.Get <float>();
         Vector2 vector = new Vector2();
         vector.x = (float)floatHandler.Read(reader.ReadFormat(), reader);
         vector.y = (float)floatHandler.Read(reader.ReadFormat(), reader);
         return(vector);
     }
     throw new FormatException(this, format, reader);
 }
Example #2
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         intHandler = intHandler ?? context.TypeHandlers.Get <int>();
         Vector3Int vector = new Vector3Int();
         vector.x = (int)intHandler.Read(reader.ReadFormat(), reader);
         vector.y = (int)intHandler.Read(reader.ReadFormat(), reader);
         vector.z = (int)intHandler.Read(reader.ReadFormat(), reader);
         return(vector);
     }
     throw new FormatException(this, format, reader);
 }
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         floatHandler = floatHandler ?? context.TypeHandlers.Get <float>();
         Quaternion quaternion = new Quaternion();
         quaternion.x = (float)floatHandler.Read(reader.ReadFormat(), reader);
         quaternion.y = (float)floatHandler.Read(reader.ReadFormat(), reader);
         quaternion.z = (float)floatHandler.Read(reader.ReadFormat(), reader);
         quaternion.w = (float)floatHandler.Read(reader.ReadFormat(), reader);
         return(quaternion);
     }
     throw new FormatException(this, format, reader);
 }
Example #4
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         floatHandler = floatHandler ?? context.TypeHandlers.Get <float>();
         int     length = reader.ReadArrayLength(format);
         float[] bytes  = new float[length];
         for (int i = 0; i < length; i++)
         {
             bytes[i] = (float)floatHandler.Read(reader.ReadFormat(), reader);
         }
         return(new Color(bytes[0], bytes[1], bytes[2], bytes[3]));
     }
     if (format.IsStringFamily)
     {
         stringHandler = stringHandler ?? context.TypeHandlers.Get <string>();
         Color color;
         ColorUtility.TryParseHtmlString((string)stringHandler.Read(format, reader), out color);
         return(color);
     }
     if (format.IsMapFamily)
     {
         mapHandler = mapHandler ?? context.TypeHandlers.Get <Dictionary <string, float> >();
         Dictionary <string, float> map = (Dictionary <string, float>)mapHandler.Read(format, reader);
         return(new Color(map["r"], map["g"], map["b"], map["a"]));
     }
     throw new FormatException(this, format, reader);
 }
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsNil)
     {
         return(null);
     }
     return(underlyingTypeHandler.Read(format, reader));
 }
Example #6
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsBin8)
     {
         binaryHandler = binaryHandler ?? context.TypeHandlers.Get <byte[]>();
         return(new Guid((byte[])binaryHandler.Read(format, reader)));
     }
     if (format.IsStr8)
     {
         stringHandler = stringHandler ?? context.TypeHandlers.Get <string>();
         return(new Guid((string)stringHandler.Read(format, reader)));
     }
     throw new FormatException(this, format, reader);
 }
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsIntFamily)
     {
         intHandler = intHandler ?? context.TypeHandlers.Get <int>();
         return(Enum.ToObject(type, intHandler.Read(format, reader)));
     }
     if (format.IsStringFamily)
     {
         stringHandler = stringHandler ?? context.TypeHandlers.Get <string>();
         return(Enum.Parse(type, (string)stringHandler.Read(format, reader), true));
     }
     if (format.IsNil)
     {
         return(Enum.ToObject(type, 0));
     }
     throw new FormatException(this, format, reader);
 }
Example #8
0
        public object Read(Format format, FormatReader reader)
        {
            IDictionary dictionary = (IDictionary)Activator.CreateInstance(type);

            if (format.IsNil)
            {
                return(dictionary);
            }
            int size = reader.ReadMapLength(format);

            while (size > 0)
            {
                object key   = keyHandler.Read(reader.ReadFormat(), reader);
                object value = valueHandler.Read(reader.ReadFormat(), reader);
                dictionary.Add(key, value);
                size = size - 1;
            }
            return(dictionary);
        }
        public object Read(Format format, FormatReader reader)
        {
            MapDefinition definition = lazyDefinition.Value;

            if (format.IsMapFamily)
            {
                object obj = Activator.CreateInstance(definition.Type);
                InvokeCallback <OnDeserializingAttribute>(obj, definition);
                int size = reader.ReadMapLength(format);
                while (size > 0)
                {
                    string name = (string)nameHandler.Read(reader.ReadFormat(), reader);
                    name = nameConverter.OnUnpack(name, definition);

                    if (definition.FieldHandlers.ContainsKey(name))
                    {
                        object value = definition.FieldHandlers[name].Read(reader.ReadFormat(), reader);
                        definition.FieldInfos[name].SetValue(obj, value);
                    }
                    else if (context.MapOptions.IgnoreUnknownFieldOnUnpack)
                    {
                        reader.Skip();
                    }
                    else
                    {
                        throw new MissingFieldException(name + " does not exist for type: " + definition.Type);
                    }
                    size = size - 1;
                }
                InvokeCallback <OnDeserializedAttribute>(obj, definition);
                return(obj);
            }
            if (format.IsEmptyArray && context.MapOptions.AllowEmptyArrayOnUnpack)
            {
                return(Activator.CreateInstance(definition.Type));
            }
            if (format.IsNil)
            {
                return(null);
            }
            throw new FormatException(this, format, reader);
        }
Example #10
0
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsArrayFamily)
     {
         int   size  = reader.ReadArrayLength(format);
         Array array = Array.CreateInstance(elementType, size);
         for (int i = 0; i < size; i++)
         {
             object value = elementTypeHandler.Read(reader.ReadFormat(), reader);
             array.SetValue(value, i);
         }
         return(array);
     }
     if (format.IsNil)
     {
         if (context.ArrayOptions.NullAsEmptyOnUnpack)
         {
             return(Array.CreateInstance(elementType, 0));
         }
         return(null);
     }
     throw new FormatException(this, format, reader);
 }
Example #11
0
        public object Read(Format format, FormatReader reader)
        {
            Type listType = typeof(List <>).MakeGenericType(new[] { innerType });

            if (format.IsArrayFamily)
            {
                IList list = (IList)Activator.CreateInstance(listType);
                int   size = reader.ReadArrayLength(format);
                for (int i = 0; i < size; i++)
                {
                    list.Add(innerTypeHandler.Read(reader.ReadFormat(), reader));
                }
                return(list);
            }
            if (format.IsNil)
            {
                if (context.ArrayOptions.NullAsEmptyOnUnpack)
                {
                    return(Activator.CreateInstance(listType));
                }
                return(null);
            }
            throw new FormatException(this, format, reader);
        }
 public object Read(Format format, FormatReader reader)
 {
     if (format.IsExtFamily)
     {
         uint length = reader.ReadExtLength(format);
         if (ExtType == reader.ReadExtType(reader.ReadFormat()))
         {
             return(ReadExt(length, reader));
         }
     }
     if (format.IsStringFamily)
     {
         stringHandler = stringHandler ?? context.TypeHandlers.Get <string>();
         string dateTimeStr = (string)stringHandler.Read(format, reader);
         return(DateTime.Parse(dateTimeStr));
     }
     if (format.IsFloatFamily || format.IsIntFamily)
     {
         doubleHandler = doubleHandler ?? context.TypeHandlers.Get <double>();
         double seconds = (double)doubleHandler.Read(format, reader);
         return(epoch.AddSeconds(seconds).ToLocalTime());
     }
     throw new FormatException(this, format, reader);
 }
 public object Read(Format format, FormatReader reader)
 {
     longHandler = longHandler ?? context.TypeHandlers.Get <long>();
     return(new TimeSpan((long)longHandler.Read(format, reader)));
 }
Example #14
0
 public object Read(Format format, FormatReader reader)
 {
     intArrayHandler = intArrayHandler ?? context.TypeHandlers.Get <int[]>();
     int[] bits = (int[])intArrayHandler.Read(format, reader);
     return(new decimal(bits));
 }