2007-08-10 11:27:13 +02:00
|
|
|
#region --- License ---
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
* See license.txt for license info
|
|
|
|
*/
|
2007-07-23 02:15:18 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
2007-08-01 23:14:39 +02:00
|
|
|
using System.IO;
|
2007-07-23 02:15:18 +02:00
|
|
|
|
2007-08-01 11:27:57 +02:00
|
|
|
namespace Bind.Structures
|
2007-07-23 02:15:18 +02:00
|
|
|
{
|
|
|
|
#region class Enum
|
|
|
|
|
|
|
|
public class Enum
|
|
|
|
{
|
2008-01-03 01:08:49 +01:00
|
|
|
internal static EnumCollection GLEnums = new EnumCollection();
|
|
|
|
internal static EnumCollection AuxEnums = new EnumCollection();
|
2007-08-01 23:14:39 +02:00
|
|
|
|
2007-11-04 16:19:38 +01:00
|
|
|
static StringBuilder translator = new StringBuilder();
|
|
|
|
string _name;
|
|
|
|
static bool enumsLoaded;
|
|
|
|
|
|
|
|
#region Initialize
|
2007-08-01 23:14:39 +02:00
|
|
|
|
2007-09-02 02:40:43 +02:00
|
|
|
internal static void Initialize(string enumFile, string enumextFile, string auxFile)
|
|
|
|
{
|
|
|
|
Initialize(enumFile, enumextFile);
|
|
|
|
|
2008-02-02 01:58:26 +01:00
|
|
|
if (!String.IsNullOrEmpty(auxFile))
|
|
|
|
using (System.IO.StreamReader sr = new System.IO.StreamReader(Path.Combine(Settings.InputPath, auxFile)))
|
|
|
|
{
|
|
|
|
AuxEnums = Bind.MainClass.Generator.ReadEnums(sr);
|
|
|
|
}
|
2007-09-02 02:40:43 +02:00
|
|
|
}
|
|
|
|
|
2007-08-05 01:39:56 +02:00
|
|
|
internal static void Initialize(string enumFile, string enumextFile)
|
2007-08-01 23:14:39 +02:00
|
|
|
{
|
|
|
|
if (!enumsLoaded)
|
|
|
|
{
|
2007-09-02 02:40:43 +02:00
|
|
|
if (!String.IsNullOrEmpty(enumFile))
|
2007-08-01 23:14:39 +02:00
|
|
|
{
|
2007-09-02 02:40:43 +02:00
|
|
|
using (StreamReader sr = Utilities.OpenSpecFile(Settings.InputPath, enumFile))
|
|
|
|
{
|
|
|
|
GLEnums = Bind.MainClass.Generator.ReadEnums(sr);
|
|
|
|
}
|
2007-08-01 23:14:39 +02:00
|
|
|
}
|
|
|
|
|
2007-09-02 02:40:43 +02:00
|
|
|
if (!String.IsNullOrEmpty(enumextFile))
|
2007-08-01 23:14:39 +02:00
|
|
|
{
|
2007-09-02 02:40:43 +02:00
|
|
|
using (StreamReader sr = Utilities.OpenSpecFile(Settings.InputPath, enumextFile))
|
2007-08-01 23:14:39 +02:00
|
|
|
{
|
2007-09-02 02:40:43 +02:00
|
|
|
foreach (Bind.Structures.Enum e in Bind.MainClass.Generator.ReadEnums(sr).Values)
|
|
|
|
{
|
|
|
|
//enums.Add(e.Name, e);
|
|
|
|
Utilities.Merge(GLEnums, e);
|
|
|
|
}
|
2007-08-01 23:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
2008-01-03 01:08:49 +01:00
|
|
|
|
2007-08-01 23:14:39 +02:00
|
|
|
enumsLoaded = true;
|
|
|
|
}
|
|
|
|
}
|
2007-11-04 16:19:38 +01:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructors
|
2007-08-01 23:14:39 +02:00
|
|
|
|
2007-08-01 11:27:57 +02:00
|
|
|
public Enum()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
public Enum(string name)
|
|
|
|
{
|
|
|
|
Name = name;
|
|
|
|
}
|
|
|
|
|
2007-11-04 16:19:38 +01:00
|
|
|
#endregion
|
2007-07-23 02:15:18 +02:00
|
|
|
|
2008-01-03 01:08:49 +01:00
|
|
|
#region public string Name
|
|
|
|
|
2007-07-23 02:15:18 +02:00
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get { return _name; }
|
|
|
|
set { _name = value; }
|
|
|
|
}
|
|
|
|
|
2008-01-03 01:08:49 +01:00
|
|
|
#endregion
|
|
|
|
|
2007-07-23 02:15:18 +02:00
|
|
|
System.Collections.Hashtable _constant_collection = new System.Collections.Hashtable();
|
|
|
|
|
|
|
|
public System.Collections.Hashtable ConstantCollection
|
|
|
|
{
|
|
|
|
get { return _constant_collection; }
|
2007-11-04 16:19:38 +01:00
|
|
|
//set { _constant_collection = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string TranslateName(string name)
|
|
|
|
{
|
2008-02-02 01:58:26 +01:00
|
|
|
if (Utilities.Keywords.Contains(name))
|
|
|
|
return name;
|
|
|
|
|
2007-11-04 16:19:38 +01:00
|
|
|
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)
|
|
|
|
{
|
2008-01-24 10:16:49 +01:00
|
|
|
bool is_after_underscore_or_number = true; // Detect if we just passed a '_' or a number and make the next char
|
2007-11-04 16:19:38 +01:00
|
|
|
// uppercase.
|
2008-01-03 01:08:49 +01:00
|
|
|
bool is_previous_uppercase = false; // Detect if previous character was uppercase, and turn
|
2007-11-04 16:19:38 +01:00
|
|
|
// the current one to lowercase.
|
|
|
|
|
|
|
|
foreach (char c in name)
|
|
|
|
{
|
|
|
|
char char_to_add;
|
2008-01-24 10:16:49 +01:00
|
|
|
if (c == '_')
|
|
|
|
is_after_underscore_or_number = true;
|
|
|
|
else
|
2007-11-04 16:19:38 +01:00
|
|
|
{
|
2008-01-24 10:16:49 +01:00
|
|
|
if (Char.IsDigit(c))
|
|
|
|
is_after_underscore_or_number = true;
|
|
|
|
char_to_add = is_after_underscore_or_number ? Char.ToUpper(c) :
|
2007-11-04 16:19:38 +01:00
|
|
|
is_previous_uppercase ? Char.ToLower(c) : c;
|
|
|
|
is_previous_uppercase = Char.IsUpper(c);
|
|
|
|
translator.Append(char_to_add);
|
2008-01-24 10:16:49 +01:00
|
|
|
is_after_underscore_or_number = false;
|
2007-11-04 16:19:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
translator[0] = Char.ToUpper(translator[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
translator.Append(name);
|
|
|
|
|
|
|
|
translator.Replace("Pname", "PName");
|
2007-11-10 19:33:44 +01:00
|
|
|
translator.Replace("SRgb", "Srgb");
|
2007-11-04 16:19:38 +01:00
|
|
|
return translator.ToString();
|
2007-07-23 02:15:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
2007-08-01 11:27:57 +02:00
|
|
|
sb.AppendLine("public enum " + Name);
|
|
|
|
sb.AppendLine("{");
|
2007-07-23 02:15:18 +02:00
|
|
|
foreach (Constant c in ConstantCollection.Values)
|
|
|
|
{
|
2007-08-01 11:27:57 +02:00
|
|
|
sb.Append(" ");
|
|
|
|
sb.Append(c.ToString());
|
2008-01-03 01:08:49 +01:00
|
|
|
if (!String.IsNullOrEmpty(c.ToString()))
|
|
|
|
sb.AppendLine(",");
|
2007-08-01 11:27:57 +02:00
|
|
|
}
|
2007-09-22 16:59:33 +02:00
|
|
|
sb.Append("}");
|
2007-08-01 11:27:57 +02:00
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region class EnumCollection
|
|
|
|
|
2008-01-03 01:08:49 +01:00
|
|
|
public class EnumCollection : Dictionary<string, Enum>
|
2007-08-01 11:27:57 +02:00
|
|
|
{
|
2007-08-01 23:14:39 +02:00
|
|
|
internal void AddRange(EnumCollection enums)
|
2007-08-01 11:27:57 +02:00
|
|
|
{
|
2007-08-01 23:14:39 +02:00
|
|
|
foreach (Enum e in enums.Values)
|
|
|
|
Utilities.Merge(this, e);
|
|
|
|
}
|
2007-07-23 02:15:18 +02:00
|
|
|
|
2007-08-01 23:14:39 +02:00
|
|
|
internal void Translate()
|
|
|
|
{
|
2008-01-03 01:08:49 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2007-08-01 23:14:39 +02:00
|
|
|
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;
|
2008-01-03 01:08:49 +01:00
|
|
|
break;
|
2007-08-01 23:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-03 01:08:49 +01:00
|
|
|
|
|
|
|
foreach (Enum e in this.Values)
|
|
|
|
foreach (Constant c in e.ConstantCollection.Values)
|
|
|
|
Constant.TranslateConstantWithReference(c, Enum.GLEnums, Enum.AuxEnums);
|
|
|
|
|
2008-01-15 00:41:53 +01:00
|
|
|
if (Settings.DropMultipleTokens)
|
2008-01-03 01:08:49 +01:00
|
|
|
{
|
|
|
|
|
2008-01-15 00:41:53 +01:00
|
|
|
// When there are multiple tokens with the same value but different extension
|
|
|
|
// drop the duplicates. Order of preference: core > ARB > EXT > vendor specific
|
|
|
|
foreach (Enum e in this.Values)
|
|
|
|
{
|
|
|
|
if (e.Name == "All")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
foreach (Constant c in e.ConstantCollection.Values)
|
2008-01-03 01:08:49 +01:00
|
|
|
{
|
2008-01-15 00:41:53 +01:00
|
|
|
foreach (Constant c2 in e.ConstantCollection.Values)
|
2008-01-03 01:08:49 +01:00
|
|
|
{
|
2008-01-15 00:41:53 +01:00
|
|
|
if (c.Name != c2.Name && c.Value == c2.Value)
|
2008-01-03 01:08:49 +01:00
|
|
|
{
|
2008-01-15 00:41:53 +01:00
|
|
|
if (c.Name.Contains(Constant.Translate("TEXTURE_DEFORMATION_BIT_SGIX")) ||
|
|
|
|
c2.Name.Contains(Constant.Translate("TEXTURE_DEFORMATION_BIT_SGIX")))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int prefer = OrderOfPreference(Utilities.GetGL2Extension(c.Name), Utilities.GetGL2Extension(c2.Name));
|
|
|
|
if (prefer == -1)
|
|
|
|
{
|
|
|
|
c2.Name = "";
|
|
|
|
c2.Value = "";
|
|
|
|
}
|
|
|
|
else if (prefer == 1)
|
|
|
|
{
|
|
|
|
c.Name = "";
|
|
|
|
c.Value = "";
|
|
|
|
}
|
2008-01-03 01:08:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-15 00:41:53 +01:00
|
|
|
}
|
2008-01-03 01:08:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return -1 for ext1, 1 for ext2 or 0 if no preference.
|
|
|
|
int OrderOfPreference(string ext1, string ext2)
|
|
|
|
{
|
|
|
|
// If one is empty and the other note, prefer the empty one.
|
|
|
|
// (empty == core)
|
|
|
|
// 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.
|
|
|
|
int ret;
|
|
|
|
ret = OrderOfPreferenceInternal(ext1, ext2);
|
|
|
|
if (ret != 0) return ret;
|
|
|
|
ext1 = ext1.Replace("Arb", ""); ext2 = ext2.Replace("Arb", "");
|
|
|
|
ret = OrderOfPreferenceInternal(ext1, ext2);
|
|
|
|
if (ret != 0) return ret;
|
|
|
|
ext1 = ext1.Replace("Ext", ""); ext2 = ext2.Replace("Ext", "");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer the empty string over the non-empty.
|
|
|
|
int OrderOfPreferenceInternal(string ext1, string ext2)
|
|
|
|
{
|
|
|
|
if (String.IsNullOrEmpty(ext1) && !String.IsNullOrEmpty(ext2))
|
|
|
|
return -1;
|
|
|
|
else if (String.IsNullOrEmpty(ext2) && !String.IsNullOrEmpty(ext1))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2007-11-04 16:19:38 +01:00
|
|
|
}
|
2007-10-21 17:48:52 +02:00
|
|
|
|
2007-11-04 16:19:38 +01:00
|
|
|
new bool TryGetValue(string key, out Enum value)
|
|
|
|
{
|
|
|
|
return base.TryGetValue(key, out value);
|
2007-07-23 02:15:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|