using primary constructors, and more [] syntax

This commit is contained in:
Wizou 2024-03-08 12:07:37 +01:00
parent d0460f296c
commit 8eb5b29d97
8 changed files with 18 additions and 28 deletions

View file

@ -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>(T filter, Func<T, char, T> 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;
}

View file

@ -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;
}

View file

@ -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;

View file

@ -80,7 +80,7 @@ namespace WTelegram
}
public void Dispose() { OnChanged?.Invoke(); storage?.Dispose(); sha256.Dispose(); sha1.Dispose(); }
public List<ISecretChat> Chats => chats.Values.ToList<ISecretChat>();
public List<ISecretChat> Chats => [.. chats.Values];
public bool IsChatActive(int chat_id) => !(chats.GetValueOrDefault(chat_id)?.flags.HasFlag(SecretChat.Flags.requestChat) ?? true);

View file

@ -192,11 +192,9 @@ namespace WTelegram
}
}
internal class ActionStore : MemoryStream
internal class ActionStore(byte[] initial, Action<byte[]> save) : MemoryStream(initial ?? [])
{
private readonly Action<byte[]> _save;
public ActionStore(byte[] initial, Action<byte[]> 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) { }
}
}

View file

@ -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];
}
/// <summary>Converts the (plain text + entities) format used by Telegram messages into a <a href="https://core.telegram.org/bots/api/#markdownv2-style">Markdown text</a></summary>
@ -362,7 +362,7 @@ namespace TL
offset++;
}
text = sb.ToString();
return entities.Count == 0 ? null : entities.ToArray();
return entities.Count == 0 ? null : [.. entities];
}
/// <summary>Converts the (plain text + entities) format used by Telegram messages into an <a href="https://core.telegram.org/bots/api/#html-style">HTML-formatted text</a></summary>

View file

@ -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;
}

View file

@ -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];