From 1d75beae7a29ff3c7d4592710053edaf3e4ea25d Mon Sep 17 00:00:00 2001 From: Anatoly Brizhan Date: Sat, 13 Jul 2024 20:10:30 +0200 Subject: [PATCH 1/2] Add SerializedSortedDictionary --- .../SerializedSortedDictionary.cs | 54 +++++++++++++++++++ .../SerializedSortedDictionary.cs.meta | 3 ++ 2 files changed, 57 insertions(+) create mode 100644 Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedSortedDictionary.cs create mode 100644 Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedSortedDictionary.cs.meta diff --git a/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedSortedDictionary.cs b/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedSortedDictionary.cs new file mode 100644 index 0000000..fa86e92 --- /dev/null +++ b/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedSortedDictionary.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using Better.Commons.Runtime.Utility; +using UnityEngine; + +namespace Better.Commons.Runtime.DataStructures.SerializedTypes +{ + [Serializable] + public class SerializedSortedDictionary : SortedDictionary, ISerializationCallbackReceiver + { + // TODO: Add CustomDrawer for key-item table + [SerializeField] private TKey[] _keys; + [SerializeField] private TValue[] _values; + + public SerializedSortedDictionary() + { + } + + public SerializedSortedDictionary(IComparer comparer) : base(comparer) + { + } + + public SerializedSortedDictionary(IDictionary dictionary) : base(dictionary) + { + } + + public SerializedSortedDictionary(IDictionary dictionary, IComparer comparer) : base(dictionary, comparer) + { + } + + void ISerializationCallbackReceiver.OnBeforeSerialize() + { + _keys = Keys.ToArray(); + _values = Values.ToArray(); + } + + void ISerializationCallbackReceiver.OnAfterDeserialize() + { + if (_keys == null || _values == null || _keys.Length != _values.Length) + { + DebugUtility.LogException(); + return; + } + + Clear(); + for (var i = 0; i < _keys.Length; ++i) + { + Add(_keys[i], _values[i]); + } + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedSortedDictionary.cs.meta b/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedSortedDictionary.cs.meta new file mode 100644 index 0000000..928c378 --- /dev/null +++ b/Assets/BetterCommons/Runtime/DataStructures/SerializedTypes/SerializedSortedDictionary.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 47fa5d74ad30481e913e27228cacf48b +timeCreated: 1720893961 \ No newline at end of file From 2e9e960a31e6d53b53d35a49358d40b17ccab221 Mon Sep 17 00:00:00 2001 From: Anatoly Brizhan Date: Sat, 13 Jul 2024 20:10:47 +0200 Subject: [PATCH 2/2] Update package version to 0.0.17 --- Assets/BetterCommons/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/BetterCommons/package.json b/Assets/BetterCommons/package.json index 88edb0e..3f66e12 100644 --- a/Assets/BetterCommons/package.json +++ b/Assets/BetterCommons/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.commons", "displayName": "Better Commons", - "version": "0.0.16", + "version": "0.0.17", "unity": "2021.3", "description": " ", "dependencies": {