TLSharp/TeleSharp.TL/TLVector.cs

163 lines
4.3 KiB
C#
Raw Normal View History

2016-09-24 15:38:26 +02:00
using System;
2017-11-09 10:52:29 +01:00
using System.Collections;
2016-09-24 15:38:26 +02:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TeleSharp.TL
{
2017-11-09 10:52:29 +01:00
public class TLVector<T> : TLObject, IList<T>
2016-09-24 15:38:26 +02:00
{
[TLObject(481674261)]
private List<T> lists;
public TLVector(IEnumerable<T> collection)
{
lists = new List<T>(collection);
}
public TLVector()
{
lists = new List<T>();
}
2017-11-09 10:52:29 +01:00
public T this[int index]
{
get { return lists[index]; }
set { lists[index] = value; }
}
2016-09-24 15:38:26 +02:00
public override int Constructor
{
get
{
return 481674261;
}
}
2017-11-09 10:52:29 +01:00
public int Count => lists.Count;
public bool IsReadOnly => ((IList<T>)lists).IsReadOnly;
public void Add(T item)
{
lists.Add(item);
}
public void Clear()
{
lists.Clear();
}
public bool Contains(T item)
{
return lists.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
lists.CopyTo(array, arrayIndex);
}
2016-09-24 15:38:26 +02:00
public override void DeserializeBody(BinaryReader br)
{
int count = br.ReadInt32();
2016-10-29 08:53:45 +02:00
for (var i = 0; i < count; i++)
2016-09-24 15:38:26 +02:00
{
if (typeof(T) == typeof(int))
{
2016-10-29 08:53:45 +02:00
lists.Add((T)Convert.ChangeType(br.ReadInt32(), typeof(T)));
2016-09-24 15:38:26 +02:00
}
2016-10-29 08:53:45 +02:00
else if (typeof(T) == typeof(long))
2016-09-24 15:38:26 +02:00
{
lists.Add((T)Convert.ChangeType(br.ReadInt64(), typeof(T)));
}
2016-10-29 08:53:45 +02:00
else if (typeof(T) == typeof(string))
2016-09-24 15:38:26 +02:00
{
lists.Add((T)Convert.ChangeType(StringUtil.Deserialize(br), typeof(T)));
}
2016-10-29 08:53:45 +02:00
else if (typeof(T) == typeof(double))
2016-09-24 15:38:26 +02:00
{
lists.Add((T)Convert.ChangeType(br.ReadDouble(), typeof(T)));
}
else if (typeof(T).BaseType == typeof(TLObject))
{
int constructor = br.ReadInt32();
Type type = TLContext.getType(constructor);
object obj = Activator.CreateInstance(type);
type.GetMethod("DeserializeBody").Invoke(obj, new object[] { br });
lists.Add((T)Convert.ChangeType(obj, type));
2016-09-24 15:38:26 +02:00
}
}
}
2017-11-09 10:52:29 +01:00
public IEnumerator<T> GetEnumerator()
{
return lists.GetEnumerator();
}
public int IndexOf(T item)
{
return lists.IndexOf(item);
}
public void Insert(int index, T item)
{
lists.Insert(index, item);
}
public bool Remove(T item)
{
return lists.Remove(item);
}
public void RemoveAt(int index)
{
lists.RemoveAt(index);
}
2016-09-24 15:38:26 +02:00
public override void SerializeBody(BinaryWriter bw)
{
2016-10-29 08:53:45 +02:00
bw.Write(Constructor);
bw.Write(lists.Count());
2016-10-29 08:53:45 +02:00
foreach (var item in lists)
{
if (typeof(T) == typeof(int))
{
var res = (int)Convert.ChangeType(item, typeof(int));
bw.Write(res);
}
else if (typeof(T) == typeof(long))
{
var res = (long)Convert.ChangeType(item, typeof(long));
bw.Write(res);
}
else if (typeof(T) == typeof(string))
{
var res = (string)(Convert.ChangeType(item, typeof(string)));
StringUtil.Serialize(res, bw);
}
else if (typeof(T) == typeof(double))
{
var res = (double)Convert.ChangeType(item, typeof(double));
bw.Write(res);
}
else if (typeof(T).BaseType == typeof(TLObject))
{
var res = (TLObject)(object)item;
2016-10-29 08:53:45 +02:00
res.SerializeBody(bw);
}
2016-10-29 08:53:45 +02:00
}
2016-09-24 15:38:26 +02:00
}
2017-11-09 10:52:29 +01:00
IEnumerator IEnumerable.GetEnumerator()
{
return lists.GetEnumerator();
}
2016-09-24 15:38:26 +02:00
}
}