/
TypeReference.cs
262 lines (220 loc) · 9.45 KB
/
TypeReference.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
namespace TypeReferences
{
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using SolidUtilities;
using UnityEngine;
using UnityEngine.Serialization;
/// <summary>
/// Reference to <see cref="System.Type"/> with support for Unity serialization.
/// </summary>
[Serializable]
public partial class TypeReference : ISerializationCallbackReceiver
{
private static readonly HashSet<string> _reportedMissingValues = new HashSet<string>();
[SerializeField] internal bool GuidAssignmentFailed;
[SerializeField] internal string GUID;
[FormerlySerializedAs("TypeNameAndAssembly")]
[SerializeField] internal string _typeNameAndAssembly;
public string TypeNameAndAssembly => _typeNameAndAssembly;
[SerializeField] internal bool _suppressLogs;
private Type _type;
private bool _needToLogTypeNotFound;
private bool _typeChanged;
/// <summary>
/// Initializes a new instance of the <see cref="TypeReference"/> class with Type equal to null.
/// </summary>
/// <remarks>
/// The fully empty constructor without even named parameters is needed for the class to be deserialized in
/// IL2CPP builds. For some reason, if the class doesn't have a fully empty constructor, OnAfterDeserialize
/// will not be called in IL2CPP builds.</remarks>
public TypeReference()
{
_suppressLogs = false;
}
/// <summary>
/// Initializes a new instance of the <see cref="TypeReference"/> class with Type equal to null.
/// </summary>
/// <param name="suppressLogs">
/// Whether to suppress logs that show up when a type disappeared or was renamed. Default is <c>false</c>.
/// </param>
public TypeReference(bool suppressLogs = false)
{
_suppressLogs = suppressLogs;
}
/// <summary>
/// Initializes a new instance of the <see cref="TypeReference"/> class
/// with Type equal to the name of the type passed in.
/// </summary>
/// <param name="assemblyQualifiedTypeName">Assembly qualified type name.</param>
/// <param name="guid">The GUID of a script where the type is located. If null, it will be found automatically.</param>
/// <param name="suppressLogs">
/// Whether to suppress logs that show up when a type disappeared or was renamed. Default is <c>false</c>.
/// </param>
public TypeReference(string assemblyQualifiedTypeName, string guid = null, bool suppressLogs = false)
: this(suppressLogs)
{
Type = IsNotEmpty(assemblyQualifiedTypeName)
? Type.GetType(assemblyQualifiedTypeName)
: null;
GUID = guid;
}
/// <summary>
/// Initializes a new instance of the <see cref="TypeReference"/> class
/// with Type equal to the passed type.
/// </summary>
/// <param name="type">Type.</param>
/// <param name="guid">The GUID of a script where the type is located. If null, it will be found automatically.</param>
/// <param name="suppressLogs">
/// Whether to suppress logs that show up when a type disappeared or was renamed. Default is <c>false</c>.
/// </param>
/// <exception cref="System.ArgumentException">
/// If <paramref name="type"/> does not have a full name.
/// </exception>
public TypeReference(Type type, string guid = null, bool suppressLogs = false)
: this(suppressLogs)
{
Type = type;
GUID = guid;
}
/// <summary>
/// Gets or sets type of class reference.
/// </summary>
/// <exception cref="System.ArgumentException">
/// If <paramref name="value"/> does not have a full name.
/// </exception>
public Type Type
{
get
{
// Workaround for a bug in Unity where a SerializeReference field affects the deserialization of a SerializeField field placed nearby.
if (_type == null && IsNotEmpty(_typeNameAndAssembly))
_type = TryGetTypeFromSerializedFields();
return _type;
}
set
{
MakeSureTypeHasName(value);
if (_type == value)
return;
_type = value;
_typeNameAndAssembly = GetTypeNameAndAssembly(value);
_typeChanged = true;
}
}
public static implicit operator Type(TypeReference typeReference) => typeReference?.Type;
public static implicit operator TypeReference(Type type) => new TypeReference(type);
public override string ToString() => (Type == null || Type.FullName == null) ? "None" : Type.FullName;
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
_type = IsNotEmpty(_typeNameAndAssembly) ? TryGetTypeFromSerializedFields() : null;
SubscribeToDelayCall();
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
if (_typeChanged)
{
SetClassGuidIfExists(Type);
_typeChanged = false;
}
UnsubscribeFromDelayCall();
}
public static string GetTypeNameFromNameAndAssembly(string typeNameAndAssembly)
{
// Remove the assembly name first, then remove the namespace from full type name.
return typeNameAndAssembly.GetSubstringBefore(',').GetSubstringAfterLast('.');
}
internal static string GetTypeNameAndAssembly(Type type)
{
MakeSureTypeHasName(type);
return type != null
? $"{type.FullName}, {type.GetShortAssemblyName()}"
: string.Empty;
}
/// <summary>
/// Get GUID of the file that contains the class of the given type.
/// It works only for MonoBehaviours, ScriptableObjects, and other classes
/// where the name of the file must match the class name.
/// </summary>
/// <param name="type">Type of the class to search for.</param>
/// <returns>String representing the GUID of the file, or empty string if no file found.</returns>
[NotNull]
internal static string GetClassGUID(Type type)
{
if (type == null || type.FullName == null)
return string.Empty;
return GetGUIDFromType(type);
}
private static bool IsNotEmpty(string value) => ! string.IsNullOrEmpty(value);
private static void MakeSureTypeHasName(Type type)
{
if (type != null && type.FullName == null)
throw new ArgumentException($"'{type}' does not have full name.", nameof(type));
}
private void LogTypeNotFound()
{
if (_suppressLogs)
return;
if (_reportedMissingValues.Contains(_typeNameAndAssembly))
return;
ReportObjectsWithMissingValue(_typeNameAndAssembly);
_reportedMissingValues.Add(_typeNameAndAssembly);
}
/// <summary>
/// Sometimes, the fact that the type disappeared is found during the deserialization. A warning cannot be
/// logged during the deserialization, so it must be delayed with help of this method.
/// </summary>
private void LogTypeNotFoundIfNeeded()
{
if (! _needToLogTypeNotFound)
return;
LogTypeNotFound();
_needToLogTypeNotFound = false;
}
private void SetClassGuidIfExists(Type type)
{
// common case optimization
if (TypeCannotHaveGUID())
{
GUID = string.Empty;
return;
}
try
{
GUID = GetClassGUID(type);
}
catch (UnityException) // thrown on assembly recompiling if field initialization is used on field.
{
// GUID will be found in SerializedTypeReference.SetGuidIfAssignmentFailed()
GuidAssignmentFailed = true;
GUID = string.Empty;
}
}
private bool TypeCannotHaveGUID()
{
if (string.IsNullOrEmpty(_typeNameAndAssembly))
return false;
int charAfterWhiteSpace = _typeNameAndAssembly.IndexOf(' ') + 1;
string assemblyName = _typeNameAndAssembly.Substring(
charAfterWhiteSpace,
_typeNameAndAssembly.Length - charAfterWhiteSpace);
return assemblyName == "mscorlib"
|| assemblyName == "netstandard"
|| assemblyName.StartsWith("System.")
|| assemblyName.StartsWith("Microsoft.")
|| assemblyName.StartsWith("Unity.")
|| assemblyName.StartsWith("UnityEngine.")
|| assemblyName.StartsWith("UnityEditor.");
}
[CanBeNull]
private Type TryGetTypeFromSerializedFields()
{
var type = Type.GetType(_typeNameAndAssembly);
// If GUID is not empty, there is still hope the type will be found in the TryUpdatingTypeUsingGUID method.
if (type == null && GUID.Length == 0)
_needToLogTypeNotFound = true;
return type;
}
}
}