summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/VRM/VRM/UniGLTF/Editor/Serialization/FieldSerializationInfo.cs
blob: 3ca000776f772f9a83fcfa440171e68e7cea9bbd (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UniJSON;

namespace UniGLTF
{
    public class FieldSerializationInfo
    {
        FieldInfo m_fi;
        public FieldInfo FieldInfo
        {
            get { return m_fi; }
        }

        public string Name
        {
            get { return FieldInfo.Name; }
        }

        public string Path
        {
            get;
            private set;
        }

        public string FunctionName
        {
            get
            {
                return "Deserialize_" + Path
                .Replace("/", "_")
                .Replace("[]", "_")
                ;
            }
        }

        JsonSchemaAttribute m_attr;

        public IValueSerialization Serialization
        {
            get;
            private set;
        }

        public FieldSerializationInfo(FieldInfo fi, string path)
        {
            m_fi = fi;
            Path = path + "/" + fi.Name;
            m_attr = fi.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(JsonSchemaAttribute)) as JsonSchemaAttribute;

            Serialization = GetSerialization(m_fi.FieldType, Path);
        }

        static IValueSerialization GetSerialization(Type t, string path)
        {
            if (t.IsArray)
            {
                return new ArraySerialization(t,
                    GetSerialization(t.GetElementType(), path + "[]"));
            }
            else if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>))
            {
                return new ListSerialization(t,
                    GetSerialization(t.GetGenericArguments()[0], path + "[]"));
            }
            else if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Dictionary<,>)
                && t.GetGenericArguments()[0] == typeof(string))
            {
                return new StringKeyDictionarySerialization(t, 
                    GetSerialization(t.GetGenericArguments()[1], path));
            }

            // GetCollectionType(fi.FieldType, out suffix, out t);
            if (t == typeof(sbyte))
            {
                return new Int8Serialization();
            }
            else if (t == typeof(short))
            {
                return new Int16Serialization();
            }
            else if (t == typeof(int))
            {
                return new Int32Serialization();
            }
            else if (t == typeof(long))
            {
                return new Int64Serialization();
            }
            else if (t == typeof(byte))
            {
                return new UInt8Serialization();
            }
            else if (t == typeof(ushort))
            {
                return new UInt16Serialization();
            }
            else if (t == typeof(uint))
            {
                return new UInt32Serialization();
            }
            else if (t == typeof(ulong))
            {
                return new UInt64Serialization();
            }
            else if (t == typeof(float))
            {
                return new SingleSerialization();
            }
            else if (t == typeof(double))
            {
                return new DoubleSerialization();
            }
            else if (t == typeof(string))
            {
                return new StringSerialization();
            }
            else if (t == typeof(bool))
            {
                return new BooleanSerialization();
            }
            else if (t.IsEnum)
            {
                return new EnumIntSerialization(t);
            }

            return new ObjectSerialization(t, path);
        }

        public override string ToString()
        {
            var sb = new StringBuilder();

            var typeName = BaseJsonSchemaAttribute.GetTypeName(m_fi.FieldType);

            if (m_attr != null)
            {
                sb.AppendLine(string.Format("{0}: {1}", Path, m_attr.GetInfo(m_fi)));
            }
            else
            {
                sb.AppendLine(string.Format("{0}: {1}", Path, typeName));
            }

            sb.Append(Serialization.ToString());
            return sb.ToString();
        }
    }
}