Example #1
0
        public static void GetSettings(Mod mod, out IniData settings, out ModSettingsConfiguration config)
        {
            // Load config
            if (!TryGetConfig(mod, out config))
            {
                throw new ArgumentException("Mod has no associated settings.");
            }

            // Load serialized settings or recreate them
            string path = SettingsPath(mod);

            if (File.Exists(path))
            {
                settings = parser.ReadFile(path);

                var header = settings.Sections.GetSectionData(internalSection);
                if (header == null || header.Keys[settingsVersionKey] != config.version)
                {
                    ResetSettings(mod, ref settings, config);
                    Debug.LogFormat("Settings for {0} are incompatible with current version. " +
                                    "New settings have been recreated with default values", mod.Title);
                }
            }
            else
            {
                settings = null;
                ResetSettings(mod, ref settings, config);
                Debug.LogFormat("Missing settings for {0}. " +
                                "New settings have been recreated with default values.", mod.Title);
            }
        }
Example #2
0
        private void OnEnable()
        {
            _version        = serializedObject.FindProperty("version");
            _isPreset       = serializedObject.FindProperty("isPreset");
            _presetSettings = serializedObject.FindProperty("presetSettings");
            _presets        = serializedObject.FindProperty("presets");

            _sections = serializedObject.FindProperty("sections");

            Target        = (ModSettingsConfiguration)target;
            sectionsCount = Target.sections.Length;
        }
        private static bool Serialize(ModSettingsConfiguration config)
        {
            fsSerializer fsSerializer = new fsSerializer();
            fsData       data;

            if (fsSerializer.TrySerialize(typeof(fsSerializer), config, out data).Succeeded)
            {
                File.WriteAllText(JsonPath, fsJsonPrinter.PrettyJson(data));
                return(true);
            }

            return(false);
        }
        private static bool Deserialize(ModSettingsConfiguration config)
        {
            fsSerializer fsSerializer = new fsSerializer();

            if (File.Exists(JsonPath))
            {
                string serializedData = File.ReadAllText(JsonPath);
                fsData data           = fsJsonParser.Parse(serializedData);
                return(fsSerializer.TryDeserialize(data, ref config).Succeeded);
            }

            return(false);
        }
Example #5
0
        protected override void Setup()
        {
            // Get settings
            ModSettingsReader.GetSettings(mod, out data, out defaultSettings);
            config  = ModSettingsReader.GetConfig(mod);
            presets = ModSettingsReader.GetPresets(mod);

            // Setup base panel
            ParentPanel.BackgroundColor          = Color.clear;
            modSettingsPanel.BackgroundColor     = panelBackgroundColor;
            modSettingsPanel.Outline.Enabled     = true;
            modSettingsPanel.HorizontalAlignment = HorizontalAlignment.Center;
            modSettingsPanel.Position            = new Vector2(0, 8);
            modSettingsPanel.Size = new Vector2(320, 175);
            NativePanel.Components.Add(modSettingsPanel);

            // Initialize window
            Init();
        }
Example #6
0
        private void OnEnable()
        {
            _version        = serializedObject.FindProperty("version");
            _isPreset       = serializedObject.FindProperty("isPreset");
            _presetSettings = serializedObject.FindProperty("presetSettings");
            _presets        = serializedObject.FindProperty("presets");
            _sections       = serializedObject.FindProperty("sections");

            sections = new ReorderableList(serializedObject, _sections, true, true, true, true);
            sections.drawHeaderCallback  = Sections_DrawHeaderCallback;
            sections.drawElementCallback = Sections_DrawElementCallback;
            sections.onAddCallback       = Sections_OnAddCallback;
            sections.onRemoveCallback    = Sections_OnRemoveCallback;

            for (int i = 0; i < _sections.arraySize; i++)
            {
                AddKeysList(i);
            }

            Target = (ModSettingsConfiguration)target;
        }
Example #7
0
        public static bool TryGetConfig(Mod mod, out ModSettingsConfiguration config, bool legacySupport = true)
        {
            if (mod.AssetBundle.Contains("modsettings.asset"))
            {
                config = mod.GetAsset <ModSettingsConfiguration>("modsettings.asset");
                return(true);
            }

            if (legacySupport)
            {
                // Support for old mods
                if (mod.AssetBundle.Contains(mod.Title + ".ini.txt"))
                {
                    var data = GetIniDataFromTextAsset(mod.GetAsset <TextAsset>(mod.Title + ".ini.txt"));
                    config = ParseIniToConfig(data);
                    return(true);
                }
                else if (mod.AssetBundle.Contains("modsettings.ini.txt"))
                {
                    var data = GetIniDataFromTextAsset(mod.GetAsset <TextAsset>("modsettings.ini.txt"));
                    config = ParseIniToConfig(data);
                    return(true);
                }

                // Eventually this will no longer be supported as file name can be changed by user.
                if (mod.AssetBundle.Contains(mod.FileName + ".ini.txt"))
                {
                    Debug.LogWarningFormat("{0} is using an obsolete modsettings filename!", mod.Title);
                    var data = GetIniDataFromTextAsset(mod.GetAsset <TextAsset>(mod.FileName + ".ini.txt"));
                    config = ParseIniToConfig(data);
                    return(true);
                }
            }

            config = null;
            return(false);
        }
Example #8
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            if (_isPreset.boolValue)
            {
                EditorGUILayout.HelpBox("Create a preset for a mod. Remember to add it to the list of presets", MessageType.Info);
            }
            else
            {
                EditorGUILayout.HelpBox("Create settings for a mod. Remember to name the file 'modsettings.asset'", MessageType.Info);
            }

            // Header
            EditorGUILayout.LabelField("Header", EditorStyles.boldLabel);
            using (new EditorGUI.DisabledScope(_isPreset.boolValue))
                EditorGUILayout.PropertyField(_version);
            EditorGUILayout.PropertyField(_isPreset);

            // Presets tools
            if (_isPreset.boolValue)
            {
                presetSyncExpanded = EditorGUILayout.Foldout(presetSyncExpanded, "Sync", true);
                if (presetSyncExpanded)
                {
                    EditorGUILayout.HelpBox("Sync preset with main settings (doesn't reset compatible values)", MessageType.None);
                    parent = (ModSettingsConfiguration)EditorGUILayout.ObjectField("Parent", parent, typeof(ModSettingsConfiguration), true);
                    if (parent)
                    {
                        addNewKeys = EditorGUILayout.Toggle(new GUIContent("Add New Keys", "Add missing keys or only sync existing ones?"), addNewKeys);
                        if (GUILayout.Button("Sync"))
                        {
                            Target.Sync(parent, addNewKeys);
                        }
                    }
                }
                EditorGUILayout.PropertyField(_presetSettings, true);
            }
            else
            {
                EditorGUILayout.PropertyField(_presets, true);
            }

            // Settings
            EditorGUILayout.Separator();
            EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);

            sectionsExpanded = EditorGUILayout.Foldout(sectionsExpanded, "Sections", true);
            if (sectionsExpanded)
            {
                EditorGUI.indentLevel++;

                editMode = EditorGUILayout.Toggle("Edit Mode", editMode);

                using (new EditorGUI.DisabledScope(true))
                {
                    sectionsCount = EditorGUILayout.IntField("Size", sectionsCount);
                }

                var sectionNames = new List <string>();

                for (int i = 0; i < _sections.arraySize; i++)
                {
                    SerializedProperty _section     = _sections.GetArrayElementAtIndex(i);
                    SerializedProperty _sectionName = _section.FindPropertyRelative("name");
                    if (string.IsNullOrEmpty(_sectionName.stringValue))
                    {
                        _sectionName.stringValue = "Section";
                    }
                    sectionNames.Add(_sectionName.stringValue);

                    if (IsSectionFoldoutExpanded(i, _sectionName.stringValue))
                    {
                        EditorGUI.indentLevel++;

                        using (new EditorGUI.DisabledScope(_isPreset.boolValue))
                            EditorGUILayout.PropertyField(_sectionName);

                        SerializedProperty _keys = _section.FindPropertyRelative("keys");

                        var keyNames = new List <string>();

                        using (new EditorGUI.DisabledScope(true))
                        {
                            int thisKeysCount = KeysCount(i);
                        }

                        for (int j = 0; j < _keys.arraySize; j++)
                        {
                            SerializedProperty _key     = _keys.GetArrayElementAtIndex(j);
                            SerializedProperty _keyName = _key.FindPropertyRelative("name");
                            if (string.IsNullOrEmpty(_keyName.stringValue))
                            {
                                _keyName.stringValue = "Key";
                            }
                            keyNames.Add(_keyName.stringValue);

                            if (IsKeyFoldoutExpanded(i, j, _keyName.stringValue))
                            {
                                SerializedProperty _type = _key.FindPropertyRelative("type");

                                using (new EditorGUI.DisabledScope(_isPreset.boolValue))
                                {
                                    EditorGUILayout.PropertyField(_keyName);
                                    EditorGUILayout.PropertyField(_key.FindPropertyRelative("description"));
                                    EditorGUILayout.PropertyField(_type);
                                }

                                var keyType = (ModSettingsKey.KeyType)_type.enumValueIndex;
                                switch (keyType)
                                {
                                case ModSettingsKey.KeyType.Toggle:
                                    EditorGUILayout.PropertyField(_key.FindPropertyRelative("toggle").FindPropertyRelative("value"));
                                    break;

                                case ModSettingsKey.KeyType.MultipleChoice:
                                    SerializedProperty _multipleChoice = _key.FindPropertyRelative("multipleChoice");
                                    SerializedProperty _selected       = _multipleChoice.FindPropertyRelative("selected");
                                    _selected.intValue = EditorGUILayout.Popup(_selected.intValue, Target.sections[i].keys[j].multipleChoice.choices);
                                    using (new EditorGUI.DisabledScope(_isPreset.boolValue))
                                        EditorGUILayout.PropertyField(_multipleChoice.FindPropertyRelative("choices"), true);
                                    break;

                                case ModSettingsKey.KeyType.Slider:
                                    SerializedProperty _slider    = _key.FindPropertyRelative("slider");
                                    SerializedProperty _sliderMin = _slider.FindPropertyRelative("min");
                                    SerializedProperty _sliderMax = _slider.FindPropertyRelative("max");
                                    if (_sliderMin.intValue == 0 && _sliderMax.intValue == 0)
                                    {
                                        _sliderMax.intValue = 100;
                                    }
                                    EditorGUILayout.IntSlider(_slider.FindPropertyRelative("value"), _sliderMin.intValue, _sliderMax.intValue);
                                    GUILayout.BeginHorizontal();
                                    using (new EditorGUI.DisabledScope(_isPreset.boolValue))
                                    {
                                        EditorGUILayout.PropertyField(_sliderMin);
                                        EditorGUILayout.PropertyField(_sliderMax);
                                    }
                                    GUILayout.EndHorizontal();
                                    break;

                                case ModSettingsKey.KeyType.FloatSlider:
                                    SerializedProperty _floatSlider      = _key.FindPropertyRelative("floatSlider");
                                    SerializedProperty _floatSliderValue = _floatSlider.FindPropertyRelative("value");
                                    SerializedProperty _floatSliderMin   = _floatSlider.FindPropertyRelative("min");
                                    SerializedProperty _floatSliderMax   = _floatSlider.FindPropertyRelative("max");
                                    if (_floatSliderMin.floatValue == 0 && _floatSliderMax.floatValue == 0)
                                    {
                                        _floatSliderMax.floatValue = 1;
                                    }
                                    EditorGUILayout.Slider(_floatSliderValue, _floatSliderMin.floatValue, _floatSliderMax.floatValue);
                                    GUILayout.BeginHorizontal();
                                    using (new EditorGUI.DisabledScope(_isPreset.boolValue))
                                    {
                                        EditorGUILayout.PropertyField(_floatSliderMin);
                                        EditorGUILayout.PropertyField(_floatSliderMax);
                                    }
                                    GUILayout.EndHorizontal();
                                    break;

                                case ModSettingsKey.KeyType.Tuple:
                                    SerializedProperty _tuple = _key.FindPropertyRelative("tuple");
                                    GUILayout.BeginHorizontal();
                                    EditorGUILayout.PropertyField(_tuple.FindPropertyRelative("first"));
                                    EditorGUILayout.PropertyField(_tuple.FindPropertyRelative("second"));
                                    GUILayout.EndHorizontal();
                                    break;

                                case ModSettingsKey.KeyType.FloatTuple:
                                    SerializedProperty _floatTuple = _key.FindPropertyRelative("floatTuple");
                                    GUILayout.BeginHorizontal();
                                    EditorGUILayout.PropertyField(_floatTuple.FindPropertyRelative("first"));
                                    EditorGUILayout.PropertyField(_floatTuple.FindPropertyRelative("second"));
                                    GUILayout.EndHorizontal();
                                    break;

                                case ModSettingsKey.KeyType.Text:
                                    EditorGUILayout.PropertyField(_key.FindPropertyRelative("text").FindPropertyRelative("text"));
                                    break;

                                case ModSettingsKey.KeyType.Color:
                                    EditorGUILayout.PropertyField(_key.FindPropertyRelative("color").FindPropertyRelative("color"));
                                    break;
                                }

                                if (editMode)
                                {
                                    keysCount[i] += InsertOrRemove(_keys, j, i);
                                }
                            }
                        }

                        EditorGUI.indentLevel--;

                        if (editMode)
                        {
                            sectionsCount += InsertOrRemove(_sections, i);
                        }

                        if (DuplicatesDetected(keyNames))
                        {
                            EditorGUILayout.HelpBox("Multiple keys with the same name in a section detected!", MessageType.Error);
                        }
                    }
                }

                EditorGUI.indentLevel--;

                if (DuplicatesDetected(sectionNames))
                {
                    EditorGUILayout.HelpBox("Multiple sections with the same name detected!", MessageType.Error);
                }
            }

            // Import/Export
            EditorGUILayout.Separator();
            EditorGUILayout.LabelField("Import/Export", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox("Import from/export to Untracked/modsettings.json, export to Untracked/modsettings.ini", MessageType.None);

            if (GUILayout.Button("Import"))
            {
                Target.Import();
            }

            if (GUILayout.Button("Export"))
            {
                Target.Export();
            }

            if (GUILayout.Button("Export (Ini)"))
            {
                Target.ExportToIni();
            }

            serializedObject.ApplyModifiedProperties();
        }
        /// <summary>
        /// Load settings from IniData.
        /// This will be read from the ini file on disk the first time.
        /// </summary>
        private void LoadSettings()
        {
            // Read file
            if (data == null)
            {
                data = parser.ReadFile(path);
                ModSettingsReader.UpdateSettings(ref data, defaultSettings, Mod);
                config = ModSettingsReader.GetConfig(Mod);
            }

            // Read settings
            foreach (SectionData section in data.Sections.Where(x => x.SectionName != ModSettingsReader.internalSection))
            {
                // Section label
                TextLabel textLabel = new TextLabel();
                textLabel.Text                = section.SectionName;
                textLabel.TextColor           = sectionTitleColor;
                textLabel.Position            = new Vector2(x, y);
                textLabel.HorizontalAlignment = HorizontalAlignment.None;
                currentPanel.Components.Add(textLabel);
                MovePosition(spacing + 4);
                List <string> comments = section.Comments;
                int           comment  = 0;

                foreach (KeyData key in section.Keys)
                {
                    // Setting label
                    TextLabel settingName = new TextLabel();
                    settingName.Text                = key.KeyName;
                    settingName.Position            = new Vector2(x, y);
                    settingName.HorizontalAlignment = HorizontalAlignment.None;
                    if (comment < comments.Count)
                    {
                        settingName.ToolTip     = defaultToolTip;
                        settingName.ToolTipText = comments[comment];
                        comment++;
                    }
                    currentPanel.Components.Add(settingName);

                    // Setting field
                    ModSettingsKey configKey;
                    if (config && config.Key(section.SectionName, key.KeyName, out configKey))
                    {
                        settingName.ToolTipText = configKey.description;

                        // Use config file
                        switch (configKey.type)
                        {
                        case ModSettingsKey.KeyType.Toggle:
                            AddCheckBox(key.Value == "True");
                            break;

                        case ModSettingsKey.KeyType.Slider:
                            var slider = configKey.slider;
                            int startValue;
                            if (!int.TryParse(key.Value, out startValue))
                            {
                                startValue = 0;
                            }
                            AddSlider(slider.min, slider.max, startValue, key.KeyName);
                            break;

                        case ModSettingsKey.KeyType.FloatSlider:
                            var   floatSlider = configKey.floatSlider;
                            float floatStartValue;
                            if (!float.TryParse(key.Value, out floatStartValue))
                            {
                                floatStartValue = 0;
                            }
                            AddSlider(floatSlider.min, floatSlider.max, floatStartValue, key.KeyName);
                            break;

                        case ModSettingsKey.KeyType.Tuple:
                        case ModSettingsKey.KeyType.FloatTuple:
                            int index  = key.Value.IndexOf(ModSettingsReader.tupleDelimiterChar);
                            var first  = GetTextbox(95, 19.6f, key.Value.Substring(0, index));
                            var second = GetTextbox(116, 19.6f, key.Value.Substring(index + ModSettingsReader.tupleDelimiterChar.Length));
                            modTuples.Add(new Tuple <TextBox, TextBox>(first, second));
                            break;

                        case ModSettingsKey.KeyType.Text:
                        case ModSettingsKey.KeyType.MultipleChoice:     //TODO
                            TextBox textBox = GetTextbox(95, 40, key.Value);
                            modTextBoxes.Add(textBox);
                            break;

                        case ModSettingsKey.KeyType.Color:
                            TextBox colorBox = GetTextbox(95, 40, key.Value);
                            modTextBoxes.Add(colorBox);
                            int hexColor;
                            if (colorBox.DefaultText.Length != 8 || !int.TryParse(colorBox.DefaultText, System.Globalization.NumberStyles.HexNumber,
                                                                                  System.Globalization.CultureInfo.InvariantCulture, out hexColor))
                            {
                                colorBox.DefaultText = "FFFFFFFF";
                            }
                            // Use box background as a preview of the color
                            Color32 color = ModSettingsReader.ColorFromString(colorBox.DefaultText);
                            colorBox.BackgroundColor = color;
                            colorBox.ToolTip         = defaultToolTip;
                            colorBox.ToolTipText     = color.ToString();
                            break;
                        }
                    }
                    else
                    {
                        // Legacy support
                        if (key.Value == "True")
                        {
                            AddCheckBox(true);
                        }
                        else if (key.Value == "False")
                        {
                            AddCheckBox(false);
                        }
                        else if (key.Value.Contains(ModSettingsReader.tupleDelimiterChar)) // Tuple
                        {
                            int index  = key.Value.IndexOf(ModSettingsReader.tupleDelimiterChar);
                            var first  = GetTextbox(95, 19.6f, key.Value.Substring(0, index));
                            var second = GetTextbox(116, 19.6f, key.Value.Substring(index + ModSettingsReader.tupleDelimiterChar.Length));
                            modTuples.Add(new Tuple <TextBox, TextBox>(first, second));
                        }
                        else
                        {
                            TextBox textBox = GetTextbox(95, 40, key.Value);
                            modTextBoxes.Add(textBox);

                            // Color
                            if (textBox.DefaultText.Length == 8)
                            {
                                // Check if is a hex number or just a string with lenght eight
                                int hexColor;
                                if (int.TryParse(textBox.DefaultText, System.Globalization.NumberStyles.HexNumber,
                                                 System.Globalization.CultureInfo.InvariantCulture, out hexColor))
                                {
                                    // Use box background as a preview of the color
                                    Color32 color = ModSettingsReader.ColorFromString(textBox.DefaultText);
                                    textBox.BackgroundColor = color;
                                    textBox.ToolTip         = defaultToolTip;
                                    textBox.ToolTipText     = color.ToString();
                                }
                            }
                        }
                    }

                    MovePosition(spacing);
                }
            }
        }
Example #10
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            lineHeight = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

            if (isPreset = _isPreset.boolValue)
            {
                EditorGUILayout.HelpBox("Create a preset for a mod. This is a group of a portion or all settings values. " +
                                        "Remember to sync it with the main settings asset.", MessageType.Info);
            }
            else
            {
                EditorGUILayout.HelpBox("Create settings for a mod. Remember to name the file 'modsettings.asset'. " +
                                        "You can acces them in game through DaggerfallWorkshop.Game.Utility.ModSupport.ModSettings.", MessageType.Info);
            }

            // Header
            EditorGUILayout.LabelField("Header", EditorStyles.boldLabel);
            EditorGUILayout.Separator();
            using (new EditorGUI.DisabledScope(isPreset))
                EditorGUILayout.PropertyField(_version);
            EditorGUILayout.PropertyField(_isPreset);

            // Presets tools
            if (isPreset)
            {
                presetSyncExpanded = EditorGUILayout.Foldout(presetSyncExpanded, "Sync", true);
                if (presetSyncExpanded)
                {
                    EditorGUILayout.HelpBox("Sync preset with main settings (doesn't reset compatible values)", MessageType.None);
                    parent = (ModSettingsConfiguration)EditorGUILayout.ObjectField("Parent", parent, typeof(ModSettingsConfiguration), true);
                    if (parent)
                    {
                        addNewKeys = EditorGUILayout.Toggle(new GUIContent("Add New Keys", "Add missing keys or only sync existing ones?"), addNewKeys);
                        if (GUILayout.Button("Sync"))
                        {
                            Target.Sync(parent, addNewKeys);
                        }
                    }
                }

                EditorGUILayout.PropertyField(_presetSettings, new GUIContent("Info"), true);
            }
            else
            {
                if (presets == null)
                {
                    presets = new ReorderableList(serializedObject, _presets, true, true, true, true);
                    presets.drawHeaderCallback  = Presets_DrawHeaderCallback;
                    presets.drawElementCallback = Presets_DrawElementCallback;
                }
                EditorGUI.indentLevel++;
                presets.DoLayoutList();
                EditorGUI.indentLevel--;
            }

            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            // Settings
            EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel);
            EditorGUILayout.Separator();
            selectionGridSelected = GUILayout.SelectionGrid(selectionGridSelected, new string[] { "Sections", "Keys" }, 2);

            sections.draggable = sections.displayAdd = sections.displayRemove = !isPreset;
            foreach (var keyControl in keys)
            {
                keyControl.draggable = keyControl.displayAdd = keyControl.displayRemove = !isPreset;
            }

            if (selectionGridSelected == 0)
            {
                sections.DoLayoutList();
            }

            bool duplicateSections = false, duplicateKeys = false;

            sectionNames.Clear();
            keyNames.Clear();
            for (int i = 0; i < _sections.arraySize; i++)
            {
                SerializedProperty _section     = _sections.GetArrayElementAtIndex(i);
                SerializedProperty _sectionName = _section.FindPropertyRelative("name");
                _sectionName.stringValue = !string.IsNullOrEmpty(_sectionName.stringValue) ?
                                           _sectionName.stringValue.Replace(" ", string.Empty) : "Section";
                sectionNames.Add(_sectionName.stringValue);

                EditorGUI.indentLevel++;

                SerializedProperty _keys = _section.FindPropertyRelative("keys");
                var keyNames             = new List <string>();
                for (int j = 0; j < _keys.arraySize; j++)
                {
                    SerializedProperty _keyName = _keys.GetArrayElementAtIndex(j).FindPropertyRelative("name");
                    _keyName.stringValue = !string.IsNullOrEmpty(_keyName.stringValue) ?
                                           _keyName.stringValue.Replace(" ", string.Empty) : "Key";
                    keyNames.Add(_keyName.stringValue);
                }

                duplicateKeys |= DuplicatesDetected(keyNames);
                this.keyNames.Add(keyNames);

                if (selectionGridSelected == 1)
                {
                    keys[currentSection = i].DoLayoutList();
                }

                EditorGUI.indentLevel--;
            }

            duplicateSections |= DuplicatesDetected(sectionNames);

            if (duplicateSections)
            {
                EditorGUILayout.HelpBox("Multiple sections with the same name detected!", MessageType.Error);
            }
            if (duplicateKeys)
            {
                EditorGUILayout.HelpBox("Multiple keys with the same name in a section detected!", MessageType.Error);
            }

            EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);

            // Import/Export
            EditorGUILayout.LabelField("Import/Export", EditorStyles.boldLabel);
            EditorGUILayout.Separator();
            EditorGUILayout.HelpBox(string.Format("Import from/export to Untracked/modsettings.{0}", parseFormat == 0 ? "ini" : "json"), MessageType.None);
            parseFormat = GUILayout.SelectionGrid(parseFormat, new string[] { "Ini", "Json" }, 2);
            if (parseFormat == 0)
            {
                if (GUILayout.Button("Import"))
                {
                    Target.ImportFromIni();
                }
                else if (GUILayout.Button("Export"))
                {
                    Target.ExportToIni();
                }
            }
            else
            {
                if (GUILayout.Button("Import"))
                {
                    Target.Import();
                }
                else if (GUILayout.Button("Export"))
                {
                    Target.Export();
                }
            }

            serializedObject.ApplyModifiedProperties();
        }
Example #11
0
        public static void ParseIniToConfig(IniData iniData, ModSettingsConfiguration config)
        {
            var configSections = new List <ModSettingsConfiguration.Section>();

            foreach (SectionData section in iniData.Sections)
            {
                var configSection = new ModSettingsConfiguration.Section();
                configSection.name = section.SectionName;

                List <ModSettingsKey> keys = new List <ModSettingsKey>();
                foreach (KeyData key in section.Keys)
                {
                    var configKey = new ModSettingsKey();
                    configKey.name = key.KeyName;

                    if (key.Value == "True" || key.Value == "False")
                    {
                        configKey.type         = ModSettingsKey.KeyType.Toggle;
                        configKey.toggle       = new ModSettingsKey.Toggle();
                        configKey.toggle.value = bool.Parse(key.Value);
                    }
                    else if (key.Value.Contains(tupleDelimiterChar))
                    {
                        configKey.type       = ModSettingsKey.KeyType.FloatTuple;
                        configKey.floatTuple = new ModSettingsKey.FloatTuple();
                        int index = key.Value.IndexOf(tupleDelimiterChar);
                        float.TryParse(key.Value.Substring(0, index), out configKey.floatTuple.first);
                        float.TryParse(key.Value.Substring(index + tupleDelimiterChar.Length), out configKey.floatTuple.second);
                    }
                    else if (IsHexColor(key.Value))
                    {
                        configKey.type           = ModSettingsKey.KeyType.Color;
                        configKey.color          = new ModSettingsKey.Tint();
                        configKey.color.HexColor = key.Value;
                    }
                    else
                    {
                        configKey.type      = ModSettingsKey.KeyType.Text;
                        configKey.text      = new ModSettingsKey.Text();
                        configKey.text.text = key.Value;
                    }

                    keys.Add(configKey);
                }

                if (section.SectionName == internalSection)
                {
                    // Header
                    config.version = section.Keys[settingsVersionKey];

                    // Add section only if there are other hidden keys
                    if (keys.Count > 1)
                    {
                        configSection.keys = keys.Where(x => x.name != settingsVersionKey).ToArray();
                        configSections.Add(configSection);
                    }
                }
                else
                {
                    // Settings
                    configSection.keys = keys.ToArray();
                    configSections.Add(configSection);
                }
            }
            config.sections = configSections.ToArray();
        }
Example #12
0
        public static IniData ParseConfigToIni(ModSettingsConfiguration config)
        {
            var iniData = new IniData();

            // Header
            var header = new SectionData(internalSection);

            header.Keys.AddKey(settingsVersionKey, config.version);

            if (config.isPreset)
            {
                header.Keys.AddKey("PresetName", config.presetSettings.name);
                header.Keys.AddKey("PresetAuthor", config.presetSettings.author);
                header.Keys.AddKey("Description", config.presetSettings.description);
            }

            iniData.Sections.Add(header);

            // Settings
            foreach (var section in config.sections)
            {
                var sectionData = new SectionData(section.name);

                foreach (var key in section.keys)
                {
                    KeyData keyData = new KeyData(key.name);

                    switch (key.type)
                    {
                    case ModSettingsKey.KeyType.Toggle:
                        keyData.Value = key.toggle.value.ToString();
                        break;

                    case ModSettingsKey.KeyType.MultipleChoice:
                        keyData.Value = key.multipleChoice.selected.ToString();
                        break;

                    case ModSettingsKey.KeyType.Slider:
                        keyData.Value = key.slider.value.ToString();
                        break;

                    case ModSettingsKey.KeyType.FloatSlider:
                        keyData.Value = key.floatSlider.value.ToString();
                        break;

                    case ModSettingsKey.KeyType.Tuple:
                        keyData.Value = key.tuple.first + tupleDelimiterChar + key.tuple.second;
                        break;

                    case ModSettingsKey.KeyType.FloatTuple:
                        keyData.Value = key.floatTuple.first + tupleDelimiterChar + key.floatTuple.second;
                        break;

                    case ModSettingsKey.KeyType.Text:
                        keyData.Value = key.text.text;
                        break;

                    case ModSettingsKey.KeyType.Color:
                        keyData.Value = key.color.HexColor;
                        break;
                    }

                    sectionData.Keys.AddKey(keyData);
                }

                if (section.name == internalSection)
                {
                    iniData.Sections.GetSectionData(internalSection).Merge(sectionData);
                }
                else
                {
                    iniData.Sections.Add(sectionData);
                }
            }

            return(iniData);
        }
Example #13
0
 /// <summary>
 /// Save default settings to disk and set them as current settings.
 /// </summary>
 public static void ResetSettings(Mod mod, ref IniData settings, ModSettingsConfiguration config)
 {
     settings = ParseConfigToIni(config);
     parser.WriteFile(SettingsPath(mod), settings);
 }
Example #14
0
 /// <summary>
 /// Save default settings to disk.
 /// </summary>
 public static void ResetSettings(Mod mod, ModSettingsConfiguration config)
 {
     parser.WriteFile(SettingsPath(mod), ParseConfigToIni(config));
 }
        /// <summary>
        /// Sync version and UI controls/checks from parent to this preset.
        /// A preset can have even only a portion of keys (typically a section).
        /// </summary>
        /// <param name="parent">Main settings.</param>
        /// <param name="addNewKeys">Add missing keys or sync only found ones?</param>
        public void Sync(ModSettingsConfiguration parent, bool addNewKeys)
        {
            if (!parent)
            {
                Debug.LogError("Parent not found");
                return;
            }

            version = parent.version;

            var childSections = new List <Section>();

            foreach (var parentSection in parent.sections)
            {
                Section section = this[parentSection.name];
                if (section == null)
                {
                    if (!addNewKeys)
                    {
                        continue;
                    }

                    section      = new Section();
                    section.name = parentSection.name;
                    section.keys = new ModSettingsKey[0];
                }

                var childKeys = new List <ModSettingsKey>();
                foreach (var parentKey in parentSection.keys)
                {
                    ModSettingsKey key = section[parentKey.name];
                    if (key == null)
                    {
                        if (!addNewKeys)
                        {
                            continue;
                        }

                        key      = new ModSettingsKey();
                        key.name = parentKey.name;
                    }

                    key.description = parentKey.description;
                    key.type        = parentKey.type;
                    switch (parentKey.type)
                    {
                    case KeyType.MultipleChoice:
                        if (key.multipleChoice == null)
                        {
                            key.multipleChoice = new ModSettingsKey.MultipleChoice();
                        }
                        key.multipleChoice.choices = parentKey.multipleChoice.choices;
                        break;

                    case KeyType.Slider:
                        if (key.slider == null)
                        {
                            key.slider = new ModSettingsKey.Slider();
                        }
                        key.slider.max = parentKey.slider.max;
                        key.slider.min = parentKey.slider.min;
                        break;

                    case KeyType.FloatSlider:
                        if (key.floatSlider == null)
                        {
                            key.floatSlider = new ModSettingsKey.FloatSlider();
                        }
                        key.floatSlider.max = parentKey.floatSlider.max;
                        key.floatSlider.min = parentKey.floatSlider.min;
                        break;
                    }

                    childKeys.Add(key);
                }

                section.keys = childKeys.ToArray();

                childSections.Add(section);
            }

            sections = childSections.ToArray();

            // Add to list of presets
            string presetPath = UnityEditor.AssetDatabase.GetAssetPath(this.GetInstanceID());
            string presetName = Path.GetFileNameWithoutExtension(presetPath);

            if (!parent.presets.Contains(presetName))
            {
                var presets = new List <string>(parent.presets);
                presets.Add(presetName);
                parent.presets = presets.ToArray();
            }
        }