File size: 4,433 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 UnityEngine;

namespace UnityEditor.SettingsManagement
{
    [Serializable]
    sealed class SettingsDictionary : ISerializationCallbackReceiver
    {
        [Serializable]
        struct SettingsKeyValuePair
        {
            public string type;
            public string key;
            public string value;
        }

#pragma warning disable 0649
        [SerializeField]
        List<SettingsKeyValuePair> m_DictionaryValues = new List<SettingsKeyValuePair>();
#pragma warning restore 0649

        internal Dictionary<Type, Dictionary<string, string>> dictionary = new Dictionary<Type, Dictionary<string, string>>();

        public bool ContainsKey<T>(string key)
        {
            return dictionary.ContainsKey(typeof(T)) && dictionary[typeof(T)].ContainsKey(key);
        }

        public void Set<T>(string key, T value)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");

            var type = typeof(T).AssemblyQualifiedName;

            SetJson(type, key, ValueWrapper<T>.Serialize(value));
        }

        internal void SetJson(string type, string key, string value)
        {
            var typeValue = Type.GetType(type);

            if (typeValue == null)
                throw new ArgumentException("\"type\" must be an assembly qualified type name.");

            Dictionary<string, string> entries;

            if (!dictionary.TryGetValue(typeValue, out entries))
                dictionary.Add(typeValue, entries = new Dictionary<string, string>());

            if (entries.ContainsKey(key))
                entries[key] = value;
            else
                entries.Add(key, value);
        }

        public T Get<T>(string key, T fallback = default(T))
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");

            Dictionary<string, string> entries;

            if (dictionary.TryGetValue(typeof(T), out entries) && entries.ContainsKey(key))
            {
                try
                {
                    return ValueWrapper<T>.Deserialize(entries[key]);
                }
                catch
                {
                    return fallback;
                }
            }

            return fallback;
        }

        public void Remove<T>(string key)
        {
            Dictionary<string, string> entries;

            if (!dictionary.TryGetValue(typeof(T), out entries) || !entries.ContainsKey(key))
                return;

            entries.Remove(key);
        }

        public void OnBeforeSerialize()
        {
            if (m_DictionaryValues == null)
                return;

            m_DictionaryValues.Clear();

            foreach (var type in dictionary)
            {
                foreach (var entry in type.Value)
                {
                    m_DictionaryValues.Add(new SettingsKeyValuePair()
                    {
                        type = type.Key.AssemblyQualifiedName,
                        key = entry.Key,
                        value = entry.Value
                    });
                }
            }
        }

        public void OnAfterDeserialize()
        {
            dictionary.Clear();

            foreach (var entry in m_DictionaryValues)
            {
                Dictionary<string, string> entries;

                var type = Type.GetType(entry.type);

                if (type == null)
                {
                    Debug.LogWarning("Could not instantiate type \"" + entry.type + "\". Skipping key: " + entry.key + ".");
                    continue;
                }

                if (dictionary.TryGetValue(type, out entries))
                    entries.Add(entry.key, entry.value);
                else
                    dictionary.Add(type, new Dictionary<string, string>() { { entry.key, entry.value } });
            }
        }

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

            foreach (var type in dictionary)
            {
                sb.AppendLine("Type: " + type.Key);

                foreach (var entry in type.Value)
                {
                    sb.AppendLine(string.Format("   {0,-64}{1}", entry.Key, entry.Value));
                }
            }

            return sb.ToString();
        }
    }
}