@@ -197,8 +197,28 @@ public LocalConfigBranchDictionary(IDictionary<string, ConfigBranch> dictionary)
197
197
}
198
198
199
199
[ Serializable ]
200
- class RemoteConfigBranchDictionary : SerializableNestedDictionary < string , ConfigBranch > , IRemoteConfigBranchDictionary
200
+ public class ArrayContainer < T >
201
201
{
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
+
202
222
public RemoteConfigBranchDictionary ( )
203
223
{ }
204
224
@@ -208,6 +228,72 @@ public RemoteConfigBranchDictionary(IDictionary<string, IDictionary<string, Conf
208
228
{
209
229
Add ( pair . Key , pair . Value . ToDictionary ( valuePair => valuePair . Key , valuePair => valuePair . Value ) ) ;
210
230
}
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
+ }
211
297
}
212
298
213
299
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
613
699
var now = DateTimeOffset . Now ;
614
700
configRemotes = new ConfigRemoteDictionary ( remoteDictionary ) ;
615
701
remoteConfigBranches = new RemoteConfigBranchDictionary ( branchDictionary ) ;
616
-
617
702
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
-
630
703
SaveData ( now , true ) ;
631
704
}
632
705
0 commit comments