A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using System.Collections.ObjectModel;
3
4namespace Unity.VisualScripting
5{
6 public sealed class Namespace
7 {
8 private Namespace(string fullName)
9 {
10 FullName = fullName;
11
12 if (fullName != null)
13 {
14 var parts = fullName.Split('.');
15
16 Name = parts[parts.Length - 1];
17
18 if (parts.Length > 1)
19 {
20 Root = parts[0];
21 Parent = fullName.Substring(0, fullName.LastIndexOf('.'));
22 }
23 else
24 {
25 Root = this;
26 IsRoot = true;
27 Parent = Global;
28 }
29 }
30 else
31 {
32 Root = this;
33 IsRoot = true;
34 IsGlobal = true;
35 }
36 }
37
38 public Namespace Root { get; }
39 public Namespace Parent { get; }
40 public string FullName { get; }
41 public string Name { get; }
42 public bool IsRoot { get; }
43 public bool IsGlobal { get; }
44
45 public IEnumerable<Namespace> Ancestors
46 {
47 get
48 {
49 var ancestor = Parent;
50
51 while (ancestor != null)
52 {
53 yield return ancestor;
54 ancestor = ancestor.Parent;
55 }
56 }
57 }
58
59 public IEnumerable<Namespace> AndAncestors()
60 {
61 yield return this;
62
63 foreach (var ancestor in Ancestors)
64 {
65 yield return ancestor;
66 }
67 }
68
69 public override int GetHashCode()
70 {
71 if (FullName == null)
72 {
73 return 0;
74 }
75
76 return FullName.GetHashCode();
77 }
78
79 public override string ToString()
80 {
81 return FullName;
82 }
83
84 static Namespace()
85 {
86 collection = new Collection();
87 }
88
89 private static readonly Collection collection;
90
91 public static Namespace Global { get; } = new Namespace(null);
92
93 public static Namespace FromFullName(string fullName)
94 {
95 if (fullName == null)
96 {
97 return Global;
98 }
99
100 Namespace @namespace;
101
102 if (!collection.TryGetValue(fullName, out @namespace))
103 {
104 @namespace = new Namespace(fullName);
105 collection.Add(@namespace);
106 }
107
108 return @namespace;
109 }
110
111 public override bool Equals(object obj)
112 {
113 var other = obj as Namespace;
114
115 if (other == null)
116 {
117 return false;
118 }
119
120 return FullName == other.FullName;
121 }
122
123 public static implicit operator Namespace(string fullName)
124 {
125 return FromFullName(fullName);
126 }
127
128 public static implicit operator string(Namespace @namespace)
129 {
130 return @namespace.FullName;
131 }
132
133 public static bool operator ==(Namespace a, Namespace b)
134 {
135 if (ReferenceEquals(a, b))
136 {
137 return true;
138 }
139
140 if (((object)a == null) || ((object)b == null))
141 {
142 return false;
143 }
144
145 return a.Equals(b);
146 }
147
148 public static bool operator !=(Namespace a, Namespace b)
149 {
150 return !(a == b);
151 }
152
153 private class Collection : KeyedCollection<string, Namespace>, IKeyedCollection<string, Namespace>
154 {
155 protected override string GetKeyForItem(Namespace item)
156 {
157 return item.FullName;
158 }
159
160 public new bool TryGetValue(string key, out Namespace value)
161 {
162 if (Dictionary == null)
163 {
164 value = default(Namespace);
165 return false;
166 }
167
168 return Dictionary.TryGetValue(key, out value);
169 }
170 }
171 }
172}