No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnitCategory.cs 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting.FullSerializer;
  4. namespace Unity.VisualScripting
  5. {
  6. [AttributeUsage(AttributeTargets.Class)]
  7. [fsObject(Converter = typeof(UnitCategoryConverter))]
  8. public class UnitCategory : Attribute
  9. {
  10. public UnitCategory(string fullName)
  11. {
  12. Ensure.That(nameof(fullName)).IsNotNull(fullName);
  13. fullName = fullName.Replace('\\', '/');
  14. this.fullName = fullName;
  15. var parts = fullName.Split('/');
  16. name = parts[parts.Length - 1];
  17. if (parts.Length > 1)
  18. {
  19. root = new UnitCategory(parts[0]);
  20. parent = new UnitCategory(fullName.Substring(0, fullName.LastIndexOf('/')));
  21. }
  22. else
  23. {
  24. root = this;
  25. isRoot = true;
  26. }
  27. }
  28. public UnitCategory root { get; }
  29. public UnitCategory parent { get; }
  30. public string fullName { get; }
  31. public string name { get; }
  32. public bool isRoot { get; }
  33. public IEnumerable<UnitCategory> ancestors
  34. {
  35. get
  36. {
  37. var ancestor = parent;
  38. while (ancestor != null)
  39. {
  40. yield return ancestor;
  41. ancestor = ancestor.parent;
  42. }
  43. }
  44. }
  45. public IEnumerable<UnitCategory> AndAncestors()
  46. {
  47. yield return this;
  48. foreach (var ancestor in ancestors)
  49. {
  50. yield return ancestor;
  51. }
  52. }
  53. public override bool Equals(object obj)
  54. {
  55. return obj is UnitCategory && ((UnitCategory)obj).fullName == fullName;
  56. }
  57. public override int GetHashCode()
  58. {
  59. return fullName.GetHashCode();
  60. }
  61. public override string ToString()
  62. {
  63. return fullName;
  64. }
  65. public static bool operator ==(UnitCategory a, UnitCategory b)
  66. {
  67. if (ReferenceEquals(a, b))
  68. {
  69. return true;
  70. }
  71. if ((object)a == null || (object)b == null)
  72. {
  73. return false;
  74. }
  75. return a.Equals(b);
  76. }
  77. public static bool operator !=(UnitCategory a, UnitCategory b)
  78. {
  79. return !(a == b);
  80. }
  81. }
  82. }