PageRenderTime 321ms CodeModel.GetById 230ms app.highlight 80ms RepoModel.GetById 1ms app.codeStats 1ms

/WorldView/SettingsManager.cs

#
C# | 1006 lines | 827 code | 163 blank | 16 comment | 116 complexity | 10f38049729d682c93321110ea1eb000 MD5 | raw file
   1using System;
   2using System.Collections.Generic;
   3using System.Linq;
   4using System.Xml;
   5using System.IO;
   6using System.Text;
   7using System.Drawing;
   8using MoreTerra.Structures;
   9using MoreTerra.Structures.TerraInfo;
  10
  11namespace MoreTerra
  12{
  13    public sealed class SettingsManager
  14    {
  15        public class UserSettings
  16        {
  17            public string SettingsName;
  18            public string InputWorldDirectory;
  19            public string OutputPreviewDirectory;
  20            public bool IsChestFilterEnabled;
  21            public Boolean UseOfficialColors;
  22            public Boolean AreWiresDrawable;
  23            public bool AreWallsDrawable;
  24            public Boolean OpenImageAfterDraw;
  25            public Boolean ShowChestItems;
  26            public Boolean ShowNormalItems;
  27            public Boolean ShowChestTypes;
  28            public Boolean UseCustomMarkers;
  29            public Int32 ChestListSortType;
  30            public Int32 CropImageType;
  31
  32            //            public SerializableDictionary<string, MarkerInfo> MarkerStates;
  33            public Dictionary<String, MarkerSettings> MarkerStates;
  34
  35            // This contains the actual list we use to filter with.
  36            public List<String> ChestFilterItems;
  37
  38            public UserSettings()
  39            {
  40
  41            }
  42
  43            public UserSettings(UserSettings copy, String newName)
  44            {
  45                SettingsName = newName;
  46                InputWorldDirectory = copy.InputWorldDirectory;
  47                OutputPreviewDirectory = copy.OutputPreviewDirectory;
  48                IsChestFilterEnabled = copy.IsChestFilterEnabled;
  49                UseOfficialColors = copy.UseOfficialColors;
  50                AreWallsDrawable = copy.AreWallsDrawable;
  51                OpenImageAfterDraw = copy.OpenImageAfterDraw;
  52                ShowChestTypes = copy.ShowChestTypes;
  53                UseCustomMarkers = copy.UseCustomMarkers;
  54                ChestListSortType = copy.ChestListSortType;
  55                ShowChestItems = copy.ShowChestItems;
  56                ShowNormalItems = copy.ShowNormalItems;
  57
  58                MarkerStates = new Dictionary<String, MarkerSettings>();
  59
  60                foreach (KeyValuePair<String, MarkerSettings> kvp in copy.MarkerStates)
  61                    MarkerStates.Add(kvp.Key, kvp.Value);
  62
  63                ChestFilterItems = new List<String>();
  64
  65                foreach (String s in copy.ChestFilterItems)
  66                    ChestFilterItems.Add(s);
  67            }
  68        }
  69
  70        public class ColorSettings
  71        {
  72
  73        }
  74
  75        private static SettingsManager instance = null;
  76        private static readonly object mutex = new object();
  77        private Dictionary<string, UserSettings> settingsList;
  78        private UserSettings settings;
  79
  80        private List<ColorSettings> colorSettingsList;
  81        private ColorSettings colorSettings;
  82
  83        // These items get saved into the settings file.
  84        // These are our Global Settings.
  85        private const Int32 UserSettingsVersion = 1;
  86        private Int32 curSettings;
  87        private Int32 HighestVersion;
  88        private String curSettingsName;
  89
  90        #region Constructors
  91        private SettingsManager()
  92        {
  93            UserSettings us = new UserSettings();
  94
  95            this.settingsList = new Dictionary<string, UserSettings>();//List<UserSettings>();
  96            this.settingsList.Add("Default", us);//.Add(us);
  97            curSettings = 0;
  98
  99            this.HighestVersion = Global.CurrentVersion;
 100
 101            SetDefaults(us);
 102
 103            this.settings = us;
 104        }
 105        #endregion
 106
 107        #region Initialization
 108        private void SetDefaults(UserSettings us)
 109        {
 110            // Initialize Marker States
 111            us.MarkerStates = new Dictionary<String, MarkerSettings>();
 112
 113            foreach (KeyValuePair<Int32, MarkerInfo> kvp in Global.Instance.Info.Markers)
 114            {
 115                us.MarkerStates.Add(kvp.Value.name, new MarkerSettings());
 116            }
 117
 118            // Initialize Item Filter
 119            us.ChestFilterItems = new List<String>();
 120            us.ChestFilterItems.Sort();
 121
 122            us.SettingsName = "Default";
 123            us.IsChestFilterEnabled = false;
 124            us.UseOfficialColors = true;
 125            us.AreWallsDrawable = true;
 126            us.OpenImageAfterDraw = true;
 127            us.ShowChestTypes = false;
 128            us.UseCustomMarkers = false;
 129            us.ShowChestItems = true;
 130            us.ShowNormalItems = false;
 131            us.AreWiresDrawable = true;
 132
 133            us.ChestListSortType = 0;
 134            us.CropImageType = 0;
 135
 136            us.InputWorldDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My Games\\Terraria\\Worlds");
 137            if (!Directory.Exists(us.InputWorldDirectory))
 138            {
 139                us.InputWorldDirectory = string.Empty;
 140            }
 141
 142            us.OutputPreviewDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
 143            if (!Directory.Exists(us.OutputPreviewDirectory))
 144            {
 145                us.OutputPreviewDirectory = string.Empty;
 146            }
 147        }
 148
 149        public static SettingsManager Instance
 150        {
 151            get
 152            {
 153                lock (mutex)
 154                {
 155                    if (instance == null)
 156                    {
 157                        instance = new SettingsManager();
 158                    }
 159                    return instance;
 160                }
 161            }
 162        }
 163
 164        public void Initialize()
 165        {
 166            // Initialization
 167            if (System.IO.File.Exists(Global.ApplicationUserSettingsFile))
 168                LoadSettings();
 169        }
 170        #endregion
 171
 172        #region SettingsList Helper Functions
 173        public String SettingsName(Int32 name)
 174        {
 175            if ((name < 0) || (name >= settingsList.Count))
 176                return String.Empty;
 177
 178            return settingsList.ElementAt(name).Value.SettingsName;
 179        }
 180
 181        public Int32 SettingsCount
 182        {
 183            get
 184            {
 185                return settingsList.Count;
 186            }
 187        }
 188
 189        public Int32 CurrentSettings
 190        {
 191            get
 192            {
 193                return curSettings;
 194            }
 195            set
 196            {
 197                if ((value < 0) || (value >= settingsList.Count))
 198                    return;
 199
 200                curSettings = value;
 201                settings = settingsList.ElementAt(value).Value;//settingsList[value]
 202                curSettingsName = settings.SettingsName;
 203            }
 204        }
 205
 206        public string CurrentSettingsName
 207        {
 208            get
 209            {
 210                return curSettingsName;
 211            }
 212            set
 213            {
 214                if (settingsList.ContainsKey(value))
 215                {
 216                    curSettings = settingsList.Keys.ToList().FindIndex(delegate(string Key)
 217                    {
 218                        return Key == value;
 219                    });
 220                    curSettingsName = value;
 221                    this.settings = settingsList[value];
 222                }
 223            }
 224        }
 225
 226        // This does no range check as it is done in the Delete button event handler.
 227        public void DeleteSettings(Int32 toDelete)
 228        {
 229            KeyValuePair<string, UserSettings> setting = settingsList.ElementAt(toDelete);
 230            settingsList.Remove(setting.Key);//RemoveAt(toDelete);
 231            if (toDelete == curSettings)
 232            {
 233                curSettings = -1;
 234                settings = null;
 235            }
 236        }
 237
 238        public Boolean AddNewSettings(String newName)
 239        {
 240            UserSettings us;
 241            Int32 i;
 242
 243            if (newName == String.Empty)
 244                return false;
 245
 246            if (settingsList.ContainsKey(newName))
 247            {
 248                return false;
 249            }
 250
 251            us = new UserSettings(settings, newName);
 252
 253            settingsList.Add(newName, us);
 254
 255            return true;
 256        }
 257        #endregion
 258
 259        #region Marker Helper Functions
 260        public bool DrawMarker(Int16 type)
 261        {
 262            if (type >= TileProperties.Unknown)
 263                return false;
 264
 265            String index = Global.Instance.Info.Tiles[type].markerName;
 266
 267            // convert to string index
 268            if (this.settings.MarkerStates.ContainsKey(index))
 269                return this.settings.MarkerStates[index].Drawing;
 270
 271            return false;
 272        }
 273
 274        public bool DrawMarker(ChestType type)
 275        {
 276            if (type == ChestType.Unknown)
 277                return false;
 278
 279            String chestType = Global.Instance.Info.MarkerImageToName(Enum.GetName(typeof(ChestType), type));
 280
 281            if (this.settings.MarkerStates.ContainsKey(chestType))
 282                return this.settings.MarkerStates[chestType].Drawing;
 283
 284            return false;
 285        }
 286
 287        public bool DrawMarker(MarkerType type)
 288        {
 289            if (type == MarkerType.Unknown)
 290                return false;
 291
 292            String markerType = Global.Instance.Info.MarkerImageToName(Enum.GetName(typeof(MarkerType), type));
 293
 294            if (this.settings.MarkerStates.ContainsKey(markerType))
 295                return this.settings.MarkerStates[markerType].Drawing;
 296
 297            return false;
 298        }
 299
 300        public bool DrawMarker(NPCType type)
 301        {
 302            String npcType = Global.Instance.Info.MarkerImageToName(Enum.GetName(typeof(NPCType), type));
 303
 304            if (this.settings.MarkerStates.ContainsKey(npcType))
 305                return this.settings.MarkerStates[npcType].Drawing;
 306
 307            return false;
 308        }
 309
 310        public bool DrawMarker(string marker)
 311        {
 312            if (this.settings.MarkerStates.ContainsKey(marker))
 313                return this.settings.MarkerStates[marker].Drawing;
 314
 315            return false;
 316        }
 317
 318        public void MarkerVisible(string key, bool status)
 319        {
 320            this.settings.MarkerStates[key].Drawing = status;
 321        }
 322        #endregion
 323
 324        #region Filter Helper Functions
 325        public void FilterItem(string itemName, bool status)
 326        {
 327            if (status == true)
 328                this.settings.ChestFilterItems.Add(itemName);
 329            else
 330                this.settings.ChestFilterItems.Remove(itemName);
 331        }
 332        #endregion
 333
 334        #region GetSet Functions
 335        public string InputWorldDirectory
 336        {
 337            get
 338            {
 339                return this.settings.InputWorldDirectory;
 340            }
 341            set
 342            {
 343                this.settings.InputWorldDirectory = value;
 344            }
 345        }
 346
 347        public string OutputPreviewDirectory
 348        {
 349            get
 350            {
 351                return this.settings.OutputPreviewDirectory;
 352            }
 353            set
 354            {
 355                this.settings.OutputPreviewDirectory = value;
 356            }
 357        }
 358
 359        public Dictionary<string, MarkerSettings> MarkerStates
 360        {
 361            get
 362            {
 363                return this.settings.MarkerStates;
 364            }
 365        }
 366
 367        public bool FilterChests
 368        {
 369            get
 370            {
 371                return this.settings.IsChestFilterEnabled;
 372            }
 373            set
 374            {
 375                this.settings.IsChestFilterEnabled = value;
 376            }
 377        }
 378
 379        public Boolean OfficialColors
 380        {
 381            get
 382            {
 383                return this.settings.UseOfficialColors;
 384            }
 385            set
 386            {
 387                this.settings.UseOfficialColors = value;
 388            }
 389        }
 390
 391        public Boolean DrawWires
 392        {
 393            get
 394            {
 395                return this.settings.AreWiresDrawable;
 396            }
 397            set
 398            {
 399                this.settings.AreWiresDrawable = value;
 400            }
 401        }
 402
 403        public bool DrawWalls
 404        {
 405            get
 406            {
 407                return this.settings.AreWallsDrawable;
 408            }
 409            set
 410            {
 411                this.settings.AreWallsDrawable = value;
 412            }
 413        }
 414
 415        public Boolean OpenImage
 416        {
 417            get
 418            {
 419                return this.settings.OpenImageAfterDraw;
 420            }
 421            set
 422            {
 423                this.settings.OpenImageAfterDraw = value;
 424            }
 425        }
 426
 427        public Int32 SortChestsBy
 428        {
 429            get
 430            {
 431                return this.settings.ChestListSortType;
 432            }
 433            set
 434            {
 435                this.settings.ChestListSortType = value;
 436            }
 437        }
 438
 439        public Int32 CropImageUsing
 440        {
 441            get
 442            {
 443                return this.settings.CropImageType;
 444            }
 445            set
 446            {
 447                this.settings.CropImageType = value;
 448            }
 449        }
 450
 451        // This is the highest version we have opened and said "Don't show again" to.
 452        public Int32 TopVersion
 453        {
 454            get
 455            {
 456                return this.HighestVersion;
 457            }
 458            set
 459            {
 460                this.HighestVersion = value;
 461            }
 462        }
 463
 464        public List<String> FilterItemStates
 465        {
 466            get
 467            {
 468                return this.settings.ChestFilterItems;
 469            }
 470        }
 471
 472        public Boolean ShowChestTypes
 473        {
 474            get
 475            {
 476                return this.settings.ShowChestTypes;
 477            }
 478            set
 479            {
 480                this.settings.ShowChestTypes = value;
 481            }
 482        }
 483
 484        public Boolean CustomMarkers
 485        {
 486            get
 487            {
 488                return settings.UseCustomMarkers;
 489            }
 490            set
 491            {
 492                settings.UseCustomMarkers = value;
 493            }
 494        }
 495
 496        public Boolean ShowChestItems
 497        {
 498            get
 499            {
 500                return settings.ShowChestItems;
 501            }
 502            set
 503            {
 504                settings.ShowChestItems = value;
 505            }
 506        }
 507
 508        public Boolean ShowNormalItems
 509        {
 510            get
 511            {
 512                return settings.ShowNormalItems;
 513            }
 514            set
 515            {
 516                settings.ShowNormalItems = value;
 517            }
 518        }
 519        #endregion
 520
 521        #region Load/Save Functions
 522        public void LoadSettings()
 523        {
 524            curSettingsName = "Default";
 525            Int32 settingsNodeCount;
 526            UserSettings us;
 527            Int32 settingsVer;
 528            XmlNode baseNode;
 529            XmlNodeList settingsNodes;
 530            XmlNode parseNode;
 531            XmlNodeList parseNodeList;
 532            XmlDocument xmlDoc = new XmlDocument();
 533            xmlDoc.Load(Global.ApplicationUserSettingsFile);
 534
 535            baseNode = xmlDoc.DocumentElement;
 536
 537            // There's no UserSettings element so we'll use the default settings.
 538            if (baseNode.Name != "UserSettings")
 539                return;
 540
 541            if (baseNode.Attributes["version"] == null)
 542                settingsVer = 0;
 543            else
 544                settingsVer = Int32.Parse(baseNode.Attributes["version"].Value);
 545
 546            switch (settingsVer)
 547            {
 548                case 0:
 549                    break;
 550                case 1:
 551                    parseNode = baseNode.SelectSingleNode("GlobalSettings");
 552
 553                    if (parseNode == null)
 554                        break;
 555
 556                    parseNodeList = parseNode.SelectNodes("item");
 557
 558                    foreach (XmlNode node in parseNodeList)
 559                    {
 560                        parseNode = node.Attributes["name"];
 561
 562                        if (parseNode == null)
 563                            continue;
 564
 565                        switch (parseNode.Value)
 566                        {
 567                            case "UseSettings":
 568                                parseNode = node.Attributes["value"];
 569
 570                                if (parseNode != null)
 571                                    curSettingsName = parseNode.Value;
 572                                break;
 573                            case "HighestVersion":
 574                                parseNode = node.Attributes["value"];
 575
 576                                if (parseNode != null)
 577                                    this.HighestVersion = Int32.Parse(parseNode.Value);
 578                                break;
 579                            default:
 580                                break;
 581                        }
 582                    }
 583                    break;
 584                default:
 585                    break;
 586            }
 587
 588            if (settingsVer > 0)
 589                settingsNodes = baseNode.SelectNodes("Settings");
 590            else
 591                settingsNodes = xmlDoc.SelectNodes("UserSettings");
 592
 593            settingsNodeCount = 0;
 594            foreach (XmlNode settingsNode in settingsNodes)
 595            {
 596                String settingsName;
 597
 598                parseNode = settingsNode.Attributes["name"];
 599
 600                if (parseNode == null)
 601                    settingsName = "Default";
 602                else
 603                    settingsName = parseNode.Value;
 604
 605                us = null;
 606                if (settingsList.ContainsKey(settingsName))
 607                {
 608                    us = settingsList[settingsName];
 609                }
 610                //foreach (UserSettings testUs in this.settingsList)
 611                //{
 612                //    if (testUs.SettingsName == settingsName)
 613                //        us = testUs;
 614                //}
 615
 616                if (us == null)
 617                {
 618                    us = new UserSettings();
 619                    SetDefaults(us);
 620                    us.SettingsName = settingsName;
 621
 622                    this.settingsList.Add(settingsName, us);
 623                }
 624
 625                if (us.SettingsName == curSettingsName)
 626                {
 627                    this.settings = us;
 628                    curSettings = settingsNodeCount;
 629                }
 630                settingsNodeCount++;
 631
 632                switch (settingsVer)
 633                {
 634                    case 0:
 635                        #region UserSettings version 0 Loader
 636                        foreach (XmlNode node in settingsNode)
 637                        {
 638                            switch (node.Name)
 639                            {
 640                                case "InputWorldDirectory":
 641                                    us.InputWorldDirectory = node.InnerXml;
 642                                    break;
 643                                case "OutputPreviewDirectory":
 644                                    us.OutputPreviewDirectory = node.InnerXml;
 645                                    break;
 646                                case "IsChestFilterEnabled":
 647                                    us.IsChestFilterEnabled = Boolean.Parse(node.InnerXml);
 648                                    break;
 649                                case "IsWallsDrawable":
 650                                    us.AreWallsDrawable = Boolean.Parse(node.InnerXml);
 651                                    break;
 652                                case "OpenImageAfterDraw":
 653                                    us.OpenImageAfterDraw = Boolean.Parse(node.InnerXml);
 654                                    break;
 655                                case "ShowChestTypes":
 656                                    us.ShowChestTypes = Boolean.Parse(node.InnerXml);
 657                                    break;
 658                                case "UseCustomMarkers":
 659                                    us.UseCustomMarkers = Boolean.Parse(node.InnerXml);
 660                                    break;
 661                                case "ChestListSortType":
 662                                    us.ChestListSortType = Int32.Parse(node.InnerXml);
 663                                    break;
 664                                case "HighestVersion":
 665                                    this.HighestVersion = Int32.Parse(node.InnerXml);
 666                                    break;
 667                                case "SymbolStates":
 668                                    parseNodeList = node.SelectNodes("item");
 669
 670                                    foreach (XmlNode n in parseNodeList)
 671                                    {
 672                                        String Key;
 673                                        Boolean Value;
 674                                        MarkerSettings mi;
 675
 676                                        parseNode = n.SelectSingleNode("key").SelectSingleNode("string");
 677                                        Key = parseNode.InnerXml;
 678
 679                                        parseNode = n.SelectSingleNode("value").SelectSingleNode("boolean");
 680                                        Value = Boolean.Parse(parseNode.InnerXml);
 681
 682                                        if (us.MarkerStates.ContainsKey(Key))
 683                                            us.MarkerStates[Key].Drawing = Value;
 684                                        else
 685                                        {
 686                                            String newKey = Global.Instance.Info.MarkerImageToName(Key);
 687
 688                                            if (!us.MarkerStates.ContainsKey(newKey))
 689                                                newKey = String.Empty;
 690
 691                                            if (newKey == String.Empty)
 692                                            {
 693                                                mi = new MarkerSettings();
 694                                                mi.Drawing = Value;
 695                                                us.MarkerStates.Add(Key, mi);
 696                                            }
 697                                            else
 698                                            {
 699                                                us.MarkerStates[newKey].Drawing = Value;
 700                                            }
 701                                        }
 702                                    }
 703                                    break;
 704                                case "ChestFilterItems":
 705                                    parseNodeList = node.SelectNodes("item");
 706
 707                                    foreach (XmlNode n in parseNodeList)
 708                                    {
 709                                        String Key;
 710                                        Boolean Value;
 711
 712                                        parseNode = n.SelectSingleNode("key").SelectSingleNode("string");
 713                                        Key = parseNode.InnerXml;
 714
 715                                        parseNode = n.SelectSingleNode("value").SelectSingleNode("boolean");
 716                                        Value = Boolean.Parse(parseNode.InnerXml);
 717
 718                                        if (Value == true)
 719                                            us.ChestFilterItems.Add(Key);
 720                                    }
 721                                    break;
 722                                default:
 723                                    break;
 724                            }
 725                        }
 726                        #endregion
 727                        break;
 728                    case 1:
 729                        #region UserSettings version 1 Loader
 730                        foreach (XmlNode node in settingsNode)
 731                        {
 732                            parseNode = node.Attributes["name"];
 733
 734                            if (parseNode == null)
 735                                continue;
 736
 737                            switch (parseNode.Value)
 738                            {
 739                                case "InputWorldDirectory":
 740                                    parseNode = node.Attributes["value"];
 741
 742                                    if (parseNode != null)
 743                                        us.InputWorldDirectory = parseNode.Value;
 744                                    break;
 745                                case "OutputPreviewDirectory":
 746                                    parseNode = node.Attributes["value"];
 747
 748                                    if (parseNode != null)
 749                                        us.OutputPreviewDirectory = parseNode.Value;
 750                                    break;
 751                                case "IsChestFilterEnabled":
 752                                    parseNode = node.Attributes["value"];
 753
 754                                    if (parseNode != null)
 755                                        us.IsChestFilterEnabled = Boolean.Parse(parseNode.Value);
 756                                    break;
 757                                case "UseOfficialColors":
 758                                    parseNode = node.Attributes["value"];
 759
 760                                    if (parseNode != null)
 761                                        us.UseOfficialColors = Boolean.Parse(parseNode.Value);
 762                                    break;
 763                                case "AreWiresDrawable":
 764                                    parseNode = node.Attributes["value"];
 765
 766                                    if (parseNode != null)
 767                                        us.AreWiresDrawable = Boolean.Parse(parseNode.Value);
 768                                    break;
 769                                case "AreWallsDrawable":
 770                                    parseNode = node.Attributes["value"];
 771
 772                                    if (parseNode != null)
 773                                        us.AreWallsDrawable = Boolean.Parse(parseNode.Value);
 774                                    break;
 775                                case "OpenImageAfterDraw":
 776                                    parseNode = node.Attributes["value"];
 777
 778                                    if (parseNode != null)
 779                                        us.OpenImageAfterDraw = Boolean.Parse(parseNode.Value);
 780                                    break;
 781                                case "ShowChestTypes":
 782                                    parseNode = node.Attributes["value"];
 783
 784                                    if (parseNode != null)
 785                                        us.ShowChestTypes = Boolean.Parse(parseNode.Value);
 786                                    break;
 787                                case "UseCustomMarkers":
 788                                    parseNode = node.Attributes["value"];
 789
 790                                    if (parseNode != null)
 791                                        us.UseCustomMarkers = Boolean.Parse(parseNode.Value);
 792                                    break;
 793                                case "ChestListSortType":
 794                                    parseNode = node.Attributes["value"];
 795
 796                                    if (parseNode != null)
 797                                        us.ChestListSortType = Int32.Parse(parseNode.Value);
 798                                    break;
 799                                case "CropImageType":
 800                                    parseNode = node.Attributes["value"];
 801
 802                                    if (parseNode != null)
 803                                        us.CropImageType = Int32.Parse(parseNode.Value);
 804                                    break;
 805                                case "ShowChestItems":
 806                                    parseNode = node.Attributes["value"];
 807
 808                                    if (parseNode != null)
 809                                        us.ShowChestItems = Boolean.Parse(parseNode.Value);
 810                                    break;
 811                                case "ShowNormalItems":
 812                                    parseNode = node.Attributes["value"];
 813
 814                                    if (parseNode != null)
 815                                        us.ShowNormalItems = Boolean.Parse(parseNode.Value);
 816                                    break;
 817                                case "MarkerStates":
 818                                    parseNodeList = node.SelectNodes("listitem");
 819
 820                                    foreach (XmlNode n in parseNodeList)
 821                                    {
 822                                        String Name;
 823                                        MarkerSettings mi;
 824
 825                                        parseNode = n.Attributes["name"];
 826
 827                                        if (parseNode == null)
 828                                            break;
 829
 830                                        Name = parseNode.Value;
 831
 832                                        if (us.MarkerStates.TryGetValue(Name, out mi) == false)
 833                                            mi = new MarkerSettings();
 834
 835                                        parseNode = n.Attributes["draw"];
 836
 837                                        if (parseNode != null)
 838                                            mi.Drawing = Boolean.Parse(parseNode.Value);
 839
 840                                        parseNode = n.Attributes["filter"];
 841
 842                                        if (parseNode != null)
 843                                            mi.Filtering = Boolean.Parse(parseNode.Value);
 844
 845                                        parseNode = n.Attributes["min"];
 846
 847                                        if (parseNode != null)
 848                                            mi.Min = Int32.Parse(parseNode.Value);
 849
 850                                        parseNode = n.Attributes["max"];
 851
 852                                        if (parseNode != null)
 853                                            mi.Max = Int32.Parse(parseNode.Value);
 854
 855                                        if (!us.MarkerStates.ContainsKey(Name))
 856                                            us.MarkerStates.Add(Name, mi);
 857                                    }
 858                                    break;
 859                                case "ChestFilterItems":
 860                                    parseNode = node.Attributes["filter"];
 861
 862                                    if (parseNode == null)
 863                                        continue;
 864
 865                                    String[] splitList = parseNode.Value.Split(';');
 866
 867                                    foreach (String s in splitList)
 868                                    {
 869                                        us.ChestFilterItems.Add(s);
 870                                    }
 871                                    break;
 872                                default:
 873                                    break;
 874                            }
 875                        }
 876                        #endregion
 877                        break;
 878                    default:
 879                        return;
 880                }
 881            }
 882
 883            parseNode = baseNode.SelectSingleNode("CustomColors");
 884
 885            if (parseNode != null)
 886            {
 887                parseNode = parseNode.Attributes["list"];
 888
 889                if (parseNode != null)
 890                {
 891                    String[] colorList = parseNode.Value.Split(';');
 892                    Color newColor;
 893
 894                    for (Int32 sPos = 0; sPos < colorList.Length; sPos += 2)
 895                    {
 896                        if (Global.TryParseColor(colorList[sPos + 1], out newColor) == false)
 897                            continue;
 898
 899                        if (Global.Instance.Info.Colors.ContainsKey(colorList[sPos]))
 900                            continue;
 901
 902
 903                        Global.Instance.Info.AddCustomColor(colorList[sPos], newColor);
 904                    }
 905                }
 906            }
 907        }
 908
 909        public void SaveSettings()
 910        {
 911            StringBuilder sb = new StringBuilder();
 912            String splitter;
 913            FileStream stream = new FileStream(Global.ApplicationUserSettingsFile, FileMode.Create);
 914            StreamWriter writer = new StreamWriter(stream);
 915
 916            writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
 917            writer.WriteLine("<UserSettings version=\"{0}\">", SettingsManager.UserSettingsVersion);
 918
 919            writer.WriteLine("  <GlobalSettings>");
 920            writer.WriteLine("    <item name=\"UseSettings\" value=\"{0}\" />",
 921                this.settings.SettingsName);
 922            writer.WriteLine("    <item name=\"HighestVersion\" value=\"{0}\" />", this.HighestVersion);
 923            writer.WriteLine("  </GlobalSettings>");
 924
 925            foreach (UserSettings us in this.settingsList.Values)
 926            {
 927
 928                writer.WriteLine("  <Settings name=\"{0}\">",
 929                    us.SettingsName);
 930                writer.WriteLine("    <item name=\"InputWorldDirectory\" value=\"{0}\" />",
 931                    us.InputWorldDirectory);
 932                writer.WriteLine("    <item name=\"OutputPreviewDirectory\" value=\"{0}\" />",
 933                    us.OutputPreviewDirectory);
 934                writer.WriteLine("    <item name=\"IsChestFilterEnabled\" value=\"{0}\" />",
 935                    us.IsChestFilterEnabled);
 936                writer.WriteLine("    <item name=\"UseOfficialColors\" value=\"{0}\" />",
 937                    us.UseOfficialColors);
 938                writer.WriteLine("    <item name=\"ShowChestItems\" value=\"{0}\" />",
 939                    us.ShowChestItems);
 940                writer.WriteLine("    <item name=\"ShowNormalItems\" value=\"{0}\" />",
 941                    us.ShowNormalItems);
 942                writer.WriteLine("    <item name=\"AreWiresDrawable\" value=\"{0}\" />",
 943                    us.AreWiresDrawable);
 944                writer.WriteLine("    <item name=\"AreWallsDrawable\" value=\"{0}\" />",
 945                    us.AreWallsDrawable);
 946                writer.WriteLine("    <item name=\"OpenImageAfterDraw\" value=\"{0}\" />",
 947                    us.OpenImageAfterDraw);
 948                writer.WriteLine("    <item name=\"ShowChestTypes\" value=\"{0}\" />",
 949                    us.ShowChestTypes);
 950                writer.WriteLine("    <item name=\"CropImageType\" value=\"{0}\" />",
 951                    us.CropImageType);
 952                writer.WriteLine("    <item name=\"UseCustomMarkers\" value=\"{0}\" />",
 953                    us.UseCustomMarkers);
 954                writer.WriteLine("    <item name=\"ChestListSortType\" value=\"{0}\" />",
 955                    us.ChestListSortType);
 956
 957                writer.WriteLine("    <item name=\"MarkerStates\">");
 958                foreach (KeyValuePair<String, MarkerSettings> kvp in us.MarkerStates)
 959                {
 960                    writer.WriteLine("      <listitem name=\"{0}\" draw=\"{1}\" filter=\"{2}\" min=\"{3}\" max=\"{4}\" />",
 961                        kvp.Key, kvp.Value.Drawing, kvp.Value.Filtering, kvp.Value.Min, kvp.Value.Max);
 962                }
 963                writer.WriteLine("    </item>");
 964
 965                sb.Clear();
 966                splitter = "";
 967                foreach (String s in us.ChestFilterItems)
 968                {
 969                    sb.Append(splitter + s);
 970                    splitter = ";";
 971                }
 972
 973                if (sb.Length != 0)
 974                    writer.WriteLine("    <item name=\"ChestFilterItems\" filter=\"{0}\" />", sb.ToString());
 975
 976                writer.WriteLine("  </Settings>");
 977            }
 978
 979            sb.Clear();
 980            splitter = "";
 981            foreach (KeyValuePair<String, ColorInfo> kvp in Global.Instance.Info.Colors)
 982            {
 983                if (kvp.Value.isCustom == true)
 984                {
 985                    sb.AppendFormat("{0}{1};{2}", splitter, kvp.Key, Global.ToColorString(kvp.Value.color));
 986                    splitter = ";";
 987                }
 988            }
 989
 990            if (sb.Length != 0)
 991                writer.WriteLine("  <CustomColors list=\"{0}\" />", sb.ToString());
 992
 993            writer.WriteLine("</UserSettings>");
 994            writer.Close();
 995
 996        }
 997        #endregion
 998
 999        public void Shutdown()
1000        {
1001            SaveSettings();
1002        }
1003
1004
1005    }
1006}