Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BaseCodeGeneratorWithSite.cs
Go to the documentation of this file.
1 
2 /***************************************************************************
3 
4 Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
5 Copyright (c) Microsoft Corporation. All rights reserved.
6 This code is licensed under the Visual Studio SDK license terms.
7 THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
8 ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
9 IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
10 PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
11 
12 ***************************************************************************/
13 
14 using System;
15 using System.CodeDom.Compiler;
16 using System.Diagnostics;
17 using System.Runtime.InteropServices;
18 using EnvDTE;
19 using EnvDTE80;
20 using VSLangProj;
21 using VSOLE = Microsoft.VisualStudio.OLE.Interop;
22 using Microsoft.VisualStudio;
23 using Microsoft.VisualStudio.Shell;
24 using Microsoft.VisualStudio.Shell.Interop;
25 using Microsoft.VisualStudio.Designer.Interfaces;
26 
27 namespace SiliconStudio.Paradox.VisualStudio.CodeGenerator
28 {
29  /// <summary>
30  /// Base code generator with site implementation
31  /// </summary>
32  public abstract class BaseCodeGeneratorWithSite : BaseCodeGenerator, VSOLE.IObjectWithSite
33  {
34  private object site = null;
35  private CodeDomProvider codeDomProvider = null;
36  private ServiceProvider serviceProvider = null;
37  private ServiceProvider globalProvider = null;
38 
39  #region IObjectWithSite Members
40 
41  /// <summary>
42  /// GetSite method of IOleObjectWithSite
43  /// </summary>
44  /// <param name="riid">interface to get</param>
45  /// <param name="ppvSite">IntPtr in which to stuff return value</param>
46  void Microsoft.VisualStudio.OLE.Interop.IObjectWithSite.GetSite(ref Guid riid, out IntPtr ppvSite)
47  {
48  if (site == null)
49  {
50  throw new COMException("object is not sited", VSConstants.E_FAIL);
51  }
52 
53  IntPtr pUnknownPointer = Marshal.GetIUnknownForObject(site);
54  IntPtr intPointer = IntPtr.Zero;
55  Marshal.QueryInterface(pUnknownPointer, ref riid, out intPointer);
56 
57  if (intPointer == IntPtr.Zero)
58  {
59  throw new COMException("site does not support requested interface", VSConstants.E_NOINTERFACE);
60  }
61 
62  ppvSite = intPointer;
63  }
64 
65  /// <summary>
66  /// SetSite method of IOleObjectWithSite
67  /// </summary>
68  /// <param name="pUnkSite">site for this object to use</param>
69  void Microsoft.VisualStudio.OLE.Interop.IObjectWithSite.SetSite(object pUnkSite)
70  {
71  site = pUnkSite;
72  codeDomProvider = null;
73  serviceProvider = null;
74  }
75 
76  #endregion
77 
78  /// <summary>
79  /// Demand-creates a ServiceProvider
80  /// </summary>
81  protected ServiceProvider SiteServiceProvider
82  {
83  get
84  {
85  if (serviceProvider == null)
86  {
87  serviceProvider = new ServiceProvider(site as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
88  Debug.Assert(serviceProvider != null, "Unable to get ServiceProvider from site object.");
89  }
90  return serviceProvider;
91  }
92  }
93 
94  protected ServiceProvider GlobalServiceProvider
95  {
96  get
97  {
98  if (globalProvider == null)
99  {
100  var siteServiceProvider = this.SiteServiceProvider;
101  if (siteServiceProvider != null)
102  {
103  var service = siteServiceProvider.GetService(typeof(IVsHierarchy)) as IVsHierarchy;
104  if (service != null)
105  {
106  VSOLE.IServiceProvider oleServiceProvider;
107  ErrorHandler.ThrowOnFailure(service.GetSite(out oleServiceProvider));
108  if (oleServiceProvider != null)
109  this.globalProvider = new ServiceProvider(oleServiceProvider);
110  }
111  }
112  }
113  return this.globalProvider;
114  }
115  }
116 
117  /// <summary>
118  /// Method to get a service by its GUID
119  /// </summary>
120  /// <param name="serviceGuid">GUID of service to retrieve</param>
121  /// <returns>An object that implements the requested service</returns>
122  protected object GetService(Guid serviceGuid)
123  {
124  return SiteServiceProvider.GetService(serviceGuid);
125  }
126 
127  /// <summary>
128  /// Method to get a service by its Type
129  /// </summary>
130  /// <param name="serviceType">Type of service to retrieve</param>
131  /// <returns>An object that implements the requested service</returns>
132  protected object GetService(Type serviceType)
133  {
134  return SiteServiceProvider.GetService(serviceType);
135  }
136 
137  /// <summary>
138  /// Returns a CodeDomProvider object for the language of the project containing
139  /// the project item the generator was called on
140  /// </summary>
141  /// <returns>A CodeDomProvider object</returns>
142  protected virtual CodeDomProvider GetCodeProvider()
143  {
144  if (codeDomProvider == null)
145  {
146  //Query for IVSMDCodeDomProvider/SVSMDCodeDomProvider for this project type
147  IVSMDCodeDomProvider provider = GetService(typeof(SVSMDCodeDomProvider)) as IVSMDCodeDomProvider;
148  if (provider != null)
149  {
150  codeDomProvider = provider.CodeDomProvider as CodeDomProvider;
151  }
152  else
153  {
154  //In the case where no language specific CodeDom is available, fall back to C#
155  codeDomProvider = CodeDomProvider.CreateProvider("C#");
156  }
157  }
158  return codeDomProvider;
159  }
160 
161  /// <summary>
162  /// Gets the default extension of the output file from the CodeDomProvider
163  /// </summary>
164  /// <returns></returns>
165  protected override string GetDefaultExtension()
166  {
167  CodeDomProvider codeDom = GetCodeProvider();
168  Debug.Assert(codeDom != null, "CodeDomProvider is NULL.");
169  string extension = codeDom.FileExtension;
170  if (extension != null && extension.Length > 0)
171  {
172  extension = "." + extension.TrimStart(".".ToCharArray());
173  }
174  return extension;
175  }
176 
177  /// <summary>
178  /// Returns the EnvDTE.ProjectItem object that corresponds to the project item the code
179  /// generator was called on
180  /// </summary>
181  /// <returns>The EnvDTE.ProjectItem of the project item the code generator was called on</returns>
183  {
184  object p = GetService(typeof(ProjectItem));
185  Debug.Assert(p != null, "Unable to get Project Item.");
186  return (ProjectItem)p;
187  }
188 
189  /// <summary>
190  /// Returns the EnvDTE.Project object of the project containing the project item the code
191  /// generator was called on
192  /// </summary>
193  /// <returns>
194  /// The EnvDTE.Project object of the project containing the project item the code generator was called on
195  /// </returns>
196  protected Project GetProject()
197  {
198  return GetProjectItem().ContainingProject;
199  }
200 
201  /// <summary>
202  /// Returns the VSLangProj.VSProjectItem object that corresponds to the project item the code
203  /// generator was called on
204  /// </summary>
205  /// <returns>The VSLangProj.VSProjectItem of the project item the code generator was called on</returns>
206  protected VSProjectItem GetVSProjectItem()
207  {
208  return (VSProjectItem)GetProjectItem().Object;
209  }
210 
211  /// <summary>
212  /// Returns the VSLangProj.VSProject object of the project containing the project item the code
213  /// generator was called on
214  /// </summary>
215  /// <returns>
216  /// The VSLangProj.VSProject object of the project containing the project item
217  /// the code generator was called on
218  /// </returns>
219  protected VSProject GetVSProject()
220  {
221  return (VSProject)GetProject().Object;
222  }
223  }
224 }
object GetService(Guid serviceGuid)
Method to get a service by its GUID
ProjectItem GetProjectItem()
Returns the EnvDTE.ProjectItem object that corresponds to the project item the code generator was cal...
Project GetProject()
Returns the EnvDTE.Project object of the project containing the project item the code generator was c...
VSProject GetVSProject()
Returns the VSLangProj.VSProject object of the project containing the project item the code generator...
object GetService(Type serviceType)
Method to get a service by its Type
EnvDTE.Project Project
EnvDTE.ProjectItem ProjectItem
A managed wrapper for VS's concept of an IVsSingleFileGenerator which is a custom tool invoked at des...
VSProjectItem GetVSProjectItem()
Returns the VSLangProj.VSProjectItem object that corresponds to the project item the code generator w...
override string GetDefaultExtension()
Gets the default extension of the output file from the CodeDomProvider
virtual CodeDomProvider GetCodeProvider()
Returns a CodeDomProvider object for the language of the project containing the project item the gene...
Microsoft.VisualStudio.OLE.Interop VSOLE