Example #1
0
        private GumState ToGumState(FlatRedBall.Glue.SaveClasses.StateSave glueState, GlueElement glueElement)
        {
            var gumState = new GumState();

            gumState.Name = glueState.Name;

            VariableGroupDictionary variableGroups = new VariableGroupDictionary();

            foreach (var glueVariable in glueState.InstructionSaves)
            {
                AddGumVariables(glueVariable, null, glueElement, gumState.Variables, gumState.VariableLists, variableGroups, isInState: true);
            }

            ApplyVariableGroups(variableGroups, glueElement, gumState.Variables);


            // everything should set value
            foreach (var gumVariable in gumState.Variables)
            {
                gumVariable.SetsValue = true;
            }


            return(gumState);
        }
        public static VariableSave GetVariableRecursive(this StateSave stateSave, string variableName)
        {
            VariableSave variableSave = stateSave.GetVariableSave(variableName);

            if (variableSave == null)
            {
                // Is this thing the default?
                ElementSave parent = stateSave.ParentContainer;

                if (parent != null && stateSave != parent.DefaultState)
                {
                    variableSave = stateSave.GetVariableSave(variableName);

                    if (variableSave == null)
                    {
                        variableSave = parent.DefaultState.GetVariableSave(variableName);
                    }
                }

                if (variableSave == null && parent != null)
                {
                    ElementSave baseElement = GetBaseElementFromVariable(variableName, parent);

                    if (baseElement != null)
                    {
                        string nameInBase = variableName;

                        if (StringFunctions.ContainsNoAlloc(variableName, '.'))
                        {
                            // this variable is set on an instance, but we're going into the
                            // base type, so we want to get the raw variable and not the variable
                            // as tied to an instance.
                            nameInBase = variableName.Substring(nameInBase.IndexOf('.') + 1);
                        }

                        return(baseElement.DefaultState.GetVariableRecursive(nameInBase));
                    }
                }
            }

            return(variableSave);
        }
Example #3
0
        public StateSave Clone()
        {
            StateSave toReturn = new StateSave();

            toReturn.Name      = this.Name;
            toReturn.Variables = new List <VariableSave>();
            foreach (var variable in this.Variables)
            {
                toReturn.Variables.Add(variable.Clone());
            }

            foreach (var variableList in this.VariableLists)
            {
                toReturn.VariableLists.Add(variableList.Clone());
            }

            toReturn.ParentContainer = this.ParentContainer;

            return(toReturn);
        }
Example #4
0
        private static bool TrySetReservedValues(StateSave stateSave, string variableName, object value, InstanceSave instanceSave)
        {
            bool isReservedName = false;

            // Check for reserved names
            if (variableName == "Name")
            {
                stateSave.ParentContainer.Name = value as string;
                isReservedName = true;
            }
            else if (variableName == "Base Type")
            {
                stateSave.ParentContainer.BaseType = value.ToString();
                isReservedName = true; // don't do anything
            }

            if (StringFunctions.ContainsNoAlloc(variableName, '.'))
            {
                string instanceName = variableName.Substring(0, variableName.IndexOf('.'));

                ElementSave elementSave = stateSave.ParentContainer;

                // This is a variable on an instance
                if (variableName.EndsWith(".Name"))
                {
                    instanceSave.Name = (string)value;
                    isReservedName    = true;
                }
                else if (variableName.EndsWith(".Base Type"))
                {
                    instanceSave.BaseType = value.ToString();
                    isReservedName        = true;
                }
                else if (variableName.EndsWith(".Locked"))
                {
                    instanceSave.Locked = (bool)value;
                    isReservedName      = true;
                }
            }
            return(isReservedName);
        }
Example #5
0
        public StateSave Clone()
        {
            StateSave toReturn = new StateSave();

            toReturn.Name      = this.Name;
            toReturn.Variables = new List <VariableSave>();
            for (int i = 0; i < Variables.Count; i++)
            {
                var variable = this.Variables[i];

                toReturn.Variables.Add(variable.Clone());
            }

            for (int i = 0; i < this.VariableLists.Count; i++)
            {
                toReturn.VariableLists.Add(VariableLists[i].Clone());
            }

            toReturn.ParentContainer = this.ParentContainer;

            return(toReturn);
        }
Example #6
0
        public static void SetFrom(this StateSave stateSave, StateSave otherStateSave)
        {
            stateSave.Name = otherStateSave.Name;
            // We don't want to do this because the otherStateSave may not have a parent
            //stateSave.ParentContainer = otherStateSave.ParentContainer;

            stateSave.Variables.Clear();
            stateSave.VariableLists.Clear();

            foreach (VariableSave variable in otherStateSave.Variables)
            {
                stateSave.Variables.Add(FileManager.CloneSaveObject(variable));
            }

            foreach (VariableListSave variableList in otherStateSave.VariableLists)
            {
                stateSave.VariableLists.Add(FileManager.CloneSaveObject(variableList));
            }

#if GUM
            stateSave.FixEnumerations();
#endif
        }
Example #7
0
 public static StateSave Clone(this StateSave whatToClone)
 {
     return(whatToClone.Clone <StateSave>());
 }
Example #8
0
        /// <summary>
        /// Assigns a value to a variable.  If the variable doesn't exist then the variable is instantiated, then the value is assigned.
        /// </summary>
        /// <param name="stateSave">The StateSave that contains the variable.  The variable will be added to this StateSave if it doesn't exist.</param>
        /// <param name="variableName">The name of the variable to look for.</param>
        /// <param name="value">The value to assign to the variable.</param>
        /// <param name="instanceSave">The instance that owns this variable.  This may be null.</param>
        /// <param name="variableType">The type of the variable.  This is only needed if the value is null.</param>
        private static VariableSave AssignVariableSave(this StateSave stateSave, string variableName, object value,
                                                       InstanceSave instanceSave, string variableType = null, bool isFile = false)
        {
            // Not a reserved variable, so use the State's variables
            VariableSave variableSave = stateSave.GetVariableSave(variableName);

            if (variableSave == null)
            {
                variableSave = new VariableSave();

                // If the variableType is not null, give it priority
                if (!string.IsNullOrEmpty(variableType))
                {
                    variableSave.Type = variableType;
                }

                else if (value is bool)
                {
                    variableSave.Type = "bool";
                }
                else if (value is float)
                {
                    variableSave.Type = "float";
                }
                else if (value is int)
                {
                    variableSave.Type = "int";
                }
                else if (value is int?)
                {
                    variableSave.Type = "int?";
                }
                // account for enums
                else if (value is string)
                {
                    variableSave.Type = "string";
                }
                else if (value == null)
                {
                    variableSave.Type = variableType;
                }
                else
                {
                    variableSave.Type = value.GetType().ToString();
                }

                variableSave.IsFile = isFile;

                variableSave.Name = variableName;

                stateSave.Variables.Add(variableSave);
            }



            // There seems to be
            // two ways to indicate
            // that a variable has a
            // source object.  One is
            // to pass a InstanceSave to
            // this method, another is to
            // include a '.' in the name.  If
            // an instanceSave is passed, then
            // a dot MUST be present.  I don't think
            // we allow a dot to exist without a variable
            // representing a variable on an instance save,
            // so I'm not sure why we even require an InstanceSave.
            // Also, it seems like code (especially plugins) may not
            // know to pass an InstanceSave and may assume that the dot
            // is all that's needed.  If so, we shouldn't be strict and require
            // a non-null InstanceSave.
            //if (instanceSave != null)
            // Update:  We used to only check this when first creating a Variable, but
            // there's no harm in forcing the source object.  Let's do that.
            // Update:  Turns out we do need the instance so that we can get the base type
            // to find out if the variable IsFile or not.  If the InstanceSave is null, but
            // we have a sourceObjectName that we determine by the presence of a dot, then let's
            // try to find the InstanceSave
            if (StringFunctions.ContainsNoAlloc(variableName, '.'))
            {
                string rootName         = variableSave.Name.Substring(variableSave.Name.IndexOf('.') + 1);
                string sourceObjectName = variableSave.Name.Substring(0, variableSave.Name.IndexOf('.'));

                if (instanceSave == null && stateSave.ParentContainer != null)
                {
                    instanceSave = stateSave.ParentContainer.GetInstance(sourceObjectName);
                }

                //ElementSave baseElement = ObjectFinder.Self.GetRootStandardElementSave(instanceSave);

                //VariableSave baseVariableSave = baseElement.DefaultState.GetVariableSave(rootName);
                if (instanceSave != null)
                {
                    // can we get this from the base element?
                    var  instanceBase = ObjectFinder.Self.GetElementSave(instanceSave);
                    bool found        = false;

                    if (instanceBase != null)
                    {
                        VariableSave baseVariableSave = instanceBase.DefaultState.Variables.FirstOrDefault(item => item.ExposedAsName == rootName || item.Name == rootName);
                        if (baseVariableSave != null)
                        {
                            variableSave.IsFile = baseVariableSave.IsFile;
                            found = true;
                        }
                    }

                    if (!found)
                    {
                        VariableSave baseVariableSave = ObjectFinder.Self.GetRootStandardElementSave(instanceSave).DefaultState.GetVariableSave(rootName);
                        if (baseVariableSave != null)
                        {
                            variableSave.IsFile = baseVariableSave.IsFile;
                        }
                    }
                }
            }

            variableSave.SetsValue = true;

            variableSave.Value = value;

            return(variableSave);
        }
Example #9
0
        public static object GetValueRecursive(this StateSave stateSave, string variableName)
        {
            object value = stateSave.GetValue(variableName);

            if (value == null)
            {
                // Is this thing the default?
                ElementSave parent = stateSave.ParentContainer;

                // I don't know if we need this code
                // because if we got in here, then the non-default failed to find a value
                // Update July 12, 2013
                // Not sure why I commented this code out.  This code lets us check a non-default
                // state, and if it doesn't contain a value, then we look at the default state in this
                // element.  Then if that fails, we can climb up the inheritance tree.
                // Let's see if we can get something from the non-default first
                bool wasFound = false;
                if (parent != null && stateSave != parent.DefaultState)
                {
                    // try to get it from the stateSave
                    var foundVariable = stateSave.GetVariableRecursive(variableName);
                    if (foundVariable != null && foundVariable.SetsValue)
                    {
                        // Why do we early out here?
                        //return foundVariable.Value;
                        value    = foundVariable.Value;
                        wasFound = true;
                    }
                }

                if (!wasFound && parent != null)
                {
                    if (!string.IsNullOrEmpty(parent.BaseType))
                    {
                        // eventually pass the state, but for now use default
                        value = TryToGetValueFromInheritance(variableName, parent.BaseType);
                    }

                    if (value == null)
                    {
                        ElementSave baseElement = GetBaseElementFromVariable(variableName, parent);

                        if (baseElement != null)
                        {
                            string nameInBase = variableName;

                            if (StringFunctions.ContainsNoAlloc(variableName, '.'))
                            {
                                // this variable is set on an instance, but we're going into the
                                // base type, so we want to get the raw variable and not the variable
                                // as tied to an instance.
                                nameInBase = variableName.Substring(nameInBase.IndexOf('.') + 1);
                            }

                            value = baseElement.DefaultState.GetValueRecursive(nameInBase);
                        }
                    }


                    if (value == null && parent is ComponentSave)
                    {
                        StateSave defaultStateForComponent = StandardElementsManager.Self.GetDefaultStateFor("Component");
                        if (defaultStateForComponent != null)
                        {
                            value = defaultStateForComponent.GetValueRecursive(variableName);
                        }
                    }
                }
            }


            return(value);
        }
Example #10
0
        public static void SetValue(this StateSave stateSave, string variableName, object value,
                                    InstanceSave instanceSave = null, string variableType = null)
        {
            bool isReservedName = TrySetReservedValues(stateSave, variableName, value, instanceSave);

            VariableSave variableSave           = stateSave.GetVariableSave(variableName);
            var          coreVariableDefinition = stateSave.GetVariableRecursive(variableName);

            string exposedVariableSourceName = null;

            if (!string.IsNullOrEmpty(coreVariableDefinition?.ExposedAsName) && instanceSave == null)
            {
                exposedVariableSourceName = coreVariableDefinition.Name;
            }
            string rootName = variableName;

            if (StringFunctions.ContainsNoAlloc(variableName, '.'))
            {
                rootName = variableName.Substring(variableName.IndexOf('.') + 1);
            }



            if (!isReservedName)
            {
                bool isFile = false;

                // Why might instanceSave be null?
                // The reason is because StateSaves
                // are used both for actual game data
                // as well as temporary variable containers.
                // If a StateSave is a temporary container then
                // instanceSave may (probably will be) null.
                if (instanceSave != null)
                {
                    VariableSave temp = variableSave;
                    if (variableSave == null)
                    {
                        temp      = new VariableSave();
                        temp.Name = variableName;
                    }
                    isFile = temp.GetIsFileFromRoot(instanceSave);
                }
                else
                {
                    VariableSave temp = variableSave;
                    if (variableSave == null)
                    {
                        temp      = new VariableSave();
                        temp.Name = variableName;
                    }
                    isFile = temp.GetIsFileFromRoot(stateSave.ParentContainer);
                }


                if (value != null && value is IList)
                {
                    stateSave.AssignVariableListSave(variableName, value, instanceSave);
                }
                else
                {
                    variableSave = stateSave.AssignVariableSave(variableName, value, instanceSave, variableType, isFile);

                    variableSave.IsFile = isFile;

                    if (!string.IsNullOrEmpty(exposedVariableSourceName))
                    {
                        variableSave.ExposedAsName = variableName;
                        variableSave.Name          = exposedVariableSourceName;
                    }

                    stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
                }



                if (isFile &&
                    value is string &&
                    !FileManager.IsRelative((string)value))
                {
                    string directoryToMakeRelativeTo = FileManager.GetDirectory(ObjectFinder.Self.GumProjectSave.FullFileName);

                    const bool preserveCase = true;
                    value = FileManager.MakeRelative((string)value, directoryToMakeRelativeTo, preserveCase);

                    // re-assign the value using the relative name now
                    var assignedVariable = stateSave.AssignVariableSave(variableName, value, instanceSave, variableType, isFile);
                }
            }
        }
Example #11
0
        /// <summary>
        /// Returns the first instance of an existing VariableSave recursively.
        /// </summary>
        /// <param name="stateSave">The possible state that contains the variable. If it doesn't, then the code will recursively go to base types.</param>
        /// <param name="variableName"></param>
        /// <returns></returns>
        public static VariableSave GetVariableRecursive(this StateSave stateSave, string variableName)
        {
            VariableSave variableSave = stateSave.GetVariableSave(variableName);

            if (variableSave == null)
            {
                // 1. Go to the default state if it's not a default
                bool shouldGoToDefaultState = false;
                // 2. Go to the base type if the variable is on the container itself, or if the instance is DefinedByBase
                bool shouldGoToBaseType = false;
                // 3. Go to the instance if it's on an instance and we're not going to the default state or base type
                bool shouldGoToInstanceComponent = false;

                // Is this thing the default?
                ElementSave elementContainingState = stateSave.ParentContainer;

                if (elementContainingState != null)
                {
                    if (elementContainingState != null && stateSave != elementContainingState.DefaultState)
                    {
                        shouldGoToDefaultState = true;
                    }

                    var          isVariableOnInstance = variableName.Contains('.');
                    InstanceSave instance             = null;
                    bool         canGoToBase          = false;

                    var hasBaseType = !string.IsNullOrEmpty(elementContainingState.BaseType);
                    var isVariableDefinedOnThisInheritanceLevel = false;

                    var instanceName = VariableSave.GetSourceObject(variableName);
                    instance = elementContainingState.Instances.FirstOrDefault(item => item.Name == instanceName);

                    if (isVariableOnInstance && hasBaseType)
                    {
                        if (instance != null && instance.DefinedByBase == false)
                        {
                            isVariableDefinedOnThisInheritanceLevel = true;
                        }
                    }
                    else if (!hasBaseType)
                    {
                        isVariableDefinedOnThisInheritanceLevel = true;
                    }

                    canGoToBase = isVariableOnInstance == false ||
                                  isVariableDefinedOnThisInheritanceLevel == false;

                    if (!shouldGoToDefaultState)
                    {
                        shouldGoToBaseType = canGoToBase;
                    }

                    if (!shouldGoToDefaultState && !shouldGoToBaseType)
                    {
                        shouldGoToInstanceComponent = isVariableOnInstance;
                    }


                    if (shouldGoToDefaultState)
                    {
                        variableSave = elementContainingState.DefaultState.GetVariableSave(variableName);
                        if (variableSave == null)
                        {
                            shouldGoToBaseType = canGoToBase;
                        }
                    }

                    if (shouldGoToBaseType)
                    {
                        var baseElement = ObjectFinder.Self.GetElementSave(elementContainingState.BaseType);

                        if (baseElement != null)
                        {
                            variableSave = baseElement.DefaultState.GetVariableRecursive(variableName);
                        }
                    }
                    else if (shouldGoToInstanceComponent)
                    {
                        ElementSave instanceElement = null;
                        if (instance != null)
                        {
                            instanceElement = ObjectFinder.Self.GetElementSave(instance);
                        }

                        if (instanceElement != null)
                        {
                            variableSave = instanceElement.DefaultState.GetVariableRecursive(VariableSave.GetRootName(variableName));
                        }
                    }
                }
            }

            return(variableSave);
        }
Example #12
0
        private static void UpdateTextStateCategory()
        {
            var textStandard = AppState.Self.GumProjectSave.StandardElements
                               .FirstOrDefault(item => item.Name == "Text");

            if (textStandard != null)
            {
                var added = false;

                var category = textStandard
                               .Categories.FirstOrDefault(item => item.Name == "ColorCategory");

                if (category == null)
                {
                    category      = new Gum.DataTypes.Variables.StateSaveCategory();
                    category.Name = "ColorCategory";
                    textStandard.Categories.Add(category);

                    added = true;
                }

                var grayState = category.States
                                .FirstOrDefault(item => item.Name == "Gray");

                if (grayState == null)
                {
                    grayState      = new Gum.DataTypes.Variables.StateSave();
                    grayState.Name = "Gray";
                    category.States.Add(grayState);

                    grayState.Variables.Add(new Gum.DataTypes.Variables.VariableSave
                    {
                        Type      = "int",
                        Name      = "Blue",
                        Value     = 208,
                        Category  = "Rendering",
                        SetsValue = true
                    });

                    grayState.Variables.Add(new Gum.DataTypes.Variables.VariableSave
                    {
                        Type      = "int",
                        Name      = "Green",
                        Value     = 208,
                        Category  = "Rendering",
                        SetsValue = true
                    });

                    grayState.Variables.Add(new Gum.DataTypes.Variables.VariableSave
                    {
                        Type      = "int",
                        Name      = "Red",
                        Value     = 208,
                        Category  = "Rendering",
                        SetsValue = true
                    });

                    grayState.Initialize();

                    added = true;
                }

                var blackState = category.States
                                 .FirstOrDefault(item => item.Name == "Black");

                if (blackState == null)
                {
                    blackState      = new Gum.DataTypes.Variables.StateSave();
                    blackState.Name = "Black";
                    category.States.Add(blackState);

                    blackState.Variables.Add(new Gum.DataTypes.Variables.VariableSave
                    {
                        Type      = "int",
                        Name      = "Blue",
                        Value     = 49,
                        Category  = "Rendering",
                        SetsValue = true
                    });

                    blackState.Variables.Add(new Gum.DataTypes.Variables.VariableSave
                    {
                        Type      = "int",
                        Name      = "Green",
                        Value     = 49,
                        Category  = "Rendering",
                        SetsValue = true
                    });

                    blackState.Variables.Add(new Gum.DataTypes.Variables.VariableSave
                    {
                        Type      = "int",
                        Name      = "Red",
                        Value     = 49,
                        Category  = "Rendering",
                        SetsValue = true
                    });

                    blackState.Initialize();

                    added = true;
                }

                if (added)
                {
                    AppCommands.Self.SaveStandardElement(textStandard);
                }
            }
        }
        public static void SetValue(this StateSave stateSave, string variableName, object value, InstanceSave instanceSave = null, string variableType = null)
        {
            bool isReservedName = TrySetReservedValues(stateSave, variableName, value, instanceSave);

            VariableSave variableSave = stateSave.GetVariableSave(variableName);

            string exposedVariableSourceName = null;

            string rootName = variableName;

            if (variableName.Contains('.'))
            {
                rootName = variableName.Substring(variableName.IndexOf('.') + 1);
            }
            else if (stateSave.ParentContainer != null && stateSave.ParentContainer.DefaultState != stateSave)
            {
                // This isn't the default state, so let's ask the default state if this is an exposed variable...
                var defaultState = stateSave.ParentContainer.DefaultState;

                var found = defaultState.Variables.FirstOrDefault(item => item.ExposedAsName == variableName);
                if (found != null)
                {
                    exposedVariableSourceName = found.Name;
                }
            }

            if (!isReservedName)
            {
                if (value != null && value is IList)
                {
                    stateSave.AssignVariableListSave(variableName, value, instanceSave);
                }
                else
                {
                    variableSave = stateSave.AssignVariableSave(variableName, value, instanceSave, variableType);

                    if (!string.IsNullOrEmpty(exposedVariableSourceName))
                    {
                        variableSave.ExposedAsName = variableName;
                        variableSave.Name          = exposedVariableSourceName;
                    }

                    stateSave.Variables.Sort((first, second) => first.Name.CompareTo(second.Name));
                }


                bool isFile = false;

                // Why might instanceSave be null?
                // The reason is because StateSaves
                // are used both for actual game data
                // as well as temporary variable containers.
                // If a StateSave is a temporary container then
                // instanceSave may (probably will be) null.
                if (instanceSave != null)
                {
                    isFile = variableSave.GetIsFileFromRoot(instanceSave);
                }
                else if (variableSave != null)
                {
                    isFile = variableSave.IsFile;
                }

                if (isFile &&
                    value is string &&
                    !FileManager.IsRelative((string)value))
                {
                    string directoryToMakeRelativeTo = FileManager.GetDirectory(ObjectFinder.Self.GumProjectSave.FullFileName);

                    const bool preserveCase = true;
                    value = FileManager.MakeRelative((string)value, directoryToMakeRelativeTo, preserveCase);

                    // re-assign the value using the relative name now
                    stateSave.AssignVariableSave(variableName, value, instanceSave, variableType);
                }
            }
        }