Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ProjectTemplatingHost.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.CodeDom.Compiler;
5 using System.Collections.Generic;
6 using System.Dynamic;
7 using System.IO;
8 using System.Linq;
9 using System.Reflection;
10 using System.Text;
11 using Microsoft.CSharp.RuntimeBinder;
12 using Microsoft.VisualStudio.TextTemplating;
13 using SiliconStudio.Core;
14 using SiliconStudio.Core.Diagnostics;
15 using SiliconStudio.Core.IO;
16 
17 namespace SiliconStudio.ProjectTemplating
18 {
19  internal class ProjectTemplatingHost : Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost, ITextTemplatingSessionHost
20  {
21  public ProjectTemplatingHost(ILogger log, string templateFile, string rootDirectory, ExpandoObject expando, IEnumerable<string> assemblies)
22  {
23  if (log == null) throw new ArgumentNullException("log");
24  if (templateFile == null) throw new ArgumentNullException("templateFile");
25  if (rootDirectory == null) throw new ArgumentNullException("rootDirectory");
26  if (expando == null) throw new ArgumentNullException("expando");
27  this.log = log;
28  this.TemplateFile = templateFile;
29  this.rootDirectory = rootDirectory;
30 
31  Session = new CustomTemplatingSession(expando);
32 
33  var assembliesToLoad = new List<string>()
34  {
35  "System.Core",
36  typeof(RuntimeBinderException).Assembly.FullName,
37  "Mono.TextTemplating",
38  typeof(PlatformType).Assembly.FullName,
39  typeof(UPath).Assembly.FullName,
40  "SiliconStudio.ProjectTemplating"
41  };
42  assembliesToLoad.AddRange(assemblies);
43  StandardAssemblyReferences = assembliesToLoad;
44 
45  StandardImports = new List<string>()
46  {
47  "System.Linq",
48  "System.Text",
49  "System.Collections.Generic",
50  "System.Dynamic",
51  "SiliconStudio.ProjectTemplating"
52  };
53  }
54 
55  public IList<string> StandardAssemblyReferences { get; private set; }
56 
57  public IList<string> StandardImports { get; private set; }
58 
59  public string TemplateFile { get; set; }
60 
61  private readonly string rootDirectory;
62 
63  private readonly ILogger log;
64 
65  public object GetHostOption(string optionName)
66  {
67  object value = null;
68  if (Session != null)
69  {
70  Session.TryGetValue(optionName, out value);
71  }
72  return value;
73  }
74 
75  public bool LoadIncludeText(string requestFileName, out string content, out string location)
76  {
77  content = null;
78  location = Path.Combine(rootDirectory, requestFileName);
79  if (File.Exists(location))
80  {
81  content = File.ReadAllText(location);
82  return true;
83  }
84  return false;
85  }
86 
87  public void LogErrors(CompilerErrorCollection errors)
88  {
89  for (int i = 0; i < errors.Count; i++)
90  {
91  var error = errors[i];
92  // error.FileName can be null resulting in an exception in error.ToString()
93  var msg = error.FileName == null ? error.ErrorText : error.ToString();
94  log.Error(msg);
95  }
96  }
97 
98  public AppDomain ProvideTemplatingAppDomain(string content)
99  {
100  return AppDomain.CurrentDomain;
101  }
102 
103  public string ResolveAssemblyReference(string assemblyReference)
104  {
105  var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name.Equals(assemblyReference, StringComparison.Ordinal) || x.GetName().FullName.Equals(assemblyReference, StringComparison.Ordinal));
106  if (loadedAssembly != null)
107  return loadedAssembly.Location;
108 
109  try
110  {
111 
112 
113  Assembly assembly = Assembly.Load(assemblyReference);
114  if (assembly != null)
115  {
116  return assembly.Location;
117  }
118  }
119  catch (FileNotFoundException) { }
120  catch (FileLoadException) { }
121  catch (BadImageFormatException) { }
122 
123  return null;
124  }
125 
126  public Type ResolveDirectiveProcessor(string processorName)
127  {
128  throw new NotImplementedException();
129  }
130 
131  public string ResolveParameterValue(string directiveId, string processorName, string parameterName)
132  {
133  throw new NotImplementedException();
134  }
135 
136  public string ResolvePath(string path)
137  {
138  throw new NotImplementedException();
139  }
140 
141  public void SetFileExtension(string extension)
142  {
143  }
144 
145  public void SetOutputEncoding(Encoding encoding, bool fromOutputDirective)
146  {
147  }
148 
149  public ITextTemplatingSession CreateSession()
150  {
151  throw new NotImplementedException();
152  }
153 
154  public ITextTemplatingSession Session { get; set; }
155  }
156 }
PlatformType
Describes the platform operating system.
Definition: PlatformType.cs:9
System.Text.Encoding Encoding
System.IO.File File
Base class that describes a uniform path and provides method to manipulate them. Concrete class are U...
Definition: UPath.cs:21
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.
The template can be applied to an existing PackageSession.
Interface for logging.
Definition: ILogger.cs:8