Example #1
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 #2
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);
                }
            }
        }