EmitCSharp.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. // #define USENEWTONSOFT
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using static System.FormattableString;
  8. namespace SharpGLTF.CodeGen
  9. {
  10. using SchemaReflection;
  11. /// <summary>
  12. /// Takes a <see cref="SchemaReflection.SchemaType.Context"/> and emits
  13. /// all its enums and classes as c# source code
  14. /// </summary>
  15. class CSharpEmitter
  16. {
  17. #region runtime types
  18. class _RuntimeType
  19. {
  20. internal _RuntimeType(SchemaType t) { _PersistentType = t; }
  21. private readonly SchemaType _PersistentType;
  22. public string RuntimeNamespace { get; set; }
  23. public string RuntimeName { get; set; }
  24. public List<string> Comments { get; } = new List<string>();
  25. private readonly Dictionary<string, _RuntimeField> _Fields = new Dictionary<string, _RuntimeField>();
  26. private readonly Dictionary<string, _RuntimeEnum> _Enums = new Dictionary<string, _RuntimeEnum>();
  27. public _RuntimeField UseField(FieldInfo finfo)
  28. {
  29. var key = $"{finfo.PersistentName}";
  30. if (_Fields.TryGetValue(key, out _RuntimeField rfield)) return rfield;
  31. rfield = new _RuntimeField(finfo);
  32. _Fields[key] = rfield;
  33. return rfield;
  34. }
  35. public _RuntimeEnum UseEnum(string name)
  36. {
  37. var key = name;
  38. if (_Enums.TryGetValue(key, out _RuntimeEnum renum)) return renum;
  39. renum = new _RuntimeEnum(name);
  40. _Enums[key] = renum;
  41. return renum;
  42. }
  43. }
  44. class _RuntimeEnum
  45. {
  46. internal _RuntimeEnum(string name) { _Name = name; }
  47. private readonly string _Name;
  48. }
  49. class _RuntimeField
  50. {
  51. internal _RuntimeField(FieldInfo f) { _PersistentField = f; }
  52. private readonly FieldInfo _PersistentField;
  53. public string PrivateField { get; set; }
  54. public string PublicProperty { get; set; }
  55. public string CollectionContainer { get; set; }
  56. public string DictionaryContainer { get; set; }
  57. // MinVal, MaxVal, readonly, static
  58. // serialization sections
  59. // deserialization sections
  60. // validation sections
  61. // clone sections
  62. }
  63. private readonly Dictionary<string, _RuntimeType> _Types = new Dictionary<string, _RuntimeType>();
  64. private string _DefaultCollectionContainer = "TItem[]";
  65. #endregion
  66. #region setup & declaration
  67. private static string _SanitizeName(string name)
  68. {
  69. return name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase);
  70. }
  71. private _RuntimeType _UseType(SchemaType stype)
  72. {
  73. var key = $"{stype.PersistentName}";
  74. if (_Types.TryGetValue(key, out _RuntimeType rtype)) return rtype;
  75. rtype = new _RuntimeType(stype)
  76. {
  77. RuntimeName = _SanitizeName(stype.PersistentName)
  78. };
  79. _Types[key] = rtype;
  80. return rtype;
  81. }
  82. private bool _TryGetType(SchemaType stype, out _RuntimeType rtype)
  83. {
  84. var key = $"{stype.PersistentName}";
  85. return _Types.TryGetValue(key, out rtype);
  86. }
  87. private _RuntimeField _UseField(FieldInfo finfo) { return _UseType(finfo.DeclaringClass).UseField(finfo); }
  88. public void SetRuntimeName(SchemaType stype, string newName, string runtimeNamespace = null)
  89. {
  90. var t = _UseType(stype);
  91. t.RuntimeNamespace = runtimeNamespace;
  92. t.RuntimeName = newName;
  93. }
  94. public void AddRuntimeComment(string persistentName, string comment)
  95. {
  96. if (!_Types.TryGetValue(persistentName, out _RuntimeType t)) return;
  97. t.Comments.Add(comment);
  98. }
  99. public IReadOnlyList<string> GetRuntimeComments(SchemaType cls)
  100. {
  101. return !_TryGetType(cls, out var rtype)
  102. ? Array.Empty<string>()
  103. : (IReadOnlyList<string>)rtype.Comments;
  104. }
  105. public void SetRuntimeName(string persistentName, string runtimeName, string runtimeNamespace = null)
  106. {
  107. if (!_Types.TryGetValue(persistentName, out _RuntimeType t)) return;
  108. t.RuntimeNamespace = runtimeNamespace;
  109. t.RuntimeName = runtimeName;
  110. }
  111. public string GetRuntimeName(string persistentName)
  112. {
  113. return _Types[persistentName].RuntimeName;
  114. }
  115. public string GetRuntimeNamespace(string persistentName)
  116. {
  117. return _Types[persistentName].RuntimeNamespace ?? Constants.OutputNamespace;
  118. }
  119. public void SetFieldName(FieldInfo finfo, string name) { _UseField(finfo).PrivateField = name; }
  120. public string GetFieldRuntimeName(FieldInfo finfo) { return _UseField(finfo).PrivateField; }
  121. public void SetPropertyName(FieldInfo finfo, string name) { _UseField(finfo).PublicProperty = name; }
  122. public string GetPropertyName(FieldInfo finfo) { return _UseField(finfo).PublicProperty; }
  123. public void SetCollectionContainer(string container) { _DefaultCollectionContainer = container; }
  124. public void SetCollectionContainer(FieldInfo finfo, string container) { _UseField(finfo).CollectionContainer = container; }
  125. public void SetFieldToChildrenList(SchemaType.Context ctx, string persistentName, string fieldName)
  126. {
  127. var classType = ctx.FindClass(persistentName);
  128. if (classType == null) return;
  129. var field = classType.UseField(fieldName);
  130. var runtimeName = this.GetRuntimeName(persistentName);
  131. this.SetCollectionContainer(field, $"ChildrenList<TItem,{runtimeName}>");
  132. }
  133. public void SetFieldToChildrenDictionary(SchemaType.Context ctx, string persistentName, string fieldName)
  134. {
  135. var classType = ctx.FindClass(persistentName);
  136. if (classType == null) return;
  137. var field = classType.UseField(fieldName);
  138. var runtimeName = this.GetRuntimeName(persistentName);
  139. this.SetCollectionContainer(field, $"ChildrenDictionary<TItem,{runtimeName}>");
  140. }
  141. public void DeclareClass(ClassType type)
  142. {
  143. _UseType(type);
  144. foreach(var f in type.Fields)
  145. {
  146. var runtimeName = _SanitizeName(f.PersistentName).Replace("@","at", StringComparison.Ordinal);
  147. SetFieldName(f, $"_{runtimeName}");
  148. SetPropertyName(f, runtimeName);
  149. }
  150. }
  151. public void DeclareEnum(EnumType type)
  152. {
  153. _UseType(type);
  154. foreach (var f in type.Values)
  155. {
  156. // SetFieldName(f, $"_{runtimeName}");
  157. // SetPropertyName(f, runtimeName);
  158. }
  159. }
  160. public void DeclareContext(SchemaType.Context context)
  161. {
  162. foreach(var ctype in context.Classes)
  163. {
  164. DeclareClass(ctype);
  165. }
  166. foreach (var etype in context.Enumerations)
  167. {
  168. DeclareEnum(etype);
  169. }
  170. }
  171. internal string _GetRuntimeName(SchemaType type) { return _GetRuntimeName(type, null); }
  172. private string _GetRuntimeName(SchemaType type, _RuntimeField extra)
  173. {
  174. switch (type)
  175. {
  176. case null: throw new ArgumentNullException(nameof(type));
  177. case ObjectType anyType: return anyType.PersistentName;
  178. case StringType strType: return strType.PersistentName;
  179. case BlittableType blitType:
  180. {
  181. var tname = blitType.DataType.Name;
  182. return blitType.IsNullable ? $"{tname}?" : tname;
  183. }
  184. case ArrayType arrayType:
  185. {
  186. var container = extra?.CollectionContainer;
  187. if (string.IsNullOrWhiteSpace(container)) container = _DefaultCollectionContainer;
  188. return container.Replace("TItem", _GetRuntimeName(arrayType.ItemType), StringComparison.Ordinal);
  189. }
  190. case DictionaryType dictType:
  191. {
  192. var key = _GetRuntimeName(dictType.KeyType);
  193. var val = _GetRuntimeName(dictType.ValueType);
  194. var container = extra?.CollectionContainer ?? string.Empty;
  195. if (container.StartsWith("ChildrenDictionary<"))
  196. {
  197. if (key == "String") return container.Replace("TItem", val, StringComparison.Ordinal);
  198. }
  199. return $"Dictionary<{key},{val}>";
  200. }
  201. case EnumType enumType: return _UseType(enumType).RuntimeName;
  202. case ClassType classType: return _UseType(classType).RuntimeName;
  203. default: throw new NotImplementedException(type.PersistentName);
  204. }
  205. }
  206. private string _GetConstantRuntimeName(SchemaType type)
  207. {
  208. switch (type)
  209. {
  210. case StringType strType: return $"const {typeof(string).Name}";
  211. case BlittableType blitType:
  212. {
  213. var tname = blitType.DataType.Name;
  214. if (blitType.DataType == typeof(int)) return $"const {tname}";
  215. if (blitType.DataType == typeof(float)) return $"const {tname}";
  216. if (blitType.DataType == typeof(double)) return $"const {tname}";
  217. return $"static readonly {tname}";
  218. }
  219. case EnumType enumType: return $"const {_UseType(enumType).RuntimeName}";
  220. case ArrayType aType: return $"static readonly {_UseType(aType).RuntimeName}";
  221. default: throw new NotImplementedException();
  222. }
  223. }
  224. private Object _GetConstantRuntimeValue(SchemaType type, Object value)
  225. {
  226. if (value == null) throw new ArgumentNullException(nameof(value));
  227. switch (type)
  228. {
  229. case StringType _:
  230. return value is string
  231. ? value
  232. : Convert.ChangeType(value, typeof(string), System.Globalization.CultureInfo.InvariantCulture);
  233. case BlittableType btype:
  234. {
  235. if (btype.DataType == typeof(bool).GetTypeInfo())
  236. {
  237. if (value is bool) return value;
  238. var str = value as string;
  239. if (str.ToUpperInvariant() == "FALSE") return false;
  240. if (str.ToUpperInvariant() == "TRUE") return true;
  241. throw new NotImplementedException();
  242. }
  243. return value is string
  244. ? value
  245. : Convert.ChangeType(value, btype.DataType.AsType(), System.Globalization.CultureInfo.InvariantCulture);
  246. }
  247. case EnumType etype:
  248. {
  249. var etypeName = _GetRuntimeName(type);
  250. if (value is string) return $"{etypeName}.{value}";
  251. else return $"({etypeName}){value}";
  252. }
  253. case ArrayType aType:
  254. {
  255. var atypeName = _GetRuntimeName(type);
  256. return value.ToString();
  257. }
  258. default: throw new NotImplementedException();
  259. }
  260. }
  261. #endregion
  262. #region emit
  263. public string EmitContext(SchemaType.Context context)
  264. {
  265. var sb = new StringBuilder();
  266. sb.AppendLine("// <auto-generated/>");
  267. sb.AppendLine();
  268. sb.AppendLine("//------------------------------------------------------------------------------------------------");
  269. sb.AppendLine("// This file has been programatically generated; DON´T EDIT!");
  270. sb.AppendLine("//------------------------------------------------------------------------------------------------");
  271. sb.AppendLine();
  272. sb.AppendLine("#pragma warning disable SA1001");
  273. sb.AppendLine("#pragma warning disable SA1027");
  274. sb.AppendLine("#pragma warning disable SA1028");
  275. sb.AppendLine("#pragma warning disable SA1121");
  276. sb.AppendLine("#pragma warning disable SA1205");
  277. sb.AppendLine("#pragma warning disable SA1309");
  278. sb.AppendLine("#pragma warning disable SA1402");
  279. sb.AppendLine("#pragma warning disable SA1505");
  280. sb.AppendLine("#pragma warning disable SA1507");
  281. sb.AppendLine("#pragma warning disable SA1508");
  282. sb.AppendLine("#pragma warning disable SA1652");
  283. sb.AppendLine();
  284. sb.AppendLine("using System;");
  285. sb.AppendLine("using System.Collections.Generic;");
  286. sb.AppendLine("using System.Linq;");
  287. sb.AppendLine("using System.Text;");
  288. sb.AppendLine("using System.Numerics;");
  289. sb.AppendLine("using System.Text.Json;");
  290. string currentNamespace = null;
  291. void _setCurrentNamespace(string ns)
  292. {
  293. if (currentNamespace == ns) return;
  294. if (currentNamespace != null) sb.AppendLine("}");
  295. currentNamespace = ns;
  296. if (currentNamespace != null)
  297. {
  298. sb.AppendLine();
  299. sb.AppendLine($"namespace {currentNamespace}");
  300. sb.AppendLine("{");
  301. sb.AppendLine("using Collections;".Indent(1));
  302. sb.AppendLine();
  303. }
  304. }
  305. foreach (var etype in context.Enumerations)
  306. {
  307. _setCurrentNamespace(GetRuntimeNamespace(etype.PersistentName));
  308. var cout = EmitEnum(etype);
  309. sb.AppendLine(cout);
  310. sb.AppendLine();
  311. }
  312. foreach (var ctype in context.Classes)
  313. {
  314. if (ctype.IgnoredByEmitter) continue;
  315. _setCurrentNamespace(GetRuntimeNamespace(ctype.PersistentName));
  316. var cout = EmitClass(ctype);
  317. sb.AppendLine(cout);
  318. sb.AppendLine();
  319. }
  320. _setCurrentNamespace(null);
  321. return sb.ToString();
  322. }
  323. public string EmitEnum(EnumType type)
  324. {
  325. var sb = new StringBuilder();
  326. foreach (var l in type.Description.EmitSummary(0)) sb.EmitLine(1, l);
  327. sb.EmitLine(1, $"public enum {_GetRuntimeName(type)}");
  328. sb.EmitLine(1, "{");
  329. if (type.UseIntegers)
  330. {
  331. foreach (var kvp in type.Values)
  332. {
  333. var k = kvp.Key;
  334. sb.EmitLine(2, $"{k} = {kvp.Value},");
  335. }
  336. }
  337. else
  338. {
  339. foreach (var kvp in type.Values)
  340. {
  341. var k = kvp.Key;
  342. sb.EmitLine(2, $"{k},");
  343. }
  344. }
  345. sb.EmitLine(1, "}");
  346. return sb.ToString();
  347. }
  348. public string EmitClass(ClassType type)
  349. {
  350. var xclass = new CSharpClassEmitter(this)
  351. {
  352. ClassSummary = type.Description,
  353. ClassDeclaration = _GetClassDeclaration(type),
  354. HasBaseClass = type.BaseClass != null
  355. };
  356. xclass.AddComments(type);
  357. xclass.AddFields(type);
  358. return String.Join("\r\n",xclass.EmitCode().Indent(1));
  359. }
  360. private string _GetClassDeclaration(ClassType type)
  361. {
  362. var classDecl = string.Empty;
  363. classDecl += "partial ";
  364. classDecl += "class ";
  365. classDecl += _GetRuntimeName(type);
  366. if (type.BaseClass != null) classDecl += $" : {_GetRuntimeName(type.BaseClass)}";
  367. return classDecl;
  368. }
  369. internal IEnumerable<string> _GetClassField(FieldInfo f)
  370. {
  371. var tdecl = _GetRuntimeName(f.FieldType, _UseField(f));
  372. var fname = GetFieldRuntimeName(f);
  373. string defval = string.Empty;
  374. if (f.DefaultValue != null)
  375. {
  376. var tconst = _GetConstantRuntimeName(f.FieldType);
  377. var vconst = _GetConstantRuntimeValue(f.FieldType, f.DefaultValue);
  378. // fix boolean value
  379. if (vconst is Boolean bconst) vconst = bconst ? "true" : "false";
  380. defval = $"{fname}Default";
  381. yield return Invariant($"private {tconst} {defval} = {vconst};");
  382. }
  383. if (f.ExclusiveMinimumValue != null)
  384. {
  385. var tconst = _GetConstantRuntimeName(f.FieldType);
  386. var vconst = _GetConstantRuntimeValue(f.FieldType, f.ExclusiveMinimumValue);
  387. yield return Invariant($"private {tconst} {fname}ExclusiveMinimum = {vconst};");
  388. }
  389. if (f.InclusiveMinimumValue != null)
  390. {
  391. var tconst = _GetConstantRuntimeName(f.FieldType);
  392. var vconst = _GetConstantRuntimeValue(f.FieldType, f.InclusiveMinimumValue);
  393. yield return Invariant($"private {tconst} {fname}Minimum = {vconst};");
  394. }
  395. if (f.InclusiveMaximumValue != null)
  396. {
  397. var tconst = _GetConstantRuntimeName(f.FieldType);
  398. var vconst = _GetConstantRuntimeValue(f.FieldType, f.InclusiveMaximumValue);
  399. yield return Invariant($"private {tconst} {fname}Maximum = {vconst};");
  400. }
  401. if (f.ExclusiveMaximumValue != null)
  402. {
  403. var tconst = _GetConstantRuntimeName(f.FieldType);
  404. var vconst = _GetConstantRuntimeValue(f.FieldType, f.ExclusiveMaximumValue);
  405. yield return Invariant($"private {tconst} {fname}ExclusiveMaximum = {vconst};");
  406. }
  407. if (f.MinItems > 0)
  408. {
  409. yield return $"private const int {fname}MinItems = {f.MinItems};";
  410. }
  411. if (f.MaxItems > 0 && f.MaxItems < int.MaxValue)
  412. {
  413. yield return $"private const int {fname}MaxItems = {f.MaxItems};";
  414. }
  415. if (f.FieldType is EnumType etype && etype.IsNullable) tdecl = tdecl + "?";
  416. yield return string.IsNullOrEmpty(defval) ? $"private {tdecl} {fname};" : $"private {tdecl} {fname} = {defval};";
  417. yield return string.Empty;
  418. }
  419. #endregion
  420. }
  421. /// <summary>
  422. /// Utility class to emit a <see cref="SchemaReflection.ClassType"/>
  423. /// as c# source code
  424. /// </summary>
  425. class CSharpClassEmitter
  426. {
  427. #region constructor
  428. public CSharpClassEmitter(CSharpEmitter emitter)
  429. {
  430. _Emitter = emitter;
  431. }
  432. #endregion
  433. #region data
  434. private readonly CSharpEmitter _Emitter;
  435. private readonly List<string> _Comments = new List<string>();
  436. private readonly List<string> _Fields = new List<string>();
  437. private readonly List<string> _SerializerBody = new List<string>();
  438. private readonly List<string> _DeserializerSwitchBody = new List<string>();
  439. public string ClassSummary { get; set; }
  440. public string ClassDeclaration { get; set; }
  441. public bool HasBaseClass { get; set; }
  442. private const string _READERMODIFIER = "ref ";
  443. #endregion
  444. #region API
  445. public void AddComments(ClassType type)
  446. {
  447. _Comments.AddRange(_Emitter.GetRuntimeComments(type));
  448. }
  449. public void AddFields(ClassType type)
  450. {
  451. foreach (var f in type.Fields)
  452. {
  453. var trname = _Emitter._GetRuntimeName(f.FieldType);
  454. var frname = _Emitter.GetFieldRuntimeName(f);
  455. _Fields.AddRange(_Emitter._GetClassField(f));
  456. if (f.FieldType is EnumType etype)
  457. {
  458. // emit serializer
  459. var smethod = etype.UseIntegers ? "SerializePropertyEnumValue" : "SerializePropertyEnumSymbol";
  460. smethod = $"{smethod}<{trname}>(writer, \"{f.PersistentName}\", {frname}";
  461. if (f.DefaultValue != null) smethod += $", {frname}Default";
  462. smethod += ");";
  463. this.AddFieldSerializerCase(smethod);
  464. // emit deserializer
  465. this.AddFieldDeserializerCase(f.PersistentName, $"{frname} = DeserializePropertyValue<{_Emitter._GetRuntimeName(etype)}>({_READERMODIFIER}reader);");
  466. continue;
  467. }
  468. this.AddFieldSerializerCase(_GetJSonSerializerMethod(f));
  469. this.AddFieldDeserializerCase(f.PersistentName, _GetJSonDeserializerMethod(f));
  470. }
  471. }
  472. private string _GetJSonSerializerMethod(FieldInfo f)
  473. {
  474. var pname = f.PersistentName;
  475. var fname = _Emitter.GetFieldRuntimeName(f);
  476. if (f.FieldType is ClassType ctype)
  477. {
  478. return $"SerializePropertyObject(writer, \"{pname}\", {fname});";
  479. }
  480. if (f.FieldType is ArrayType atype)
  481. {
  482. if (f.MinItems > 0) return $"SerializeProperty(writer, \"{pname}\", {fname}, {fname}MinItems);";
  483. return $"SerializeProperty(writer,\"{pname}\",{fname});";
  484. }
  485. if (f.DefaultValue != null) return $"SerializeProperty(writer, \"{pname}\", {fname}, {fname}Default);";
  486. return $"SerializeProperty(writer, \"{pname}\", {fname});";
  487. }
  488. private string _GetJSonDeserializerMethod(FieldInfo f)
  489. {
  490. var fname = _Emitter.GetFieldRuntimeName(f);
  491. var ownerTypeName = _Emitter._GetRuntimeName(f.DeclaringClass);
  492. if (f.FieldType is ArrayType atype)
  493. {
  494. var titem = _Emitter._GetRuntimeName(atype.ItemType);
  495. return $"DeserializePropertyList<{ownerTypeName}, {titem}>({_READERMODIFIER}reader, this, {fname});";
  496. }
  497. else if (f.FieldType is DictionaryType dtype)
  498. {
  499. var titem = _Emitter._GetRuntimeName(dtype.ValueType);
  500. return $"DeserializePropertyDictionary<{ownerTypeName}, {titem}>({_READERMODIFIER}reader, this, {fname});";
  501. }
  502. var fieldTypeName = _Emitter._GetRuntimeName(f.FieldType);
  503. return $"DeserializePropertyValue<{ownerTypeName}, {fieldTypeName}>({_READERMODIFIER}reader, this, out {fname});";
  504. }
  505. public void AddFieldSerializerCase(string line) { _SerializerBody.Add(line); }
  506. public void AddFieldDeserializerCase(string persistentName, string line)
  507. {
  508. _DeserializerSwitchBody.Add($"case \"{persistentName}\": {line} break;");
  509. }
  510. public IEnumerable<string> EmitCode()
  511. {
  512. #if USENEWTONSOFT
  513. var readerType = "JsonReader";
  514. var writerType = "JsonWriter";
  515. #else
  516. var readerType = "ref Utf8JsonReader";
  517. var writerType = "Utf8JsonWriter";
  518. #endif
  519. foreach (var l in _Comments) yield return $"// {l}";
  520. foreach (var l in ClassSummary.EmitSummary(0)) yield return l;
  521. yield return "#if NET6_0_OR_GREATER";
  522. yield return "[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]";
  523. yield return "#endif";
  524. yield return "[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"SharpGLTF.CodeGen\", \"1.0.0.0\")]";
  525. yield return ClassDeclaration;
  526. yield return "{";
  527. yield return string.Empty;
  528. foreach (var l in _Fields.Indent(1)) yield return l;
  529. yield return string.Empty;
  530. // yield return "/// <inheritdoc />".Indent(1);
  531. yield return $"protected override void SerializeProperties({writerType} writer)".Indent(1);
  532. yield return "{".Indent(1);
  533. if (HasBaseClass) yield return "base.SerializeProperties(writer);".Indent(2);
  534. foreach (var l in _SerializerBody.Indent(2)) yield return l;
  535. yield return "}".Indent(1);
  536. yield return string.Empty;
  537. // yield return "/// <inheritdoc />".Indent(1);
  538. yield return $"protected override void DeserializeProperty(string jsonPropertyName, {readerType} reader)".Indent(1);
  539. yield return "{".Indent(1);
  540. yield return "switch (jsonPropertyName)".Indent(2);
  541. yield return "{".Indent(2);
  542. foreach (var l in _DeserializerSwitchBody.Indent(3)) yield return l;
  543. if (HasBaseClass) yield return $"default: base.DeserializeProperty(jsonPropertyName,{_READERMODIFIER}reader); break;".Indent(3);
  544. else yield return "default: throw new NotImplementedException();".Indent(3);
  545. yield return "}".Indent(2);
  546. yield return "}".Indent(1);
  547. yield return string.Empty;
  548. yield return "}";
  549. }
  550. #endregion
  551. }
  552. }