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
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using UnityEngine;
public abstract class SerializableDictionaryBase<TKey, TValue, TValueStorage> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{
[SerializeField]
TKey[] m_keys;
[SerializeField]
TValueStorage[] m_values;
public SerializableDictionaryBase()
{
}
public SerializableDictionaryBase(IDictionary<TKey, TValue> dict) : base(dict.Count)
{
foreach (var kvp in dict)
{
this[kvp.Key] = kvp.Value;
}
}
protected SerializableDictionaryBase(SerializationInfo info, StreamingContext context) : base(info,context){}
protected abstract void SetValue(TValueStorage[] storage, int i, TValue value);
protected abstract TValue GetValue(TValueStorage[] storage, int i);
public void CopyFrom(IDictionary<TKey, TValue> dict)
{
this.Clear();
foreach (var kvp in dict)
{
this[kvp.Key] = kvp.Value;
}
}
public void OnAfterDeserialize()
{
if(m_keys != null && m_values != null && m_keys.Length == m_values.Length)
{
this.Clear();
int n = m_keys.Length;
for(int i = 0; i < n; ++i)
{
this[m_keys[i]] = GetValue(m_values, i);
}
m_keys = null;
m_values = null;
}
}
public void OnBeforeSerialize()
{
int n = this.Count;
m_keys = new TKey[n];
m_values = new TValueStorage[n];
int i = 0;
foreach(var kvp in this)
{
m_keys[i] = kvp.Key;
SetValue(m_values, i, kvp.Value);
++i;
}
}
}
public class SerializableDictionary<TKey, TValue> : SerializableDictionaryBase<TKey, TValue, TValue>
{
public SerializableDictionary()
{
}
public SerializableDictionary(IDictionary<TKey, TValue> dict) : base(dict)
{
}
protected SerializableDictionary(SerializationInfo info, StreamingContext context) : base(info,context){}
protected override TValue GetValue(TValue[] storage, int i)
{
return storage[i];
}
protected override void SetValue(TValue[] storage, int i, TValue value)
{
storage[i] = value;
}
}
public static class SerializableDictionary
{
public class Storage<T>
{
public T data;
}
}
public class SerializableDictionary<TKey, TValue, TValueStorage> : SerializableDictionaryBase<TKey, TValue, TValueStorage> where TValueStorage : SerializableDictionary.Storage<TValue>, new()
{
public SerializableDictionary()
{
}
public SerializableDictionary(IDictionary<TKey, TValue> dict) : base(dict)
{
}
protected SerializableDictionary(SerializationInfo info, StreamingContext context) : base(info,context){}
protected override TValue GetValue(TValueStorage[] storage, int i)
{
return storage[i].data;
}
protected override void SetValue(TValueStorage[] storage, int i, TValue value)
{
storage[i] = new TValueStorage();
storage[i].data = value;
}
}
|