2009-02-22 11:43:35 +01:00
|
|
|
#region --- License ---
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
* See license.txt for license info
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using System.IO;
|
2009-05-05 23:00:48 +02:00
|
|
|
using System.Globalization;
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
namespace Bind.Structures
|
|
|
|
{
|
|
|
|
#region class Enum
|
|
|
|
|
|
|
|
public class Enum
|
|
|
|
{
|
|
|
|
internal static EnumCollection GLEnums = new EnumCollection();
|
|
|
|
internal static EnumCollection AuxEnums = new EnumCollection();
|
|
|
|
|
|
|
|
static StringBuilder translator = new StringBuilder();
|
|
|
|
string _name;
|
|
|
|
static bool enumsLoaded;
|
|
|
|
|
|
|
|
#region Initialize
|
|
|
|
|
|
|
|
internal static void Initialize(string enumFile, string enumextFile, string auxFile)
|
|
|
|
{
|
|
|
|
Initialize(enumFile, enumextFile);
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(auxFile))
|
|
|
|
using (System.IO.StreamReader sr = new System.IO.StreamReader(Path.Combine(Settings.InputPath, auxFile)))
|
|
|
|
{
|
|
|
|
AuxEnums = Bind.MainClass.Generator.ReadEnums(sr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void Initialize(string enumFile, string enumextFile)
|
|
|
|
{
|
|
|
|
if (!enumsLoaded)
|
|
|
|
{
|
|
|
|
if (!String.IsNullOrEmpty(enumFile))
|
|
|
|
{
|
|
|
|
using (StreamReader sr = Utilities.OpenSpecFile(Settings.InputPath, enumFile))
|
|
|
|
{
|
|
|
|
GLEnums = Bind.MainClass.Generator.ReadEnums(sr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(enumextFile))
|
|
|
|
{
|
|
|
|
using (StreamReader sr = Utilities.OpenSpecFile(Settings.InputPath, enumextFile))
|
|
|
|
{
|
|
|
|
foreach (Bind.Structures.Enum e in Bind.MainClass.Generator.ReadEnums(sr).Values)
|
|
|
|
{
|
|
|
|
//enums.Add(e.Name, e);
|
|
|
|
Utilities.Merge(GLEnums, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enumsLoaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
public Enum()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
public Enum(string name)
|
|
|
|
{
|
|
|
|
Name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-05-05 23:00:48 +02:00
|
|
|
#region Public Members
|
|
|
|
|
|
|
|
// Returns true if the enum contains a collection of flags, i.e. 1, 2, 4, 8, ...
|
|
|
|
public bool IsFlagCollection
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
// It seems that all flag collections contain "Mask" in their names.
|
|
|
|
// This looks like a heuristic, but it holds 100% in practice
|
|
|
|
// (checked all enums to make sure).
|
|
|
|
return Name.Contains("Mask");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
#region public string Name
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get { return _name; }
|
|
|
|
set { _name = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-05-05 23:00:48 +02:00
|
|
|
#region ConstantCollection
|
|
|
|
|
2009-02-28 19:50:16 +01:00
|
|
|
Dictionary<string, Constant> _constant_collection = new Dictionary<string, Constant>();
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2009-02-28 19:50:16 +01:00
|
|
|
public IDictionary<string, Constant> ConstantCollection
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
|
|
|
get { return _constant_collection; }
|
|
|
|
}
|
|
|
|
|
2009-05-05 23:00:48 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region TranslateName
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
public static string TranslateName(string name)
|
|
|
|
{
|
|
|
|
if (Utilities.Keywords.Contains(name))
|
|
|
|
return name;
|
|
|
|
|
|
|
|
translator.Remove(0, translator.Length); // Trick to avoid allocating a new StringBuilder.
|
|
|
|
|
|
|
|
// Translate the constant's name to match .Net naming conventions
|
|
|
|
if ((Settings.Compatibility & Settings.Legacy.NoAdvancedEnumProcessing) == Settings.Legacy.None)
|
|
|
|
{
|
|
|
|
bool is_after_underscore_or_number = true; // Detect if we just passed a '_' or a number and make the next char
|
|
|
|
// uppercase.
|
|
|
|
bool is_previous_uppercase = false; // Detect if previous character was uppercase, and turn
|
|
|
|
// the current one to lowercase.
|
|
|
|
|
|
|
|
foreach (char c in name)
|
|
|
|
{
|
|
|
|
char char_to_add;
|
2009-02-28 20:29:34 +01:00
|
|
|
if (c == '_' || c == '-')
|
2009-02-22 11:43:35 +01:00
|
|
|
is_after_underscore_or_number = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Char.IsDigit(c))
|
|
|
|
is_after_underscore_or_number = true;
|
|
|
|
char_to_add = is_after_underscore_or_number ? Char.ToUpper(c) :
|
|
|
|
is_previous_uppercase ? Char.ToLower(c) : c;
|
|
|
|
is_previous_uppercase = Char.IsUpper(c);
|
|
|
|
translator.Append(char_to_add);
|
|
|
|
is_after_underscore_or_number = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
translator[0] = Char.ToUpper(translator[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
translator.Append(name);
|
|
|
|
|
|
|
|
translator.Replace("Pname", "PName");
|
|
|
|
translator.Replace("SRgb", "Srgb");
|
|
|
|
return translator.ToString();
|
|
|
|
}
|
|
|
|
|
2009-05-05 23:00:48 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ToString
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2009-02-28 19:50:16 +01:00
|
|
|
List<Constant> constants = new List<Constant>(ConstantCollection.Values);
|
|
|
|
constants.Sort(delegate(Constant c1, Constant c2)
|
|
|
|
{
|
|
|
|
int ret = String.Compare(c1.Value, c2.Value);
|
|
|
|
if (ret == 0)
|
|
|
|
return String.Compare(c1.Name, c2.Name);
|
|
|
|
return ret;
|
|
|
|
});
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2009-05-05 23:00:48 +02:00
|
|
|
if (IsFlagCollection)
|
|
|
|
sb.AppendLine("[Flags]");
|
2009-02-22 11:43:35 +01:00
|
|
|
sb.AppendLine("public enum " + Name);
|
|
|
|
sb.AppendLine("{");
|
2009-02-28 19:50:16 +01:00
|
|
|
|
|
|
|
foreach (Constant c in constants)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
|
|
|
sb.Append(" ");
|
|
|
|
sb.Append(c.ToString());
|
|
|
|
if (!String.IsNullOrEmpty(c.ToString()))
|
|
|
|
sb.AppendLine(",");
|
|
|
|
}
|
|
|
|
sb.Append("}");
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
2009-05-05 23:00:48 +02:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region class EnumCollection
|
|
|
|
|
2009-05-29 17:57:01 +02:00
|
|
|
public class EnumCollection : SortedDictionary<string, Enum>
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
|
|
|
internal void AddRange(EnumCollection enums)
|
|
|
|
{
|
|
|
|
foreach (Enum e in enums.Values)
|
|
|
|
Utilities.Merge(this, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void Translate()
|
|
|
|
{
|
|
|
|
// Translate enum names.
|
|
|
|
{
|
|
|
|
List<string> keys_to_update = new List<string>();
|
|
|
|
foreach (Enum e in this.Values)
|
|
|
|
{
|
|
|
|
string name = Enum.TranslateName(e.Name);
|
|
|
|
if (name != e.Name)
|
|
|
|
{
|
|
|
|
keys_to_update.Add(e.Name);
|
|
|
|
e.Name = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (string name in keys_to_update)
|
|
|
|
{
|
|
|
|
Enum e = this[name];
|
|
|
|
this.Remove(name);
|
|
|
|
this.Add(e.Name, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
keys_to_update = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Enum e in this.Values)
|
|
|
|
{
|
|
|
|
foreach (Constant c in e.ConstantCollection.Values)
|
|
|
|
{
|
|
|
|
// There are cases when a value is an aliased constant, with no enum specified.
|
|
|
|
// (e.g. FOG_COORD_ARRAY_TYPE = GL_FOG_COORDINATE_ARRAY_TYPE)
|
|
|
|
// In this case try searching all enums for the correct constant to alias (stupid opengl specs).
|
|
|
|
if (String.IsNullOrEmpty(c.Reference) && !Char.IsDigit(c.Value[0]))
|
|
|
|
{
|
|
|
|
foreach (Enum @enum in this.Values)
|
|
|
|
{
|
|
|
|
// Skip generic GLenum
|
|
|
|
if (@enum.Name == "GLenum")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (@enum.ConstantCollection.ContainsKey(c.Value))
|
|
|
|
{
|
|
|
|
c.Reference = @enum.Name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Enum e in this.Values)
|
2009-05-29 17:57:01 +02:00
|
|
|
{
|
|
|
|
restart:
|
2009-02-22 11:43:35 +01:00
|
|
|
foreach (Constant c in e.ConstantCollection.Values)
|
2009-05-29 17:57:01 +02:00
|
|
|
{
|
|
|
|
bool result = Constant.TranslateConstantWithReference(c, Enum.GLEnums, Enum.AuxEnums);
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
e.ConstantCollection.Remove(c.Name);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
if (Settings.DropMultipleTokens)
|
|
|
|
{
|
|
|
|
// When there are multiple tokens with the same value but different extension
|
|
|
|
// drop the duplicates. Order of preference: core > ARB > EXT > vendor specific
|
2009-02-28 19:50:16 +01:00
|
|
|
|
|
|
|
List<Constant> removed_tokens = new List<Constant>();
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
foreach (Enum e in this.Values)
|
|
|
|
{
|
|
|
|
if (e.Name == "All")
|
|
|
|
continue;
|
|
|
|
|
2009-02-28 19:50:16 +01:00
|
|
|
// This implementation is a not very bright O(n^2).
|
2009-02-22 11:43:35 +01:00
|
|
|
foreach (Constant c in e.ConstantCollection.Values)
|
|
|
|
{
|
|
|
|
foreach (Constant c2 in e.ConstantCollection.Values)
|
|
|
|
{
|
|
|
|
if (c.Name != c2.Name && c.Value == c2.Value)
|
|
|
|
{
|
|
|
|
int prefer = OrderOfPreference(Utilities.GetGL2Extension(c.Name), Utilities.GetGL2Extension(c2.Name));
|
|
|
|
if (prefer == -1)
|
2009-02-28 19:50:16 +01:00
|
|
|
removed_tokens.Add(c2);
|
2009-02-22 11:43:35 +01:00
|
|
|
else if (prefer == 1)
|
2009-02-28 19:50:16 +01:00
|
|
|
removed_tokens.Add(c);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-28 19:50:16 +01:00
|
|
|
|
|
|
|
foreach (Constant c in removed_tokens)
|
|
|
|
e.ConstantCollection.Remove(c.Name);
|
|
|
|
removed_tokens.Clear();
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return -1 for ext1, 1 for ext2 or 0 if no preference.
|
|
|
|
int OrderOfPreference(string ext1, string ext2)
|
|
|
|
{
|
2009-02-28 19:50:16 +01:00
|
|
|
// If one is empty and the other not, prefer the empty one (empty == core)
|
2009-02-22 11:43:35 +01:00
|
|
|
// Otherwise check for Arb and Ext. To reuse the logic for the
|
|
|
|
// empty check, let's try to remove first Arb, then Ext from the strings.
|
2009-02-28 19:50:16 +01:00
|
|
|
int ret = PreferEmpty(ext1, ext2);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
ext1 = ext1.Replace("Arb", ""); ext2 = ext2.Replace("Arb", "");
|
2009-02-28 19:50:16 +01:00
|
|
|
ret = PreferEmpty(ext1, ext2);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
ext1 = ext1.Replace("Ext", ""); ext2 = ext2.Replace("Ext", "");
|
2009-02-28 19:50:16 +01:00
|
|
|
return PreferEmpty(ext1, ext2);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer the empty string over the non-empty.
|
2009-02-28 19:50:16 +01:00
|
|
|
int PreferEmpty(string ext1, string ext2)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
|
|
|
if (String.IsNullOrEmpty(ext1) && !String.IsNullOrEmpty(ext2))
|
|
|
|
return -1;
|
|
|
|
else if (String.IsNullOrEmpty(ext2) && !String.IsNullOrEmpty(ext1))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
new bool TryGetValue(string key, out Enum value)
|
|
|
|
{
|
|
|
|
return base.TryGetValue(key, out value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|