Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Container.cs
Go to the documentation of this file.
2
4
6{
7 private sealed class Site : ISite, IServiceProvider
8 {
9 private string _name;
10
11 public IComponent Component { get; }
12
13 public IContainer Container { get; }
14
15 public bool DesignMode => false;
16
17 public string Name
18 {
19 get
20 {
21 return _name;
22 }
23 [RequiresUnreferencedCode("The Type of components in the container cannot be statically discovered to validate the name.")]
24 set
25 {
26 if (value == null || _name == null || !value.Equals(_name))
27 {
28 ((Container)Container).ValidateName(Component, value);
29 _name = value;
30 }
31 }
32 }
33
34 internal Site(IComponent component, Container container, string name)
35 {
36 Component = component;
37 Container = container;
38 _name = name;
39 }
40
41 public object GetService(Type service)
42 {
43 if (!(service == typeof(ISite)))
44 {
45 return ((Container)Container).GetService(service);
46 }
47 return this;
48 }
49 }
50
51 private ISite[] _sites;
52
53 private int _siteCount;
54
56
58
59 private bool _checkedFilter;
60
61 private readonly object _syncObj = new object();
62
64 {
65 get
66 {
67 lock (_syncObj)
68 {
69 if (_components == null)
70 {
72 for (int i = 0; i < _siteCount; i++)
73 {
74 array[i] = _sites[i].Component;
75 }
77 if (_filter == null && _checkedFilter)
78 {
79 _checkedFilter = false;
80 }
81 }
82 if (!_checkedFilter)
83 {
85 _checkedFilter = true;
86 }
87 if (_filter != null)
88 {
90 if (componentCollection != null)
91 {
92 _components = componentCollection;
93 }
94 }
95 return _components;
96 }
97 }
98 }
99
101 {
102 Dispose(disposing: false);
103 }
104
105 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "No name is provided.")]
106 public virtual void Add(IComponent? component)
107 {
108 Add(component, null);
109 }
110
111 [RequiresUnreferencedCode("The Type of components in the container cannot be statically discovered to validate the name.")]
112 public virtual void Add(IComponent? component, string? name)
113 {
114 lock (_syncObj)
115 {
116 if (component == null)
117 {
118 return;
119 }
120 ISite site = component.Site;
121 if (site != null && site.Container == this)
122 {
123 return;
124 }
125 if (_sites == null)
126 {
127 _sites = new ISite[4];
128 }
129 else
130 {
131 ValidateName(component, name);
132 if (_sites.Length == _siteCount)
133 {
134 ISite[] array = new ISite[_siteCount * 2];
136 _sites = array;
137 }
138 }
139 site?.Container.Remove(component);
140 ISite site2 = CreateSite(component, name);
141 _sites[_siteCount++] = site2;
142 component.Site = site2;
143 _components = null;
144 }
145 }
146
147 protected virtual ISite CreateSite(IComponent component, string? name)
148 {
149 return new Site(component, this, name);
150 }
151
152 public void Dispose()
153 {
154 Dispose(disposing: true);
155 GC.SuppressFinalize(this);
156 }
157
158 protected virtual void Dispose(bool disposing)
159 {
160 if (!disposing)
161 {
162 return;
163 }
164 lock (_syncObj)
165 {
166 while (_siteCount > 0)
167 {
168 ISite site = _sites[--_siteCount];
169 site.Component.Site = null;
170 site.Component.Dispose();
171 }
172 _sites = null;
173 _components = null;
174 }
175 }
176
177 protected virtual object? GetService(Type service)
178 {
179 if (!(service == typeof(IContainer)))
180 {
181 return null;
182 }
183 return this;
184 }
185
186 public virtual void Remove(IComponent? component)
187 {
188 Remove(component, preserveSite: false);
189 }
190
191 private void Remove(IComponent component, bool preserveSite)
192 {
193 lock (_syncObj)
194 {
195 ISite site = component?.Site;
196 if (site == null || site.Container != this)
197 {
198 return;
199 }
200 if (!preserveSite)
201 {
202 component.Site = null;
203 }
204 for (int i = 0; i < _siteCount; i++)
205 {
206 if (_sites[i] == site)
207 {
208 _siteCount--;
209 Array.Copy(_sites, i + 1, _sites, i, _siteCount - i);
210 _sites[_siteCount] = null;
211 _components = null;
212 break;
213 }
214 }
215 }
216 }
217
218 protected void RemoveWithoutUnsiting(IComponent? component)
219 {
220 Remove(component, preserveSite: true);
221 }
222
223 [RequiresUnreferencedCode("The Type of components in the container cannot be statically discovered.")]
224 protected virtual void ValidateName(IComponent component, string? name)
225 {
226 if (component == null)
227 {
228 throw new ArgumentNullException("component");
229 }
230 if (name == null)
231 {
232 return;
233 }
234 for (int i = 0; i < Math.Min(_siteCount, _sites.Length); i++)
235 {
236 ISite site = _sites[i];
237 if (site != null && site.Name != null && string.Equals(site.Name, name, StringComparison.OrdinalIgnoreCase) && site.Component != component)
238 {
240 if (inheritanceAttribute.InheritanceLevel != InheritanceLevel.InheritedReadOnly)
241 {
243 }
244 }
245 }
246 }
247}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
virtual ComponentCollection FilterComponents(ComponentCollection components)
object GetService(Type service)
Definition Container.cs:41
Site(IComponent component, Container container, string name)
Definition Container.cs:34
virtual ComponentCollection Components
Definition Container.cs:64
virtual void ValidateName(IComponent component, string? name)
Definition Container.cs:224
virtual ? object GetService(Type service)
Definition Container.cs:177
ContainerFilterService _filter
Definition Container.cs:57
virtual void Dispose(bool disposing)
Definition Container.cs:158
virtual void Add(IComponent? component)
Definition Container.cs:106
ComponentCollection _components
Definition Container.cs:55
virtual void Add(IComponent? component, string? name)
Definition Container.cs:112
virtual void Remove(IComponent? component)
Definition Container.cs:186
virtual ISite CreateSite(IComponent component, string? name)
Definition Container.cs:147
void RemoveWithoutUnsiting(IComponent? component)
Definition Container.cs:218
void Remove(IComponent component, bool preserveSite)
Definition Container.cs:191
static AttributeCollection GetAttributes([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType)
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string DuplicateComponentName
Definition SR.cs:88
Definition SR.cs:7
void Remove(IComponent? component)
IContainer? Container
Definition ISite.cs:9