/// <summary> /// Writes a meta-interface (an empty interface implementing the given child interfaces) to a C# source file. /// </summary> /// <param name="ns">The namespace of the meta-interface.</param> /// <param name="file">The source file to write the meta-interface to.</param> /// <param name="name">The name of the meta-interface, without the "I" prefix.</param> /// <param name="ints">A set of child interfaces to implement.</param> /// <returns>An asynchronous task.</returns> public static async Task WriteMetaInterfaceAsync(string ns, string file, string name, IEnumerable <string> ints) { var interfaces = ints.ToArray(); using (var sw = new StreamWriter(File.Open(file, FileMode.Create, FileAccess.ReadWrite, FileShare.Inheritable))) { sw.WriteLine("// <auto-generated />"); sw.Write(EmbeddedResources.LicenseText(Path.GetFileName(file))); sw.WriteLine(); sw.WriteLine("namespace " + ns); sw.WriteLine("{"); sw.Write(" internal interface I" + name + " : " + interfaces[0]); for (var i = 1; i < interfaces.Length; i++) { sw.WriteLine(","); sw.Write(" " + interfaces[i]); } sw.WriteLine(); sw.WriteLine(" {"); sw.WriteLine(" }"); sw.WriteLine("}"); await sw.FlushAsync(); } }
/// <summary> /// Writes an enum to a file. /// </summary> /// <param name="enum">The enum to write.</param> /// <param name="file">The file to write to.</param> /// <param name="ns">The namespace of this enum.</param> /// <param name="prefix">The constant prefix for the profile.</param> public static void WriteEnum(EnumerationSignature @enum, string file, string ns, string prefix) { using (var sw = new StreamWriter(File.Open(file, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))) { sw.WriteLine("// <auto-generated />"); sw.WriteLine(EmbeddedResources.LicenseText(Path.GetFileName(file))); sw.WriteLine("using System;"); sw.WriteLine(); sw.WriteLine("namespace " + ns); sw.WriteLine("{"); sw.WriteLine(" /// <summary>"); sw.Write(" /// An OpenGL enum"); if (@enum.Tokens.Any()) { sw.Write(" containing values "); sw.Write(@enum.Tokens.OrderBy(x => x.Value).FirstOrDefault()?.Value); sw.Write(" through "); sw.Write(@enum.Tokens.OrderBy(x => x.Value).LastOrDefault()?.Value); } sw.WriteLine("."); sw.WriteLine(" /// </summary>"); sw.WriteLine(" public enum " + @enum.Name); sw.WriteLine(" {"); WriteTokens(sw, @enum.Tokens, prefix); sw.WriteLine(" }"); sw.WriteLine("}"); } }
private static async Task WriteTemplateMixedModePartAsync( Project project, IGeneratorSettings settings) { var dir = Path.Combine(Program.Arguments.OutputPath, settings.OutputSubfolder, settings.Namespace, settings.ClassName); using (var sw = new StreamWriter(File.Open(Path.Combine(dir, settings.ClassName + ".cs"), FileMode.Create, FileAccess.ReadWrite, FileShare.Inheritable))) { sw.WriteLine(EmbeddedResources.LicenseText(Path.GetFileName(settings.ClassName + ".cs"))); sw.WriteLine("using System;"); sw.WriteLine("using System.Runtime.InteropServices;"); sw.WriteLine("using System.Text;"); sw.WriteLine("using AdvancedDLSupport;"); sw.WriteLine("using OpenToolkit.Core.Loader;"); sw.WriteLine("using OpenToolkit.Core.Native;"); sw.WriteLine(); sw.WriteLine("namespace " + settings.Namespace); sw.WriteLine("{"); var nm = settings.NameContainer.ClassName; sw.WriteLine(" public partial class " + settings.ClassName + " : NativeLibraryBase, I" + settings.ClassName); sw.WriteLine(" {"); sw.WriteLine(" /// <inheritdoc cref=\"NativeLibraryBase\"/>"); sw.WriteLine(" protected " + settings.ClassName + "(string path, ImplementationOptions options)"); sw.WriteLine(" : base(path, options)"); sw.WriteLine(" {"); sw.WriteLine(" }"); sw.WriteLine(); sw.WriteLine(" /// <summary>"); sw.WriteLine(" /// Gets the library name container for this profile."); sw.WriteLine(" /// </summary>"); sw.Write(" public IPlatformLibraryNameContainer NameContainer => new"); sw.WriteLine(" " + nm + "();"); sw.WriteLine(" }"); sw.WriteLine("}"); await sw.FlushAsync(); } }
/// <summary> /// Asynchronously writes this interface to a file. /// </summary> /// <param name="i">The interface.</param> /// <param name="file">The file to write to.</param> /// <param name="ns">This interface's namespace.</param> /// <param name="prefix">The function prefix for this interface.</param> /// <param name="doc">The profile's documentation.</param> /// <param name="rootNamespace">The profile's root namespace.</param> /// <returns>The asynchronous task.</returns> public static async Task WriteInterfaceAsync(this Interface i, string file, string ns, string prefix, ProfileDocumentation doc, string rootNamespace) { using (var sw = new StreamWriter(File.Open(file, FileMode.Create, FileAccess.ReadWrite, FileShare.Inheritable))) { sw.WriteLine("// <auto-generated />"); sw.WriteLine(EmbeddedResources.LicenseText(Path.GetFileName(file))); sw.WriteLine("using AdvancedDLSupport;"); sw.WriteLine("using " + rootNamespace + ";"); sw.WriteLine("using OpenToolkit.Core.Native;"); sw.WriteLine("using System;"); sw.WriteLine("using System.Runtime.InteropServices;"); sw.WriteLine("using System.Text;"); sw.WriteLine(); sw.WriteLine("namespace " + ns); sw.WriteLine("{"); sw.WriteLine(" internal interface " + i.InterfaceName); sw.Write(" {"); foreach (var function in i.Functions) { sw.WriteLine(); using (var sr = new StringReader(Utilities.GetDocumentation(function, doc))) { string line; while ((line = await sr.ReadLineAsync()) != null) { sw.WriteLine(" " + line); } } if (function.IsDeprecated) { var str = "Deprecated"; if (function.DeprecatedIn != null) { str += " since " + function.DeprecatedIn.ToString(2); } if (!string.IsNullOrWhiteSpace(function.DeprecationReason)) { str += " - " + function.DeprecationReason; } sw.WriteLine($" [Obsolete(\"{str}\")]"); } sw.WriteLine ( " [NativeSymbol(\"" + prefix + function.NativeEntrypoint + "\")]" ); sw.WriteLine ( " " + "[AutoGenerated(" + $"Category = \"{function.Categories.First()}\", " + $"Version = \"{function.IntroducedIn}\", " + $"EntryPoint = \"{prefix}{function.NativeEntrypoint}\", " + $"Source = \"{function.Source}\"" + ")]" ); using (var sr = new StringReader(Utilities.GetDeclarationString(function) + ";")) { string line; while ((line = await sr.ReadLineAsync()) != null) { sw.WriteLine(" " + line); } } } sw.WriteLine(" }"); sw.WriteLine("}"); await sw.FlushAsync(); } }
private static async Task WriteNativeMixedModePartAsync(Project project, IGeneratorSettings settings) { var ns = project.Extension == "Core" ? settings.Namespace : settings.ExtensionNamespace + "." + Utilities.ConvertExtensionNameToNamespace(project.Extension); var dir = project.Extension == "Core" ? Path.Combine(Program.Arguments.OutputPath, settings.OutputSubfolder, settings.Namespace, settings.ClassName) : Path.Combine(Program.Arguments.OutputPath, settings.OutputSubfolder, ProfileWriter.ExtensionsFolder, ns); var file = project.Extension == "Core" ? settings.ClassName : NativeIdentifierTranslator.TranslateIdentifierName(project.Extension); using (var sw = new StreamWriter(File.Open(Path.Combine(dir, file + ".Native.cs"), FileMode.Create, FileAccess.ReadWrite, FileShare.Inheritable))) { sw.WriteLine("// <auto-generated />"); sw.WriteLine(EmbeddedResources.LicenseText(Path.GetFileName(file))); sw.WriteLine("using AdvancedDLSupport;"); sw.WriteLine("using OpenToolkit.Core.Native;"); sw.WriteLine("using OpenToolkit.Core.Loader;"); sw.WriteLine("using OpenToolkit.Core.Extensions;"); sw.WriteLine("using " + settings.Namespace + ";"); sw.WriteLine("using System;"); sw.WriteLine("using System.Runtime.InteropServices;"); sw.WriteLine("using System.Text;"); sw.WriteLine(); sw.WriteLine("namespace " + ns); sw.WriteLine("{"); if (project.Extension != "Core") { sw.WriteLine($" [Extension(\"{settings.ConstantPrefix}{project.Extension}\")]"); } sw.Write(" public abstract partial class " + file); if (project.Extension != "Core") { sw.WriteLine(" : ExtensionBase, I" + file); sw.WriteLine(" {"); sw.WriteLine(" /// <inheritdoc cref=\"ExtensionBase\"/>"); sw.WriteLine($" protected {file}(string path, ImplementationOptions options)"); sw.WriteLine(" : base(path, options)"); sw.WriteLine(" {"); sw.WriteLine(" }"); sw.WriteLine(); } else { sw.WriteLine(); sw.WriteLine(" {"); } foreach (var function in project.Interfaces.SelectMany(x => x.Functions)) { sw.WriteLine(" /// <inheritdoc/>"); if (function.IsDeprecated) { var str = "Deprecated"; if (function.DeprecatedIn != null) { str += " since " + function.DeprecatedIn.ToString(2); } if (!string.IsNullOrWhiteSpace(function.DeprecationReason)) { str += " - " + function.DeprecationReason; } sw.WriteLine($" [Obsolete(\"{str}\")]"); } sw.Write(" public abstract "); sw.WriteLine(Utilities.GetDeclarationString(function) + ";"); sw.WriteLine(); } sw.WriteLine(" }"); sw.WriteLine("}"); await sw.FlushAsync(); } }
private static async Task WriteOverloadsMixedModePartAsync(Project project, IGeneratorSettings settings, ProfileDocumentation docs) { var file = project.Extension == "Core" ? settings.ClassName : NativeIdentifierTranslator.TranslateIdentifierName(project.Extension); var ns = project.Extension == "Core" ? settings.Namespace : settings.ExtensionNamespace + "." + Utilities.ConvertExtensionNameToNamespace(project.Extension); var dir = project.Extension == "Core" ? Path.Combine(Program.Arguments.OutputPath, settings.OutputSubfolder, settings.Namespace, settings.ClassName) : Path.Combine(Program.Arguments.OutputPath, settings.OutputSubfolder, ProfileWriter.ExtensionsFolder, ns); using (var sw = new StreamWriter(File.Open(Path.Combine(dir, file + ".Overloads.cs"), FileMode.Create, FileAccess.ReadWrite, FileShare.Inheritable))) { sw.WriteLine("// <auto-generated />"); sw.WriteLine(EmbeddedResources.LicenseText(Path.GetFileName(file))); sw.WriteLine("using AdvancedDLSupport;"); sw.WriteLine("using OpenToolkit.Core.Native;"); sw.WriteLine("using OpenToolkit.Core.Extensions;"); sw.WriteLine("using System;"); sw.WriteLine("using System.Runtime.InteropServices;"); sw.WriteLine("using System.Text;"); sw.WriteLine("using " + settings.Namespace + ";"); sw.WriteLine(); sw.WriteLine("namespace " + ns); sw.WriteLine("{"); sw.WriteLine(" /// <summary>"); sw.WriteLine(" /// Contains bindings to the " + settings.APIIdentifier + " API."); sw.WriteLine(" /// </summary>"); sw.WriteLine(" public partial class " + file); sw.WriteLine(" {"); foreach (var overload in project.Overloads) { sw.WriteLine(" /// <summary>"); if (docs.HasDocumentation(overload.Item1)) { sw.WriteLine(" /// " + docs.GetDocumentation(overload.Item1)?.Purpose); } else { sw.WriteLine(" /// To be added."); } sw.WriteLine(" /// </summary>"); if (overload.Item1.IsDeprecated) { var str = "Deprecated"; if (overload.Item1.DeprecatedIn != null) { str += " since " + overload.Item1.DeprecatedIn.ToString(2); } if (!string.IsNullOrWhiteSpace(overload.Item1.DeprecationReason)) { str += " - " + overload.Item1.DeprecationReason; } sw.WriteLine($" [Obsolete(\"{str}\")]"); } sw.WriteLine( " " + "[AutoGenerated(" + $"Category = \"{overload.Item1.Categories.First()}\", " + $"Version = \"{overload.Item1.IntroducedIn}\", " + $"EntryPoint = \"{settings.FunctionPrefix}{overload.Item1.NativeEntrypoint}\", " + $"Source = \"{overload.Item1.Source}\"" + ")]"); sw.Write(" public "); var decl = Utilities.GetDeclarationString(overload.Item1); if (!decl.Contains("unsafe ")) { sw.Write("unsafe "); } sw.WriteLine(decl); sw.WriteLine(" {"); foreach (var line in overload.Item2.ToString() .Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)) { sw.Write(" " + line); } sw.WriteLine(" }"); sw.WriteLine(); } sw.WriteLine(" }"); sw.WriteLine("}"); await sw.FlushAsync(); } }