Example #1
0
        public static void Write()
        {
            var newSettings = Serialise.SerialiseSettings().Select(s => new Setting {
                Key = s.Key, Value = s.Value
            });

            Settings.Clear();
            Settings.AddRange(newSettings);

            var root = new GenericStructure("Sledge");

            // Settings
            var settings = new GenericStructure("Settings");

            foreach (var setting in Settings)
            {
                settings.AddProperty(setting.Key, setting.Value);
            }
            root.Children.Add(settings);

            // Recent Files
            var recents = new GenericStructure("RecentFiles");
            var i       = 1;

            foreach (var file in RecentFiles.OrderBy(x => x.Order).Select(x => x.Location).Where(File.Exists))
            {
                recents.AddProperty(i.ToString(CultureInfo.InvariantCulture), file);
                i++;
            }
            root.Children.Add(recents);

            // Games > Fgds/Wads
            foreach (var game in Games)
            {
                var g = new GenericStructure("Game");
                game.Write(g);
                root.Children.Add(g);
            }

            // Builds
            foreach (var build in Builds)
            {
                var b = new GenericStructure("Build");
                build.Write(b);
                root.Children.Add(b);
            }

            // Hotkeys
            Hotkeys = Sledge.Settings.Hotkeys.GetHotkeys().ToList();
            var hotkeys = new GenericStructure("Hotkeys");

            foreach (var g in Hotkeys.GroupBy(x => x.ID))
            {
                var count = 0;
                foreach (var hotkey in g)
                {
                    hotkeys.AddProperty(hotkey.ID + ":" + count, hotkey.HotkeyString);
                    count++;
                }
            }
            root.Children.Add(hotkeys);

            // Additional
            var additional = new GenericStructure("AdditionalSettings");

            foreach (var kv in AdditionalSettings)
            {
                var child = new GenericStructure(kv.Key);
                child.Children.Add(kv.Value);
                additional.Children.Add(child);
            }
            root.Children.Add(additional);

            // Favourite textures
            var favTextures = new GenericStructure("FavouriteTextures");

            favTextures.Children.Add(GenericStructure.Serialise(FavouriteTextureFolders));
            root.Children.Add(favTextures);

            File.WriteAllText(SettingsFile, root.ToString());
        }
Example #2
0
        public static void Read()
        {
            Builds.Clear();
            Games.Clear();
            RecentFiles.Clear();
            Settings.Clear();
            Hotkeys.Clear();
            AdditionalSettings.Clear();
            FavouriteTextureFolders.Clear();

            var root = ReadSettingsFile();

            if (root == null)
            {
                return;
            }

            var settings = root.Children.FirstOrDefault(x => x.Name == "Settings");

            if (settings != null)
            {
                foreach (var key in settings.GetPropertyKeys())
                {
                    Settings.Add(new Setting {
                        Key = key, Value = settings[key]
                    });
                }
            }
            var recents = root.Children.FirstOrDefault(x => x.Name == "RecentFiles");

            if (recents != null)
            {
                foreach (var key in recents.GetPropertyKeys())
                {
                    int i;
                    if (int.TryParse(key, out i))
                    {
                        RecentFiles.Add(new RecentFile {
                            Location = recents[key], Order = i
                        });
                    }
                }
            }
            var games = root.Children.Where(x => x.Name == "Game");

            foreach (var game in games)
            {
                var g = new Game();
                g.Read(game);
                Games.Add(g);
            }
            var builds = root.Children.Where(x => x.Name == "Build");

            foreach (var build in builds)
            {
                var b = new Build();
                b.Read(build);
                Builds.Add(b);
            }
            var hotkeys = root.Children.FirstOrDefault(x => x.Name == "Hotkeys");

            if (hotkeys != null)
            {
                foreach (var key in hotkeys.GetPropertyKeys())
                {
                    var spl = key.Split(':');
                    Hotkeys.Add(new Hotkey {
                        ID = spl[0], HotkeyString = hotkeys[key]
                    });
                }
            }

            Serialise.DeserialiseSettings(Settings.ToDictionary(x => x.Key, x => x.Value));
            Sledge.Settings.Hotkeys.SetupHotkeys(Hotkeys);

            var additionalSettings = root.Children.FirstOrDefault(x => x.Name == "AdditionalSettings");

            if (additionalSettings != null)
            {
                foreach (var child in additionalSettings.Children)
                {
                    if (child.Children.Count > 0)
                    {
                        AdditionalSettings.Add(child.Name, child.Children[0]);
                    }
                }
            }

            var favTextures = root.Children.FirstOrDefault(x => x.Name == "FavouriteTextures");

            if (favTextures != null && favTextures.Children.Any())
            {
                try
                {
                    var ft = GenericStructure.Deserialise <List <FavouriteTextureFolder> >(favTextures.Children[0]);
                    if (ft != null)
                    {
                        FavouriteTextureFolders.AddRange(ft);
                    }
                    FixFavouriteNames(FavouriteTextureFolders);
                }
                catch
                {
                    // Nope
                }
            }

            if (!File.Exists(SettingsFile))
            {
                Write();
            }
        }