TheWatcher/AutoShutdown/Plogon.cs

65 lines
2.2 KiB
C#

using System;
using System.Reflection;
using AutoRetainerAPI;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using ECommons;
using LLib;
namespace AutoShutdown;
public class Plogon : IDalamudPlugin
{
private readonly AutoRetainerApi _autoRetainerApi;
private readonly DalamudReflector _reflector;
private readonly IPluginLog _pluginLog;
private readonly IClientState _clientState;
public Plogon(DalamudPluginInterface pluginInterface, IFramework framework, IPluginLog pluginLog, IClientState clientState)
{
ECommonsMain.Init(pluginInterface, this);
_autoRetainerApi = new AutoRetainerApi();
_reflector = new DalamudReflector(pluginInterface, framework, pluginLog);
_pluginLog = pluginLog;
_clientState = clientState;
_clientState.Login += SetupShutdown;
framework.RunOnTick(SetupShutdown, TimeSpan.FromSeconds(5));
}
private long StartedAt { get; } = Environment.TickCount64;
private long ShutdownAt => StartedAt + TimeSpan.FromDays(2).Ticks;
private void SetupShutdown()
{
if (_autoRetainerApi.Ready && _reflector.TryGetDalamudPlugin("AutoRetainer", out var autoRetainer, false, true))
{
var shutdown = autoRetainer.GetType().Assembly.GetType("AutoRetainer.Modules.Shutdown")!;
var shutdownAt = shutdown.GetField("ShutdownAt", BindingFlags.Static | BindingFlags.NonPublic)!;
var forceShutdownAt = shutdown.GetField("ForceShutdownAt", BindingFlags.Static | BindingFlags.NonPublic)!;
var currentShutdownAt = (long?)shutdownAt.GetValue(null) ?? 0;
if (currentShutdownAt == 0)
{
_pluginLog.Information($"Setting shutdown date to {new DateTime(ShutdownAt)}");
shutdownAt.SetValue(null, ShutdownAt);
forceShutdownAt.SetValue(null, ShutdownAt + TimeSpan.FromMinutes(10).Ticks);
}
else
{
_pluginLog.Information($"Shutdown is already set to {new DateTime(currentShutdownAt)}");
}
}
}
public void Dispose()
{
_clientState.Login -= SetupShutdown;
_reflector.Dispose();
_autoRetainerApi.Dispose();
ECommonsMain.Dispose();
}
}