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