mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace TeleSharp.TL
|
|
{
|
|
public class ObjectUtils
|
|
{
|
|
public static object DeserializeObject(BinaryReader reader)
|
|
{
|
|
var 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;
|
|
}
|
|
if (t.IsSubclassOf(typeof(TLObject)))
|
|
{
|
|
((TLObject) obj).DeserializeBody(reader);
|
|
return obj;
|
|
}
|
|
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");
|
|
var t = new TLVector<T>();
|
|
t.DeserializeBody(reader);
|
|
return t;
|
|
}
|
|
}
|
|
} |