-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectDeserializer.cs
51 lines (50 loc) · 1.57 KB
/
ObjectDeserializer.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TLSchema;
namespace TLSchema
{
public class ObjectUtils
{
public static object DeserializeObject(BinaryReader reader)
{
int Constructor = reader.ReadInt32();
object obj;
Type t = null;
try
{
t = TLContext.getType(Constructor);
obj = Activator.CreateInstance(t);
}
catch (Exception ex)
{
throw new InvalidDataException("Constructor Invalid Or Context.Init Not Called !", ex);
}
if (t.IsSubclassOf(typeof(TLMethod)))
{
((TLMethod)obj).DeserializeResponse(reader);
return obj;
}
else if (t.IsSubclassOf(typeof(TLObject)))
{
((TLObject)obj).DeserializeBody(reader);
return obj;
}
else throw new NotImplementedException("Weird Type : " + t.Namespace + " | " + t.Name);
}
public static void SerializeObject(object obj, BinaryWriter writer)
{
((TLObject)obj).SerializeBody(writer);
}
public static TLVector<T> DeserializeVector<T>(BinaryReader reader)
{
if (reader.ReadInt32() != 481674261) throw new InvalidDataException("Bad Constructor");
TLVector<T> t = new TLVector<T>();
t.DeserializeBody(reader);
return t;
}
}
}