From 3ea897402cf057aa0021afd547810fe13c8e508b Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Sun, 13 Sep 2020 18:18:28 +0800 Subject: [PATCH] TgSharp.TL: fix invalid Context type initialization for TLVector Fixes https://github.com/nblockchain/TgSharp/issues/15 --- src/TgSharp.TL/ObjectDeserializer.cs | 4 +++- src/TgSharp.TL/TLContext.cs | 14 +++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/TgSharp.TL/ObjectDeserializer.cs b/src/TgSharp.TL/ObjectDeserializer.cs index 08d7b2b..564a927 100644 --- a/src/TgSharp.TL/ObjectDeserializer.cs +++ b/src/TgSharp.TL/ObjectDeserializer.cs @@ -12,6 +12,7 @@ namespace TgSharp.TL public static object DeserializeObject(BinaryReader reader) { int Constructor = reader.ReadInt32(); + object obj; Type t = null; try @@ -21,8 +22,9 @@ namespace TgSharp.TL } catch (Exception ex) { - throw new InvalidDataException("Constructor Invalid Or Context.Init Not Called !", ex); + throw new InvalidDataException("Invalid constructor, or invalid TLContext static initialization", ex); } + if (t.IsSubclassOf(typeof(TLMethod))) { ((TLMethod)obj).DeserializeResponse(reader); diff --git a/src/TgSharp.TL/TLContext.cs b/src/TgSharp.TL/TLContext.cs index 2d04707..b0c3c86 100644 --- a/src/TgSharp.TL/TLContext.cs +++ b/src/TgSharp.TL/TLContext.cs @@ -19,7 +19,19 @@ namespace TgSharp.TL where t.IsSubclassOf(typeof(TLObject)) where t.GetCustomAttribute(typeof(TLObjectAttribute)) != null select t).ToDictionary(x => ((TLObjectAttribute)x.GetCustomAttribute(typeof(TLObjectAttribute))).Constructor, x => x); - Types.Add(481674261, typeof(TLVector<>)); + + + var vectorTypeId = 481674261; + var genericVectorType = typeof (TLVector<>); + + Type type; + if (Types.TryGetValue(vectorTypeId, out type)) { + if (type != genericVectorType && type != typeof(TLVector)) { + throw new InvalidOperationException ($"Type {vectorTypeId} should have been a TLVector type but was {type}"); + } + } else { + Types [vectorTypeId] = genericVectorType; + } } public static Type getType(int Constructor)