Example #1
0
 /// <summary>
 /// Encode a collection
 /// </summary>
 ByteString EncodeCollection(Type type, object value)
 {
     if (TypeUtils.IsAListCollectionType(type))
     {
         var encodedList = new Schema.KRPC.List();
         var list        = (System.Collections.IList)value;
         var valueType   = type.GetGenericArguments().Single();
         foreach (var item in list)
         {
             encodedList.Items.Add(Encode(valueType, item));
         }
         return(ProtocolBuffers.WriteMessage(encodedList));
     }
     else if (TypeUtils.IsADictionaryCollectionType(type))
     {
         var keyType           = type.GetGenericArguments() [0];
         var valueType         = type.GetGenericArguments() [1];
         var encodedDictionary = new Schema.KRPC.Dictionary();
         foreach (System.Collections.DictionaryEntry entry in (System.Collections.IDictionary)value)
         {
             var encodedEntry = new Schema.KRPC.DictionaryEntry();
             encodedEntry.Key   = Encode(keyType, entry.Key);
             encodedEntry.Value = Encode(valueType, entry.Value);
             encodedDictionary.Entries.Add(encodedEntry);
         }
         return(ProtocolBuffers.WriteMessage(encodedDictionary));
     }
     else if (TypeUtils.IsASetCollectionType(type))
     {
         var encodedSet = new Schema.KRPC.Set();
         var set        = (System.Collections.IEnumerable)value;
         var valueType  = type.GetGenericArguments().Single();
         foreach (var item in set)
         {
             encodedSet.Items.Add(Encode(valueType, item));
         }
         return(ProtocolBuffers.WriteMessage(encodedSet));
     }
     else     // a tuple
     // TODO: this is ugly
     {
         var encodedTuple = new Schema.KRPC.Tuple();
         var valueTypes   = type.GetGenericArguments().ToArray();
         var genericType  = Type.GetType("KRPC.Utils.Tuple`" + valueTypes.Length);
         var tupleType    = genericType.MakeGenericType(valueTypes);
         for (int i = 0; i < valueTypes.Length; i++)
         {
             var property = tupleType.GetProperty("Item" + (i + 1));
             var item     = property.GetGetMethod().Invoke(value, null);
             encodedTuple.Items.Add(Encode(valueTypes [i], item));
         }
         return(ProtocolBuffers.WriteMessage(encodedTuple));
     }
 }
Example #2
0
        static void WriteDictionary(object value, CodedOutputStream stream)
        {
            var encodedDictionary = new Schema.KRPC.Dictionary();

            using (var internalBuffer = new MemoryStream()) {
                var internalStream = new CodedOutputStream(internalBuffer);
                foreach (DictionaryEntry entry in (IDictionary)value)
                {
                    var encodedEntry = new Schema.KRPC.DictionaryEntry();
                    encodedEntry.Key   = EncodeObject(entry.Key, internalBuffer, internalStream);
                    encodedEntry.Value = EncodeObject(entry.Value, internalBuffer, internalStream);
                    encodedDictionary.Entries.Add(encodedEntry);
                }
            }
            encodedDictionary.WriteTo(stream);
        }
Example #3
0
        static void WriteDictionary(object value, Type type, Stream stream)
        {
            var keyType           = type.GetGenericArguments() [0];
            var valueType         = type.GetGenericArguments() [1];
            var encodedDictionary = new Schema.KRPC.Dictionary();

            using (var internalBuffer = new MemoryStream()) {
                var internalStream = new CodedOutputStream(internalBuffer);
                foreach (DictionaryEntry entry in (IDictionary)value)
                {
                    var encodedEntry = new Schema.KRPC.DictionaryEntry();
                    encodedEntry.Key   = EncodeObject(entry.Key, keyType, internalBuffer, internalStream);
                    encodedEntry.Value = EncodeObject(entry.Value, valueType, internalBuffer, internalStream);
                    encodedDictionary.Entries.Add(encodedEntry);
                }
            }
            encodedDictionary.WriteTo(stream);
        }
Example #4
0
 /// <summary>
 /// Encode a collection
 /// </summary>
 ByteString EncodeCollection(Type type, object value)
 {
     if (TypeUtils.IsAListCollectionType (type)) {
         var encodedList = new Schema.KRPC.List ();
         var list = (System.Collections.IList)value;
         var valueType = type.GetGenericArguments ().Single ();
         foreach (var item in list)
             encodedList.Items.Add (Encode (valueType, item));
         return ProtocolBuffers.WriteMessage (encodedList);
     } else if (TypeUtils.IsADictionaryCollectionType (type)) {
         var keyType = type.GetGenericArguments () [0];
         var valueType = type.GetGenericArguments () [1];
         var encodedDictionary = new Schema.KRPC.Dictionary ();
         foreach (System.Collections.DictionaryEntry entry in (System.Collections.IDictionary) value) {
             var encodedEntry = new Schema.KRPC.DictionaryEntry ();
             encodedEntry.Key = Encode (keyType, entry.Key);
             encodedEntry.Value = Encode (valueType, entry.Value);
             encodedDictionary.Entries.Add (encodedEntry);
         }
         return ProtocolBuffers.WriteMessage (encodedDictionary);
     } else if (TypeUtils.IsASetCollectionType (type)) {
         var encodedSet = new Schema.KRPC.Set ();
         var set = (System.Collections.IEnumerable)value;
         var valueType = type.GetGenericArguments ().Single ();
         foreach (var item in set)
             encodedSet.Items.Add (Encode (valueType, item));
         return ProtocolBuffers.WriteMessage (encodedSet);
     } else { // a tuple
         // TODO: this is ugly
         var encodedTuple = new Schema.KRPC.Tuple ();
         var valueTypes = type.GetGenericArguments ().ToArray ();
         var genericType = Type.GetType ("KRPC.Utils.Tuple`" + valueTypes.Length);
         var tupleType = genericType.MakeGenericType (valueTypes);
         for (int i = 0; i < valueTypes.Length; i++) {
             var property = tupleType.GetProperty ("Item" + (i + 1));
             var item = property.GetGetMethod ().Invoke (value, null);
             encodedTuple.Items.Add (Encode (valueTypes [i], item));
         }
         return ProtocolBuffers.WriteMessage (encodedTuple);
     }
 }