Big update. Replaced Newtonsoft with System.Text.Json where possible, removed Spectre, tried to suppress the pile of compiler warnings I get on the GUI project, and tried to correct an issue where sometimes the session token retrieved is not usable.

This commit is contained in:
barelyprofessional
2024-06-14 23:03:05 +08:00
parent 98f7b2b27e
commit cdad1d6549
21 changed files with 281 additions and 251 deletions

View File

@@ -1,9 +1,10 @@
using System.Net;
using System.Net.WebSockets;
using System.Text.Json;
using System.Xml;
using KfChatDotNetWsClient.Models;
using KfChatDotNetWsClient.Models.Events;
using KfChatDotNetWsClient.Models.Json;
using Newtonsoft.Json;
using NLog;
using Websocket.Client;
// It's a fucking lie. You must use conditional access or you WILL get NullReferenceErrors if an event is not in use
@@ -135,7 +136,7 @@ public class ChatClient
Dictionary<string, object> packetType = new Dictionary<string, object>();
try
{
packetType = JsonConvert.DeserializeObject<Dictionary<string, object>>(message.Text)!;
packetType = JsonSerializer.Deserialize<Dictionary<string, object>>(message.Text)!;
}
catch (Exception e)
{
@@ -198,23 +199,21 @@ public class ChatClient
public void EditMessage(int messageId, string newMessage)
{
// Explicitly set formatting to none as it must be inline (Newtonsoft will do this by default but just wanting to be explicit)
var payload = JsonConvert.SerializeObject(new EditMessageJsonModel {Id = messageId, Message = newMessage},
Formatting.None);
var payload = JsonSerializer.Serialize(new EditMessageJsonModel {Id = messageId, Message = newMessage});
_logger.Debug($"Editing {messageId} with '{newMessage}'");
_wsClient.Send($"/edit {payload}");
}
private void WsDeleteMessagesReceived(ResponseMessage message)
{
var data = JsonConvert.DeserializeObject<DeleteMessagesJsonModel>(message.Text);
var data = JsonSerializer.Deserialize<DeleteMessagesJsonModel>(message.Text);
_logger.Debug($"Received delete packet for messages: {string.Join(',', data.MessageIdsToDelete)}");
OnDeleteMessages?.Invoke(this, data.MessageIdsToDelete);
}
private void WsChatMessagesReceived(ResponseMessage message)
{
var data = JsonConvert.DeserializeObject<MessagesJsonModel>(message.Text);
var data = JsonSerializer.Deserialize<MessagesJsonModel>(message.Text);
var messages = new List<MessageModel>();
foreach (var chatMessage in data.Messages)
{
@@ -250,14 +249,14 @@ public class ChatClient
_logger.Debug($"Received {messages.Count} chat messages");
if (messages.Count == 1)
{
_logger.Debug($"{JsonConvert.SerializeObject(messages[0], Formatting.Indented)}");
_logger.Debug($"{JsonSerializer.Serialize(messages[0])}");
}
OnMessages?.Invoke(this, messages, data);
}
private void WsChatUsersJoined(ResponseMessage message)
{
var data = JsonConvert.DeserializeObject<UsersJsonModel>(message.Text);
var data = JsonSerializer.Deserialize<UsersJsonModel>(message.Text);
var users = new List<UserModel>();
foreach (var user in data.Users.Keys)
{
@@ -277,7 +276,7 @@ public class ChatClient
private void WsChatUsersParted(ResponseMessage message)
{
// {"user":{"1337":false}}
var data = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, bool>>>(message.Text);
var data = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, bool>>>(message.Text);
var usersParted = data!["user"].Select(user => int.Parse(user.Key)).ToList();
_logger.Debug($"Following users have parted: {string.Join(',', usersParted)}");
OnUsersParted?.Invoke(this, usersParted);

View File

@@ -8,7 +8,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="Websocket.Client" Version="5.1.1" />
</ItemGroup>

View File

@@ -1,9 +1,9 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace KfChatDotNetWsClient.Models.Json;
public class DeleteMessagesJsonModel
{
[JsonProperty("delete")]
[JsonPropertyName("delete")]
public List<int> MessageIdsToDelete { get; set; }
}

View File

@@ -1,12 +1,12 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace KfChatDotNetWsClient.Models.Json;
public class EditMessageJsonModel
{
[JsonProperty("id")]
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonProperty("message")]
[JsonPropertyName("message")]
public string Message { get; set; }
}

View File

@@ -1,5 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text.Json.Serialization;
namespace KfChatDotNetWsClient.Models.Json;
@@ -28,32 +27,32 @@ public class MessagesJsonModel
{
public class AuthorModel
{
[JsonProperty("id")]
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonProperty("username")]
[JsonPropertyName("username")]
public string Username { get; set; }
[JsonProperty("avatar_url")]
[JsonPropertyName("avatar_url")]
public Uri AvatarUrl { get; set; }
}
public class MessageModel
{
[JsonProperty("author")]
[JsonPropertyName("author")]
public AuthorModel Author { get; set; }
[JsonProperty("message")]
[JsonPropertyName("message")]
public string Message { get; set; }
[JsonProperty("message_id")]
[JsonPropertyName("message_id")]
public int MessageId { get; set; }
[JsonProperty("message_edit_date")]
[JsonPropertyName("message_edit_date")]
public int MessageEditDate { get; set; }
[JsonProperty("message_date")]
[JsonPropertyName("message_date")]
public int MessageDate { get; set; }
[JsonProperty("message_raw")]
[JsonPropertyName("message_raw")]
public string MessageRaw { get; set; }
[JsonProperty("room_id")]
[JsonPropertyName("room_id")]
public int RoomId { get; set; }
}
[JsonProperty("messages")]
[JsonPropertyName("messages")]
public List<MessageModel> Messages { get; set; }
}

View File

@@ -1,5 +1,4 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace KfChatDotNetWsClient.Models.Json;
// {
@@ -17,16 +16,16 @@ public class UsersJsonModel
{
public class UserModel
{
[JsonProperty("id")]
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonProperty("username")]
[JsonPropertyName("username")]
public string Username { get; set; }
[JsonProperty("avatar_url")]
[JsonPropertyName("avatar_url")]
public Uri AvatarUrl { get; set; }
[JsonProperty("last_activity")]
[JsonPropertyName("last_activity")]
public int LastActivity { get; set; }
}
[JsonProperty("users")]
[JsonPropertyName("users")]
public Dictionary<string, UserModel> Users { get; set; }
}