Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
VsHelper.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Xml;
6 using Microsoft.VisualStudio;
7 using Microsoft.VisualStudio.Shell.Interop;
8 using System.Diagnostics;
9 using Microsoft.VisualStudio.Shell;
10 
11 namespace SiliconStudio.Paradox.VisualStudio
12 {
13  internal static class VsHelper
14  {
15  public static IVsHierarchy GetCurrentHierarchy(IServiceProvider provider)
16  {
17  EnvDTE.DTE vs = (EnvDTE.DTE)provider.GetService(typeof(EnvDTE.DTE)); if (vs == null) throw new InvalidOperationException("DTE not found."); return ToHierarchy(vs.SelectedItems.Item(1).ProjectItem.ContainingProject);
18  }
19  public static IVsHierarchy ToHierarchy(EnvDTE.Project project)
20  {
21  if (project == null) throw new ArgumentNullException("project"); string projectGuid = null; // DTE does not expose the project GUID that exists at in the msbuild project file. // Cannot use MSBuild object model because it uses a static instance of the Engine, // and using the Project will cause it to be unloaded from the engine when the // GC collects the variable that we declare.
22  using (XmlReader projectReader = XmlReader.Create(project.FileName))
23  {
24  projectReader.MoveToContent();
25  object nodeName = projectReader.NameTable.Add("ProjectGuid");
26  while (projectReader.Read())
27  {
28  if (Object.Equals(projectReader.LocalName, nodeName))
29  {
30  projectGuid = (String)projectReader.ReadElementContentAsString(); break;
31  }
32  }
33  }
34  Debug.Assert(!String.IsNullOrEmpty(projectGuid));
35  IServiceProvider serviceProvider = new ServiceProvider(project.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider); return VsShellUtilities.GetHierarchy(serviceProvider, new Guid(projectGuid));
36  }
37  public static IVsProject ToVsProject(EnvDTE.Project project)
38  {
39  if (project == null) throw new ArgumentNullException("project");
40  IVsProject vsProject = ToHierarchy(project) as IVsProject;
41  if (vsProject == null)
42  {
43  throw new ArgumentException("Project is not a VS project.");
44  }
45  return vsProject;
46  }
47  public static EnvDTE.Project ToDteProject(IVsHierarchy hierarchy)
48  {
49  if (hierarchy == null) throw new ArgumentNullException("hierarchy");
50  object prjObject = null;
51  if (hierarchy.GetProperty(0xfffffffe, -2027, out prjObject) >= 0)
52  {
53  return (EnvDTE.Project)prjObject;
54  }
55  else
56  {
57  throw new ArgumentException("Hierarchy is not a project.");
58  }
59  }
60  public static EnvDTE.Project ToDteProject(IVsProject project)
61  {
62  if (project == null) throw new ArgumentNullException("project");
63  return ToDteProject(project as IVsHierarchy);
64  }
65 
66  public static IEnumerable<EnvDTE.Project> GetDteProjectsInSolution(IVsSolution solution)
67  {
68  if (solution == null)
69  yield break;
70 
71  foreach (var hier in GetProjectsInSolution(solution))
72  {
73  EnvDTE.Project project = null;
74 
75  try
76  {
77  project = ToDteProject(hier);
78  }
79  catch (Exception)
80  {
81  }
82 
83  if (project != null)
84  yield return project;
85  }
86  }
87 
88  private static IEnumerable<IVsHierarchy> GetProjectsInSolution(IVsSolution solution)
89  {
90  if (solution == null) throw new ArgumentNullException("solution");
91 
92  IEnumHierarchies enumHierarchies;
93  var guid = Guid.Empty;
94  solution.GetProjectEnum((uint)__VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION, ref guid, out enumHierarchies);
95 
96  // Invalid result?
97  if (enumHierarchies == null)
98  yield break;
99 
100  var hierarchyArray = new IVsHierarchy[1];
101  uint hierarchyFetched;
102 
103  // Fetch one by one
104  while (enumHierarchies.Next(1, hierarchyArray, out hierarchyFetched) == VSConstants.S_OK && hierarchyFetched == 1)
105  {
106  if (hierarchyArray[0] != null)
107  yield return hierarchyArray[0];
108  }
109  }
110  }
111 }