|
@@ -16,170 +16,73 @@ namespace SharpGLTF.CodeGen
|
|
|
/// Takes a <see cref="SchemaReflection.SchemaType.Context"/> and emits
|
|
/// Takes a <see cref="SchemaReflection.SchemaType.Context"/> and emits
|
|
|
/// all its enums and classes as c# source code
|
|
/// all its enums and classes as c# source code
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
- public class CSharpEmitter
|
|
|
|
|
|
|
+ public partial class CSharpEmitter
|
|
|
{
|
|
{
|
|
|
- #region runtime types
|
|
|
|
|
-
|
|
|
|
|
- class _RuntimeType
|
|
|
|
|
- {
|
|
|
|
|
- internal _RuntimeType(SchemaType t) { _PersistentType = t; }
|
|
|
|
|
-
|
|
|
|
|
- private readonly SchemaType _PersistentType;
|
|
|
|
|
-
|
|
|
|
|
- public string RuntimeNamespace { get; set; }
|
|
|
|
|
-
|
|
|
|
|
- public string RuntimeName { get; set; }
|
|
|
|
|
-
|
|
|
|
|
- public List<string> Comments { get; } = new List<string>();
|
|
|
|
|
-
|
|
|
|
|
- private readonly Dictionary<string, _RuntimeField> _Fields = new Dictionary<string, _RuntimeField>();
|
|
|
|
|
- private readonly Dictionary<string, _RuntimeEnum> _Enums = new Dictionary<string, _RuntimeEnum>();
|
|
|
|
|
-
|
|
|
|
|
- public _RuntimeField UseField(FieldInfo finfo)
|
|
|
|
|
- {
|
|
|
|
|
- var key = $"{finfo.PersistentName}";
|
|
|
|
|
-
|
|
|
|
|
- if (_Fields.TryGetValue(key, out _RuntimeField rfield)) return rfield;
|
|
|
|
|
-
|
|
|
|
|
- rfield = new _RuntimeField(finfo);
|
|
|
|
|
-
|
|
|
|
|
- _Fields[key] = rfield;
|
|
|
|
|
-
|
|
|
|
|
- return rfield;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public _RuntimeEnum UseEnum(string name)
|
|
|
|
|
- {
|
|
|
|
|
- var key = name;
|
|
|
|
|
-
|
|
|
|
|
- if (_Enums.TryGetValue(key, out _RuntimeEnum renum)) return renum;
|
|
|
|
|
-
|
|
|
|
|
- renum = new _RuntimeEnum(name);
|
|
|
|
|
-
|
|
|
|
|
- _Enums[key] = renum;
|
|
|
|
|
-
|
|
|
|
|
- return renum;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- class _RuntimeEnum
|
|
|
|
|
- {
|
|
|
|
|
- internal _RuntimeEnum(string name) { _Name = name; }
|
|
|
|
|
-
|
|
|
|
|
- private readonly string _Name;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- class _RuntimeField
|
|
|
|
|
- {
|
|
|
|
|
- internal _RuntimeField(FieldInfo f) { _PersistentField = f; }
|
|
|
|
|
-
|
|
|
|
|
- private readonly FieldInfo _PersistentField;
|
|
|
|
|
-
|
|
|
|
|
- public string PrivateField { get; set; }
|
|
|
|
|
- public string PublicProperty { get; set; }
|
|
|
|
|
-
|
|
|
|
|
- public string CollectionContainer { get; set; }
|
|
|
|
|
- public string DictionaryContainer { get; set; }
|
|
|
|
|
-
|
|
|
|
|
- // MinVal, MaxVal, readonly, static
|
|
|
|
|
-
|
|
|
|
|
- // serialization sections
|
|
|
|
|
- // deserialization sections
|
|
|
|
|
- // validation sections
|
|
|
|
|
- // clone sections
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ #region data
|
|
|
|
|
|
|
|
private readonly Dictionary<string, _RuntimeType> _Types = new Dictionary<string, _RuntimeType>();
|
|
private readonly Dictionary<string, _RuntimeType> _Types = new Dictionary<string, _RuntimeType>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private string _DefaultCollectionContainer = "TItem[]";
|
|
private string _DefaultCollectionContainer = "TItem[]";
|
|
|
|
|
|
|
|
- #endregion
|
|
|
|
|
-
|
|
|
|
|
- #region setup & declaration
|
|
|
|
|
-
|
|
|
|
|
- private static string _SanitizeName(string name)
|
|
|
|
|
- {
|
|
|
|
|
- return name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private _RuntimeType _UseType(SchemaType stype)
|
|
|
|
|
- {
|
|
|
|
|
- var key = $"{stype.PersistentName}";
|
|
|
|
|
-
|
|
|
|
|
- if (_Types.TryGetValue(key, out _RuntimeType rtype)) return rtype;
|
|
|
|
|
-
|
|
|
|
|
- rtype = new _RuntimeType(stype)
|
|
|
|
|
- {
|
|
|
|
|
- RuntimeName = _SanitizeName(stype.PersistentName)
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ private System.Diagnostics.DebuggerBrowsableState? _FieldsBrowsableState;
|
|
|
|
|
|
|
|
- _Types[key] = rtype;
|
|
|
|
|
|
|
+ #endregion
|
|
|
|
|
|
|
|
- return rtype;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ #region setup & declaration - types
|
|
|
|
|
|
|
|
- private bool _TryGetType(SchemaType stype, out _RuntimeType rtype)
|
|
|
|
|
|
|
+ public void SetRuntimeName(string persistentName, string runtimeName, string runtimeNamespace = null)
|
|
|
{
|
|
{
|
|
|
- var key = $"{stype.PersistentName}";
|
|
|
|
|
|
|
+ if (!_Types.TryGetValue(persistentName, out _RuntimeType t)) return;
|
|
|
|
|
|
|
|
- return _Types.TryGetValue(key, out rtype);
|
|
|
|
|
|
|
+ t.RuntimeNamespace = runtimeNamespace;
|
|
|
|
|
+ t.RuntimeName = runtimeName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private _RuntimeField _UseField(FieldInfo finfo) { return _UseType(finfo.DeclaringClass).UseField(finfo); }
|
|
|
|
|
-
|
|
|
|
|
- public void SetRuntimeName(SchemaType stype, string newName, string runtimeNamespace = null)
|
|
|
|
|
|
|
+ public void SetRuntimeName(SchemaType stype, string runtimeName, string runtimeNamespace = null)
|
|
|
{
|
|
{
|
|
|
var t = _UseType(stype);
|
|
var t = _UseType(stype);
|
|
|
|
|
|
|
|
t.RuntimeNamespace = runtimeNamespace;
|
|
t.RuntimeNamespace = runtimeNamespace;
|
|
|
- t.RuntimeName = newName;
|
|
|
|
|
|
|
+ t.RuntimeName = runtimeName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void AddRuntimeComment(string persistentName, string comment)
|
|
public void AddRuntimeComment(string persistentName, string comment)
|
|
|
{
|
|
{
|
|
|
if (!_Types.TryGetValue(persistentName, out _RuntimeType t)) return;
|
|
if (!_Types.TryGetValue(persistentName, out _RuntimeType t)) return;
|
|
|
|
|
|
|
|
- t.Comments.Add(comment);
|
|
|
|
|
|
|
+ t.RuntimeComments.Add(comment);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public IReadOnlyList<string> GetRuntimeComments(SchemaType cls)
|
|
public IReadOnlyList<string> GetRuntimeComments(SchemaType cls)
|
|
|
{
|
|
{
|
|
|
return !_TryGetType(cls, out var rtype)
|
|
return !_TryGetType(cls, out var rtype)
|
|
|
? Array.Empty<string>()
|
|
? Array.Empty<string>()
|
|
|
- : (IReadOnlyList<string>)rtype.Comments;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void SetRuntimeName(string persistentName, string runtimeName, string runtimeNamespace = null)
|
|
|
|
|
- {
|
|
|
|
|
- if (!_Types.TryGetValue(persistentName, out _RuntimeType t)) return;
|
|
|
|
|
-
|
|
|
|
|
- t.RuntimeNamespace = runtimeNamespace;
|
|
|
|
|
- t.RuntimeName = runtimeName;
|
|
|
|
|
|
|
+ : (IReadOnlyList<string>)rtype.RuntimeComments;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Gets the runtime name associated to the type with the given <paramref name="persistentName"/>
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="persistentName">The persistent name of the type.</param>
|
|
|
|
|
+ /// <returns>The runtime name associated to the type.</returns>
|
|
|
public string GetRuntimeName(string persistentName)
|
|
public string GetRuntimeName(string persistentName)
|
|
|
{
|
|
{
|
|
|
return _Types[persistentName].RuntimeName;
|
|
return _Types[persistentName].RuntimeName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// Gets the runtime namespace associated to the type with the given <paramref name="persistentName"/>
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="persistentName">The persistent name of the type.</param>
|
|
|
|
|
+ /// <returns>The runtime namespace associated to the type.</returns>
|
|
|
public string GetRuntimeNamespace(string persistentName)
|
|
public string GetRuntimeNamespace(string persistentName)
|
|
|
{
|
|
{
|
|
|
return _Types[persistentName].RuntimeNamespace ?? Constants.OutputNamespace;
|
|
return _Types[persistentName].RuntimeNamespace ?? Constants.OutputNamespace;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void SetFieldName(FieldInfo finfo, string name) { _UseField(finfo).PrivateField = name; }
|
|
|
|
|
-
|
|
|
|
|
- public string GetFieldRuntimeName(FieldInfo finfo) { return _UseField(finfo).PrivateField; }
|
|
|
|
|
-
|
|
|
|
|
- public void SetPropertyName(FieldInfo finfo, string name) { _UseField(finfo).PublicProperty = name; }
|
|
|
|
|
-
|
|
|
|
|
- public string GetPropertyName(FieldInfo finfo) { return _UseField(finfo).PublicProperty; }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
- public void SetCollectionContainer(string container) { _DefaultCollectionContainer = container; }
|
|
|
|
|
|
|
|
|
|
- public void SetCollectionContainer(FieldInfo finfo, string container) { _UseField(finfo).CollectionContainer = container; }
|
|
|
|
|
|
|
+ public void SetDefaultCollectionContainer(string container) { _DefaultCollectionContainer = container; }
|
|
|
|
|
|
|
|
public void SetFieldToChildrenList(SchemaType.Context ctx, string persistentName, string fieldName)
|
|
public void SetFieldToChildrenList(SchemaType.Context ctx, string persistentName, string fieldName)
|
|
|
{
|
|
{
|
|
@@ -203,11 +106,11 @@ namespace SharpGLTF.CodeGen
|
|
|
{
|
|
{
|
|
|
_UseType(type);
|
|
_UseType(type);
|
|
|
|
|
|
|
|
- foreach(var f in type.Fields)
|
|
|
|
|
|
|
+ foreach (var f in type.Fields)
|
|
|
{
|
|
{
|
|
|
- var runtimeName = _SanitizeName(f.PersistentName).Replace("@","at", StringComparison.Ordinal);
|
|
|
|
|
|
|
+ var runtimeName = _SanitizeName(f.PersistentName).Replace("@", "at", StringComparison.Ordinal);
|
|
|
|
|
|
|
|
- SetFieldName(f, $"_{runtimeName}");
|
|
|
|
|
|
|
+ SetFieldRuntimeName(f, $"_{runtimeName}");
|
|
|
SetPropertyName(f, runtimeName);
|
|
SetPropertyName(f, runtimeName);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -225,17 +128,59 @@ namespace SharpGLTF.CodeGen
|
|
|
|
|
|
|
|
public void DeclareContext(SchemaType.Context context)
|
|
public void DeclareContext(SchemaType.Context context)
|
|
|
{
|
|
{
|
|
|
- foreach(var ctype in context.Classes)
|
|
|
|
|
- {
|
|
|
|
|
- DeclareClass(ctype);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ foreach (var ctype in context.Classes) { DeclareClass(ctype); }
|
|
|
|
|
|
|
|
- foreach (var etype in context.Enumerations)
|
|
|
|
|
|
|
+ foreach (var etype in context.Enumerations) { DeclareEnum(etype); }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #endregion
|
|
|
|
|
+
|
|
|
|
|
+ #region setup & declaration - fields
|
|
|
|
|
+
|
|
|
|
|
+ public void SetFieldRuntimeName(FieldInfo finfo, string name) { _UseField(finfo).PrivateField = name; }
|
|
|
|
|
+
|
|
|
|
|
+ public string GetFieldRuntimeName(FieldInfo finfo) { return _UseField(finfo).PrivateField; }
|
|
|
|
|
+
|
|
|
|
|
+ public void SetPropertyName(FieldInfo finfo, string name) { _UseField(finfo).PublicProperty = name; }
|
|
|
|
|
+
|
|
|
|
|
+ public string GetPropertyName(FieldInfo finfo) { return _UseField(finfo).PublicProperty; }
|
|
|
|
|
+
|
|
|
|
|
+ public void SetCollectionContainer(FieldInfo finfo, string container) { _UseField(finfo).CollectionContainer = container; }
|
|
|
|
|
+
|
|
|
|
|
+ #endregion
|
|
|
|
|
+
|
|
|
|
|
+ #region core API
|
|
|
|
|
+
|
|
|
|
|
+ private static string _SanitizeName(string name)
|
|
|
|
|
+ {
|
|
|
|
|
+ return name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private _RuntimeType _UseType(SchemaType stype)
|
|
|
|
|
+ {
|
|
|
|
|
+ var key = $"{stype.PersistentName}";
|
|
|
|
|
+
|
|
|
|
|
+ if (_Types.TryGetValue(key, out _RuntimeType rtype)) return rtype;
|
|
|
|
|
+
|
|
|
|
|
+ rtype = new _RuntimeType(stype)
|
|
|
{
|
|
{
|
|
|
- DeclareEnum(etype);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ RuntimeName = _SanitizeName(stype.PersistentName)
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ _Types[key] = rtype;
|
|
|
|
|
+
|
|
|
|
|
+ return rtype;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private bool _TryGetType(SchemaType stype, out _RuntimeType rtype)
|
|
|
|
|
+ {
|
|
|
|
|
+ var key = $"{stype.PersistentName}";
|
|
|
|
|
+
|
|
|
|
|
+ return _Types.TryGetValue(key, out rtype);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private _RuntimeField _UseField(FieldInfo finfo) { return _UseType(finfo.DeclaringClass).UseField(finfo); }
|
|
|
|
|
+
|
|
|
internal string _GetRuntimeName(SchemaType type) { return _GetRuntimeName(type, null); }
|
|
internal string _GetRuntimeName(SchemaType type) { return _GetRuntimeName(type, null); }
|
|
|
|
|
|
|
|
private string _GetRuntimeName(SchemaType type, _RuntimeField extra)
|
|
private string _GetRuntimeName(SchemaType type, _RuntimeField extra)
|
|
@@ -509,9 +454,14 @@ namespace SharpGLTF.CodeGen
|
|
|
if (type.BaseClass != null) classDecl += $" : {_GetRuntimeName(type.BaseClass)}";
|
|
if (type.BaseClass != null) classDecl += $" : {_GetRuntimeName(type.BaseClass)}";
|
|
|
return classDecl;
|
|
return classDecl;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ internal IEnumerable<string> _EmitClassField(FieldInfo f)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (_FieldsBrowsableState.HasValue)
|
|
|
|
|
+ {
|
|
|
|
|
+ yield return $"[System.Diagnostics.DebuggerBrowsable({_FieldsBrowsableState.Value})]";
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- internal IEnumerable<string> _GetClassField(FieldInfo f)
|
|
|
|
|
- {
|
|
|
|
|
var tdecl = _GetRuntimeName(f.FieldType, _UseField(f));
|
|
var tdecl = _GetRuntimeName(f.FieldType, _UseField(f));
|
|
|
var fname = GetFieldRuntimeName(f);
|
|
var fname = GetFieldRuntimeName(f);
|
|
|
|
|
|
|
@@ -638,7 +588,7 @@ namespace SharpGLTF.CodeGen
|
|
|
var trname = _Emitter._GetRuntimeName(f.FieldType);
|
|
var trname = _Emitter._GetRuntimeName(f.FieldType);
|
|
|
var frname = _Emitter.GetFieldRuntimeName(f);
|
|
var frname = _Emitter.GetFieldRuntimeName(f);
|
|
|
|
|
|
|
|
- _Fields.AddRange(_Emitter._GetClassField(f));
|
|
|
|
|
|
|
+ _Fields.AddRange(_Emitter._EmitClassField(f));
|
|
|
|
|
|
|
|
AddFieldReflection(f);
|
|
AddFieldReflection(f);
|
|
|
|
|
|