TheWatcher/AutoShutdown/Plogon.cs

74 lines
2.6 KiB
C#
Raw Normal View History

2024-03-08 23:34:40 +00:00
using System;
2024-03-10 15:38:09 +00:00
using System.Diagnostics;
2024-03-08 23:34:40 +00:00
using System.Reflection;
2024-03-10 15:38:09 +00:00
using System.Windows.Forms;
2024-03-08 23:34:40 +00:00
using AutoRetainerAPI;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using ECommons;
2024-03-10 15:38:09 +00:00
using FFXIVClientStructs.FFXIV.Client.System.Framework;
2024-03-08 23:34:40 +00:00
using LLib;
namespace AutoShutdown;
public class Plogon : IDalamudPlugin
{
private readonly AutoRetainerApi _autoRetainerApi;
private readonly DalamudReflector _reflector;
private readonly IPluginLog _pluginLog;
private readonly IClientState _clientState;
2024-03-10 15:38:09 +00:00
public Plogon(DalamudPluginInterface pluginInterface, IFramework framework, IPluginLog pluginLog,
IClientState clientState)
2024-03-08 23:34:40 +00:00
{
ECommonsMain.Init(pluginInterface, this);
_autoRetainerApi = new AutoRetainerApi();
_reflector = new DalamudReflector(pluginInterface, framework, pluginLog);
_pluginLog = pluginLog;
_clientState = clientState;
2024-03-10 15:38:09 +00:00
_clientState.Logout += SetupShutdown;
2024-03-08 23:34:40 +00:00
2024-03-10 15:38:09 +00:00
framework.RunOnTick(() => SetupShutdown(), TimeSpan.FromSeconds(10));
2024-03-08 23:34:40 +00:00
}
private long StartedAt { get; } = Environment.TickCount64;
2024-03-10 15:38:09 +00:00
private long ShutdownAt => StartedAt + (long)TimeSpan.FromDays(2).TotalMilliseconds;
2024-03-08 23:34:40 +00:00
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)
{
2024-03-10 15:38:09 +00:00
_pluginLog.Information(
$"Setting shutdown date to {new DateTime(TimeSpan.FromMilliseconds(ShutdownAt).Ticks)}");
2024-03-08 23:34:40 +00:00
shutdownAt.SetValue(null, ShutdownAt);
2024-03-10 15:38:09 +00:00
forceShutdownAt.SetValue(null, ShutdownAt + (long)TimeSpan.FromMinutes(10).TotalMilliseconds);
2024-03-08 23:34:40 +00:00
}
else
{
2024-03-10 15:38:09 +00:00
_pluginLog.Information(
$"Shutdown is already set to {new DateTime(TimeSpan.FromMilliseconds(currentShutdownAt).Ticks)}");
2024-03-08 23:34:40 +00:00
}
}
2024-03-10 15:38:09 +00:00
// move to background using alt+esc
SendKeys.SendWait("%({esc})");
2024-03-08 23:34:40 +00:00
}
public void Dispose()
{
2024-03-10 15:38:09 +00:00
_clientState.Logout -= SetupShutdown;
2024-03-08 23:34:40 +00:00
_reflector.Dispose();
_autoRetainerApi.Dispose();
ECommonsMain.Dispose();
}
}