Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReferenceConverter.cs
Go to the documentation of this file.
5
7
9{
10 private struct ReferenceComparer : IComparer<object>
11 {
13
18
19 public int Compare(object item1, object item2)
20 {
23 return string.Compare(strA, strB, StringComparison.InvariantCulture);
24 }
25 }
26
27 private static readonly string s_none = System.SR.toStringNone;
28
29 private readonly Type _type;
30
32 {
33 _type = type;
34 }
35
36 public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
37 {
38 if (sourceType == typeof(string) && context != null)
39 {
40 return true;
41 }
42 return base.CanConvertFrom(context, sourceType);
43 }
44
45 public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
46 {
47 if (value is string text)
48 {
49 string text2 = text.Trim();
50 if (!string.Equals(text2, s_none) && context != null)
51 {
53 {
54 object reference = referenceService.GetReference(text2);
55 if (reference != null)
56 {
57 return reference;
58 }
59 }
60 IContainer container = context.Container;
61 if (container != null)
62 {
63 object obj = container.Components[text2];
64 if (obj != null)
65 {
66 return obj;
67 }
68 }
69 }
70 return null;
71 }
72 return base.ConvertFrom(context, culture, value);
73 }
74
75 public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
76 {
77 if (destinationType == typeof(string))
78 {
79 if (value != null)
80 {
82 {
83 string name = referenceService.GetName(value);
84 if (name != null)
85 {
86 return name;
87 }
88 }
90 {
91 string text = component.Site?.Name;
92 if (text != null)
93 {
94 return text;
95 }
96 }
97 return string.Empty;
98 }
99 return s_none;
100 }
101 return base.ConvertTo(context, culture, value, destinationType);
102 }
103
105 {
106 List<object> list = null;
107 if (context != null)
108 {
109 list = new List<object> { null };
111 {
112 object[] references = referenceService.GetReferences(_type);
113 if (references != null)
114 {
115 for (int i = 0; i < references.Length; i++)
116 {
117 if (IsValueAllowed(context, references[i]))
118 {
119 list.Add(references[i]);
120 }
121 }
122 }
123 }
124 else
125 {
126 IContainer container = context.Container;
127 if (container != null)
128 {
130 foreach (IComponent item in components)
131 {
132 if (item != null && _type != null && _type.IsInstanceOfType(item) && IsValueAllowed(context, item))
133 {
134 list.Add(item);
135 }
136 }
137 }
138 }
139 list.Sort(new ReferenceComparer(this));
140 }
141 return new StandardValuesCollection(list);
142 }
143
145 {
146 return true;
147 }
148
150 {
151 return true;
152 }
153
154 protected virtual bool IsValueAllowed(ITypeDescriptorContext context, object value)
155 {
156 return true;
157 }
158}
void Add(TKey key, TValue value)
override? object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
virtual bool IsValueAllowed(ITypeDescriptorContext context, object value)
override? object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
override bool GetStandardValuesSupported(ITypeDescriptorContext? context)
override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
override bool GetStandardValuesExclusive(ITypeDescriptorContext? context)
override StandardValuesCollection GetStandardValues(ITypeDescriptorContext? context)
string? ConvertToString(object? value)
static bool IsComObject(object o)
Definition Marshal.cs:353
static string toStringNone
Definition SR.cs:128
Definition SR.cs:7
virtual bool IsInstanceOfType([NotNullWhen(true)] object? o)
Definition Type.cs:1020
ComponentCollection Components
Definition IContainer.cs:7
object? GetService(Type serviceType)