XmlDocLoader.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.Diagnostics;
  20. using System.IO;
  21. using System.Runtime.CompilerServices;
  22. using ICSharpCode.NRefactory.Documentation;
  23. using Mono.Cecil;
  24. namespace ICSharpCode.ILSpy.XmlDoc
  25. {
  26. /// <summary>
  27. /// Helps finding and loading .xml documentation.
  28. /// </summary>
  29. public static class XmlDocLoader
  30. {
  31. static readonly Lazy<XmlDocumentationProvider> mscorlibDocumentation = new Lazy<XmlDocumentationProvider>(LoadMscorlibDocumentation);
  32. static readonly ConditionalWeakTable<ModuleDefinition, XmlDocumentationProvider> cache = new ConditionalWeakTable<ModuleDefinition, XmlDocumentationProvider>();
  33. static XmlDocumentationProvider LoadMscorlibDocumentation()
  34. {
  35. string xmlDocFile = FindXmlDocumentation("mscorlib.dll", TargetRuntime.Net_4_0)
  36. ?? FindXmlDocumentation("mscorlib.dll", TargetRuntime.Net_2_0);
  37. if (xmlDocFile != null)
  38. return new XmlDocumentationProvider(xmlDocFile);
  39. else
  40. return null;
  41. }
  42. public static XmlDocumentationProvider MscorlibDocumentation {
  43. get { return mscorlibDocumentation.Value; }
  44. }
  45. public static XmlDocumentationProvider LoadDocumentation(ModuleDefinition module)
  46. {
  47. if (module == null)
  48. throw new ArgumentNullException("module");
  49. lock (cache) {
  50. XmlDocumentationProvider xmlDoc;
  51. if (!cache.TryGetValue(module, out xmlDoc)) {
  52. string xmlDocFile = LookupLocalizedXmlDoc(module.FullyQualifiedName);
  53. if (xmlDocFile == null) {
  54. xmlDocFile = FindXmlDocumentation(Path.GetFileName(module.FullyQualifiedName), module.Runtime);
  55. }
  56. if (xmlDocFile != null) {
  57. xmlDoc = new XmlDocumentationProvider(xmlDocFile);
  58. cache.Add(module, xmlDoc);
  59. } else {
  60. xmlDoc = null;
  61. }
  62. }
  63. return xmlDoc;
  64. }
  65. }
  66. static readonly string referenceAssembliesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), @"Reference Assemblies\Microsoft\\Framework");
  67. static readonly string frameworkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"Microsoft.NET\Framework");
  68. static string FindXmlDocumentation(string assemblyFileName, TargetRuntime runtime)
  69. {
  70. string fileName;
  71. switch (runtime) {
  72. case TargetRuntime.Net_1_0:
  73. fileName = LookupLocalizedXmlDoc(Path.Combine(frameworkPath, "v1.0.3705", assemblyFileName));
  74. break;
  75. case TargetRuntime.Net_1_1:
  76. fileName = LookupLocalizedXmlDoc(Path.Combine(frameworkPath, "v1.1.4322", assemblyFileName));
  77. break;
  78. case TargetRuntime.Net_2_0:
  79. fileName = LookupLocalizedXmlDoc(Path.Combine(frameworkPath, "v2.0.50727", assemblyFileName))
  80. ?? LookupLocalizedXmlDoc(Path.Combine(referenceAssembliesPath, "v3.5"))
  81. ?? LookupLocalizedXmlDoc(Path.Combine(referenceAssembliesPath, "v3.0"))
  82. ?? LookupLocalizedXmlDoc(Path.Combine(referenceAssembliesPath, @".NETFramework\v3.5\Profile\Client"));
  83. break;
  84. case TargetRuntime.Net_4_0:
  85. default:
  86. fileName = LookupLocalizedXmlDoc(Path.Combine(referenceAssembliesPath, @".NETFramework\v4.0", assemblyFileName))
  87. ?? LookupLocalizedXmlDoc(Path.Combine(frameworkPath, "v4.0.30319", assemblyFileName));
  88. break;
  89. }
  90. return fileName;
  91. }
  92. static string LookupLocalizedXmlDoc(string fileName)
  93. {
  94. if (string.IsNullOrEmpty(fileName))
  95. return null;
  96. string xmlFileName = Path.ChangeExtension(fileName, ".xml");
  97. string currentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
  98. string localizedXmlDocFile = GetLocalizedName(xmlFileName, currentCulture);
  99. Debug.WriteLine("Try find XMLDoc @" + localizedXmlDocFile);
  100. if (File.Exists(localizedXmlDocFile)) {
  101. return localizedXmlDocFile;
  102. }
  103. Debug.WriteLine("Try find XMLDoc @" + xmlFileName);
  104. if (File.Exists(xmlFileName)) {
  105. return xmlFileName;
  106. }
  107. if (currentCulture != "en") {
  108. string englishXmlDocFile = GetLocalizedName(xmlFileName, "en");
  109. Debug.WriteLine("Try find XMLDoc @" + englishXmlDocFile);
  110. if (File.Exists(englishXmlDocFile)) {
  111. return englishXmlDocFile;
  112. }
  113. }
  114. return null;
  115. }
  116. static string GetLocalizedName(string fileName, string language)
  117. {
  118. string localizedXmlDocFile = Path.GetDirectoryName(fileName);
  119. localizedXmlDocFile = Path.Combine(localizedXmlDocFile, language);
  120. localizedXmlDocFile = Path.Combine(localizedXmlDocFile, Path.GetFileName(fileName));
  121. return localizedXmlDocFile;
  122. }
  123. }
  124. }