Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BaseCodeGenerator.cs
Go to the documentation of this file.
1 /***************************************************************************
2 
3 Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
4 Copyright (c) Microsoft Corporation. All rights reserved.
5 This code is licensed under the Visual Studio SDK license terms.
6 THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
7 ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
8 IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
9 PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
10 
11 ***************************************************************************/
12 
13 using System;
14 using System.Diagnostics;
15 using System.IO;
16 using System.Runtime.InteropServices;
17 using Microsoft.VisualStudio;
18 using Microsoft.VisualStudio.Shell.Interop;
19 
20 namespace SiliconStudio.Paradox.VisualStudio.CodeGenerator
21 {
22  /// <summary>
23  /// A managed wrapper for VS's concept of an IVsSingleFileGenerator which is
24  /// a custom tool invoked at design time which can take any file as an input
25  /// and provide any file as output.
26  /// </summary>
27  public abstract class BaseCodeGenerator : IVsSingleFileGenerator
28  {
29  private IVsGeneratorProgress codeGeneratorProgress;
30  private string codeFileNameSpace = String.Empty;
31  private string codeFilePath = String.Empty;
32 
33  #region IVsSingleFileGenerator Members
34 
35  /// <summary>
36  /// Implements the IVsSingleFileGenerator.DefaultExtension method.
37  /// Returns the extension of the generated file
38  /// </summary>
39  /// <param name="pbstrDefaultExtension">Out parameter, will hold the extension that is to be given to the output file name. The returned extension must include a leading period</param>
40  /// <returns>S_OK if successful, E_FAIL if not</returns>
41  int IVsSingleFileGenerator.DefaultExtension(out string pbstrDefaultExtension)
42  {
43  try
44  {
45  pbstrDefaultExtension = GetDefaultExtension();
46  return VSConstants.S_OK;
47  }
48  catch (Exception e)
49  {
50  Trace.WriteLine("The call to GetDefaultExtension() has failed: ");
51  Trace.WriteLine(e.ToString());
52  pbstrDefaultExtension = string.Empty;
53  return VSConstants.E_FAIL;
54  }
55  }
56 
57  /// <summary>
58  /// Implements the IVsSingleFileGenerator.Generate method.
59  /// Executes the transformation and returns the newly generated output file, whenever a custom tool is loaded, or the input file is saved
60  /// </summary>
61  /// <param name="wszInputFilePath">The full path of the input file. May be a null reference (Nothing in Visual Basic) in future releases of Visual Studio, so generators should not rely on this value</param>
62  /// <param name="bstrInputFileContents">The contents of the input file. This is either a UNICODE BSTR (if the input file is text) or a binary BSTR (if the input file is binary). If the input file is a text file, the project system automatically converts the BSTR to UNICODE</param>
63  /// <param name="wszDefaultNamespace">This parameter is meaningful only for custom tools that generate code. It represents the namespace into which the generated code will be placed. If the parameter is not a null reference (Nothing in Visual Basic) and not empty, the custom tool can use the following syntax to enclose the generated code</param>
64  /// <param name="rgbOutputFileContents">[out] Returns an array of bytes to be written to the generated file. You must include UNICODE or UTF-8 signature bytes in the returned byte array, as this is a raw stream. The memory for rgbOutputFileContents must be allocated using the .NET Framework call, System.Runtime.InteropServices.AllocCoTaskMem, or the equivalent Win32 system call, CoTaskMemAlloc. The project system is responsible for freeing this memory</param>
65  /// <param name="pcbOutput">[out] Returns the count of bytes in the rgbOutputFileContent array</param>
66  /// <param name="pGenerateProgress">A reference to the IVsGeneratorProgress interface through which the generator can report its progress to the project system</param>
67  /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns E_FAIL</returns>
68  int IVsSingleFileGenerator.Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace, IntPtr[] rgbOutputFileContents, out uint pcbOutput, IVsGeneratorProgress pGenerateProgress)
69  {
70  if (bstrInputFileContents == null)
71  {
72  throw new ArgumentNullException(bstrInputFileContents);
73  }
74 
75  codeFilePath = wszInputFilePath;
76  codeFileNameSpace = wszDefaultNamespace;
77  codeGeneratorProgress = pGenerateProgress;
78 
79  byte[] bytes = GenerateCode(wszInputFilePath, bstrInputFileContents);
80 
81  if (bytes == null)
82  {
83  // This signals that GenerateCode() has failed. Tasklist items have been put up in GenerateCode()
84  rgbOutputFileContents = null;
85  pcbOutput = 0;
86 
87  // Return E_FAIL to inform Visual Studio that the generator has failed (so that no file gets generated)
88  return VSConstants.E_FAIL;
89  }
90  else
91  {
92  // The contract between IVsSingleFileGenerator implementors and consumers is that
93  // any output returned from IVsSingleFileGenerator.Generate() is returned through
94  // memory allocated via CoTaskMemAlloc(). Therefore, we have to convert the
95  // byte[] array returned from GenerateCode() into an unmanaged blob.
96 
97  int outputLength = bytes.Length;
98  rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(outputLength);
99  Marshal.Copy(bytes, 0, rgbOutputFileContents[0], outputLength);
100  pcbOutput = (uint)outputLength;
101  return VSConstants.S_OK;
102  }
103  }
104 
105  #endregion
106 
107  /// <summary>
108  /// Namespace for the file
109  /// </summary>
110  protected string FileNameSpace
111  {
112  get
113  {
114  return codeFileNameSpace;
115  }
116  }
117 
118  /// <summary>
119  /// File-path for the input file
120  /// </summary>
121  protected string InputFilePath
122  {
123  get
124  {
125  return codeFilePath;
126  }
127  }
128 
129  /// <summary>
130  /// Interface to the VS shell object we use to tell our progress while we are generating
131  /// </summary>
132  internal IVsGeneratorProgress CodeGeneratorProgress
133  {
134  get
135  {
136  return codeGeneratorProgress;
137  }
138  }
139 
140  /// <summary>
141  /// Gets the default extension for this generator
142  /// </summary>
143  /// <returns>String with the default extension for this generator</returns>
144  protected abstract string GetDefaultExtension();
145 
146  /// <summary>
147  /// The method that does the actual work of generating code given the input file
148  /// </summary>
149  /// <param name="inputFileContent">File contents as a string</param>
150  /// <returns>The generated code file as a byte-array</returns>
151  protected abstract byte[] GenerateCode(string inputFileName, string inputFileContent);
152 
153  /// <summary>
154  /// Method that will communicate an error via the shell callback mechanism
155  /// </summary>
156  /// <param name="level">Level or severity</param>
157  /// <param name="message">Text displayed to the user</param>
158  /// <param name="line">Line number of error</param>
159  /// <param name="column">Column number of error</param>
160  protected virtual void GeneratorError(uint level, string message, uint line, uint column)
161  {
162  IVsGeneratorProgress progress = CodeGeneratorProgress;
163  if (progress != null)
164  {
165  progress.GeneratorError(0, level, message, line, column);
166  }
167  }
168 
169  /// <summary>
170  /// Method that will communicate a warning via the shell callback mechanism
171  /// </summary>
172  /// <param name="level">Level or severity</param>
173  /// <param name="message">Text displayed to the user</param>
174  /// <param name="line">Line number of warning</param>
175  /// <param name="column">Column number of warning</param>
176  protected virtual void GeneratorWarning(uint level, string message, uint line, uint column)
177  {
178  IVsGeneratorProgress progress = CodeGeneratorProgress;
179  if (progress != null)
180  {
181  progress.GeneratorError(1, level, message, line, column);
182  }
183  }
184  }
185 }
virtual void GeneratorWarning(uint level, string message, uint line, uint column)
Method that will communicate a warning via the shell callback mechanism
virtual void GeneratorError(uint level, string message, uint line, uint column)
Method that will communicate an error via the shell callback mechanism
A managed wrapper for VS's concept of an IVsSingleFileGenerator which is a custom tool invoked at des...