From 51a90dcd28d327ef0d587eb10ec14557b619c299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9=20=D0=9C=D0=B8?= =?UTF-8?q?=D1=85=D0=B5=D0=B5=D0=B2?= Date: Sat, 24 Apr 2021 14:13:07 +0700 Subject: [PATCH 1/2] Adding the namespace of a field from another module. --- .../Bindings/BindingsGenerator.CSharp.cs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs index 44d3b457c..4f3475e6c 100644 --- a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs +++ b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs @@ -1041,25 +1041,30 @@ namespace Flax.Build.Bindings contents.AppendLine(); // Using declarations - contents.AppendLine("using System;"); - contents.AppendLine("using System.ComponentModel;"); - contents.AppendLine("using System.Runtime.CompilerServices;"); - contents.AppendLine("using System.Runtime.InteropServices;"); + HashSet AddedNamespaces = new HashSet() { "System", "System.ComponentModel", "System.Runtime.CompilerServices", "System.Runtime.InteropServices" }; foreach (var e in moduleInfo.Children) { - bool tmp = false; foreach (var apiTypeInfo in e.Children) { + if (apiTypeInfo is ClassInfo classinfo) + { + foreach (FieldInfo apifield in classinfo.Fields) + { + ApiTypeInfo ApiFieldInfo = FindApiTypeInfo(buildData, apifield.Type, classinfo); + if (ApiFieldInfo != null && !string.IsNullOrWhiteSpace(ApiFieldInfo.Namespace) && ApiFieldInfo.Namespace != apiTypeInfo.Namespace) + { + AddedNamespaces.Add(ApiFieldInfo.Namespace); + } + } + } if (apiTypeInfo.Namespace != "FlaxEngine") { - tmp = true; - contents.AppendLine("using FlaxEngine;"); - break; + AddedNamespaces.Add("FlaxEngine"); } } - if (tmp) - break; } + foreach (string Namespace in AddedNamespaces) + contents.AppendLine($"using {Namespace};"); // TODO: custom using declarations support // TODO: generate using declarations based on references modules (eg. using FlaxEngine, using Plugin1 in game API) From c4fa30d39c3747e7474ab6ca087affad45e7ed99 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 28 Apr 2021 11:41:46 +0200 Subject: [PATCH 2/2] Add sorting for used namespaces in bindings and include nested types checks #477 --- .../Bindings/BindingsGenerator.CSharp.cs | 56 +++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs index b8c391b7a..d41ecd501 100644 --- a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs +++ b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs @@ -11,6 +11,9 @@ namespace Flax.Build.Bindings { partial class BindingsGenerator { + private static readonly HashSet CSharpUsedNamespaces = new HashSet(); + private static readonly List CSharpUsedNamespacesSorted = new List(); + private static readonly Dictionary CSharpNativeToManagedBasicTypes = new Dictionary() { // Language types @@ -1045,6 +1048,30 @@ namespace Flax.Build.Bindings return true; } + private static void GenerateCSharpCollectNamespaces(BuildData buildData, ApiTypeInfo apiType, HashSet usedNamespaces) + { + if (apiType is ClassInfo classInfo) + { + foreach (var field in classInfo.Fields) + { + var fieldInfo = FindApiTypeInfo(buildData, field.Type, classInfo); + if (fieldInfo != null && !string.IsNullOrWhiteSpace(fieldInfo.Namespace) && fieldInfo.Namespace != apiType.Namespace) + usedNamespaces.Add(fieldInfo.Namespace); + } + } + else if (apiType is StructureInfo structureInfo) + { + foreach (var field in structureInfo.Fields) + { + var fieldInfo = FindApiTypeInfo(buildData, field.Type, structureInfo); + if (fieldInfo != null && !string.IsNullOrWhiteSpace(fieldInfo.Namespace) && fieldInfo.Namespace != apiType.Namespace) + usedNamespaces.Add(fieldInfo.Namespace); + } + } + foreach (var child in apiType.Children) + GenerateCSharpCollectNamespaces(buildData, child, usedNamespaces); + } + private static void GenerateCSharp(BuildData buildData, ModuleInfo moduleInfo, ref BindingsResult bindings) { var contents = new StringBuilder(); @@ -1059,29 +1086,24 @@ namespace Flax.Build.Bindings contents.AppendLine(); // Using declarations - HashSet usedNamespaces = new HashSet() { "System", "System.ComponentModel", "System.ComponentModel", "System.Runtime.CompilerServices", "System.Runtime.InteropServices" }; + CSharpUsedNamespaces.Clear(); + CSharpUsedNamespaces.Add("System"); + CSharpUsedNamespaces.Add("System.ComponentModel"); + CSharpUsedNamespaces.Add("System.Globalization"); + CSharpUsedNamespaces.Add("System.Runtime.CompilerServices"); + CSharpUsedNamespaces.Add("System.Runtime.InteropServices"); + CSharpUsedNamespaces.Add("FlaxEngine"); foreach (var e in moduleInfo.Children) { foreach (var apiTypeInfo in e.Children) { - if (apiTypeInfo is ClassInfo classinfo) - { - foreach (var apifield in classinfo.Fields) - { - var fieldInfo = FindApiTypeInfo(buildData, apifield.Type, classinfo); - if (fieldInfo != null && !string.IsNullOrWhiteSpace(fieldInfo.Namespace) && fieldInfo.Namespace != apiTypeInfo.Namespace) - { - usedNamespaces.Add(fieldInfo.Namespace); - } - } - } - if (apiTypeInfo.Namespace != "FlaxEngine") - { - usedNamespaces.Add("FlaxEngine"); - } + GenerateCSharpCollectNamespaces(buildData, apiTypeInfo, CSharpUsedNamespaces); } } - foreach (var e in usedNamespaces) + CSharpUsedNamespacesSorted.Clear(); + CSharpUsedNamespacesSorted.AddRange(CSharpUsedNamespaces); + CSharpUsedNamespacesSorted.Sort(); + foreach (var e in CSharpUsedNamespacesSorted) contents.AppendLine($"using {e};"); // TODO: custom using declarations support // TODO: generate using declarations based on references modules (eg. using FlaxEngine, using Plugin1 in game API)