Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit eb4c2d0

Browse files
Correctly serializing remote branch dictionaries
1 parent 854b189 commit eb4c2d0

File tree

2 files changed

+87
-66
lines changed

2 files changed

+87
-66
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,28 @@ public LocalConfigBranchDictionary(IDictionary<string, ConfigBranch> dictionary)
197197
}
198198

199199
[Serializable]
200-
class RemoteConfigBranchDictionary : SerializableNestedDictionary<string, ConfigBranch>, IRemoteConfigBranchDictionary
200+
public class ArrayContainer<T>
201201
{
202+
[SerializeField] public T[] Values = new T[0];
203+
}
204+
205+
[Serializable]
206+
public class StringArrayContainer: ArrayContainer<string>
207+
{
208+
}
209+
210+
[Serializable]
211+
public class ConfigBranchArrayContainer : ArrayContainer<ConfigBranch>
212+
{
213+
}
214+
215+
[Serializable]
216+
class RemoteConfigBranchDictionary : Dictionary<string, Dictionary<string, ConfigBranch>>, ISerializationCallbackReceiver, IRemoteConfigBranchDictionary
217+
{
218+
[SerializeField] private string[] keys = new string[0];
219+
[SerializeField] private StringArrayContainer[] subKeys = new StringArrayContainer[0];
220+
[SerializeField] private ConfigBranchArrayContainer[] subKeyValues = new ConfigBranchArrayContainer[0];
221+
202222
public RemoteConfigBranchDictionary()
203223
{ }
204224

@@ -208,6 +228,72 @@ public RemoteConfigBranchDictionary(IDictionary<string, IDictionary<string, Conf
208228
{
209229
Add(pair.Key, pair.Value.ToDictionary(valuePair => valuePair.Key, valuePair => valuePair.Value));
210230
}
231+
}
232+
233+
// save the dictionary to lists
234+
public void OnBeforeSerialize()
235+
{
236+
var keyList = new List<string>();
237+
var subKeysList = new List<StringArrayContainer>();
238+
var subKeysValuesList = new List<ConfigBranchArrayContainer>();
239+
240+
foreach (var pair in this)
241+
{
242+
var pairKey = pair.Key;
243+
keyList.Add(pairKey);
244+
245+
var serializeSubKeys = new List<string>();
246+
var serializeSubKeyValues = new List<ConfigBranch>();
247+
248+
var subDictionary = pair.Value;
249+
foreach (var subPair in subDictionary)
250+
{
251+
serializeSubKeys.Add(subPair.Key);
252+
serializeSubKeyValues.Add(subPair.Value);
253+
}
254+
255+
subKeysList.Add(new StringArrayContainer { Values = serializeSubKeys.ToArray() });
256+
subKeysValuesList.Add(new ConfigBranchArrayContainer { Values = serializeSubKeyValues.ToArray() });
257+
}
258+
259+
keys = keyList.ToArray();
260+
subKeys = subKeysList.ToArray();
261+
subKeyValues = subKeysValuesList.ToArray();
262+
}
263+
264+
// load dictionary from lists
265+
public void OnAfterDeserialize()
266+
{
267+
Clear();
268+
269+
if (keys.Length != subKeys.Length || subKeys.Length != subKeyValues.Length)
270+
{
271+
throw new Exception("Deserialization length mismatch");
272+
}
273+
274+
for (var remoteIndex = 0; remoteIndex < keys.Length; remoteIndex++)
275+
{
276+
var remote = keys[remoteIndex];
277+
278+
var subKeyContainer = subKeys[remoteIndex];
279+
var subKeyValueContainer = subKeyValues[remoteIndex];
280+
281+
if (subKeyContainer.Values.Length != subKeyValueContainer.Values.Length)
282+
{
283+
throw new Exception("Deserialization length mismatch");
284+
}
285+
286+
var branchesDictionary = new Dictionary<string, ConfigBranch>();
287+
for (var branchIndex = 0; branchIndex < subKeyContainer.Values.Length; branchIndex++)
288+
{
289+
var remoteBranchKey = subKeyContainer.Values[branchIndex];
290+
var remoteBranch = subKeyValueContainer.Values[branchIndex];
291+
292+
branchesDictionary.Add(remoteBranchKey, remoteBranch);
293+
}
294+
295+
Add(remote, branchesDictionary);
296+
}
211297
}
212298

213299
IEnumerator<KeyValuePair<string, IDictionary<string, ConfigBranch>>> IEnumerable<KeyValuePair<string, IDictionary<string, ConfigBranch>>>.GetEnumerator()
@@ -613,20 +699,7 @@ public void SetRemotes(IDictionary<string, ConfigRemote> remoteDictionary, IDict
613699
var now = DateTimeOffset.Now;
614700
configRemotes = new ConfigRemoteDictionary(remoteDictionary);
615701
remoteConfigBranches = new RemoteConfigBranchDictionary(branchDictionary);
616-
617702
Logger.Trace("SetRemotes {0}", now);
618-
619-
Logger.Trace("remoteDictionary.Length: {0}", remoteDictionary.Count);
620-
Logger.Trace("branchDictionary.Length: {0}", branchDictionary.Count);
621-
622-
foreach (var remotePair in remoteConfigBranches)
623-
{
624-
foreach (var remotePairBranch in remotePair.Value)
625-
{
626-
Logger.Trace("remoteConfigBranches Remote:{0} Branch:{1} BranchObject:{2}", remotePair.Key, remotePairBranch.Key, remotePairBranch.Value);
627-
}
628-
}
629-
630703
SaveData(now, true);
631704
}
632705

src/UnityExtension/Assets/Editor/GitHub.Unity/SerializableDictionary.cs

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,56 +41,4 @@ public void OnAfterDeserialize()
4141
this.Add(keys[i], values[i]);
4242
}
4343
}
44-
45-
[Serializable]
46-
public class ArrayContainer
47-
{
48-
[SerializeField]
49-
public object[] Values = new object[0];
50-
}
51-
52-
[Serializable]
53-
public class SerializableNestedDictionary<TKey, TValue> : Dictionary<TKey, Dictionary<TKey, TValue>>, ISerializationCallbackReceiver
54-
{
55-
[SerializeField] private TKey[] keys = new TKey[0];
56-
[SerializeField] private ArrayContainer[] subKeys = new ArrayContainer[0];
57-
[SerializeField] private ArrayContainer[] subKeyValues = new ArrayContainer[0];
58-
59-
// save the dictionary to lists
60-
public void OnBeforeSerialize()
61-
{
62-
var keyList = new List<TKey>();
63-
var subKeysList = new List<ArrayContainer>();
64-
var subKeysValuesList = new List<ArrayContainer>();
65-
66-
foreach (var pair in this)
67-
{
68-
var pairKey = pair.Key;
69-
keyList.Add(pairKey);
70-
71-
var serializeSubKeys = new List<TKey>();
72-
var serializeSubKeyValues = new List<TValue>();
73-
74-
var subDictionary = pair.Value;
75-
foreach (var subPair in subDictionary)
76-
{
77-
serializeSubKeys.Add(subPair.Key);
78-
serializeSubKeyValues.Add(subPair.Value);
79-
}
80-
81-
subKeysList.Add(new ArrayContainer { Values = serializeSubKeys.Cast<object>().ToArray() });
82-
subKeysValuesList.Add(new ArrayContainer { Values = serializeSubKeyValues.Cast<object>().ToArray() });
83-
}
84-
85-
keys = keyList.ToArray();
86-
subKeys = subKeysList.ToArray();
87-
subKeyValues = subKeysValuesList.ToArray();
88-
}
89-
90-
// load dictionary from lists
91-
public void OnAfterDeserialize()
92-
{
93-
this.Clear();
94-
}
95-
}
9644
}

0 commit comments

Comments
 (0)