Skip to content

Commit f3ca76b

Browse files
committed
fix #242: correctly handle UserEmpty in ReadTLDictionary
1 parent abeed47 commit f3ca76b

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

generator/MTProtoGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static void Execute(SourceProductionContext context, (Compilation compilation, I
208208

209209
foreach (var nullable in nullables)
210210
makeTL.AppendLine($"\t\t\t0x{nullable.Value:X8} => null,");
211-
makeTL.AppendLine("\t\t\tvar ctorNb => throw new Exception($\"Cannot find type for ctor #{ctorNb:x}\")");
211+
makeTL.AppendLine("\t\t\tvar ctorNb => throw new WTelegram.WTException($\"Cannot find type for ctor #{ctorNb:x}\")");
212212
makeTL.AppendLine("\t\t};");
213213
namespaces["TL"]["Layer"] = makeTL.ToString();
214214
foreach (var namesp in namespaces)

src/Helpers.cs

-2
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,7 @@ public class IndirectStream(Stream innerStream) : Stream
267267

268268
public class WTException : ApplicationException
269269
{
270-
public readonly int ErrorCode;
271270
public WTException(string message) : base(message) { }
272-
public WTException(string message, int code) : base(message) => ErrorCode = code;
273271
public WTException(string message, Exception innerException) : base(message, innerException) { }
274272
}
275273
}

src/TL.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public sealed class IfFlagAttribute(int bit) : Attribute
3232
public readonly int Bit = bit;
3333
}
3434

35-
public sealed class RpcException(int code, string message, int x = -1) : WTelegram.WTException(message, code)
35+
public sealed class RpcException(int code, string message, int x = -1) : WTelegram.WTException(message)
3636
{
37-
public int Code => ErrorCode;
37+
public readonly int Code = code;
3838
/// <summary>The value of X in the message, -1 if no variable X was found</summary>
3939
public readonly int X = x;
4040
public override string ToString() { var str = base.ToString(); return str.Insert(str.IndexOf(':') + 1, " " + Code); }
@@ -278,8 +278,10 @@ internal static Dictionary<long, T> ReadTLDictionary<T>(this BinaryReader reader
278278
var dict = new Dictionary<long, T>(count);
279279
for (int i = 0; i < count; i++)
280280
{
281-
var value = (T)reader.ReadTLObject();
282-
dict[value.ID] = value is UserEmpty ? null : value;
281+
var obj = reader.ReadTLObject();
282+
if (obj is T value) dict[value.ID] = value;
283+
else if (obj is UserEmpty ue) dict[ue.id] = null;
284+
else throw new InvalidCastException($"ReadTLDictionary got '{obj?.GetType().Name}' instead of '{typeof(T).Name}'");
283285
}
284286
return dict;
285287
}

0 commit comments

Comments
 (0)