Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GlslKeywords.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.IO;
6 using System.Text.RegularExpressions;
7 using SiliconStudio.Shaders.Properties;
8 
9 namespace SiliconStudio.Shaders.Convertor
10 {
11  /// <summary>
12  /// GlslKeywords
13  /// </summary>
14  public class GlslKeywords
15  {
16  /// <summary>
17  /// Name of the default keywords.glsl file
18  /// </summary>
19  private const string KeywordsFileName = "keywords.glsl";
20 
21  /// <summary>
22  /// Regex to remove pseudo C++ comments
23  /// </summary>
24  private static readonly Regex StripComments = new Regex("//.*");
25 
26  /// <summary>
27  /// Regsitered tokens
28  /// </summary>
29  private static readonly HashSet<string> Tokens = new HashSet<string>();
30 
31  /// <summary>
32  /// Initializes the <see cref="GlslKeywords"/> class by loading the keywords file.
33  /// </summary>
34  /// <remarks>
35  /// It tries to load the "keywords.glsl" file from the same directory than this assembly (allowing overrides)
36  /// else it loads it from an internal resource of this assembly.
37  /// </remarks>
38  static GlslKeywords()
39  {
40  Stream stream = null;
41  try
42  {
43  // Try to load from the
44  var keywordFilePath = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(typeof (GlslKeywords).Assembly.Location)), KeywordsFileName);
45  if (File.Exists(keywordFilePath))
46  stream = new FileStream(keywordFilePath, FileMode.Open, FileAccess.Read);
47 
48  if (stream == null) stream = new MemoryStream(Resources.Keywords);
49 
50  InitializeFromStream(stream);
51  } catch(Exception ex)
52  {
53  Console.WriteLine("Unable to load keywords.glsl file. Reason: " + ex);
54  } finally
55  {
56  if (stream != null)
57  try { stream.Close(); } catch {}
58  }
59  }
60 
61  /// <summary>
62  /// Initializes the tokens from a stream.
63  /// </summary>
64  /// <param name="stream">The stream.</param>
65  private static void InitializeFromStream(Stream stream)
66  {
67  if (stream == null)
68  return;
69 
70  var reader = new StreamReader(stream);
71  string line;
72  while ( (line = reader.ReadLine()) != null)
73  {
74  var newTokens = StripComments.Replace(line, "").Trim().Split((string[])null, StringSplitOptions.RemoveEmptyEntries);
75  foreach (var newToken in newTokens)
76  Tokens.Add(newToken);
77  }
78  }
79 
80  /// <summary>
81  /// Determines whether the specified identifier is a glsl reserved keyword.
82  /// </summary>
83  /// <param name="identifier">A glsl identifier.</param>
84  /// <returns>
85  /// <c>true</c> if the specified identifier is a glsl reserved keyword; otherwise, <c>false</c>.
86  /// </returns>
87  public static bool IsReserved(string identifier)
88  {
89  return Tokens.Contains(identifier);
90 
91  }
92  }
93 }
System.IO.FileMode FileMode
Definition: ScriptSync.cs:33
static bool IsReserved(string identifier)
Determines whether the specified identifier is a glsl reserved keyword.
Definition: GlslKeywords.cs:87
System.IO.File File
Tokens
Summary Canonical example of MPLEX automaton