From f901319ca4992c2efac28dbdee1be2f107cd410c Mon Sep 17 00:00:00 2001 From: Wizou Date: Sat, 13 Nov 2021 15:09:28 +0100 Subject: [PATCH] simplified Help_GetAppConfig access to Json values --- src/Client.cs | 1 + src/TL.Helpers.cs | 57 +++++++++++++++++++++++++++++++++++++++++++---- src/TL.Schema.cs | 14 +++++------- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/Client.cs b/src/Client.cs index 9942a44..d399dcc 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -1224,6 +1224,7 @@ namespace WTelegram int fileOffset = 0, maxOffsetSeen = 0; long transmitted = 0; var tasks = new Dictionary(); + progress?.Invoke(0, fileSize); bool abort = false; while (!abort) { diff --git a/src/TL.Helpers.cs b/src/TL.Helpers.cs index bdab3a5..8e567c2 100644 --- a/src/TL.Helpers.cs +++ b/src/TL.Helpers.cs @@ -367,10 +367,27 @@ namespace TL } partial class JsonObjectValue { public override string ToString() => $"{HttpUtility.JavaScriptStringEncode(key, true)}:{value}"; } - partial class JsonNull { public override string ToString() => "null"; } - partial class JsonBool { public override string ToString() => value ? "true" : "false"; } - partial class JsonNumber { public override string ToString() => value.ToString(CultureInfo.InvariantCulture); } - partial class JsonString { public override string ToString() => HttpUtility.JavaScriptStringEncode(value, true); } + partial class JSONValue { public abstract object ToNative(); } + partial class JsonNull + { + public override string ToString() => "null"; + public override object ToNative() => null; + } + partial class JsonBool + { + public override string ToString() => value ? "true" : "false"; + public override object ToNative() => value; + } + partial class JsonNumber + { + public override string ToString() => value.ToString(CultureInfo.InvariantCulture); + public override object ToNative() => value; + } + partial class JsonString + { + public override string ToString() => HttpUtility.JavaScriptStringEncode(value, true); + public override object ToNative() => value; + } partial class JsonArray { public override string ToString() @@ -380,6 +397,22 @@ namespace TL sb.Append(i == 0 ? "" : ",").Append(value[i]); return sb.Append(']').ToString(); } + public object[] ToNativeArray() => value.Select(v => v.ToNative()).ToArray(); + public override object ToNative() + { + if (value.Length == 0) return Array.Empty(); + var first = value[0].ToNative(); + var elementType = first.GetType(); + var array = Array.CreateInstance(elementType, value.Length); + array.SetValue(first, 0); + for (int i = 1; i < value.Length; i++) + { + var elem = value[i].ToNative(); + if (elem.GetType() != elementType) return ToNativeArray(); + array.SetValue(elem, i); + } + return array; + } } partial class JsonObject { @@ -390,6 +423,22 @@ namespace TL sb.Append(i == 0 ? "" : ",").Append(value[i]); return sb.Append('}').ToString(); } + public Dictionary ToDictionary() => value.ToDictionary(v => v.key, v => v.value.ToNative()); + public override object ToNative() + { + if (value.Length == 0) return new Dictionary(); + var first = value[0].value.ToNative(); + var elementType = first.GetType(); + var dic = Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(typeof(string), elementType)) as System.Collections.IDictionary; + dic.Add(value[0].key, first); + for (int i = 1; i < value.Length; i++) + { + var elem = value[i].value.ToNative(); + if (elem.GetType() != elementType) return ToDictionary(); + dic.Add(value[i].key, elem); + } + return dic; + } } public static class Markdown diff --git a/src/TL.Schema.cs b/src/TL.Schema.cs index bd23d22..d192858 100644 --- a/src/TL.Schema.cs +++ b/src/TL.Schema.cs @@ -10070,11 +10070,9 @@ namespace TL public JSONValue data; } - /// JSON key: value pair Derived classes: See - public abstract class JSONObjectValue : IObject { } /// JSON key: value pair See [TLDef(0xC0DE1BD9)] - public partial class JsonObjectValue : JSONObjectValue + public partial class JsonObjectValue : IObject { /// Key public string key; @@ -10083,7 +10081,7 @@ namespace TL } /// JSON value Derived classes: , , , , , See - public abstract class JSONValue : IObject { } + public abstract partial class JSONValue : IObject { } /// null JSON value See [TLDef(0x3F6D7B68)] public partial class JsonNull : JSONValue { } @@ -10120,7 +10118,7 @@ namespace TL public partial class JsonObject : JSONValue { /// Values - public JSONObjectValue[] value; + public JsonObjectValue[] value; } /// Table cell See @@ -10475,7 +10473,7 @@ namespace TL send_inline = 0x40, /// If set, does not allow a user to embed links in the messages of a supergroup/chat embed_links = 0x80, - /// If set, does not allow a user to send stickers in a supergroup/chat + /// If set, does not allow a user to send polls in a supergroup/chat send_polls = 0x100, /// If set, does not allow any user to change the description of a supergroup/chat change_info = 0x400, @@ -15075,7 +15073,7 @@ namespace TL path = path, }); /// Get app-specific configuration, see client configuration for more info on the result. See - public static Task Help_GetAppConfig(this Client client) + public static Task Help_GetAppConfig(this Client client) => client.CallAsync(new Help_GetAppConfig { }); @@ -18718,7 +18716,7 @@ namespace TL.Methods } [TLDef(0x98914110)] - public class Help_GetAppConfig : IMethod { } + public class Help_GetAppConfig : IMethod { } [TLDef(0x6F02F748)] public class Help_SaveAppLog : IMethod