PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/WorldView/SettingsManager.cs

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