Esempio n. 1
0
        private TypeConverter HandleGetTypeConverter(IElement container, NamedObjectSave instance, TypedMemberBase typedMember)
        {
            Type memberType = typedMember.MemberType;
            string memberName = typedMember.MemberName;

            TypeConverter typeConverter = null;

            // If the NOS references a FRB type, we need to adjust the type appropriately

            bool wasTypeModified = false;
            Type oldType = memberType;

            bool handled = false;






            if (instance.SourceType == SourceType.FlatRedBallType)
            {
                Type type = TypeManager.GetTypeFromString(instance.SourceClassType);

                if (type == typeof(Sprite) && memberName == "CurrentChainName")
                {
                    // special case handling for CurrentChainName
                    typeConverter = new AvailableAnimationChainsStringConverter(container, instance);

                    handled = true;
                }

                // Victor Chelaru
                // October 26, 2015:
                // I don't think we need this anymore - we should just let the CSVs do their job
                // defining variables and not rely on reflection. That way plugins will be able to
                // handle everything without relying on reflection
                //if (!handled && type != null)
                //{
                //    FieldInfo fieldInfo = type.GetField(memberName);
                //    if (fieldInfo != null)
                //    {
                //        memberType = fieldInfo.FieldType;
                //        wasTypeModified = true;
                //    }

                //    if (wasTypeModified == false)
                //    {
                //        PropertyInfo propertyInfo = type.GetProperty(memberName);
                //        if (propertyInfo != null)
                //        {
                //            memberType = propertyInfo.PropertyType;
                //            wasTypeModified = true;
                //        }

                //    }
                //}
            }
            else if (instance.SourceType == SourceType.File)
            {
                if (instance.ClassType == "Sprite" && memberName == "CurrentChainName")
                {
                    // special case handling for CurrentChainName
                    typeConverter = new AvailableAnimationChainsStringConverter(container, instance);

                    handled = true;
                }

            }

            if (!handled)
            {
                if (instance.DoesMemberNeedToBeSetByContainer(memberName))
                {
                    typeConverter = new AvailableNamedObjectsAndFiles(container);
                }
                else if (memberType.IsEnum)
                {
                    typeConverter = new EnumConverter(memberType);
                }
                else if (memberType == typeof(Microsoft.Xna.Framework.Color))
                {
                    typeConverter = new AvailableColorTypeConverter();
                    memberType = typeof(string);
                }
                else if (IsTypeFile(typedMember))
                {
                    AvailableFileStringConverter availableFileStringConverter = new AvailableFileStringConverter(container);
                    availableFileStringConverter.QualifiedRuntimeTypeName = memberType.FullName;
                    if(!string.IsNullOrEmpty( typedMember.CustomTypeName ))
                    {
                        availableFileStringConverter.QualifiedRuntimeTypeName = typedMember.CustomTypeName;
                    }
                    availableFileStringConverter.RemovePathAndExtension = true;
                    typeConverter = availableFileStringConverter;

                    memberType = typeof(string);
                }
                else if (instance.SourceType == SourceType.Entity && !string.IsNullOrEmpty(instance.SourceClassType))
                {

                    EntitySave entity = ObjectFinder.Self.GetEntitySave(instance.SourceClassType);

                    if (entity != null)
                    {
                        CustomVariable customVariable = entity.GetCustomVariable(memberName);

                        if (customVariable != null)
                        {
                            typeConverter = customVariable.GetTypeConverter(entity);
                        }
                    }
                }
            }
            //else if (this.SourceType == SaveClasses.SourceType.FlatRedBallType &&
            //    typedMember != null && typedMember.MemberType != null)
            //{

            //}

            if (wasTypeModified)
            {
                memberType = oldType;
            }

            return typeConverter;

        }
        private static void AppendCustomVariableInInstanceStandard(NamedObjectSave namedObject, ICodeBlock codeBlock, InstructionSave instructionSave, AssetTypeInfo ati)
        {
            object objectToParse = instructionSave.Value;

            #region Determine the right-side value to assign

            CustomVariable customVariable = null;
            EntitySave entitySave = null;
            if (namedObject.SourceType == SourceType.Entity && !string.IsNullOrEmpty(namedObject.SourceClassType))
            {
                entitySave = ObjectFinder.Self.GetEntitySave(namedObject.SourceClassType);
                if (entitySave != null)
                {
                    customVariable = entitySave.GetCustomVariable(instructionSave.Member);
                }
            }


            IElement rootElementForVariable = entitySave;
            string rootVariable = instructionSave.Member;

            while (customVariable != null && customVariable.IsTunneling)
            {
                NamedObjectSave referencedNamedObject = rootElementForVariable.GetNamedObjectRecursively(customVariable.SourceObject);
                if (referencedNamedObject != null && referencedNamedObject.IsFullyDefined && referencedNamedObject.SourceType == SourceType.Entity)
                {
                    rootElementForVariable = ObjectFinder.Self.GetIElement(referencedNamedObject.SourceClassType);
                    rootVariable = customVariable.SourceObjectProperty;

                    customVariable = rootElementForVariable.GetCustomVariable(customVariable.SourceObjectProperty);
                }
                else
                {
                    break;
                }
            }


            string value = CodeParser.ParseObjectValue(objectToParse);


            if (CustomVariableExtensionMethods.GetIsFile(instructionSave.Type))
            {
                value = value.Replace("\"", "").Replace("-", "_");

                if (value == "<NONE>")
                {
                    value = "null";
                }
            }
            else if (ShouldAssignToCsv(customVariable, value))
            {
                value = GetAssignmentToCsvItem(customVariable, entitySave, value);
            }
            else if (instructionSave.Type == "Color")
            {
                value = "Color." + value.Replace("\"", "");

            }
            else if ((customVariable != null && customVariable.GetIsVariableState()) || (customVariable == null && rootVariable == "CurrentState"))
            {
                //string type = "VariableState";
                //if (customVariable != null && customVariable.Type.ToLower() != "string")
                //{
                //    type = customVariable.Type;
                //}
                value = StateCodeGenerator.GetRightSideAssignmentValueAsString(entitySave, instructionSave);
            }
            else
            {
                value = CodeWriter.MakeLocalizedIfNecessary(namedObject, instructionSave.Member, objectToParse, value, null);
            }
            if (namedObject.DoesMemberNeedToBeSetByContainer(instructionSave.Member))
            {
                value = value.Replace("\"", "");
            }
            #endregion

            string leftSideMember = instructionSave.Member;

            bool makeRelative = !string.IsNullOrEmpty(InstructionManager.GetRelativeForAbsolute(leftSideMember));

            if (makeRelative)
            {
                if (ati != null)
                {
                    // If it can't attach, then don't try to set relative values
                    makeRelative &= ati.ShouldAttach;
                }
            }

            var objectName = namedObject.InstanceName;

            if (makeRelative)
            {
                codeBlock = codeBlock.If(objectName + ".Parent == null");
            }

            bool needsEndif = false;
            if (value.StartsWith("FlatRedBall.Graphics.ColorOperation."))
            {
                codeBlock.Line("#if FRB_MDX");

                codeBlock.Line(objectName + "." + instructionSave.Member + " = " + value.Replace("FlatRedBall.Graphics.ColorOperation.", "Microsoft.DirectX.Direct3D.TextureOperation.") + ";");
                codeBlock.Line("#else");

                needsEndif = true;
            }


            if (value.StartsWith("Microsoft.Xna.Framework.Graphics.TextureAddressMode."))
            {

                codeBlock.Line("#if FRB_MDX");

                codeBlock.Line(objectName + "." + instructionSave.Member + " = " + value.Replace("Microsoft.Xna.Framework.Graphics.TextureAddressMode.", "Microsoft.DirectX.Direct3D.TextureAddress.") + ";");
                codeBlock.Line("#else");

                needsEndif = true;
            }

            if (namedObject.DefinedByBase)
            {
                objectName = "base." + objectName;
            }
            codeBlock.Line(objectName + "." + instructionSave.Member + " = " + value + ";");

            if (needsEndif)
            {
                codeBlock.Line("#endif");
            }

            if (makeRelative)
            {
                codeBlock = codeBlock.End().Else();

                // Special Case!
                // If this NOS is
                // attached to a Camera
                // then we have to subtract
                // 40 from the position.



                if (namedObject.AttachToCamera && leftSideMember == "Z")
                {
                    value = value + " - 40.0f";
                }



                codeBlock.Line(objectName + "." + InstructionManager.GetRelativeForAbsolute(leftSideMember) + " = " + value + ";");



                codeBlock = codeBlock.End();

            }
        }