Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AttributeCollection.cs
Go to the documentation of this file.
4
6
8{
9 private struct AttributeEntry
10 {
11 public Type type;
12
13 public int index;
14 }
15
16 public static readonly AttributeCollection Empty = new AttributeCollection((Attribute[]?)null);
17
19
20 private readonly Attribute[] _attributes;
21
22 private static readonly object s_internalSyncObject = new object();
23
25
26 private int _index;
27
28 protected internal virtual Attribute[] Attributes => _attributes;
29
30 public int Count => Attributes.Length;
31
32 public virtual Attribute this[int index] => Attributes[index];
33
34 public virtual Attribute? this[[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields)] Type attributeType]
35 {
36 get
37 {
38 if (attributeType == null)
39 {
40 throw new ArgumentNullException("attributeType");
41 }
43 {
44 if (_foundAttributeTypes == null)
45 {
47 }
48 int i;
49 for (i = 0; i < 5; i++)
50 {
52 {
54 if (index != -1)
55 {
56 return Attributes[index];
57 }
59 }
60 if (_foundAttributeTypes[i].type == null)
61 {
62 break;
63 }
64 }
65 i = _index++;
66 if (_index >= 5)
67 {
68 _index = 0;
69 }
71 int num = Attributes.Length;
72 for (int j = 0; j < num; j++)
73 {
74 Attribute attribute = Attributes[j];
75 Type type = attribute.GetType();
76 if (type == attributeType)
77 {
79 return attribute;
80 }
81 }
82 for (int k = 0; k < num; k++)
83 {
84 Attribute attribute2 = Attributes[k];
85 if (attributeType.IsInstanceOfType(attribute2))
86 {
88 return attribute2;
89 }
90 }
93 }
94 }
95 }
96
97 bool ICollection.IsSynchronized => false;
98
99 object ICollection.SyncRoot => this;
100
101 int ICollection.Count => Count;
102
103 public AttributeCollection(params Attribute[]? attributes)
104 {
105 _attributes = attributes ?? Array.Empty<Attribute>();
106 for (int i = 0; i < _attributes.Length; i++)
107 {
108 if (_attributes[i] == null)
109 {
110 throw new ArgumentNullException("attributes");
111 }
112 }
113 }
114
116 : this(Array.Empty<Attribute>())
117 {
118 }
119
120 public static AttributeCollection FromExisting(AttributeCollection existing, params Attribute[]? newAttributes)
121 {
122 if (existing == null)
123 {
124 throw new ArgumentNullException("existing");
125 }
126 if (newAttributes == null)
127 {
128 newAttributes = Array.Empty<Attribute>();
129 }
130 Attribute[] array = new Attribute[existing.Count + newAttributes.Length];
131 int count = existing.Count;
132 existing.CopyTo(array, 0);
133 for (int i = 0; i < newAttributes.Length; i++)
134 {
135 if (newAttributes[i] == null)
136 {
137 throw new ArgumentNullException("newAttributes");
138 }
139 bool flag = false;
140 for (int j = 0; j < existing.Count; j++)
141 {
142 if (array[j].TypeId.Equals(newAttributes[i].TypeId))
143 {
144 flag = true;
145 array[j] = newAttributes[i];
146 break;
147 }
148 }
149 if (!flag)
150 {
151 array[count++] = newAttributes[i];
152 }
153 }
154 Attribute[] array2;
155 if (count < array.Length)
156 {
157 array2 = new Attribute[count];
158 Array.Copy(array, array2, count);
159 }
160 else
161 {
162 array2 = array;
163 }
164 return new AttributeCollection(array2);
165 }
166
167 [RequiresUnreferencedCode("The public parameterless constructor or the 'Default' static field may be trimmed from the Attribute's Type.")]
168 public bool Contains(Attribute? attribute)
169 {
170 if (attribute == null)
171 {
172 return false;
173 }
174 return this[attribute.GetType()]?.Equals(attribute) ?? false;
175 }
176
177 [RequiresUnreferencedCode("The public parameterless constructor or the 'Default' static field may be trimmed from the Attribute's Type.")]
178 public bool Contains(Attribute[]? attributes)
179 {
180 if (attributes == null)
181 {
182 return true;
183 }
184 for (int i = 0; i < attributes.Length; i++)
185 {
186 if (!Contains(attributes[i]))
187 {
188 return false;
189 }
190 }
191 return true;
192 }
193
194 protected Attribute? GetDefaultAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields)] Type attributeType)
195 {
196 if (attributeType == null)
197 {
198 throw new ArgumentNullException("attributeType");
199 }
201 {
202 if (s_defaultAttributes == null)
203 {
205 }
206 if (s_defaultAttributes.ContainsKey(attributeType))
207 {
209 }
210 Attribute attribute = null;
212 FieldInfo field = reflectionType.GetField("Default", BindingFlags.Static | BindingFlags.Public | BindingFlags.GetField);
213 if (field != null && field.IsStatic)
214 {
215 attribute = (Attribute)field.GetValue(null);
216 }
217 else
218 {
220 if (constructor != null)
221 {
222 attribute = (Attribute)constructor.Invoke(Array.Empty<object>());
223 if (!attribute.IsDefaultAttribute())
224 {
225 attribute = null;
226 }
227 }
228 }
230 return attribute;
231 }
232 }
233
235 {
236 return Attributes.GetEnumerator();
237 }
238
239 public bool Matches(Attribute? attribute)
240 {
241 for (int i = 0; i < Attributes.Length; i++)
242 {
243 if (Attributes[i].Match(attribute))
244 {
245 return true;
246 }
247 }
248 return false;
249 }
250
251 public bool Matches(Attribute[]? attributes)
252 {
253 if (attributes == null)
254 {
255 return true;
256 }
257 for (int i = 0; i < attributes.Length; i++)
258 {
259 if (!Matches(attributes[i]))
260 {
261 return false;
262 }
263 }
264 return true;
265 }
266
271
272 public void CopyTo(Array array, int index)
273 {
275 }
276}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
virtual bool IsDefaultAttribute()
Definition Attribute.cs:791
Attribute? GetDefaultAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor|DynamicallyAccessedMemberTypes.PublicFields)] Type attributeType)
AttributeCollection(params Attribute[]? attributes)
virtual ? Attribute this[[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor|DynamicallyAccessedMemberTypes.PublicFields)] Type attributeType
static AttributeCollection FromExisting(AttributeCollection existing, params Attribute[]? newAttributes)
static Type GetReflectionType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor|DynamicallyAccessedMemberTypes.PublicFields)] Type type)
object Invoke(object?[]? parameters)
object? GetValue(object? obj)
Type UnderlyingSystemType
Definition Type.cs:61
FieldInfo? GetField(string name)
Definition Type.cs:607
ConstructorInfo? GetConstructor(Type[] types)
Definition Type.cs:542
static readonly Type[] EmptyTypes
Definition Type.cs:19