Using Serilog. Added partial OBS batch request support. Added update checking. Added more commands. Added enabled/disabled TTS voices. And more.

This commit is contained in:
Tom
2024-06-17 00:19:31 +00:00
parent d4004d6230
commit 706cd06930
67 changed files with 1933 additions and 925 deletions

View File

@ -2,7 +2,7 @@ using System.Text.Json;
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
@ -11,9 +11,11 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
{
private ILogger Logger { get; }
private EmoteDatabase Emotes { get; }
private object _lock = new object();
public int OperationCode { get; set; } = 0;
public DispatchHandler(ILogger<DispatchHandler> logger, EmoteDatabase emotes) {
public DispatchHandler(ILogger logger, EmoteDatabase emotes)
{
Logger = logger;
Emotes = emotes;
}
@ -22,33 +24,82 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
{
if (message is not DispatchMessage obj || obj == null)
return;
ApplyChanges(obj?.Body?.Pulled, cf => cf.OldValue, true);
ApplyChanges(obj?.Body?.Pushed, cf => cf.Value, false);
ApplyChanges(obj?.Body?.Removed, cf => cf.OldValue, true);
ApplyChanges(obj?.Body?.Updated, cf => cf.OldValue, false, cf => cf.Value);
}
private void ApplyChanges(IEnumerable<ChangeField>? fields, Func<ChangeField, object> getter, bool removing) {
if (fields == null)
private void ApplyChanges(IEnumerable<ChangeField>? fields, Func<ChangeField, object> getter, bool removing, Func<ChangeField, object>? updater = null)
{
if (fields == null || !fields.Any() || removing && updater != null)
return;
foreach (var val in fields) {
foreach (var val in fields)
{
var value = getter(val);
if (value == null)
continue;
var o = JsonSerializer.Deserialize<EmoteField>(value.ToString(), new JsonSerializerOptions() {
var o = JsonSerializer.Deserialize<EmoteField>(value.ToString(), new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
});
if (o == null)
continue;
if (removing) {
Emotes.Remove(o.Name);
Logger.LogInformation($"Removed 7tv emote: {o.Name} (id: {o.Id})");
} else {
Emotes.Add(o.Name, o.Id);
Logger.LogInformation($"Added 7tv emote: {o.Name} (id: {o.Id})");
lock (_lock)
{
if (removing)
{
RemoveEmoteById(o.Id);
Logger.Information($"Removed 7tv emote: {o.Name} (id: {o.Id})");
}
else if (updater != null)
{
RemoveEmoteById(o.Id);
var update = updater(val);
var u = JsonSerializer.Deserialize<EmoteField>(update.ToString(), new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
});
if (u != null)
{
Emotes.Add(u.Name, u.Id);
Logger.Information($"Updated 7tv emote: from '{o.Name}' to '{u.Name}' (id: {u.Id})");
}
else
{
Logger.Warning("Failed to update 7tv emote.");
}
}
else
{
Emotes.Add(o.Name, o.Id);
Logger.Information($"Added 7tv emote: {o.Name} (id: {o.Id})");
}
}
}
}
private void RemoveEmoteById(string id)
{
string? key = null;
foreach (var e in Emotes.Emotes)
{
if (e.Value == id)
{
key = e.Key;
break;
}
}
if (key != null)
Emotes.Remove(key);
}
}
}

View File

@ -1,7 +1,7 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Context;
using TwitchChatTTS.Seven.Socket.Data;
@ -10,17 +10,18 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
public class EndOfStreamHandler : IWebSocketHandler
{
private ILogger Logger { get; }
private Configuration Configuration { get; }
private User User { get; }
private IServiceProvider ServiceProvider { get; }
private string[] ErrorCodes { get; }
private int[] ReconnectDelay { get; }
public int OperationCode { get; set; } = 7;
public EndOfStreamHandler(ILogger<EndOfStreamHandler> logger, Configuration configuration, IServiceProvider serviceProvider) {
public EndOfStreamHandler(ILogger logger, User user, IServiceProvider serviceProvider)
{
Logger = logger;
Configuration = configuration;
User = user;
ServiceProvider = serviceProvider;
ErrorCodes = [
@ -59,37 +60,40 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
{
if (message is not EndOfStreamMessage obj || obj == null)
return;
var code = obj.Code - 4000;
if (code >= 0 && code < ErrorCodes.Length)
Logger.LogWarning($"Received end of stream message (reason: {ErrorCodes[code]}, code: {obj.Code}, message: {obj.Message}).");
Logger.Warning($"Received end of stream message (reason: {ErrorCodes[code]}, code: {obj.Code}, message: {obj.Message}).");
else
Logger.LogWarning($"Received end of stream message (code: {obj.Code}, message: {obj.Message}).");
Logger.Warning($"Received end of stream message (code: {obj.Code}, message: {obj.Message}).");
await sender.DisconnectAsync();
if (code >= 0 && code < ReconnectDelay.Length && ReconnectDelay[code] < 0) {
Logger.LogError($"7tv client will remain disconnected due to a bad client implementation.");
if (code >= 0 && code < ReconnectDelay.Length && ReconnectDelay[code] < 0)
{
Logger.Error($"7tv client will remain disconnected due to a bad client implementation.");
return;
}
if (string.IsNullOrWhiteSpace(Configuration.Seven?.UserId))
if (string.IsNullOrWhiteSpace(User.SevenEmoteSetId))
return;
var context = ServiceProvider.GetRequiredService<ReconnectContext>();
await Task.Delay(ReconnectDelay[code]);
//var base_url = "@" + string.Join(",", Configuration.Seven.SevenId.Select(sub => sub.Type + "<" + string.Join(",", sub.Condition?.Select(e => e.Key + "=" + e.Value) ?? new string[0]) + ">"));
var base_url = $"@emote_set.*<object_id={Configuration.Seven.UserId.Trim()}>";
var base_url = $"@emote_set.*<object_id={User.SevenEmoteSetId}>";
string url = $"{SevenApiClient.WEBSOCKET_URL}{base_url}";
Logger.LogDebug($"7tv websocket reconnecting to {url}.");
Logger.Debug($"7tv websocket reconnecting to {url}.");
await sender.ConnectAsync(url);
if (context.SessionId != null) {
if (context.SessionId != null)
{
await sender.Send(34, new ResumeMessage() { SessionId = context.SessionId });
Logger.LogInformation("Resumed connection to 7tv websocket.");
} else {
Logger.LogDebug("7tv websocket session id not available.");
Logger.Information("Resumed connection to 7tv websocket.");
}
else
{
Logger.Information("Resumed connection to 7tv websocket on a different session.");
}
}
}

View File

@ -1,6 +1,6 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
@ -10,7 +10,8 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
private ILogger Logger { get; }
public int OperationCode { get; set; } = 6;
public ErrorHandler(ILogger<ErrorHandler> logger) {
public ErrorHandler(ILogger logger)
{
Logger = logger;
}

View File

@ -1,6 +1,6 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
@ -10,7 +10,8 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
private ILogger Logger { get; }
public int OperationCode { get; set; } = 4;
public ReconnectHandler(ILogger<ReconnectHandler> logger) {
public ReconnectHandler(ILogger logger)
{
Logger = logger;
}
@ -19,7 +20,7 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
if (message is not ReconnectMessage obj || obj == null)
return;
Logger.LogInformation($"7tv server wants us to reconnect (reason: {obj.Reason}).");
Logger.Information($"7tv server wants us to reconnect (reason: {obj.Reason}).");
}
}
}

View File

@ -1,6 +1,6 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
@ -11,7 +11,8 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
private Configuration Configuration { get; }
public int OperationCode { get; set; } = 1;
public SevenHelloHandler(ILogger<SevenHelloHandler> logger, Configuration configuration) {
public SevenHelloHandler(ILogger logger, Configuration configuration)
{
Logger = logger;
Configuration = configuration;
}
@ -23,10 +24,10 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
if (sender is not SevenSocketClient seven || seven == null)
return;
seven.Connected = true;
seven.ConnectionDetails = obj;
Logger.LogInformation("Connected to 7tv websockets.");
Logger.Information("Connected to 7tv websockets.");
}
}
}