public DataArrayEventArgsMove(IDataArray from, Blam.DatumIndex from_index, IDataArray to, Blam.DatumIndex to_index)
 {
     fromArray = from;
     fromIndex = from_index;
     toArray   = to;
     toIndex   = to_index;
 }
Example #2
0
        private void SerializeContainer(IEnumerable e, Type elementType, IDataAdapter data)
        {
            if (settings.PrimitiveAsBase64 && elementType.IsPrimitive)
            {
                int size  = ByteUtils.GetSizeOf(elementType);
                int count = 0;
                // ReSharper disable once UnusedVariable
                foreach (object o in e)
                {
                    count++;
                }

                Action <byte[], int, object> writer = ByteUtils.GetBinaryWriter(elementType);

                byte[] buffer = new byte[size * count];
                int    index  = 0;
                foreach (object o in e)
                {
                    writer(buffer, index, o);
                    index += size;
                }
                data.SetStringValue(Convert.ToBase64String(buffer));
            }
            else
            {
                IDataArray array = data.AddArray();
                foreach (object o in e)
                {
                    SerializeSubValue(elementType, o, array.AddArrayValue(), null);
                }
            }

            haveContainers = true;
        }
Example #3
0
        private void DeserializeContainer(ref object container, Type type, Type elementType, IDataAdapter data, string addMethodName, bool reverse = false)
        {
            if (container == null)
            {
                container = TypeCache.CreateParameterless(type);
            }

            Action <object, object> addAction = null;

            if (!TypeCache.TryGetTypeAccessor(type, ref addAction))
            {
                addAction = InvocationHelper.CreateSetDelegate(type, elementType, addMethodName);
                TypeCache.AddTypeAccessor(type, addAction);
            }

            if (elementType.IsPrimitive)
            {
                byte[] buffer = Convert.FromBase64String(data.GetStringValue());
                int    size   = ByteUtils.GetSizeOf(elementType);
                int    count  = buffer.Length / size;
                Func <byte[], int, object> reader = ByteUtils.GetBinaryReader(elementType);

                if (reverse)
                {
                    for (int i = count - 1; i >= 0; i--)
                    {
                        addAction(container, reader(buffer, size * i));
                    }
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        addAction(container, reader(buffer, size * i));
                    }
                }

                return;
            }

            IDataArray array = data.GetArray();

            if (reverse)
            {
                foreach (IDataAdapter element in array.GetChildrenReversed())
                {
                    addAction(container, DeserializeObject(elementType, element, null));
                }
            }
            else
            {
                foreach (IDataAdapter element in array.GetChildren())
                {
                    addAction(container, DeserializeObject(elementType, element, null));
                }
            }
        }
Example #4
0
 /// <summary>
 /// Converts an <see cref="IDataArray"/> instance to a data array with a concrete type.
 /// </summary>
 /// <typeparam name="TDataArray">The concrete data array type.</typeparam>
 /// <param name="value">The <see cref="IDataArray"/> to convert</param>
 /// <returns>The converted data array.</returns>
 public static TDataArray ToDataArray <TDataArray>(this IDataArray value)
     where TDataArray : class, IDataArray, new()
 {
     return(value == null
         ? null
         : value as TDataArray ?? new TDataArray {
         Dimensions = value.Dimensions, Data = value.Data
     });
 }
Example #5
0
        /// <summary>
        /// Serialize the object.
        /// </summary>
        /// <param name="data">Data carrier to serialize to</param>
        /// <param name="target">Object or root of object model to be serialized</param>
        /// <exception cref="ArgumentNullException">thrown if <paramref name="data"/> is null or <paramref name="target"/> is null</exception>
        public void SerializeObject <TTarget>(IDataAdapter data, TTarget target)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            haveContainers        = false;
            haveReferences        = false;
            havePrivateProperties = false;

            SerializeSubValue(typeof(TTarget), target, data, null);

            if (!settings.SaveOptimizationFlags)
            {
                return;
            }

            OptimizationFlags flags = 0;

            if (!haveContainers)
            {
                flags |= OptimizationFlags.NoContainers;
            }
            if (!haveReferences)
            {
                flags |= OptimizationFlags.NoReferences;
            }
            if (havePrivateProperties)
            {
                flags |= OptimizationFlags.PrivateProperties;
            }
            if (settings.EnumsAsValue)
            {
                flags |= OptimizationFlags.EnumAsValue;
            }

            if (flags != 0)
            {
                data.AddIntValue((long)flags, ATTRIBUTE_FLAGS, true);
            }

            if (typesCache.Count > 0)
            {
                IDataArray array = data.AddChild(ELEMENT_TYPES).AddArray();
                foreach (KeyValuePair <Type, int> pair in typesCache)
                {
                    array.AddArrayValue().SetStringValue(TypeCache.GetTypeFullName(pair.Key, settings.AssemblyQualifiedNames));
                }
            }
        }
Example #6
0
 public void ResetLava (IDataArray blocks, IDataArray data)
 {
     for (int i = 0; i < blocks.Length; i++) {
         if ((blocks[i] == BlockInfo.StationaryLava.ID || blocks[i] == BlockInfo.Lava.ID) && data[i] != 0) {
             blocks[i] = (byte)BlockInfo.Air.ID;
             data[i] = 0;
         }
         else if (blocks[i] == BlockInfo.Lava.ID) {
             blocks[i] = (byte)BlockInfo.StationaryLava.ID;
         }
     }
 }
Example #7
0
 public void ResetLava(IDataArray blocks, IDataArray data)
 {
     for (int i = 0; i < blocks.Length; i++)
     {
         if ((blocks[i] == BlockInfo.StationaryLava.ID || blocks[i] == BlockInfo.Lava.ID) && data[i] != 0)
         {
             blocks[i] = (byte)BlockInfo.Air.ID;
             data[i]   = 0;
         }
         else if (blocks[i] == BlockInfo.Lava.ID)
         {
             blocks[i] = (byte)BlockInfo.StationaryLava.ID;
         }
     }
 }
Example #8
0
        /// <summary>
        /// Deserialize object from source data.
        /// </summary>
        /// <typeparam name="T">Type of the class to deserialize root. Use <see cref="object"/> if class is unknown.</typeparam>
        /// <param name="data">Data source to deserialize from</param>
        /// <returns>The object or root of object model deserialized from <paramref name="data"/>.</returns>
        /// <exception cref="SerializationException">thrown when deserialization fails</exception>
        public T DeserializeObject <T>(IDataAdapter data)
        {
            maxObjId = 1;
            objectCache.Clear();
            flags = (OptimizationFlags)data.GetIntValue(ATTRIBUTE_FLAGS, true);

            IDataAdapter types = data.GetChild(ELEMENT_TYPES);

            if (types != null)
            {
                IDataArray array = types.GetArray();
                int        index = 1;
                foreach (IDataAdapter child in array.GetChildren())
                {
                    typeCache.Add(index++, InternalResolveTypes(child.GetStringValue()));
                }
            }

            return((T)DeserializeObject(typeof(T), data, null));
        }
Example #9
0
        private void SerializeArrayRank(Array array, Type elementType, int[] coords, int r, IDataAdapter data)
        {
            if (r == coords.Length - 1)
            {
                if (settings.PrimitiveAsBase64 && elementType.IsPrimitive)
                {
                    int size  = ByteUtils.GetSizeOf(elementType);
                    int count = array.GetLength(r);
                    Action <byte[], int, object> writer = ByteUtils.GetBinaryWriter(elementType);

                    byte[] buffer = new byte[size * count];
                    int    index  = 0;
                    for (int i = 0; i < count; i++)
                    {
                        coords[r] = i;
                        writer(buffer, index, array.GetValue(coords));
                        index += size;
                    }
                    data.SetStringValue(Convert.ToBase64String(buffer));
                }
                else
                {
                    IDataArray a = data.AddArray();
                    for (int i = 0; i < array.GetLength(r); i++)
                    {
                        coords[r] = i;
                        SerializeSubValue(elementType, array.GetValue(coords), a.AddArrayValue(), null);
                    }
                }
            }
            else
            {
                IDataArray a = data.AddArray();
                for (int i = 0; i < array.GetLength(r); i++)
                {
                    coords[r] = i;
                    SerializeArrayRank(array, elementType, coords, r + 1, a.AddArrayValue());
                }
            }
        }
Example #10
0
 public void ResetWater(IDataArray blocks, IDataArray data)
 {
     for (int i = 0; i < blocks.Length; i++) {
         if ((blocks[i] == McBlocks.StationaryWater.ID || blocks[i] == McBlocks.Water.ID) && data[i] != 0) {
             blocks[i] = (byte)McBlocks.Air.ID;
             data[i] = 0;
         }
         else if (blocks[i] == McBlocks.Water.ID) {
             blocks[i] = (byte)McBlocks.StationaryWater.ID;
         }
     }
 }
Example #11
0
 public static T[] GetData <T>(this IDataArray array) where T : class
 {
     return(array.GetData <T>(typeof(T)));
 }
Example #12
0
        private void SerializeContainer(object value, IDataAdapter data, TypeCategory category, Type[] genericArgs)
        {
            Type type = value.GetType();

            // arrays
            if (category == TypeCategory.Array)
            {
                Array array       = (Array)value;
                Type  elementType = type.GetElementType();
                int[] coords      = new int[array.Rank];
                if (array.Rank > 1)
                {
                    data.AddIntValue(array.Rank, ATTRIBUTE_ARRAY_RANK, true);
                }
                SerializeArrayRank(array, elementType, coords, 0, data);

                haveContainers = true;
                return;
            }

            // dictionaries
            if (category == TypeCategory.IDictionary)
            {
                Type keyType   = genericArgs[0];
                Type valueType = genericArgs[1];

                Func <object, object> getKeyFunc   = null;
                Func <object, object> getValueFunc = null;

                IDataArray array = data.AddArray();

                foreach (object o in (IEnumerable)value)
                {
                    if (getKeyFunc == null)
                    {
                        Type objectType = o.GetType();
                        if (!TypeCache.TryGetNamedAccessor(objectType, "Key", ref getKeyFunc))
                        {
                            getKeyFunc = InvocationHelper.CreateGetDelegate(o.GetType(), keyType, objectType.GetProperty("Key").GetMethod);
                            TypeCache.AddTypeNamedAccessor(objectType, "Key", getKeyFunc);
                        }
                        if (!TypeCache.TryGetNamedAccessor(objectType, "Value", ref getValueFunc))
                        {
                            getValueFunc = InvocationHelper.CreateGetDelegate(o.GetType(), valueType, objectType.GetProperty("Value").GetMethod);
                            TypeCache.AddTypeNamedAccessor(objectType, "Value", getValueFunc);
                        }
                    }


                    IDataAdapter itemEl = array.AddArrayValue();
                    SerializeSubValue(keyType, getKeyFunc(o), itemEl.AddChild(settings.DictionaryKeyName), null);
                    SerializeSubValue(valueType, getValueFunc(o), itemEl.AddChild(settings.DictionaryValueName), null);
                }
                haveContainers = true;

                return;
            }

            // generics
            if (category == TypeCategory.GenericIList ||
                category == TypeCategory.ISet ||
                category == TypeCategory.GenericQueue ||
                category == TypeCategory.GenericStack ||
                category == TypeCategory.LinkedList)
            {
                SerializeContainer((IEnumerable)value, genericArgs[0], data);
                return;
            }

            // non-generic versions
            if (category == TypeCategory.IList ||
                category == TypeCategory.Queue ||
                category == TypeCategory.Stack)
            {
                SerializeContainer((IEnumerable)value, typeof(object), data);
                return;
            }
        }