| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | #if !UNITY_DOTSRUNTIME |
| | 6 | |
|
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace GDX.Collections.Generic |
| | 12 | | { |
| | 13 | | #pragma warning disable IDE0049 |
| | 14 | | /// <summary> |
| | 15 | | /// A Unity serializable <see cref="Dictionary{TKey,TValue}" />. |
| | 16 | | /// </summary> |
| | 17 | | /// <remarks> |
| | 18 | | /// <para> |
| | 19 | | /// This will NOT work with <see cref="System.Object" /> based objects, use <see cref="UnityEngine.Object" / |
| | 20 | | /// you must. While .NET has solutions for creating custom serialization paths, Unity uses its own system to |
| | 21 | | /// serialize data into YAML structures. This also assumes that the types provided can be serialized by Unit |
| | 22 | | /// </para> |
| | 23 | | /// <para> |
| | 24 | | /// The process of serializing and deserializing this dictionary should not be considered performant. |
| | 25 | | /// </para> |
| | 26 | | /// <para> |
| | 27 | | /// The property drawer will not show structs, it will be blank. It does work however if done through code. |
| | 28 | | /// </para> |
| | 29 | | /// </remarks> |
| | 30 | | /// <typeparam name="TKey">The dictionary's key <see cref="Type" />.</typeparam> |
| | 31 | | /// <typeparam name="TValue">The dictionary's value <see cref="Type" />.</typeparam> |
| | 32 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 33 | | [Serializable] |
| | 34 | | #pragma warning restore IDE0049 |
| | 35 | | [VisualScriptingCompatible(1)] |
| | 36 | | public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver |
| | 37 | | { |
| | 38 | | /// <summary> |
| | 39 | | /// Type constructor. |
| | 40 | | /// </summary> |
| 3 | 41 | | public SerializableDictionary() |
| 3 | 42 | | { |
| | 43 | | #if UNITY_EDITOR |
| 3 | 44 | | m_IsSerializable = IsSerializableType(typeof(TKey)) && IsSerializableType(typeof(TValue)); |
| | 45 | | #endif // UNITY_EDITOR |
| 3 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Rehydrate the serialized data arrays back into a cohesive <see cref="Dictionary{TKey,TValue}" />. |
| | 50 | | /// </summary> |
| | 51 | | /// <remarks>Invoked by Unity, calls <see cref="LoadSerializedData" />.</remarks> |
| | 52 | | public void OnAfterDeserialize() |
| 0 | 53 | | { |
| 0 | 54 | | LoadSerializedData(); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Build out serialized data arrays and associative data, used to rehydrate during deserialization. |
| | 59 | | /// </summary> |
| | 60 | | /// <remarks>Invoked by Unity, calls <see cref="SaveSerializedData" />.</remarks> |
| | 61 | | public void OnBeforeSerialize() |
| 0 | 62 | | { |
| 0 | 63 | | SaveSerializedData(); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Get the length of the serialized data arrays. |
| | 68 | | /// </summary> |
| | 69 | | /// <returns>An integer value representing the count.</returns> |
| | 70 | | public int GetSerializedDataLength() |
| 2 | 71 | | { |
| 2 | 72 | | return m_SerializedLength; |
| 2 | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Is the <paramref name="type" /> capable of being serialized by the |
| | 77 | | /// <see cref="SerializableDictionary{TKey,TValue}" />, utilizing Unity's own serialization system? |
| | 78 | | /// </summary> |
| | 79 | | /// <returns>true/false if the type is valid.</returns> |
| | 80 | | public static bool IsSerializableType(Type type) |
| 8 | 81 | | { |
| 8 | 82 | | return type != typeof(object); |
| 8 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Load the data into the <see cref="Dictionary{TKey,TValue}" /> cached in the serialized data. |
| | 87 | | /// </summary> |
| | 88 | | /// <param name="clearAfterLoad">Should the serialized data be cleared after loading?</param> |
| | 89 | | public void LoadSerializedData(bool clearAfterLoad = true) |
| 1 | 90 | | { |
| 1 | 91 | | Clear(); |
| | 92 | |
|
| | 93 | | // If this is not serializable we need to do nothing |
| 1 | 94 | | if (!m_IsSerializable || m_SerializedLength <= 0) |
| 0 | 95 | | { |
| 0 | 96 | | return; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | // Iterate over all the serialized data and put it back into the dictionary as it once was, in order. |
| 6 | 100 | | for (int i = 0; i < m_SerializedLength; i++) |
| 2 | 101 | | { |
| | 102 | | #if UNITY_EDITOR |
| | 103 | | // If the key is already in the dataset what do we do? |
| 2 | 104 | | if (ContainsKey(m_SerializedKeys[i])) |
| 0 | 105 | | { |
| 0 | 106 | | Debug.LogError( |
| | 107 | | "A duplicate key has been detected in the serialized dictionary, the item has been removed.\nYou |
| 0 | 108 | | } |
| | 109 | | else |
| 2 | 110 | | { |
| | 111 | | #endif // UNITY_EDITOR |
| 2 | 112 | | Add(m_SerializedKeys[i], m_SerializedValues[i]); |
| | 113 | | #if UNITY_EDITOR |
| 2 | 114 | | } |
| | 115 | | #endif // UNITY_EDITOR |
| 2 | 116 | | } |
| | 117 | |
|
| | 118 | | // Remove any data cached so that references are not held. |
| 1 | 119 | | if (!clearAfterLoad) |
| 0 | 120 | | { |
| 0 | 121 | | return; |
| | 122 | | } |
| | 123 | |
|
| 1 | 124 | | m_SerializedLength = -1; |
| 1 | 125 | | m_SerializedKeys = null; |
| 1 | 126 | | m_SerializedValues = null; |
| 1 | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Overwrite data in the serialized arrays with the provided data. |
| | 131 | | /// </summary> |
| | 132 | | /// <param name="keyArray">An array of keys.</param> |
| | 133 | | /// <param name="valueArray">An array of values.</param> |
| | 134 | | public void OverwriteSerializedData(TKey[] keyArray, TValue[] valueArray) |
| 1 | 135 | | { |
| 1 | 136 | | if (keyArray.Length != valueArray.Length) |
| 0 | 137 | | { |
| 0 | 138 | | Debug.LogError("The provided array lengths must match."); |
| 0 | 139 | | return; |
| | 140 | | } |
| | 141 | |
|
| 1 | 142 | | m_SerializedKeys = keyArray; |
| 1 | 143 | | m_SerializedValues = valueArray; |
| 1 | 144 | | m_SerializedLength = keyArray.Length; |
| 1 | 145 | | } |
| | 146 | |
|
| | 147 | | /// <summary> |
| | 148 | | /// Fill serializable arrays from dictionary data. |
| | 149 | | /// </summary> |
| | 150 | | /// <remarks>We will always create the arrays so the property drawers function nicely.</remarks> |
| | 151 | | public void SaveSerializedData() |
| 2 | 152 | | { |
| | 153 | | // If this is not serializable we need to do nothing |
| 2 | 154 | | if (!m_IsSerializable) |
| 0 | 155 | | { |
| 0 | 156 | | return; |
| | 157 | | } |
| | 158 | |
|
| | 159 | | // Stash our length for future usage |
| 2 | 160 | | m_SerializedLength = Count; |
| | 161 | |
|
| | 162 | | // Create our serialized data arrays |
| 2 | 163 | | m_SerializedKeys = new TKey[m_SerializedLength]; |
| 2 | 164 | | m_SerializedValues = new TValue[m_SerializedLength]; |
| | 165 | |
|
| | 166 | | // Stash our values |
| 2 | 167 | | int index = 0; |
| 10 | 168 | | foreach (KeyValuePair<TKey, TValue> pair in this) |
| 2 | 169 | | { |
| 2 | 170 | | m_SerializedKeys[index] = pair.Key; |
| 2 | 171 | | m_SerializedValues[index] = pair.Value; |
| 2 | 172 | | index++; |
| 2 | 173 | | } |
| 2 | 174 | | } |
| | 175 | | #pragma warning disable IDE1006 |
| | 176 | | // ReSharper disable InconsistentNaming |
| | 177 | |
|
| | 178 | | /// <summary> |
| | 179 | | /// Is the dictionary completely capable of being serialized by Unity? |
| | 180 | | /// </summary> |
| | 181 | | /// <remarks>This field is determined/cached in the constructor.</remarks> |
| | 182 | | [SerializeField] bool m_IsSerializable; |
| | 183 | |
|
| | 184 | | /// <summary> |
| | 185 | | /// The length of the serialized data arrays. |
| | 186 | | /// </summary> |
| 3 | 187 | | [SerializeField] int m_SerializedLength = -1; |
| | 188 | |
|
| | 189 | | /// <summary> |
| | 190 | | /// An array of all of the keys, in order, used to recreate the base <see cref="Dictionary{TKey,TValue}" />. |
| | 191 | | /// </summary> |
| | 192 | | [SerializeField] TKey[] m_SerializedKeys; |
| | 193 | |
|
| | 194 | | /// <summary> |
| | 195 | | /// An array of all of the values, in order, used to recreate the base <see cref="Dictionary{TKey,TValue}" / |
| | 196 | | /// </summary> |
| | 197 | | [SerializeField] TValue[] m_SerializedValues; |
| | 198 | |
|
| | 199 | | // ReSharper restore InconsistentNaming |
| | 200 | | #pragma warning restore IDE1006 |
| | 201 | | } |
| | 202 | | } |
| | 203 | | #endif // !UNITY_DOTSRUNTIME |