Exemplo n.º 1
0
        public override void ReadInto(ES3Reader reader, object obj)
        {
            var array = (Array)obj;

            if (reader.StartReadCollection())
            {
                throw new NullReferenceException("The Collection we are trying to load is stored as null, which is not allowed when using ReadInto methods.");
            }

            bool iHasBeenRead = false;

            for (int i = 0; i < array.GetLength(0); i++)
            {
                bool jHasBeenRead = false;

                if (!reader.StartReadCollectionItem())
                {
                    throw new IndexOutOfRangeException("The collection we are loading is smaller than the collection provided as a parameter.");
                }

                reader.StartReadCollection();
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    if (!reader.StartReadCollectionItem())
                    {
                        throw new IndexOutOfRangeException("The collection we are loading is smaller than the collection provided as a parameter.");
                    }
                    reader.ReadInto <object>(array.GetValue(i, j), elementType);
                    jHasBeenRead = reader.EndReadCollectionItem();
                }

                if (!jHasBeenRead)
                {
                    throw new IndexOutOfRangeException("The collection we are loading is larger than the collection provided as a parameter.");
                }

                reader.EndReadCollection();

                iHasBeenRead = reader.EndReadCollectionItem();
            }

            if (!iHasBeenRead)
            {
                throw new IndexOutOfRangeException("The collection we are loading is larger than the collection provided as a parameter.");
            }

            reader.EndReadCollection();
        }
Exemplo n.º 2
0
        public override object Read(ES3Reader reader)
        {
            /*var method = typeof(ES3CollectionType).GetMethod("ReadICollection", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(elementType.type);
             * if(!(bool)method.Invoke(this, new object[] { reader, list, elementType }))
             *  return null;*/

            var genericParam = ES3Reflection.GetGenericArguments(type)[0];
            var listType     = ES3Reflection.MakeGenericType(typeof(List <>), genericParam);
            var list         = (IList)ES3Reflection.CreateInstance(listType);

            if (!reader.StartReadCollection())
            {
                // Iterate through each character until we reach the end of the array.
                while (true)
                {
                    if (!reader.StartReadCollectionItem())
                    {
                        break;
                    }
                    list.Add(reader.Read <object>(elementType));

                    if (reader.EndReadCollectionItem())
                    {
                        break;
                    }
                }

                reader.EndReadCollection();
            }

            return(ES3Reflection.CreateInstance(type, list));
        }
Exemplo n.º 3
0
        public override object Read(ES3Reader reader)
        {
            var instance = (IList)ES3Reflection.CreateInstance(type);

            if (reader.StartReadCollection())
            {
                return(null);
            }

            // Iterate through each character until we reach the end of the array.
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }
                instance.Add(reader.Read <object>(elementType));

                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            reader.EndReadCollection();

            return(instance);
        }
Exemplo n.º 4
0
        public override object Read <T>(ES3Reader reader)
        {
            if (reader.StartReadCollection())
            {
                return(null);
            }

            var stack = new Stack <T>();

            // Iterate through each character until we reach the end of the array.
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }
                stack.Push(reader.Read <T>(elementType));
                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            reader.EndReadCollection();
            return(stack);
        }
Exemplo n.º 5
0
        protected virtual bool ReadICollection <T>(ES3Reader reader, ICollection <T> collection, ES3Type elementType)
        {
            if (reader.StartReadCollection())
            {
                return(false);
            }

            // Iterate through each character until we reach the end of the array.
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }
                collection.Add(reader.Read <T>(elementType));

                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            reader.EndReadCollection();

            return(true);
        }
Exemplo n.º 6
0
        public override object Read(ES3Reader reader)
        {
            var instance = (IList)ES3Reflection.CreateInstance(ES3Reflection.MakeGenericType(typeof(List <>), elementType.type));

            if (reader.StartReadCollection())
            {
                return(null);
            }

            // Iterate through each character until we reach the end of the array.
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }
                instance.Add(reader.Read <object>(elementType));

                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            reader.EndReadCollection();

            ES3Reflection.GetMethods(instance.GetType(), "Reverse").FirstOrDefault(t => !t.IsStatic).Invoke(instance, new object[] {});
            return(ES3Reflection.CreateInstance(type, instance));
        }
Exemplo n.º 7
0
        public override void ReadInto(ES3Reader reader, object obj)
        {
            var collection = (IList)obj;

            if (collection.Count == 0)
            {
                ES3Debug.LogWarning("LoadInto/ReadInto expects a collection containing instances to load data in to, but the collection is empty.");
            }

            if (reader.StartReadCollection())
            {
                throw new NullReferenceException("The Collection we are trying to load is stored as null, which is not allowed when using ReadInto methods.");
            }

            int itemsLoaded = 0;

            // Iterate through each item in the collection and try to load it.
            foreach (var item in collection)
            {
                itemsLoaded++;

                if (!reader.StartReadCollectionItem())
                {
                    break;
                }

                reader.ReadInto <object>(item, elementType);

                // If we find a ']', we reached the end of the array.
                if (reader.EndReadCollectionItem())
                {
                    break;
                }

                // If there's still items to load, but we've reached the end of the collection we're loading into, throw an error.
                if (itemsLoaded == collection.Count)
                {
                    throw new IndexOutOfRangeException("The collection we are loading is longer than the collection provided as a parameter.");
                }
            }

            // If we loaded fewer items than the parameter collection, throw index out of range exception.
            if (itemsLoaded != collection.Count)
            {
                throw new IndexOutOfRangeException("The collection we are loading is shorter than the collection provided as a parameter.");
            }

            reader.EndReadCollection();
        }
Exemplo n.º 8
0
        public override void ReadInto <T>(ES3Reader reader, object obj)
        {
            if (reader.StartReadCollection())
            {
                throw new NullReferenceException("The Collection we are trying to load is stored as null, which is not allowed when using ReadInto methods.");
            }

            int itemsLoaded = 0;

            var stack = (Stack <T>)obj;

            // Iterate through each item in the collection and try to load it.
            foreach (var item in stack)
            {
                itemsLoaded++;

                if (!reader.StartReadCollectionItem())
                {
                    break;
                }

                reader.ReadInto <T>(item, elementType);

                // If we find a ']', we reached the end of the array.
                if (reader.EndReadCollectionItem())
                {
                    break;
                }
                // If there's still items to load, but we've reached the end of the collection we're loading into, throw an error.
                if (itemsLoaded == stack.Count)
                {
                    throw new IndexOutOfRangeException("The collection we are loading is longer than the collection provided as a parameter.");
                }
            }

            // If we loaded fewer items than the parameter collection, throw index out of range exception.
            if (itemsLoaded != stack.Count)
            {
                throw new IndexOutOfRangeException("The collection we are loading is shorter than the collection provided as a parameter.");
            }

            reader.EndReadCollection();
        }
Exemplo n.º 9
0
        public override object Read(ES3Reader reader)
        {
            if (reader.StartReadCollection())
            {
                return(null);
            }

            // Create a List to store the items as a 1D array, which we can work out the positions of by calculating the lengths of the two dimensions.
            var items   = new List <object>();
            int length1 = 0;

            // Iterate through each character until we reach the end of the array.
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }

                ReadICollection <object>(reader, items, elementType);
                length1++;

                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            int length2 = items.Count / length1;

            var array = ES3Reflection.ArrayCreateInstance(elementType.type, new int[] { length1, length2 });

            for (int i = 0; i < length1; i++)
            {
                for (int j = 0; j < length2; j++)
                {
                    array.SetValue(items[(i * length2) + j], i, j);
                }
            }

            return(array);
        }
Exemplo n.º 10
0
        public override object Read(ES3Reader reader)
        {
            var instance = new List <object>();

            if (reader.StartReadCollection())
            {
                return(null);
            }

            // Iterate through each character until we reach the end of the array.
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }
                instance.Add(reader.Read <object>(elementType));

                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            reader.EndReadCollection();

            var array = ES3Reflection.ArrayCreateInstance(elementType.type, instance.Count);
            int i     = 0;

            foreach (var item in instance)
            {
                array.SetValue(item, i);
                i++;
            }

            return(array);
        }
Exemplo n.º 11
0
        private void ReadComponents(ES3Reader reader, GameObject go)
        {
            if (reader.StartReadCollection())
            {
                return;
            }

            var components = new List <Component>(go.GetComponents <Component>());

            // Read each Component in Components array
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }

                if (reader.StartReadObject())
                {
                    return;
                }

                Type type = null;

                string propertyName;
                while (true)
                {
                    propertyName = ReadPropertyName(reader);

                    if (propertyName == ES3Type.typeFieldName)
                    {
                        type = reader.ReadType();
                    }
                    else if (propertyName == ES3ReferenceMgrBase.referencePropertyName)
                    {
                        if (type == null)
                        {
                            throw new InvalidOperationException("Cannot load Component because no type data has been stored with it, so it's not possible to determine it's type");
                        }

                        var componentRef = reader.Read_ref();

                        // Rather than loading by reference, load using the Components list.
                        var c = components.Find(x => x.GetType() == type);
                        // If the Component exists in the Component list, load into it and remove it from the list.
                        if (c != null)
                        {
                            if (ES3ReferenceMgrBase.Current != null)
                            {
                                ES3ReferenceMgrBase.Current.Add(c, componentRef);
                            }

                            ES3TypeMgr.GetOrCreateES3Type(type).ReadInto <Component>(reader, c);
                            components.Remove(c);
                        }
                        // Else, create a new Component.
                        else
                        {
                            var component = ES3TypeMgr.GetOrCreateES3Type(type).Read <Component>(reader);
                            if (component != null)
                            {
                                ES3ReferenceMgrBase.Current.Add((Component)component, componentRef);
                            }
                        }
                        break;
                    }
                    else if (propertyName == null)
                    {
                        break;
                    }
                    else
                    {
                        reader.overridePropertiesName = propertyName;
                        ReadObject <Component>(reader);
                        break;
                    }
                }

                reader.EndReadObject();

                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            reader.EndReadCollection();
        }
Exemplo n.º 12
0
        public override object Read <T>(ES3Reader reader)
        {
            if (reader.StartReadCollection())
            {
                return(null);
            }

            // Create a List to store the items as a 1D array, which we can work out the positions of by calculating the lengths of the two dimensions.
            var items   = new List <T>();
            int length1 = 0;
            int length2 = 0;

            // Iterate through each sub-array
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }
                reader.StartReadCollection();

                length1++;

                while (true)
                {
                    if (!reader.StartReadCollectionItem())
                    {
                        break;
                    }

                    ReadICollection <T>(reader, items, elementType);
                    length2++;

                    if (reader.EndReadCollectionItem())
                    {
                        break;
                    }
                }

                reader.EndReadCollection();
                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            reader.EndReadCollection();

            length2 = length2 / length1;
            int length3 = items.Count / length2 / length1;

            var array = new T[length1, length2, length3];

            for (int i = 0; i < length1; i++)
            {
                for (int j = 0; j < length2; j++)
                {
                    for (int k = 0; k < length3; k++)
                    {
                        array[i, j, k] = items[i * (length2 * length3) + (j * length3) + k];
                    }
                }
            }

            return(array);
        }