fix #242: correctly handle UserEmpty in ReadTLDictionary

This commit is contained in:
Wizou 2024-04-04 11:12:59 +02:00
parent abeed476e7
commit f3ca76bb8f
3 changed files with 7 additions and 7 deletions

View file

@ -208,7 +208,7 @@ public class MTProtoGenerator : IIncrementalGenerator
foreach (var nullable in nullables) foreach (var nullable in nullables)
makeTL.AppendLine($"\t\t\t0x{nullable.Value:X8} => null,"); makeTL.AppendLine($"\t\t\t0x{nullable.Value:X8} => null,");
makeTL.AppendLine("\t\t\tvar ctorNb => throw new Exception($\"Cannot find type for ctor #{ctorNb:x}\")"); makeTL.AppendLine("\t\t\tvar ctorNb => throw new WTelegram.WTException($\"Cannot find type for ctor #{ctorNb:x}\")");
makeTL.AppendLine("\t\t};"); makeTL.AppendLine("\t\t};");
namespaces["TL"]["Layer"] = makeTL.ToString(); namespaces["TL"]["Layer"] = makeTL.ToString();
foreach (var namesp in namespaces) foreach (var namesp in namespaces)

View file

@ -267,9 +267,7 @@ namespace WTelegram
public class WTException : ApplicationException public class WTException : ApplicationException
{ {
public readonly int ErrorCode;
public WTException(string message) : base(message) { } public WTException(string message) : base(message) { }
public WTException(string message, int code) : base(message) => ErrorCode = code;
public WTException(string message, Exception innerException) : base(message, innerException) { } public WTException(string message, Exception innerException) : base(message, innerException) { }
} }
} }

View file

@ -32,9 +32,9 @@ namespace TL
public readonly int Bit = bit; public readonly int Bit = bit;
} }
public sealed class RpcException(int code, string message, int x = -1) : WTelegram.WTException(message, code) public sealed class RpcException(int code, string message, int x = -1) : WTelegram.WTException(message)
{ {
public int Code => ErrorCode; public readonly int Code = code;
/// <summary>The value of X in the message, -1 if no variable X was found</summary> /// <summary>The value of X in the message, -1 if no variable X was found</summary>
public readonly int X = x; public readonly int X = x;
public override string ToString() { var str = base.ToString(); return str.Insert(str.IndexOf(':') + 1, " " + Code); } public override string ToString() { var str = base.ToString(); return str.Insert(str.IndexOf(':') + 1, " " + Code); }
@ -278,8 +278,10 @@ namespace TL
var dict = new Dictionary<long, T>(count); var dict = new Dictionary<long, T>(count);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
var value = (T)reader.ReadTLObject(); var obj = reader.ReadTLObject();
dict[value.ID] = value is UserEmpty ? null : value; if (obj is T value) dict[value.ID] = value;
else if (obj is UserEmpty ue) dict[ue.id] = null;
else throw new InvalidCastException($"ReadTLDictionary got '{obj?.GetType().Name}' instead of '{typeof(T).Name}'");
} }
return dict; return dict;
} }