You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.5 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.IO;
  20. using System.Threading;
  21. using System.Xml;
  22. using System.Xml.Linq;
  23. namespace ICSharpCode.ILSpy
  24. {
  25. /// <summary>
  26. /// Manages IL Spy settings.
  27. /// </summary>
  28. public class ILSpySettings
  29. {
  30. readonly XElement root;
  31. ILSpySettings()
  32. {
  33. this.root = new XElement("ILSpy");
  34. }
  35. ILSpySettings(XElement root)
  36. {
  37. this.root = root;
  38. }
  39. public XElement this[XName section] {
  40. get {
  41. return root.Element(section) ?? new XElement(section);
  42. }
  43. }
  44. /// <summary>
  45. /// Loads the settings file from disk.
  46. /// </summary>
  47. /// <returns>
  48. /// An instance used to access the loaded settings.
  49. /// </returns>
  50. public static ILSpySettings Load()
  51. {
  52. using (new MutexProtector(ConfigFileMutex))
  53. {
  54. try
  55. {
  56. XDocument doc = LoadWithoutCheckingCharacters(GetConfigFile());
  57. return new ILSpySettings(doc.Root);
  58. }
  59. catch (IOException)
  60. {
  61. return new ILSpySettings();
  62. }
  63. catch (XmlException)
  64. {
  65. return new ILSpySettings();
  66. }
  67. }
  68. }
  69. static XDocument LoadWithoutCheckingCharacters(string fileName)
  70. {
  71. return XDocument.Load(fileName, LoadOptions.None);
  72. }
  73. /// <summary>
  74. /// Saves a setting section.
  75. /// </summary>
  76. public static void SaveSettings(XElement section)
  77. {
  78. Update(
  79. delegate (XElement root) {
  80. XElement existingElement = root.Element(section.Name);
  81. if (existingElement != null)
  82. existingElement.ReplaceWith(section);
  83. else
  84. root.Add(section);
  85. });
  86. }
  87. /// <summary>
  88. /// Updates the saved settings.
  89. /// We always reload the file on updates to ensure we aren't overwriting unrelated changes performed
  90. /// by another ILSpy instance.
  91. /// </summary>
  92. public static void Update(Action<XElement> action)
  93. {
  94. using (new MutexProtector(ConfigFileMutex))
  95. {
  96. string config = GetConfigFile();
  97. XDocument doc;
  98. try
  99. {
  100. doc = LoadWithoutCheckingCharacters(config);
  101. }
  102. catch (IOException)
  103. {
  104. // ensure the directory exists
  105. Directory.CreateDirectory(Path.GetDirectoryName(config));
  106. doc = new XDocument(new XElement("ILSpy"));
  107. }
  108. catch (XmlException)
  109. {
  110. doc = new XDocument(new XElement("ILSpy"));
  111. }
  112. doc.Root.SetAttributeValue("version", RevisionClass.Major + "." + RevisionClass.Minor + "." + RevisionClass.Build + "." + RevisionClass.Revision);
  113. action(doc.Root);
  114. doc.Save(config, SaveOptions.None);
  115. }
  116. }
  117. static string GetConfigFile()
  118. {
  119. if (App.CommandLineArguments.ConfigFile != null)
  120. return App.CommandLineArguments.ConfigFile;
  121. string localPath = Path.Combine(Path.GetDirectoryName(typeof(MainWindow).Assembly.Location), "ILSpy.xml");
  122. if (File.Exists(localPath))
  123. return localPath;
  124. return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ICSharpCode\\ILSpy.xml");
  125. }
  126. const string ConfigFileMutex = "01A91708-49D1-410D-B8EB-4DE2662B3971";
  127. /// <summary>
  128. /// Helper class for serializing access to the config file when multiple ILSpy instances are running.
  129. /// </summary>
  130. sealed class MutexProtector : IDisposable
  131. {
  132. readonly Mutex mutex;
  133. public MutexProtector(string name)
  134. {
  135. bool createdNew;
  136. this.mutex = new Mutex(true, name, out createdNew);
  137. if (!createdNew)
  138. {
  139. try
  140. {
  141. mutex.WaitOne();
  142. }
  143. catch (AbandonedMutexException)
  144. {
  145. }
  146. }
  147. }
  148. public void Dispose()
  149. {
  150. mutex.ReleaseMutex();
  151. mutex.Dispose();
  152. }
  153. }
  154. }
  155. }