add signalR logging and dalamudutil disposal, halt framework update between areas

This commit is contained in:
Stanley Dimant
2022-09-08 11:48:20 +02:00
parent ca044364fc
commit e5f7c2f72d
5 changed files with 106 additions and 15 deletions

View File

@@ -1,5 +1,4 @@
using MareSynchronos.API; using MareSynchronos.API;
using MareSynchronos.FileCacheDB;
using MareSynchronos.Models; using MareSynchronos.Models;
using MareSynchronos.Utils; using MareSynchronos.Utils;
using System; using System;
@@ -42,7 +41,7 @@ namespace MareSynchronos.Managers
{ {
if (!dalamudUtil.IsGameObjectPresent(item.Key)) if (!dalamudUtil.IsGameObjectPresent(item.Key))
{ {
Logger.Debug("Object not present anymore: " + item.Key); Logger.Debug("Object not present anymore: " + item.Key.ToString("X"));
TransientResources.Remove(item.Key); TransientResources.Remove(item.Key);
} }
} }

View File

@@ -14,6 +14,7 @@ using Dalamud.Interface.Windowing;
using MareSynchronos.UI; using MareSynchronos.UI;
using MareSynchronos.Utils; using MareSynchronos.Utils;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Dalamud.Game.ClientState.Conditions;
namespace MareSynchronos namespace MareSynchronos
{ {
@@ -42,7 +43,7 @@ namespace MareSynchronos
public Plugin(DalamudPluginInterface pluginInterface, CommandManager commandManager, public Plugin(DalamudPluginInterface pluginInterface, CommandManager commandManager,
Framework framework, ObjectTable objectTable, ClientState clientState) Framework framework, ObjectTable objectTable, ClientState clientState, Condition condition)
{ {
Logger.Debug("Launching " + Name); Logger.Debug("Launching " + Name);
PluginInterface = pluginInterface; PluginInterface = pluginInterface;
@@ -60,7 +61,7 @@ namespace MareSynchronos
new FileCacheContext().Dispose(); // make sure db is initialized I guess new FileCacheContext().Dispose(); // make sure db is initialized I guess
// those can be initialized outside of game login // those can be initialized outside of game login
_dalamudUtil = new DalamudUtil(clientState, objectTable, framework); _dalamudUtil = new DalamudUtil(clientState, objectTable, framework, condition);
_apiController = new ApiController(_configuration, _dalamudUtil); _apiController = new ApiController(_configuration, _dalamudUtil);
_ipcManager = new IpcManager(PluginInterface, _dalamudUtil); _ipcManager = new IpcManager(PluginInterface, _dalamudUtil);
@@ -124,7 +125,7 @@ namespace MareSynchronos
_commandManager.RemoveHandler(CommandName); _commandManager.RemoveHandler(CommandName);
_dalamudUtil.LogIn -= DalamudUtilOnLogIn; _dalamudUtil.LogIn -= DalamudUtilOnLogIn;
_dalamudUtil.LogOut -= DalamudUtilOnLogOut; _dalamudUtil.LogOut -= DalamudUtilOnLogOut;
_uiSharedComponent.Dispose(); _uiSharedComponent.Dispose();
_settingsUi?.Dispose(); _settingsUi?.Dispose();
_introUi?.Dispose(); _introUi?.Dispose();
@@ -136,6 +137,7 @@ namespace MareSynchronos
_playerManager?.Dispose(); _playerManager?.Dispose();
_characterCacheManager?.Dispose(); _characterCacheManager?.Dispose();
_transientResourceManager?.Dispose(); _transientResourceManager?.Dispose();
_dalamudUtil.Dispose();
Logger.Debug("Shut down"); Logger.Debug("Shut down");
} }

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Threading; using System.Threading;
using Dalamud.Game; using Dalamud.Game;
using Dalamud.Game.ClientState; using Dalamud.Game.ClientState;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.ClientState.Objects; using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.ClientState.Objects.SubKinds; using Dalamud.Game.ClientState.Objects.SubKinds;
using FFXIVClientStructs.FFXIV.Client.Game.Character; using FFXIVClientStructs.FFXIV.Client.Game.Character;
@@ -24,6 +25,8 @@ namespace MareSynchronos.Utils
private readonly ClientState _clientState; private readonly ClientState _clientState;
private readonly ObjectTable _objectTable; private readonly ObjectTable _objectTable;
private readonly Framework _framework; private readonly Framework _framework;
private readonly Condition _condition;
public event LogIn? LogIn; public event LogIn? LogIn;
public event LogOut? LogOut; public event LogOut? LogOut;
public event FrameworkUpdate? FrameworkUpdate; public event FrameworkUpdate? FrameworkUpdate;
@@ -45,11 +48,12 @@ namespace MareSynchronos.Utils
return false; return false;
} }
public DalamudUtil(ClientState clientState, ObjectTable objectTable, Framework framework) public DalamudUtil(ClientState clientState, ObjectTable objectTable, Framework framework, Condition condition)
{ {
_clientState = clientState; _clientState = clientState;
_objectTable = objectTable; _objectTable = objectTable;
_framework = framework; _framework = framework;
_condition = condition;
_clientState.Login += ClientStateOnLogin; _clientState.Login += ClientStateOnLogin;
_clientState.Logout += ClientStateOnLogout; _clientState.Logout += ClientStateOnLogout;
_framework.Update += FrameworkOnUpdate; _framework.Update += FrameworkOnUpdate;
@@ -62,11 +66,16 @@ namespace MareSynchronos.Utils
private void FrameworkOnUpdate(Framework framework) private void FrameworkOnUpdate(Framework framework)
{ {
foreach (FrameworkUpdate frameworkInvocation in (FrameworkUpdate?.GetInvocationList() ?? Array.Empty<FrameworkUpdate>()).Cast<FrameworkUpdate>()) if (_condition[ConditionFlag.BetweenAreas] || _condition[ConditionFlag.BetweenAreas51])
{
return;
}
foreach (FrameworkUpdate? frameworkInvocation in (FrameworkUpdate?.GetInvocationList() ?? Array.Empty<FrameworkUpdate>()).Cast<FrameworkUpdate>())
{ {
try try
{ {
frameworkInvocation.Invoke(); frameworkInvocation?.Invoke();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -75,15 +84,23 @@ namespace MareSynchronos.Utils
} }
} }
classJobId = _clientState.LocalPlayer.ClassJob.Id; if (DateTime.Now < _delayedFrameworkUpdateCheck.AddSeconds(1)) return;
ClassJobChanged?.Invoke(); if (_clientState.LocalPlayer != null && _clientState.LocalPlayer.IsValid())
{
var newclassJobId = _clientState.LocalPlayer.ClassJob.Id;
if (DateTime.Now < _delayedFrameworkUpdateCheck.AddSeconds(0.25)) return; if (classJobId != newclassJobId)
foreach (FrameworkUpdate frameworkInvocation in (DelayedFrameworkUpdate?.GetInvocationList() ?? Array.Empty<FrameworkUpdate>()).Cast<FrameworkUpdate>()) {
classJobId = newclassJobId;
ClassJobChanged?.Invoke();
}
}
foreach (FrameworkUpdate? frameworkInvocation in (DelayedFrameworkUpdate?.GetInvocationList() ?? Array.Empty<FrameworkUpdate>()).Cast<FrameworkUpdate>())
{ {
try try
{ {
frameworkInvocation.Invoke(); frameworkInvocation?.Invoke();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -1,11 +1,37 @@
using System.Diagnostics; using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using Dalamud.Logging; using Dalamud.Logging;
using Dalamud.Utility; using Dalamud.Utility;
using Microsoft.Extensions.Logging;
namespace MareSynchronos.Utils namespace MareSynchronos.Utils
{ {
internal class Logger [ProviderAlias("Dalamud")]
public class DalamudLoggingProvider : ILoggerProvider
{ {
private readonly ConcurrentDictionary<string, Logger> _loggers =
new(StringComparer.OrdinalIgnoreCase);
public DalamudLoggingProvider()
{
}
public ILogger CreateLogger(string categoryName)
{
return _loggers.GetOrAdd(categoryName, name => new Logger(categoryName));
}
public void Dispose()
{
_loggers.Clear();
}
}
internal class Logger : ILogger
{
private readonly string name;
public static void Info(string info) public static void Info(string info)
{ {
var caller = new StackTrace().GetFrame(1)?.GetMethod()?.ReflectedType?.Name ?? "Unknown"; var caller = new StackTrace().GetFrame(1)?.GetMethod()?.ReflectedType?.Name ?? "Unknown";
@@ -40,5 +66,47 @@ namespace MareSynchronos.Utils
PluginLog.Verbose($"[{caller}] {verbose}"); PluginLog.Verbose($"[{caller}] {verbose}");
#endif #endif
} }
public Logger(string name)
{
this.name = name;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel)) return;
switch (logLevel)
{
case LogLevel.Debug:
PluginLog.Debug($"[{name}] [{eventId}] {formatter(state, exception)}");
break;
case LogLevel.Error:
case LogLevel.Critical:
PluginLog.Error($"[{name}] [{eventId}] {formatter(state, exception)}");
break;
case LogLevel.Information:
PluginLog.Information($"[{name}] [{eventId}] {formatter(state, exception)}");
break;
case LogLevel.Warning:
PluginLog.Warning($"[{name}] [{eventId}] {formatter(state, exception)}");
break;
case LogLevel.Trace:
default:
#if DEBUG
PluginLog.Debug($"[{name}] [{eventId}] {formatter(state, exception)}");
#else
PluginLog.Verbose($"[{name}] {eventId} {state} {formatter(state, exception)}");
#endif
break;
}
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public IDisposable BeginScope<TState>(TState state) => default!;
} }
} }

View File

@@ -11,6 +11,7 @@ using MareSynchronos.WebAPI.Utils;
using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client; using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
namespace MareSynchronos.WebAPI namespace MareSynchronos.WebAPI
{ {
@@ -304,6 +305,10 @@ namespace MareSynchronos.WebAPI
options.Transports = HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling; options.Transports = HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling;
}) })
.WithAutomaticReconnect(new ForeverRetryPolicy()) .WithAutomaticReconnect(new ForeverRetryPolicy())
.ConfigureLogging(a => {
a.ClearProviders().AddProvider(new DalamudLoggingProvider());
a.SetMinimumLevel(LogLevel.Trace);
})
.Build(); .Build();
} }