2011-12-02 12:27:04 +01:00
|
|
|
|
#region License
|
|
|
|
|
//
|
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2006 - 2011 the Open Toolkit library.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
//
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Bind.Structures;
|
|
|
|
|
|
|
|
|
|
namespace Bind
|
|
|
|
|
{
|
|
|
|
|
using Delegate = Bind.Structures.Delegate;
|
|
|
|
|
using Enum = Bind.Structures.Enum;
|
|
|
|
|
using Type = Bind.Structures.Type;
|
|
|
|
|
|
|
|
|
|
sealed class JavaSpecWriter : ISpecWriter
|
|
|
|
|
{
|
|
|
|
|
readonly char[] numbers = "0123456789".ToCharArray();
|
|
|
|
|
const string DigitPrefix = "T"; // Prefix for identifiers that start with a digit
|
|
|
|
|
const string OutputFileHeader = "GL.java";
|
|
|
|
|
|
|
|
|
|
BindStreamWriter sw_h = new BindStreamWriter(Path.GetTempFileName());
|
|
|
|
|
|
|
|
|
|
#region WriteBindings
|
|
|
|
|
|
|
|
|
|
public void WriteBindings(IBind generator)
|
|
|
|
|
{
|
|
|
|
|
WriteBindings(generator.Delegates, generator.Wrappers, generator.Enums);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteBindings(DelegateCollection delegates, FunctionCollection wrappers, EnumCollection enums)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Writing bindings to {0}", Settings.OutputPath);
|
|
|
|
|
if (!Directory.Exists(Settings.OutputPath))
|
|
|
|
|
Directory.CreateDirectory(Settings.OutputPath);
|
|
|
|
|
|
|
|
|
|
// Hack: Fix 3dfx extension category so it doesn't start with a digit
|
|
|
|
|
if (wrappers.ContainsKey("3dfx"))
|
|
|
|
|
{
|
|
|
|
|
var three_dee_fx = wrappers["3dfx"];
|
|
|
|
|
wrappers.Remove("3dfx");
|
|
|
|
|
wrappers.Add(DigitPrefix + "3dfx", three_dee_fx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var sw = sw_h)
|
|
|
|
|
{
|
|
|
|
|
WriteLicense(sw);
|
|
|
|
|
|
2011-12-05 12:54:06 +01:00
|
|
|
|
sw.WriteLine("package {0}.{1};", Settings.OutputNamespace, Settings.GLClass);
|
|
|
|
|
sw.WriteLine();
|
2011-12-02 12:27:04 +01:00
|
|
|
|
|
|
|
|
|
WriteDefinitions(sw, enums, wrappers, Type.CSTypes);
|
|
|
|
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
sw.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string output_header = Path.Combine(Settings.OutputPath, OutputFileHeader);
|
|
|
|
|
Move(sw_h.File, output_header);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Move(string file, string dest)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(dest))
|
|
|
|
|
File.Delete(dest);
|
|
|
|
|
File.Move(file, dest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region WriteDefinitions
|
|
|
|
|
|
|
|
|
|
void WriteDefinitions(BindStreamWriter sw,
|
2011-12-05 13:27:23 +01:00
|
|
|
|
EnumCollection enums, FunctionCollection wrappers,
|
|
|
|
|
Dictionary<string, string> CSTypes)
|
2011-12-02 12:27:04 +01:00
|
|
|
|
{
|
2011-12-05 12:54:06 +01:00
|
|
|
|
sw.WriteLine("public class {0}", Settings.GLClass);
|
2011-12-02 12:27:04 +01:00
|
|
|
|
sw.WriteLine("{");
|
|
|
|
|
sw.Indent();
|
|
|
|
|
|
|
|
|
|
foreach (string extension in wrappers.Keys)
|
|
|
|
|
{
|
|
|
|
|
if (extension != "Core")
|
|
|
|
|
{
|
2011-12-05 12:54:06 +01:00
|
|
|
|
sw.WriteLine("public static class {0}", extension);
|
2011-12-02 12:27:04 +01:00
|
|
|
|
sw.WriteLine("{");
|
|
|
|
|
sw.Indent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write wrappers
|
|
|
|
|
foreach (var f in wrappers[extension])
|
|
|
|
|
{
|
|
|
|
|
WriteWrapper(f, sw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (extension != "Core")
|
|
|
|
|
{
|
|
|
|
|
sw.Unindent();
|
2011-12-05 12:54:06 +01:00
|
|
|
|
sw.WriteLine("}");
|
2011-12-02 12:27:04 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-05 16:48:51 +01:00
|
|
|
|
WriteEnums(sw, enums);
|
|
|
|
|
|
2011-12-02 12:27:04 +01:00
|
|
|
|
sw.Unindent();
|
|
|
|
|
sw.WriteLine("}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region WriteEnums
|
|
|
|
|
|
|
|
|
|
public void WriteEnums(BindStreamWriter sw, EnumCollection enums)
|
|
|
|
|
{
|
|
|
|
|
foreach (Enum @enum in enums.Values)
|
|
|
|
|
{
|
|
|
|
|
sw.WriteLine("public enum {0}", @enum.Name);
|
|
|
|
|
sw.WriteLine("{");
|
|
|
|
|
sw.Indent();
|
2011-12-05 16:48:51 +01:00
|
|
|
|
int count = @enum.ConstantCollection.Values.Count;
|
2011-12-02 12:27:04 +01:00
|
|
|
|
foreach (var c in @enum.ConstantCollection.Values)
|
|
|
|
|
{
|
2011-12-05 16:48:51 +01:00
|
|
|
|
sw.WriteLine(String.Format("{0}({1}{2}){3}",
|
2011-12-05 12:54:06 +01:00
|
|
|
|
c.Name,
|
|
|
|
|
!String.IsNullOrEmpty(c.Reference) ? (c.Reference + Settings.NamespaceSeparator) : "",
|
2011-12-05 16:48:51 +01:00
|
|
|
|
!String.IsNullOrEmpty(c.Reference) ? c.Value : c.Value.ToLower(),
|
|
|
|
|
--count == 0 ? ";" : ","));
|
2011-12-02 12:27:04 +01:00
|
|
|
|
}
|
2011-12-05 16:48:51 +01:00
|
|
|
|
sw.WriteLine();
|
|
|
|
|
sw.WriteLine("{0} mValue;", @enum.Type);
|
|
|
|
|
sw.WriteLine("{0}({1} value) {{ mValue = value; }}", @enum.Name, @enum.Type);
|
2011-12-02 12:27:04 +01:00
|
|
|
|
sw.Unindent();
|
|
|
|
|
sw.WriteLine("}");
|
|
|
|
|
sw.WriteLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region WriteWrappers
|
|
|
|
|
|
2011-12-05 13:27:23 +01:00
|
|
|
|
static void WriteWrapper(Function f, BindStreamWriter sw)
|
|
|
|
|
{
|
2011-12-05 14:03:48 +01:00
|
|
|
|
var valid = true;
|
2011-12-05 14:28:17 +01:00
|
|
|
|
var generic_parameters = GenerateGenericTypeString(f);
|
2011-12-05 14:03:48 +01:00
|
|
|
|
var parameters = GenerateParameterString(f, out valid);
|
|
|
|
|
if (!valid)
|
|
|
|
|
return;
|
2011-12-05 13:27:23 +01:00
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(generic_parameters))
|
2011-12-05 14:03:48 +01:00
|
|
|
|
sw.WriteLine("public static <{0}> {1} {2}({3})", generic_parameters,
|
|
|
|
|
f.ReturnType, f.TrimmedName, parameters);
|
2011-12-05 13:27:23 +01:00
|
|
|
|
else
|
|
|
|
|
sw.WriteLine("public static {0} {1}({2})", f.ReturnType, f.TrimmedName,
|
|
|
|
|
parameters);
|
|
|
|
|
|
|
|
|
|
sw.WriteLine("{");
|
|
|
|
|
sw.Indent();
|
|
|
|
|
WriteMethodBody(sw, f);
|
|
|
|
|
sw.Unindent();
|
|
|
|
|
sw.WriteLine("}");
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-02 12:27:04 +01:00
|
|
|
|
static void WriteMethodBody(BindStreamWriter sw, Function f)
|
|
|
|
|
{
|
2011-12-05 12:54:06 +01:00
|
|
|
|
//var callstring = f.Parameters.CallString();
|
|
|
|
|
//if (f.ReturnType != null && !f.ReturnType.ToString().ToLower().Contains("void"))
|
|
|
|
|
// sw.WriteLine("return GLES20.{0}{1};", f.WrappedDelegate.Name, callstring);
|
|
|
|
|
//else
|
|
|
|
|
// sw.WriteLine("GLES20.{0}{1};", f.WrappedDelegate.Name, callstring);
|
2011-12-02 12:27:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-12-05 14:03:48 +01:00
|
|
|
|
#region GenerateParameterString
|
|
|
|
|
|
|
|
|
|
static string GenerateParameterString(Function f, out bool valid)
|
|
|
|
|
{
|
|
|
|
|
if (f == null)
|
|
|
|
|
throw new ArgumentNullException("f");
|
|
|
|
|
|
|
|
|
|
valid = true;
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
2011-12-05 16:48:51 +01:00
|
|
|
|
if (f.TrimmedName == "ExtGetBufferPointer")
|
|
|
|
|
;// Debugger.Break();
|
|
|
|
|
|
2011-12-05 14:03:48 +01:00
|
|
|
|
if (f.Parameters.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var p in f.Parameters)
|
|
|
|
|
{
|
|
|
|
|
if (p.Reference)
|
|
|
|
|
{
|
2011-12-05 16:48:51 +01:00
|
|
|
|
// Use a boxed type instead of primitives (i.e. "Byte" rather than "byte"), since
|
|
|
|
|
// the former are reference types. We don't need to do anything for regular reference
|
|
|
|
|
// types.
|
|
|
|
|
// Hack: we do this by upper-casing the first letter of the type. This should work for
|
|
|
|
|
// all primitive types, but won't work for enums and other reference types. In these
|
|
|
|
|
// cases, we'll just ignore the reference overload.
|
|
|
|
|
if (Char.IsLower(p.CurrentType[0]))
|
|
|
|
|
{
|
|
|
|
|
// Hack: Int -> Integer and Bool -> Boolean
|
|
|
|
|
if (p.CurrentType == "int")
|
|
|
|
|
sb.Append("Integer");
|
|
|
|
|
else if (p.CurrentType == "bool")
|
|
|
|
|
sb.Append("Boolean");
|
|
|
|
|
else
|
|
|
|
|
sb.Append(Char.ToUpper(p.CurrentType[0]) + p.CurrentType.Substring(1));
|
|
|
|
|
}
|
2011-12-05 14:03:48 +01:00
|
|
|
|
else
|
2011-12-05 16:48:51 +01:00
|
|
|
|
{
|
|
|
|
|
valid = false;
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
2011-12-05 14:03:48 +01:00
|
|
|
|
}
|
2011-12-05 16:48:51 +01:00
|
|
|
|
else if (p.Array > 0)
|
2011-12-05 14:03:48 +01:00
|
|
|
|
{
|
|
|
|
|
sb.Append(p.CurrentType);
|
2011-12-05 16:48:51 +01:00
|
|
|
|
for (int i = 0; i < p.Array; i++)
|
2011-12-05 14:03:48 +01:00
|
|
|
|
sb.Append("[]");
|
|
|
|
|
}
|
|
|
|
|
else if (p.Pointer > 0)
|
|
|
|
|
{
|
|
|
|
|
// Java does not support pointers
|
|
|
|
|
// Todo: maybe use one of the java.nio.* pointer classes?
|
|
|
|
|
valid = false;
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sb.Append(p.CurrentType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.Append(" ");
|
|
|
|
|
sb.Append(p.Name);
|
|
|
|
|
sb.Append(", ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (f.Parameters.Count > 0)
|
|
|
|
|
sb.Remove(sb.Length - 2, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2011-12-05 14:28:17 +01:00
|
|
|
|
#region GenerateGenericTypeString
|
2011-12-05 14:03:48 +01:00
|
|
|
|
|
2011-12-05 14:28:17 +01:00
|
|
|
|
static string GenerateGenericTypeString(Function f)
|
2011-12-05 14:03:48 +01:00
|
|
|
|
{
|
|
|
|
|
var parameters = f.Parameters.Where(p => p.Generic);
|
|
|
|
|
if (parameters.Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
foreach (var p in f.Parameters.Where(p => p.Generic))
|
|
|
|
|
{
|
|
|
|
|
sb.Append(p.CurrentType);
|
|
|
|
|
sb.Append(", ");
|
|
|
|
|
}
|
|
|
|
|
if (parameters.Count() > 0)
|
|
|
|
|
sb.Remove(sb.Length - 2, 2);
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2011-12-02 12:27:04 +01:00
|
|
|
|
static DocProcessor processor = new DocProcessor(Path.Combine(Settings.DocPath, Settings.DocFile));
|
|
|
|
|
static Dictionary<string, string> docfiles;
|
|
|
|
|
void WriteDocumentation(BindStreamWriter sw, Function f)
|
|
|
|
|
{
|
|
|
|
|
if (docfiles == null)
|
|
|
|
|
{
|
|
|
|
|
docfiles = new Dictionary<string, string>();
|
|
|
|
|
foreach (string file in Directory.GetFiles(Settings.DocPath))
|
|
|
|
|
{
|
|
|
|
|
docfiles.Add(Path.GetFileName(file), file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string docfile = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
docfile = Settings.FunctionPrefix + f.WrappedDelegate.Name + ".xml";
|
|
|
|
|
if (!docfiles.ContainsKey(docfile))
|
|
|
|
|
docfile = Settings.FunctionPrefix + f.TrimmedName + ".xml";
|
|
|
|
|
if (!docfiles.ContainsKey(docfile))
|
|
|
|
|
docfile = Settings.FunctionPrefix + f.TrimmedName.TrimEnd(numbers) + ".xml";
|
|
|
|
|
|
|
|
|
|
string doc = null;
|
|
|
|
|
if (docfiles.ContainsKey(docfile))
|
|
|
|
|
{
|
|
|
|
|
doc = processor.ProcessFile(docfiles[docfile]);
|
|
|
|
|
}
|
|
|
|
|
if (doc == null)
|
|
|
|
|
{
|
|
|
|
|
doc = "/// <summary></summary>";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int summary_start = doc.IndexOf("<summary>") + "<summary>".Length;
|
|
|
|
|
string warning = "[deprecated: v{0}]";
|
|
|
|
|
string category = "[requires: {0}]";
|
|
|
|
|
if (f.Deprecated)
|
|
|
|
|
{
|
|
|
|
|
warning = String.Format(warning, f.DeprecatedVersion);
|
|
|
|
|
doc = doc.Insert(summary_start, warning);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (f.Extension != "Core" && !String.IsNullOrEmpty(f.Category))
|
|
|
|
|
{
|
|
|
|
|
category = String.Format(category, f.Category);
|
|
|
|
|
doc = doc.Insert(summary_start, category);
|
|
|
|
|
}
|
|
|
|
|
else if (!String.IsNullOrEmpty(f.Version))
|
|
|
|
|
{
|
|
|
|
|
if (f.Category.StartsWith("VERSION"))
|
|
|
|
|
category = String.Format(category, "v" + f.Version);
|
|
|
|
|
else
|
|
|
|
|
category = String.Format(category, "v" + f.Version + " and " + f.Category);
|
|
|
|
|
doc = doc.Insert(summary_start, category);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sw.WriteLine(doc);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[Warning] Error processing file {0}: {1}", docfile, e.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region WriteLicense
|
|
|
|
|
|
|
|
|
|
public void WriteLicense(BindStreamWriter sw)
|
|
|
|
|
{
|
|
|
|
|
sw.WriteLine(File.ReadAllText(Path.Combine(Settings.InputPath, Settings.LicenseFile)));
|
|
|
|
|
sw.WriteLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|