예제 #1
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);
                }
            }
        }
        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);
                }
            }
        }