TheWatcher/AutoShutdown/Plogon.cs
2024-03-12 13:03:47 +01:00

77 lines
2.7 KiB
C#

using System;
using System.Reflection;
using System.Windows.Forms;
using AutoRetainerAPI;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using ECommons;
using FFXIVClientStructs.FFXIV.Client.System.Framework;
using LLib;
namespace AutoShutdown;
public sealed 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.Logout += SetupShutdown;
framework.RunOnTick(() => SetupShutdown(), TimeSpan.FromSeconds(10));
}
private long StartedAt { get; } = Environment.TickCount64;
private long ShutdownAt => StartedAt + (long)TimeSpan.FromDays(2).TotalMilliseconds;
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(TimeSpan.FromMilliseconds(ShutdownAt).Ticks)}");
shutdownAt.SetValue(null, ShutdownAt);
forceShutdownAt.SetValue(null, ShutdownAt + (long)TimeSpan.FromMinutes(10).TotalMilliseconds);
}
else
{
_pluginLog.Information(
$"Shutdown is already set to {new DateTime(TimeSpan.FromMilliseconds(currentShutdownAt).Ticks)}");
}
}
// move to background using alt+esc
unsafe
{
if (!Framework.Instance()->WindowInactive)
SendKeys.SendWait("%({esc})");
}
}
public void Dispose()
{
_clientState.Logout -= SetupShutdown;
_reflector.Dispose();
_autoRetainerApi.Dispose();
ECommonsMain.Dispose();
}
}