forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyDatabaseSerializer.cs
More file actions
182 lines (153 loc) · 6.03 KB
/
Copy pathPropertyDatabaseSerializer.cs
File metadata and controls
182 lines (153 loc) · 6.03 KB
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
namespace UnityEditor.Search
{
public enum PropertyDatabaseType : byte
{
None = 0,
FixedString,
String,
Bool,
Byte,
Short,
UnsignedShort,
Integer,
UnsignedInteger,
Long,
UnsignedLong,
Double,
Float,
GlobalObjectId,
Vector4,
Color,
Color32,
InstanceId,
GameObjectProperty,
Volatile
}
class PropertyDatabaseSerializerAttribute : Attribute
{
public Type type;
public PropertyDatabaseSerializerAttribute(Type type)
{
this.type = type;
}
}
class PropertyDatabaseDeserializerAttribute : Attribute
{
public PropertyDatabaseType type;
public PropertyDatabaseDeserializerAttribute(PropertyDatabaseType type)
{
this.type = type;
}
}
struct PropertyDatabaseSerializationArgs
{
public object value;
public PropertyStringTableView stringTableView;
public PropertyDatabaseSerializationArgs(object value, PropertyStringTableView stringTableView)
{
this.value = value;
this.stringTableView = stringTableView;
}
}
struct PropertyDatabaseDeserializationArgs
{
public PropertyDatabaseRecordValue value;
public PropertyStringTableView stringTableView;
public PropertyDatabaseDeserializationArgs(PropertyDatabaseRecordValue value, PropertyStringTableView stringTableView)
{
this.value = value;
this.stringTableView = stringTableView;
}
}
delegate PropertyDatabaseRecordValue PropertySerializerHandler(PropertyDatabaseSerializationArgs args);
delegate object PropertyDeserializerHandler(PropertyDatabaseDeserializationArgs args);
readonly struct PropertyDatabaseSerializer
{
public readonly Type type;
public readonly PropertySerializerHandler handler;
public PropertyDatabaseSerializer(Type type, PropertySerializerHandler handler)
{
this.type = type;
this.handler = handler;
}
}
readonly struct PropertyDatabaseDeserializer
{
public readonly PropertyDatabaseType type;
public readonly PropertyDeserializerHandler handler;
public PropertyDatabaseDeserializer(PropertyDatabaseType type, PropertyDeserializerHandler handler)
{
this.type = type;
this.handler = handler;
}
}
static class PropertyDatabaseSerializerManager
{
static Dictionary<Type, PropertyDatabaseSerializer> s_Serializers = new Dictionary<Type, PropertyDatabaseSerializer>();
static Dictionary<PropertyDatabaseType, PropertyDatabaseDeserializer> s_Deserializers = new Dictionary<PropertyDatabaseType, PropertyDatabaseDeserializer>();
static PropertyDatabaseSerializerManager()
{
RefreshSerializers();
}
public static void RefreshSerializers()
{
s_Serializers.Clear();
s_Deserializers.Clear();
var serializers = ReflectionUtils.LoadAllMethodsWithAttribute<PropertyDatabaseSerializerAttribute, PropertyDatabaseSerializer>((info, attribute, handler) =>
{
if (handler is PropertySerializerHandler psh)
return new PropertyDatabaseSerializer(attribute.type, psh);
throw new Exception($"Invalid {nameof(PropertyDatabaseSerializerAttribute)} handler.");
}, MethodSignature.FromDelegate<PropertySerializerHandler>());
var deserializers = ReflectionUtils.LoadAllMethodsWithAttribute<PropertyDatabaseDeserializerAttribute, PropertyDatabaseDeserializer>((info, attribute, handler) =>
{
if (handler is PropertyDeserializerHandler psh)
return new PropertyDatabaseDeserializer(attribute.type, psh);
throw new Exception($"Invalid {nameof(PropertyDatabaseDeserializerAttribute)} handler.");
}, MethodSignature.FromDelegate<PropertyDeserializerHandler>());
foreach (var propertySerializer in serializers)
{
if (s_Serializers.ContainsKey(propertySerializer.type))
continue;
s_Serializers.Add(propertySerializer.type, propertySerializer);
}
foreach (var propertyDeserializer in deserializers)
{
if (s_Deserializers.ContainsKey(propertyDeserializer.type))
continue;
s_Deserializers.Add(propertyDeserializer.type, propertyDeserializer);
}
}
public static bool SerializerExists(Type type)
{
if (type == null)
return false;
return s_Serializers.ContainsKey(type);
}
public static bool DeserializerExists(PropertyDatabaseType type)
{
return s_Deserializers.ContainsKey(type);
}
public static PropertyDatabaseRecordValue Serialize(object value, PropertyStringTableView stringTableView)
{
var type = value.GetType();
if (!s_Serializers.ContainsKey(type))
return PropertyDatabaseRecordValue.invalid;
var args = new PropertyDatabaseSerializationArgs(value, stringTableView);
return s_Serializers[type].handler(args);
}
public static object Deserialize(PropertyDatabaseRecordValue value, PropertyStringTableView stringTableView)
{
var type = (PropertyDatabaseType)value.propertyType;
if (!s_Deserializers.ContainsKey(type))
return PropertyDatabaseRecordValue.invalid;
var args = new PropertyDatabaseDeserializationArgs(value, stringTableView);
return s_Deserializers[type].handler(args);
}
}
}