From 8eb5b29d9776e918e4b5e4459d6225dde90744e2 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Fri, 8 Mar 2024 12:07:37 +0100 Subject: [PATCH] using primary constructors, and more [] syntax --- src/Client.Helpers.cs | 12 ++++++------ src/Client.cs | 2 +- src/Encryption.cs | 15 ++++----------- src/SecretChats.cs | 2 +- src/Session.cs | 6 ++---- src/TL.Extensions.cs | 4 ++-- src/TL.Helpers.cs | 2 +- src/TlsStream.cs | 3 +-- 8 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/Client.Helpers.cs b/src/Client.Helpers.cs index 4775d48..6b3c762 100644 --- a/src/Client.Helpers.cs +++ b/src/Client.Helpers.cs @@ -83,7 +83,7 @@ namespace WTelegram } } Task[] remainingTasks; - lock (tasks) remainingTasks = tasks.Values.ToArray(); + lock (tasks) remainingTasks = [.. tasks.Values]; await Task.WhenAll(remainingTasks); // wait completion and eventually propagate any task exception return isBig ? new InputFileBig { id = file_id, parts = file_total_parts, name = filename } : new InputFile { id = file_id, parts = file_total_parts, name = filename }; @@ -398,7 +398,7 @@ namespace WTelegram } } Task[] remainingTasks; - lock (tasks) remainingTasks = tasks.Values.ToArray(); + lock (tasks) remainingTasks = [.. tasks.Values]; await Task.WhenAll(remainingTasks); // wait completion and eventually propagate any task exception await outputStream.FlushAsync(); if (canSeek) outputStream.Seek(streamStartPos + maxOffsetSeen, SeekOrigin.Begin); @@ -491,8 +491,8 @@ namespace WTelegram foreach (var (key, value) in md.chats) mds.chats[key] = value; foreach (var (key, value) in md.users) mds.users[key] = value; } - mds.dialogs = dialogList.ToArray(); - mds.messages = messageList.ToArray(); + mds.dialogs = [.. dialogList]; + mds.messages = [.. messageList]; return mds; case Messages_Dialogs md: return md; default: throw new WTException("Messages_GetDialogs returned unexpected " + dialogs?.GetType().Name); @@ -526,7 +526,7 @@ namespace WTelegram await GetWithFilter(new ChannelParticipantsKicked { q = "" }, (f, c) => new ChannelParticipantsKicked { q = f.q + c }, alphabet1); await GetWithFilter(new ChannelParticipantsBanned { q = "" }, (f, c) => new ChannelParticipantsBanned { q = f.q + c }, alphabet1); } - result.participants = participants.ToArray(); + result.participants = [.. participants]; return result; async Task GetWithFilter(T filter, Func recurse = null, string alphabet = null) where T : ChannelParticipantsFilter @@ -572,7 +572,7 @@ namespace WTelegram foreach (var kvp in result.chats) resultFull.chats[kvp.Key] = kvp.Value; foreach (var kvp in result.users) resultFull.users[kvp.Key] = kvp.Value; } - resultFull.events = events.ToArray(); + resultFull.events = [.. events]; return resultFull; } diff --git a/src/Client.cs b/src/Client.cs index c12482a..cd808eb 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -1254,7 +1254,7 @@ namespace WTelegram lock (_msgsToAck) { if (_msgsToAck.Count == 0) return null; - var msgsAck = new MsgsAck { msg_ids = _msgsToAck.ToArray() }; + var msgsAck = new MsgsAck { msg_ids = [.. _msgsToAck] }; _msgsToAck.Clear(); return msgsAck; } diff --git a/src/Encryption.cs b/src/Encryption.cs index a7ede50..2069ef2 100644 --- a/src/Encryption.cs +++ b/src/Encryption.cs @@ -319,19 +319,12 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB } #if OBFUSCATION - internal class AesCtr : IDisposable + internal class AesCtr(byte[] key, byte[] ivec) : IDisposable { - readonly ICryptoTransform _encryptor; - readonly byte[] _ivec; + readonly ICryptoTransform _encryptor = AesECB.CreateEncryptor(key, null); readonly byte[] _ecount = new byte[16]; int _num; - public AesCtr(byte[] key, byte[] iv) - { - _encryptor = AesECB.CreateEncryptor(key, null); - _ivec = iv; - } - public void Dispose() => _encryptor.Dispose(); public void EncryptDecrypt(byte[] buffer, int length) @@ -340,9 +333,9 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB { if (_num == 0) { - _encryptor.TransformBlock(_ivec, 0, 16, _ecount, 0); + _encryptor.TransformBlock(ivec, 0, 16, _ecount, 0); for (int n = 15; n >= 0; n--) // increment big-endian counter - if (++_ivec[n] != 0) break; + if (++ivec[n] != 0) break; } buffer[i] ^= _ecount[_num]; _num = (_num + 1) % 16; diff --git a/src/SecretChats.cs b/src/SecretChats.cs index 9cc89cd..b022bda 100644 --- a/src/SecretChats.cs +++ b/src/SecretChats.cs @@ -80,7 +80,7 @@ namespace WTelegram } public void Dispose() { OnChanged?.Invoke(); storage?.Dispose(); sha256.Dispose(); sha1.Dispose(); } - public List Chats => chats.Values.ToList(); + public List Chats => [.. chats.Values]; public bool IsChatActive(int chat_id) => !(chats.GetValueOrDefault(chat_id)?.flags.HasFlag(SecretChat.Flags.requestChat) ?? true); diff --git a/src/Session.cs b/src/Session.cs index 950cad0..70c88ac 100644 --- a/src/Session.cs +++ b/src/Session.cs @@ -192,11 +192,9 @@ namespace WTelegram } } - internal class ActionStore : MemoryStream + internal class ActionStore(byte[] initial, Action save) : MemoryStream(initial ?? []) { - private readonly Action _save; - public ActionStore(byte[] initial, Action save) : base(initial ?? []) => _save = save; - public override void Write(byte[] buffer, int offset, int count) => _save(buffer[offset..(offset + count)]); + public override void Write(byte[] buffer, int offset, int count) => save(buffer[offset..(offset + count)]); public override void SetLength(long value) { } } } \ No newline at end of file diff --git a/src/TL.Extensions.cs b/src/TL.Extensions.cs index 26986a0..a4baf3a 100644 --- a/src/TL.Extensions.cs +++ b/src/TL.Extensions.cs @@ -165,7 +165,7 @@ namespace TL if (lastBlockQuote is { length: -1 }) lastBlockQuote.length = sb.Length - lastBlockQuote.offset; text = sb.ToString(); - return entities.Count == 0 ? null : entities.ToArray(); + return entities.Count == 0 ? null : [.. entities]; } /// Converts the (plain text + entities) format used by Telegram messages into a Markdown text @@ -362,7 +362,7 @@ namespace TL offset++; } text = sb.ToString(); - return entities.Count == 0 ? null : entities.ToArray(); + return entities.Count == 0 ? null : [.. entities]; } /// Converts the (plain text + entities) format used by Telegram messages into an HTML-formatted text diff --git a/src/TL.Helpers.cs b/src/TL.Helpers.cs index f74311b..d113743 100644 --- a/src/TL.Helpers.cs +++ b/src/TL.Helpers.cs @@ -112,7 +112,7 @@ namespace TL file = inputFile; mime_type = mimeType; if (inputFile.Name is string filename && !attribs.Any(a => a is DocumentAttributeFilename)) - attributes = attribs.Append(new DocumentAttributeFilename { file_name = filename }).ToArray(); + attributes = [.. attribs, new DocumentAttributeFilename { file_name = filename }]; else attributes = attribs; } diff --git a/src/TlsStream.cs b/src/TlsStream.cs index 5b7b7ac..b7d4632 100644 --- a/src/TlsStream.cs +++ b/src/TlsStream.cs @@ -11,9 +11,8 @@ using System.Threading.Tasks; namespace WTelegram { - class TlsStream : Helpers.IndirectStream + class TlsStream(Stream innerStream) : Helpers.IndirectStream(innerStream) { - public TlsStream(Stream innerStream) : base(innerStream) { } private int _tlsFrameleft; private readonly byte[] _tlsSendHeader = [0x17, 0x03, 0x03, 0, 0]; private readonly byte[] _tlsReadHeader = new byte[5];