diff --git a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.CSharp.cs index a0a93d058..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,26 +1086,25 @@ namespace Flax.Build.Bindings contents.AppendLine(); // Using declarations - contents.AppendLine("using System;"); - contents.AppendLine("using System.ComponentModel;"); - contents.AppendLine("using System.Globalization;"); // TODO: using declarations based on actual types usage - contents.AppendLine("using System.Runtime.CompilerServices;"); - contents.AppendLine("using 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) { - bool tmp = false; foreach (var apiTypeInfo in e.Children) { - if (apiTypeInfo.Namespace != "FlaxEngine") - { - tmp = true; - contents.AppendLine("using FlaxEngine;"); - break; - } + GenerateCSharpCollectNamespaces(buildData, apiTypeInfo, CSharpUsedNamespaces); } - if (tmp) - break; } + 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)