PalacePal/Pal.Client/Rendering/SplatoonRenderer.cs

197 lines
6.8 KiB
C#
Raw Normal View History

2023-03-26 13:47:18 +00:00
using System;
2023-02-08 15:06:43 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
2023-02-15 22:17:19 +00:00
using Dalamud.Game.ClientState;
2023-03-26 13:47:18 +00:00
using Dalamud.Plugin;
using ECommons;
using ECommons.Reflection;
using ECommons.Schedulers;
using ECommons.SplatoonAPI;
using Microsoft.Extensions.Logging;
using Pal.Client.Configuration;
2023-02-15 22:17:19 +00:00
using Pal.Client.DependencyInjection;
2023-02-18 20:12:36 +00:00
using Pal.Client.Floors;
2023-02-08 15:06:43 +00:00
namespace Pal.Client.Rendering
{
internal sealed class SplatoonRenderer : IRenderer, IDisposable
2023-02-08 15:06:43 +00:00
{
2023-02-11 20:10:45 +00:00
private const long OnTerritoryChange = -2;
2023-02-08 15:06:43 +00:00
private readonly ILogger<SplatoonRenderer> _logger;
2023-02-15 22:17:19 +00:00
private readonly DebugState _debugState;
private readonly ClientState _clientState;
private readonly Chat _chat;
2023-02-15 22:17:19 +00:00
public SplatoonRenderer(
ILogger<SplatoonRenderer> logger,
DalamudPluginInterface pluginInterface,
IDalamudPlugin dalamudPlugin,
2023-02-16 09:46:19 +00:00
DebugState debugState,
ClientState clientState,
Chat chat)
2023-02-08 15:06:43 +00:00
{
_logger = logger;
2023-02-15 22:17:19 +00:00
_debugState = debugState;
_clientState = clientState;
_chat = chat;
2023-02-15 22:17:19 +00:00
_logger.LogInformation("Initializing splatoon");
2023-02-15 22:17:19 +00:00
ECommonsMain.Init(pluginInterface, dalamudPlugin, ECommons.Module.SplatoonAPI);
2023-02-08 15:06:43 +00:00
}
2023-02-15 22:17:19 +00:00
private bool IsDisposed { get; set; }
2023-02-08 15:06:43 +00:00
public void SetLayer(ELayer layer, IReadOnlyList<IRenderElement> elements)
{
// we need to delay this, as the current framework update could be before splatoon's, in which case it would immediately delete the layout
2023-02-11 20:10:45 +00:00
_ = new TickScheduler(delegate
2023-02-08 15:06:43 +00:00
{
try
{
2023-02-15 22:17:19 +00:00
Splatoon.AddDynamicElements(ToLayerName(layer),
elements.Cast<SplatoonElement>().Select(x => x.Delegate).ToArray(),
new[] { Environment.TickCount64 + 60 * 60 * 1000, OnTerritoryChange });
2023-02-08 15:06:43 +00:00
}
catch (Exception e)
{
2023-02-18 20:12:36 +00:00
_logger.LogError(e, "Could not create splatoon layer {Layer} with {Count} elements", layer,
elements.Count);
2023-02-15 22:17:19 +00:00
_debugState.SetFromException(e);
2023-02-08 15:06:43 +00:00
}
});
}
public void ResetLayer(ELayer layer)
{
try
{
Splatoon.RemoveDynamicElements(ToLayerName(layer));
}
catch (Exception e)
{
_logger.LogError(e, "Could not reset splatoon layer {Layer}", layer);
2023-02-08 15:06:43 +00:00
}
}
private string ToLayerName(ELayer layer)
=> $"PalacePal.{layer}";
2023-02-18 20:12:36 +00:00
public IRenderElement CreateElement(MemoryLocation.EType type, Vector3 pos, uint color, bool fill = false)
2023-02-08 15:06:43 +00:00
{
MarkerConfig config = MarkerConfig.ForType(type);
Element element = new Element(ElementType.CircleAtFixedCoordinates)
{
refX = pos.X,
refY = pos.Z, // z and y are swapped
refZ = pos.Y,
offX = 0,
offY = 0,
offZ = config.OffsetY,
Filled = fill,
radius = config.Radius,
FillStep = 1,
color = color,
thicc = 2,
};
return new SplatoonElement(this, element);
2023-02-08 15:06:43 +00:00
}
public void DrawDebugItems(uint trapColor, uint hoardColor)
2023-02-08 15:06:43 +00:00
{
try
{
2023-02-15 22:17:19 +00:00
Vector3? pos = _clientState.LocalPlayer?.Position;
2023-02-08 15:06:43 +00:00
if (pos != null)
{
ResetLayer(ELayer.Test);
2023-02-08 15:06:43 +00:00
var elements = new List<IRenderElement>
{
2023-02-18 20:12:36 +00:00
CreateElement(MemoryLocation.EType.Trap, pos.Value, trapColor),
CreateElement(MemoryLocation.EType.Hoard, pos.Value, hoardColor),
2023-02-08 15:06:43 +00:00
};
if (!Splatoon.AddDynamicElements(ToLayerName(ELayer.Test),
2023-02-15 22:17:19 +00:00
elements.Cast<SplatoonElement>().Select(x => x.Delegate).ToArray(),
new[] { Environment.TickCount64 + RenderData.TestLayerTimeout }))
2023-02-08 15:06:43 +00:00
{
_chat.Message("Could not draw markers :(");
2023-02-08 15:06:43 +00:00
}
}
}
catch (Exception)
{
try
{
var pluginManager = DalamudReflector.GetPluginManager();
2023-02-15 22:17:19 +00:00
IList installedPlugins =
pluginManager.GetType().GetProperty("InstalledPlugins")?.GetValue(pluginManager) as IList ??
new List<object>();
2023-02-08 15:06:43 +00:00
foreach (var t in installedPlugins)
{
2023-02-15 22:17:19 +00:00
AssemblyName? assemblyName =
(AssemblyName?)t.GetType().GetProperty("AssemblyName")?.GetValue(t);
2023-02-08 15:06:43 +00:00
string? pluginName = (string?)t.GetType().GetProperty("Name")?.GetValue(t);
if (assemblyName?.Name == "Splatoon" && pluginName != "Splatoon")
{
_chat.Error(
2023-02-16 09:46:19 +00:00
$"Splatoon is installed under the plugin name '{pluginName}', which is incompatible with the Splatoon API.");
_chat.Message(
2023-02-16 09:46:19 +00:00
"You need to install Splatoon from the official repository at https://github.com/NightmareXIV/MyDalamudPlugins.");
2023-02-08 15:06:43 +00:00
return;
}
}
}
2023-02-15 22:17:19 +00:00
catch (Exception)
{
// not relevant
}
2023-02-08 15:06:43 +00:00
_chat.Error("Could not draw markers, is Splatoon installed and enabled?");
2023-02-08 15:06:43 +00:00
}
}
public ERenderer GetConfigValue()
=> ERenderer.Splatoon;
2023-02-08 15:06:43 +00:00
public void Dispose()
{
_logger.LogInformation("Disposing splatoon");
IsDisposed = true;
2023-02-08 15:06:43 +00:00
ResetLayer(ELayer.TrapHoard);
ResetLayer(ELayer.RegularCoffers);
ResetLayer(ELayer.Test);
2023-02-08 15:06:43 +00:00
ECommonsMain.Dispose();
}
2023-02-15 22:17:19 +00:00
private sealed class SplatoonElement : IRenderElement
2023-02-08 15:06:43 +00:00
{
2023-02-11 20:10:45 +00:00
private readonly SplatoonRenderer _renderer;
public SplatoonElement(SplatoonRenderer renderer, Element element)
2023-02-08 15:06:43 +00:00
{
2023-02-11 20:10:45 +00:00
_renderer = renderer;
2023-02-08 15:06:43 +00:00
Delegate = element;
}
public Element Delegate { get; }
2023-02-11 20:10:45 +00:00
public bool IsValid => !_renderer.IsDisposed && Delegate.IsValid();
2023-02-15 22:17:19 +00:00
2023-02-08 15:06:43 +00:00
public uint Color
{
get => Delegate.color;
set => Delegate.color = value;
}
}
}
}