BaseBuilder.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SharpGLTF
  5. {
  6. public abstract class BaseBuilder
  7. {
  8. #region lifecycle
  9. protected BaseBuilder() { }
  10. protected BaseBuilder(string name)
  11. {
  12. this.Name = name;
  13. }
  14. protected BaseBuilder(string name, IO.JsonContent extras)
  15. {
  16. this.Name = name;
  17. this.Extras = extras;
  18. }
  19. protected BaseBuilder(BaseBuilder other)
  20. {
  21. Guard.NotNull(other, nameof(other));
  22. this.Name = other.Name;
  23. this.Extras = other.Extras.DeepClone();
  24. }
  25. #endregion
  26. #region data
  27. /// <summary>
  28. /// Gets or sets the display text name, or null.
  29. /// <para><b>⚠️ DO NOT USE AS AN OBJECT ID ⚠️</b> see remarks.</para>
  30. /// </summary>
  31. /// <remarks>
  32. /// glTF does not define any rule for object names.<br/>
  33. /// This means that names can be null or non unique.<br/>
  34. /// So don't use <see cref="Name"/> for anything other than object name display.<br/>
  35. /// If you need to reference objects by some ID, use lookup tables instead.
  36. /// </remarks>
  37. public string Name { get; set; }
  38. /// <summary>
  39. /// Gets or sets the custom data of this object.
  40. /// </summary>
  41. public IO.JsonContent Extras { get; set; }
  42. protected static int GetContentHashCode(BaseBuilder x)
  43. {
  44. return x?.Name?.GetHashCode(StringComparison.InvariantCulture) ?? 0;
  45. }
  46. protected static bool AreEqualByContent(BaseBuilder x, BaseBuilder y)
  47. {
  48. if ((x, y).AreSameReference(out bool areTheSame)) return areTheSame;
  49. if (x.Name != y.Name) return false;
  50. return IO.JsonContent.AreEqualByContent(x.Extras, y.Extras, 0.0001f);
  51. }
  52. #endregion
  53. #region API
  54. internal void SetNameAndExtrasFrom(BaseBuilder source)
  55. {
  56. this.Name = source.Name;
  57. this.Extras = source.Extras.DeepClone();
  58. }
  59. internal void SetNameAndExtrasFrom(Schema2.LogicalChildOfRoot source)
  60. {
  61. this.Name = source.Name;
  62. this.Extras = source.Extras.DeepClone();
  63. }
  64. /// <summary>
  65. /// Copies the Name and Extras values to <paramref name="target"/> only if the values are defined.
  66. /// </summary>
  67. /// <param name="target">The target object</param>
  68. internal void TryCopyNameAndExtrasTo(Schema2.LogicalChildOfRoot target)
  69. {
  70. if (this.Name != null) target.Name = this.Name;
  71. if (this.Extras.Content != null) target.Extras = this.Extras.DeepClone();
  72. }
  73. #endregion
  74. }
  75. }