Ver Fonte

Added support for switching namespace, and some quality of life methods

vpenades há 2 anos atrás
pai
commit
ccf18677ca

+ 69 - 17
build/SharpGLTF.CodeGen/CodeGen/EmitCSharp.cs

@@ -26,6 +26,8 @@ namespace SharpGLTF.CodeGen
 
             private readonly SchemaType _PersistentType;
 
+            public string RuntimeNamespace { get; set; }
+
             public string RuntimeName { get; set; }
 
             private readonly Dictionary<string, _RuntimeField> _Fields = new Dictionary<string, _RuntimeField>();
@@ -83,7 +85,7 @@ namespace SharpGLTF.CodeGen
             // deserialization sections
             // validation sections
             // clone sections
-        }
+        }        
 
         private readonly Dictionary<string, _RuntimeType> _Types = new Dictionary<string, _RuntimeType>();
 
@@ -91,7 +93,7 @@ namespace SharpGLTF.CodeGen
 
         #endregion
 
-        #region setup & declaration
+        #region setup & declaration        
 
         private static string _SanitizeName(string name)
         {
@@ -116,16 +118,31 @@ namespace SharpGLTF.CodeGen
 
         private _RuntimeField _UseField(FieldInfo finfo) { return _UseType(finfo.DeclaringClass).UseField(finfo); }
 
-        public void SetRuntimeName(SchemaType stype, string newName) { _UseType(stype).RuntimeName = newName; }
+        public void SetRuntimeName(SchemaType stype, string newName, string runtimeNamespace = null)
+        {
+            var t = _UseType(stype);
 
-        public void SetRuntimeName(string persistentName, string runtimeName)
+            t.RuntimeNamespace = runtimeNamespace;
+            t.RuntimeName = newName;
+        }
+
+        public void SetRuntimeName(string persistentName, string runtimeName, string runtimeNamespace = null)
         {
             if (!_Types.TryGetValue(persistentName, out _RuntimeType t)) return;
 
+            t.RuntimeNamespace = runtimeNamespace;
             t.RuntimeName = runtimeName;
         }
 
+        public string GetRuntimeName(string persistentName)
+        {
+            return _Types[persistentName].RuntimeName;
+        }
 
+        public string GetRuntimeNamespace(string persistentName)
+        {
+            return _Types[persistentName].RuntimeNamespace ?? Constants.OutputNamespace;
+        }
 
         public void SetFieldName(FieldInfo finfo, string name) { _UseField(finfo).PrivateField = name; }
 
@@ -141,7 +158,23 @@ namespace SharpGLTF.CodeGen
 
         public void SetCollectionContainer(FieldInfo finfo, string container) { _UseField(finfo).CollectionContainer = container; }        
 
+        public void SetFieldToChildrenList(SchemaType.Context ctx, string persistentName, string fieldName)
+        {
+            var classType = ctx.FindClass(persistentName);
+            if (classType == null) return;
+            var field = classType.UseField(fieldName);
+            var runtimeName = this.GetRuntimeName(persistentName);
+            this.SetCollectionContainer(field, $"ChildrenList<TItem,{runtimeName}>");
+        }
 
+        public void SetFieldToChildrenDictionary(SchemaType.Context ctx, string persistentName, string fieldName)
+        {
+            var classType = ctx.FindClass(persistentName);
+            if (classType == null) return;
+            var field = classType.UseField(fieldName);
+            var runtimeName = this.GetRuntimeName(persistentName);
+            this.SetCollectionContainer(field, $"ChildrenDictionary<TItem,{runtimeName}>");
+        }
 
         public void DeclareClass(ClassType type)
         {
@@ -186,6 +219,8 @@ namespace SharpGLTF.CodeGen
         {
             switch (type)
             {
+                case null: throw new ArgumentNullException(nameof(type));
+
                 case ObjectType anyType: return anyType.PersistentName;
 
                 case StringType strType: return strType.PersistentName;
@@ -210,6 +245,13 @@ namespace SharpGLTF.CodeGen
                         var key = _GetRuntimeName(dictType.KeyType);
                         var val = _GetRuntimeName(dictType.ValueType);
 
+                        var container = extra?.CollectionContainer ?? string.Empty;
+
+                        if (container.StartsWith("ChildrenDictionary<"))
+                        {
+                            if (key == "String") return container.Replace("TItem", val, StringComparison.Ordinal);
+                        }
+
                         return $"Dictionary<{key},{val}>";
                     }
 
@@ -217,7 +259,7 @@ namespace SharpGLTF.CodeGen
 
                 case ClassType classType: return _UseType(classType).RuntimeName;
 
-                default: throw new NotImplementedException();
+                default: throw new NotImplementedException(type.PersistentName);
             }
         }
 
@@ -331,22 +373,30 @@ namespace SharpGLTF.CodeGen
             sb.AppendLine("using System.Linq;");
             sb.AppendLine("using System.Text;");
             sb.AppendLine("using System.Numerics;");
+            sb.AppendLine("using System.Text.Json;");            
 
-            #if USENEWTONSOFT
-            sb.AppendLine("using Newtonsoft.Json;");
-            #else
-            sb.AppendLine("using System.Text.Json;");
-            #endif
-            sb.AppendLine();
+            string currentNamespace = null;
 
-            sb.AppendLine($"namespace {Constants.OutputNamespace}");
-            sb.AppendLine("{");
+            void _setCurrentNamespace(string ns)
+            {
+                if (currentNamespace == ns) return;
+                if (currentNamespace != null) sb.AppendLine("}");
+                currentNamespace = ns;
+                if (currentNamespace != null)
+                {
+                    sb.AppendLine();
+                    sb.AppendLine($"namespace {currentNamespace}");
+                    sb.AppendLine("{");
 
-            sb.AppendLine("using Collections;".Indent(1));
-            sb.AppendLine();
+                    sb.AppendLine("using Collections;".Indent(1));
+                    sb.AppendLine();
+                }
+            }            
 
             foreach (var etype in context.Enumerations)
             {
+                _setCurrentNamespace(GetRuntimeNamespace(etype.PersistentName));
+
                 var cout = EmitEnum(etype);
 
                 sb.AppendLine(cout);
@@ -357,13 +407,15 @@ namespace SharpGLTF.CodeGen
             {
                 if (ctype.IgnoredByEmitter) continue;
 
+                _setCurrentNamespace(GetRuntimeNamespace(ctype.PersistentName));
+
                 var cout = EmitClass(ctype);
 
                 sb.AppendLine(cout);
                 sb.AppendLine();
             }
 
-            sb.AppendLine("}");
+            _setCurrentNamespace(null);
 
             return sb.ToString();
         }
@@ -490,7 +542,7 @@ namespace SharpGLTF.CodeGen
             yield return string.Empty;
         }            
 
-#endregion
+        #endregion
     }
 
     /// <summary>

+ 4 - 4
build/SharpGLTF.CodeGen/Constants.cs

@@ -70,10 +70,8 @@ namespace SharpGLTF
         /// directory within the solution where the generated code is emitted
         /// </summary>
         public static string TargetProjectDirectory => "src\\SharpGLTF.Core\\Schema2\\Generated";
-
-        public static string CesiumProjectDirectory => "src\\SharpGLTF.Cesium\\Schema2\\Generated";
-
-        public static string AgiProjectDirectory => "src\\SharpGLTF.Agi\\Schema2\\Generated";
+        public static string CesiumProjectDirectory => "src\\SharpGLTF.Ext.3DTiles\\Schema2\\Generated";
+        public static string AgiProjectDirectory => "src\\SharpGLTF.Ext.Agi\\Schema2\\Generated";
 
 
         /// <summary>
@@ -81,6 +79,8 @@ namespace SharpGLTF
         /// </summary>
         public static string OutputNamespace => "SharpGLTF.Schema2";
 
+        public static string CesiumNameSpace => "SharpGLTF.Schema2.Tiles3D";
+
         #endregion
     }
 }

+ 8 - 8
build/SharpGLTF.CodeGen/SchemaReflection/SchemaTypes.cs

@@ -54,7 +54,7 @@ namespace SharpGLTF.SchemaReflection
     /// <summary>
     /// Represents a <see cref="String"/> type.
     /// </summary>
-    [System.Diagnostics.DebuggerDisplay("{PersistentName}")]
+    [System.Diagnostics.DebuggerDisplay("StringType {PersistentName}")]
     public sealed class StringType : SchemaType
     {
         #region constructor
@@ -76,7 +76,7 @@ namespace SharpGLTF.SchemaReflection
     /// <remarks>
     /// undefined objects are serialized/deserialized as <see cref="System.Text.Json.Nodes.JsonNode"/>
     /// </remarks>
-    [System.Diagnostics.DebuggerDisplay("{PersistentName}")]
+    [System.Diagnostics.DebuggerDisplay("ObjectType {PersistentName}")]
     public sealed class ObjectType : SchemaType
     {
         #region constructor
@@ -98,7 +98,7 @@ namespace SharpGLTF.SchemaReflection
     /// <remarks>
     /// This type can optionally be set as nullable.
     /// </remarks>
-    [System.Diagnostics.DebuggerDisplay("{PersistentName}")]
+    [System.Diagnostics.DebuggerDisplay("BlittableType {PersistentName}")]
     public sealed class BlittableType : SchemaType
     {
         #region constructor        
@@ -133,7 +133,7 @@ namespace SharpGLTF.SchemaReflection
         #endregion
     }    
 
-    [System.Diagnostics.DebuggerDisplay("enum {PersistentName}")]
+    [System.Diagnostics.DebuggerDisplay("EnumType {PersistentName}")]
     public sealed class EnumType : SchemaType
     {
         #region constructor
@@ -178,7 +178,7 @@ namespace SharpGLTF.SchemaReflection
         #endregion
     }
 
-    [System.Diagnostics.DebuggerDisplay("{PersistentName}")]
+    [System.Diagnostics.DebuggerDisplay("ArrayType {PersistentName}")]
     public sealed class ArrayType : SchemaType
     {
         #region constructor
@@ -205,7 +205,7 @@ namespace SharpGLTF.SchemaReflection
         #endregion
     }    
 
-    [System.Diagnostics.DebuggerDisplay("{PersistentName}")]
+    [System.Diagnostics.DebuggerDisplay("DictionaryType {PersistentName}")]
     public sealed class DictionaryType : SchemaType
     {
         #region lifecycle
@@ -239,7 +239,7 @@ namespace SharpGLTF.SchemaReflection
     /// <summary>
     /// Represents a field property within a <see cref="ClassType"/>
     /// </summary>
-    [System.Diagnostics.DebuggerDisplay("{FieldType.PersistentName} {PersistentName}")]
+    [System.Diagnostics.DebuggerDisplay("FieldInfo {_FieldType} {FieldType.PersistentName} {PersistentName}")]
     public sealed class FieldInfo
     {
         #region lifecycle
@@ -347,7 +347,7 @@ namespace SharpGLTF.SchemaReflection
         #endregion
     }
 
-    [System.Diagnostics.DebuggerDisplay("class {PersistentName} : {BaseClass.PersistentName}")]
+    [System.Diagnostics.DebuggerDisplay("ClassType {PersistentName} : {BaseClass.PersistentName}")]
     public sealed class ClassType : SchemaType
     {
         #region constructor