Exemple #1
0
        public static SpriteSave FromSprite <T>(T spriteToCreateSaveFrom) where T : PositionedObject, IColorable,
        ICursorSelectable, IReadOnlyScalable, IAnimationChainAnimatable
        {
            SpriteSave spriteSave = new SpriteSave();

            spriteSave.SetFrom <T>(spriteToCreateSaveFrom);

            return(spriteSave);
        }
        private static T ReadCollection <T>(ContentReader input, bool isExternal, Type readType)
        {
            object objectToReturn = null;
            Type   type           = typeof(T);
            int    count          = input.ReadInt32();

            bool[] isNullArray = ReadBoolArray(input);

            if (count != isNullArray.Length)
            {
                // Used to be FileLoadException, but that's not supported
                // on the Xbox 360
                throw new Exception("The collection of type " +
                                    type.ToString() + " has " + count + " entries, but the ObjectReader " +
                                    "read " + isNullArray.Length + " bools for null entries.");
            }

            //If type is an Array or List
            if (GetInterface(type, "IList") != null)
            {
                MethodInfo listRecur = default(MethodInfo);

                //If this is an array
                if (type.IsArray)
                {
                    #region Read Array
                    Type elementType = type.GetElementType();

                    //Build the instance to return and the recursive call.
                    objectToReturn = Array.CreateInstance(type.GetElementType(), count);

                    //If this field is marked with the ExternalInstance attribute.
                    if (isExternal)
                    {
                        //If the elements of this array are also Collections
                        if (GetInterface(elementType, "IEnumerable") != null)
                        {
                            listRecur = typeof(ObjectReader).GetMethod("ReadCollection", BindingFlags.NonPublic | BindingFlags.Static,
                                                                       null, new Type[] { input.GetType(), typeof(bool), typeof(Type) },
                                                                       null);

                            //Check if null, which will be the case if running on the 360
                            if (listRecur == null)
                            {
                                listRecur = GetPrivateObjectReaderMethod("ReadCollection", new Type[] { input.GetType(), typeof(bool), typeof(Type) });
                            }

                            listRecur = listRecur.MakeGenericMethod(new Type[1] {
                                elementType
                            });
                            for (int i = 0; i < count; ++i)
                            {
                                if (!isNullArray[i])
                                {
                                    (objectToReturn as System.Collections.IList)[i] = listRecur.Invoke(null, new object[3] {
                                        input, true, elementType
                                    });
                                }
                            }
                        }

                        else
                        {
                            listRecur = typeof(ContentReader).GetMethod("ReadExternalReference").MakeGenericMethod(new Type[1] {
                                elementType
                            });
                            for (int i = 0; i < count; ++i)
                            {
                                if (!isNullArray[i])
                                {
                                    (objectToReturn as System.Collections.IList)[i] = listRecur.Invoke(input, new object[0] {
                                    });
                                }
                            }
                        }
                    }

                    else
                    {
                        listRecur = typeof(ObjectReader).GetMethod("ReadObject").MakeGenericMethod(new Type[1] {
                            elementType
                        });

                        for (int i = 0; i < count; ++i)
                        {
                            //invoke the recursive call for each element.
                            if (!isNullArray[i])
                            {
                                (objectToReturn as System.Collections.IList)[i] = listRecur.Invoke(null, new object[1] {
                                    input
                                });
                            }
                        }
                    }
                    #endregion
                }

                //Similar process, but for Lists
                else
                {
                    #region Read List
                    Type tempType = type;
                    objectToReturn = System.Activator.CreateInstance <T>();

                    //Move up the inheritence tree until you get a Generic definition
                    while (!tempType.IsGenericType)
                    {
                        if (tempType.BaseType != null)
                        {
                            tempType = tempType.BaseType;
                        }
                    }

                    if (count != 0)
                    {
                        //Build the recursive call with the Generic type
                        Type[] genericParam = tempType.GetGenericArguments();

                        if (genericParam[0] == typeof(FlatRedBall.Content.Scene.SpriteSave))
                        {
                            for (int i = 0; i < count; ++i)
                            {
                                if (!isNullArray[i])
                                {
                                    FlatRedBall.Content.Scene.SpriteSave ss =
                                        ObjectReader.ReadObject <FlatRedBall.Content.Scene.SpriteSave>(input);


                                    (objectToReturn as System.Collections.IList).Add(ss);
                                }
                            }
                        }
                        else
                        {
                            Type objectReaderType = typeof(ObjectReader);

                            MethodInfo nonGenericReadObject = objectReaderType.GetMethod("ReadObject");

                            listRecur = nonGenericReadObject.MakeGenericMethod(genericParam);


                            for (int i = 0; i < count; ++i)
                            {
                                if (!isNullArray[i])
                                {
                                    (objectToReturn as System.Collections.IList).Add(listRecur.Invoke(null, new object[1] {
                                        input
                                    }));
                                }
                            }
                        }
                    }
                    #endregion
                }
            }

            //For Dictionaries
            //ToDo: Test
            else if (GetInterface(type, "IDictionary") != null)
            {
                #region Read Dictionary
                objectToReturn = System.Activator.CreateInstance <T>();
                Type[]     genericParam = type.GetGenericArguments();
                MethodInfo dictRecur1   = typeof(ObjectReader).GetMethod("ReadObject").MakeGenericMethod(new Type[1] {
                    genericParam[0]
                });
                MethodInfo dictRecur2 = typeof(ObjectReader).GetMethod("ReadObject").MakeGenericMethod(new Type[1] {
                    genericParam[1]
                });

                for (int i = 0; i < count; ++i)
                {
                    //Dictionary values are allowed to be null, but their keys are not.
                    if (!isNullArray[i])
                    {
                        (objectToReturn as System.Collections.IDictionary).Add(dictRecur1.Invoke(null, new object[1] {
                            input
                        }),
                                                                               dictRecur2.Invoke(null, new object[1] {
                            input
                        }));
                    }
                    else
                    {
                        (objectToReturn as System.Collections.IDictionary).Add(dictRecur1.Invoke(null, new object[1] {
                            input
                        }),
                                                                               null);
                    }
                }
                #endregion
            }

            //For Stacks
            else if (GetInterface(type, "Stack") != null)
            {
#if WINDOWS_PHONE || XBOX360
                throw new NotImplementedException();
#else
                #region Read Stack
                objectToReturn = System.Activator.CreateInstance <T>();
                Type[]     genericParam = type.GetGenericArguments();
                MethodInfo stackRecur   = typeof(ObjectReader).GetMethod("ReadObject").MakeGenericMethod(new Type[1] {
                    genericParam[0]
                });

                for (int i = 0; i < count; ++i)
                {
                    if (!isNullArray[i])
                    {
                        (objectToReturn as System.Collections.Stack).Push(stackRecur.Invoke(null, new object[1] {
                            input
                        }));
                    }
                }
                #endregion
#endif
            }

            //And for Queues
            else if (GetInterface(type, "Queue") != null)
            {
#if XNA4
                throw new NotImplementedException();
#else
                #region Read Queue
                objectToReturn = System.Activator.CreateInstance <T>();
                Type[]     genericParam = type.GetGenericArguments();
                MethodInfo queueRecur   = typeof(ObjectReader).GetMethod("ReadObject").MakeGenericMethod(new Type[1] {
                    genericParam[0]
                });

                for (int i = 0; i < count; ++i)
                {
                    if (!isNullArray[i])
                    {
                        (objectToReturn as System.Collections.Queue).Enqueue(queueRecur.Invoke(null, new object[1] {
                            input
                        }));
                    }
                }
                #endregion
#endif
            }

            return((T)objectToReturn);
        }
Exemple #3
0
        public static SpriteSave FromXElement(XElement element)
        {
            SpriteSave spriteSave = new SpriteSave();

            foreach (var item in element.Attributes())
            {
                switch (item.Name.LocalName)
                {
                case "TextureAddressMode":
                    spriteSave.TextureAddressModeAsString = item.Value;
                    break;

                default:
                    throw new NotImplementedException("Unknown SpriteSave attribute: " + item.Name.LocalName);
                }
            }

            foreach (var subElement in element.Elements())
            {
                switch (subElement.Name.LocalName)
                {
                case "Active":
                    spriteSave.Active = AsBool(subElement);
                    break;

                case "Animate":
                    spriteSave.Animate = AsBool(subElement);
                    break;

                case "AnimationChainsFile":
                    spriteSave.AnimationChainsFile = subElement.Value;
                    break;

                case "BlendOperation":
                    spriteSave.BlendOperation = subElement.Value;
                    break;

                case "BottomTextureCoordinate":
                    spriteSave.BottomTextureCoordinate = AsFloat(subElement);
                    break;

                case "ColorOperation":
                    spriteSave.ColorOperation = subElement.Value;
                    break;

                case "ConstantPixelSize":
                    spriteSave.ConstantPixelSize = AsFloat(subElement);
                    break;

                case "CurrentChain":
                    spriteSave.CurrentChain = AsInt(subElement);
                    break;

                case "Fade":
                    spriteSave.Fade = AsFloat(subElement);
                    break;

                case "FadeRate":
                    spriteSave.FadeRate = AsFloat(subElement);
                    break;

                case "FlipHorizontal":
                    spriteSave.FlipHorizontal = AsBool(subElement);
                    break;

                case "FlipVertical":
                    spriteSave.FlipVertical = AsBool(subElement);
                    break;

                case "LeftTextureCoordinate":
                    spriteSave.LeftTextureCoordinate = AsFloat(subElement);
                    break;

                case "Name":
                    spriteSave.Name = subElement.Value;
                    break;

                case "Ordered":
                    spriteSave.Ordered = AsBool(subElement);
                    break;

                case "Parent":
                    spriteSave.Parent = subElement.Value;
                    break;

                case "RightTextureCoordinate":
                    spriteSave.RightTextureCoordinate = AsFloat(subElement);
                    break;

                case "RelativeX":
                    spriteSave.RelativeX = AsFloat(subElement);
                    break;

                case "RelativeY":
                    spriteSave.RelativeY = AsFloat(subElement);
                    break;

                case "RelativeZ":
                    spriteSave.RelativeZ = AsFloat(subElement);
                    break;


                case "RelativeRotationX":
                    spriteSave.RelativeRotationX = AsFloat(subElement);
                    break;

                case "RelativeRotationY":
                    spriteSave.RelativeRotationY = AsFloat(subElement);
                    break;

                case "RelativeRotationZ":
                    spriteSave.RelativeRotationZ = AsFloat(subElement);
                    break;


                case "RotationX":
                    spriteSave.RotationX = AsFloat(subElement);
                    break;

                case "RotationY":
                    spriteSave.RotationY = AsFloat(subElement);
                    break;

                case "RotationZ":
                    spriteSave.RotationZ = AsFloat(subElement);
                    break;

                case "RotationZVelocity":
                    spriteSave.RotationZVelocity = AsFloat(subElement);
                    break;

                case "ScaleX":
                    spriteSave.ScaleX = AsFloat(subElement);
                    break;

                case "ScaleY":
                    spriteSave.ScaleY = AsFloat(subElement);
                    break;

                case "ScaleXVelocity":
                    spriteSave.ScaleXVelocity = AsFloat(subElement);
                    break;

                case "ScaleYVelocity":
                    spriteSave.ScaleYVelocity = AsFloat(subElement);
                    break;

                case "TextureAddressMode":
                    spriteSave.TextureAddressMode = (TextureAddressMode)Enum.Parse(typeof(TextureAddressMode), subElement.Value, true);
                    break;

                case "Texture":
                    spriteSave.Texture = subElement.Value;
                    break;

                case "TintRed":
                    spriteSave.TintRed = AsFloat(subElement);
                    break;

                case "TintGreen":
                    spriteSave.TintGreen = AsFloat(subElement);
                    break;

                case "TintBlue":
                    spriteSave.TintBlue = AsFloat(subElement);
                    break;

                case "TintRedRate":
                    spriteSave.TintRedRate = AsFloat(subElement);
                    break;

                case "TintGreenRate":
                    spriteSave.TintGreenRate = AsFloat(subElement);
                    break;

                case "TintBlueRate":
                    spriteSave.TintBlueRate = AsFloat(subElement);
                    break;

                case "TopTextureCoordinate":
                    spriteSave.TopTextureCoordinate = AsFloat(subElement);
                    break;

                case "Visible":
                    spriteSave.Visible = AsBool(subElement);
                    break;

                case "XAcceleration":
                    spriteSave.XAcceleration = AsFloat(subElement);
                    break;

                case "YAcceleration":
                    spriteSave.YAcceleration = AsFloat(subElement);
                    break;

                case "ZAcceleration":
                    spriteSave.ZAcceleration = AsFloat(subElement);
                    break;

                case "XVelocity":
                    spriteSave.XVelocity = AsFloat(subElement);
                    break;

                case "YVelocity":
                    spriteSave.YVelocity = AsFloat(subElement);
                    break;

                case "ZVelocity":
                    spriteSave.ZVelocity = AsFloat(subElement);
                    break;

                case "X":
                    spriteSave.X = AsFloat(subElement);
                    break;

                case "Y":
                    spriteSave.Y = AsFloat(subElement);
                    break;

                case "Z":
                    spriteSave.Z = AsFloat(subElement);
                    break;

                default:
                    throw new NotImplementedException();

                    //break;
                }
            }


            return(spriteSave);
        }