forked from Shardstone/trail-into-darkness
added serialzed dictionary
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "AYellowpaper.SerializedCollections",
|
||||
"references": [],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d525ad6bd40672747bde77962f1c401e
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f1444c3990d36354e9d53f3797d31080
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,82 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
internal class DictionaryLookupTable<TKey, TValue> : IKeyable
|
||||
{
|
||||
private SerializedDictionary<TKey, TValue> _dictionary;
|
||||
private Dictionary<TKey, List<int>> _occurences = new Dictionary<TKey, List<int>>();
|
||||
|
||||
private static readonly List<int> EmptyList = new List<int>();
|
||||
|
||||
public IEnumerable Keys => _dictionary.Keys;
|
||||
|
||||
public DictionaryLookupTable(SerializedDictionary<TKey, TValue> dictionary)
|
||||
{
|
||||
_dictionary = dictionary;
|
||||
}
|
||||
|
||||
public IReadOnlyList<int> GetOccurences(object key)
|
||||
{
|
||||
if (key is TKey castKey && _occurences.TryGetValue(castKey, out var list))
|
||||
return list;
|
||||
|
||||
return EmptyList;
|
||||
}
|
||||
|
||||
public void RecalculateOccurences()
|
||||
{
|
||||
_occurences.Clear();
|
||||
|
||||
int count = _dictionary._serializedList.Count;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var kvp = _dictionary._serializedList[i];
|
||||
if (!SerializedCollectionsUtility.IsValidKey(kvp.Key))
|
||||
continue;
|
||||
|
||||
if (!_occurences.ContainsKey(kvp.Key))
|
||||
_occurences.Add(kvp.Key, new List<int>() { i });
|
||||
else
|
||||
_occurences[kvp.Key].Add(i);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveKey(object key)
|
||||
{
|
||||
for (int i = _dictionary._serializedList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var dictKey = _dictionary._serializedList[i].Key;
|
||||
if ((object)dictKey == key || dictKey.Equals(key))
|
||||
_dictionary._serializedList.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
_dictionary._serializedList.RemoveAt(index);
|
||||
}
|
||||
|
||||
public object GetKeyAt(int index)
|
||||
{
|
||||
return _dictionary._serializedList[index];
|
||||
}
|
||||
|
||||
public void RemoveDuplicates()
|
||||
{
|
||||
_dictionary._serializedList = _dictionary._serializedList
|
||||
.GroupBy(x => x.Key)
|
||||
.Where(x => SerializedCollectionsUtility.IsValidKey(x.Key))
|
||||
.Select(x => x.First()).ToList();
|
||||
}
|
||||
|
||||
public void AddKey(object key)
|
||||
{
|
||||
var entry = new SerializedKeyValuePair<TKey, TValue>();
|
||||
entry.Key = (TKey) key;
|
||||
_dictionary._serializedList.Add(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff547989e6c74ca418db99a6a03aa653
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
internal interface IKeyable
|
||||
{
|
||||
void RecalculateOccurences();
|
||||
IReadOnlyList<int> GetOccurences(object key);
|
||||
IEnumerable Keys { get; }
|
||||
|
||||
void AddKey(object key);
|
||||
void RemoveKey(object key);
|
||||
void RemoveAt(int index);
|
||||
object GetKeyAt(int index);
|
||||
void RemoveDuplicates();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 131781efcfa1f0c4a8f9480cc10f5699
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Packages/SerializedDictionary-main/Runtime/Scripts.meta
Normal file
8
Packages/SerializedDictionary-main/Runtime/Scripts.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 54ab7ca0599474d4281016b4f077172c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,3 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("AYellowpaper.SerializedCollections.Editor")]
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88ab3f65a23ff3a4697758e7a3ed3ac0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
public static class SerializedCollectionsUtility
|
||||
{
|
||||
public static bool IsValidKey(object obj)
|
||||
{
|
||||
// we catch this error if we are not on the main thread and simply return false as we assume the object is null
|
||||
try
|
||||
{
|
||||
return !(obj is Object unityObject && unityObject == null);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a1da1a15cc1e6754fb099902045e4572
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
[System.Serializable]
|
||||
public partial class SerializedDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
|
||||
{
|
||||
[SerializeField]
|
||||
internal List<SerializedKeyValuePair<TKey, TValue>> _serializedList = new List<SerializedKeyValuePair<TKey, TValue>>();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
internal IKeyable LookupTable
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_lookupTable == null)
|
||||
_lookupTable = new DictionaryLookupTable<TKey, TValue>(this);
|
||||
return _lookupTable;
|
||||
}
|
||||
}
|
||||
|
||||
private DictionaryLookupTable<TKey, TValue> _lookupTable;
|
||||
#endif
|
||||
|
||||
public void OnAfterDeserialize()
|
||||
{
|
||||
Clear();
|
||||
|
||||
foreach (var kvp in _serializedList)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!ContainsKey(kvp.Key))
|
||||
Add(kvp.Key, kvp.Value);
|
||||
#else
|
||||
Add(kvp.Key, kvp.Value);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
LookupTable.RecalculateOccurences();
|
||||
#else
|
||||
_serializedList.Clear();
|
||||
#endif
|
||||
}
|
||||
|
||||
public void OnBeforeSerialize()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (UnityEditor.BuildPipeline.isBuildingPlayer)
|
||||
LookupTable.RemoveDuplicates();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cfd467abf43f5944faa7fd0abcac2299
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
[Conditional("UNITY_EDITOR")]
|
||||
public class SerializedDictionaryAttribute : Attribute
|
||||
{
|
||||
public readonly string KeyName;
|
||||
public readonly string ValueName;
|
||||
|
||||
public SerializedDictionaryAttribute(string keyName = null, string valueName = null)
|
||||
{
|
||||
KeyName = keyName;
|
||||
ValueName = valueName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e3aa354f515fef24c865758f92a293ec
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AYellowpaper.SerializedCollections
|
||||
{
|
||||
[System.Serializable]
|
||||
public struct SerializedKeyValuePair<TKey, TValue>
|
||||
{
|
||||
public TKey Key;
|
||||
public TValue Value;
|
||||
|
||||
public SerializedKeyValuePair(TKey key, TValue value)
|
||||
{
|
||||
Key = key;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9f808808de12544418e1f67eb1e8a78d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user