PalacePal/Pal.Client/Rendering/SplatoonRenderer.cs

183 lines
6.4 KiB
C#
Raw Normal View History

2023-02-08 15:06:43 +00:00
using Dalamud.Logging;
using Dalamud.Plugin;
using ECommons;
using ECommons.Reflection;
using ECommons.Schedulers;
using ECommons.SplatoonAPI;
using ImGuiNET;
using System;
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;
using Dalamud.Game.Gui;
using Pal.Client.DependencyInjection;
2023-02-16 09:46:19 +00:00
using Pal.Client.Extensions;
2023-02-08 15:06:43 +00:00
namespace Pal.Client.Rendering
{
2023-02-15 22:17:19 +00:00
internal sealed class SplatoonRenderer : IRenderer, IDrawDebugItems, 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
2023-02-15 22:17:19 +00:00
private readonly DebugState _debugState;
private readonly ClientState _clientState;
private readonly ChatGui _chatGui;
2023-02-16 09:46:19 +00:00
public SplatoonRenderer(DalamudPluginInterface pluginInterface, IDalamudPlugin dalamudPlugin,
DebugState debugState,
2023-02-15 22:17:19 +00:00
ClientState clientState, ChatGui chatGui)
2023-02-08 15:06:43 +00:00
{
2023-02-15 22:17:19 +00:00
_debugState = debugState;
_clientState = clientState;
_chatGui = chatGui;
PluginLog.Information("Initializing splatoon...");
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)
{
PluginLog.Error(e, $"Could not create splatoon layer {layer} with {elements.Count} elements");
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)
{
PluginLog.Error(e, $"Could not reset splatoon layer {layer}");
}
}
private string ToLayerName(ELayer layer)
=> $"PalacePal.{layer}";
public IRenderElement CreateElement(Marker.EType type, Vector3 pos, uint color, bool fill = false)
{
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(Vector4 trapColor, Vector4 hoardColor)
{
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)
{
var elements = new List<IRenderElement>
{
CreateElement(Marker.EType.Trap, pos.Value, ImGui.ColorConvertFloat4ToU32(trapColor)),
CreateElement(Marker.EType.Hoard, pos.Value, ImGui.ColorConvertFloat4ToU32(hoardColor)),
};
2023-02-15 22:17:19 +00:00
if (!Splatoon.AddDynamicElements("PalacePal.Test",
elements.Cast<SplatoonElement>().Select(x => x.Delegate).ToArray(),
new[] { Environment.TickCount64 + 10000 }))
2023-02-08 15:06:43 +00:00
{
2023-02-16 09:46:19 +00:00
_chatGui.PalMessage("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")
{
2023-02-16 09:46:19 +00:00
_chatGui.PalError(
$"Splatoon is installed under the plugin name '{pluginName}', which is incompatible with the Splatoon API.");
_chatGui.PalMessage(
"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
2023-02-16 09:46:19 +00:00
_chatGui.PalError("Could not draw markers, is Splatoon installed and enabled?");
2023-02-08 15:06:43 +00:00
}
}
public void Dispose()
{
IsDisposed = true;
2023-02-08 15:06:43 +00:00
ResetLayer(ELayer.TrapHoard);
ResetLayer(ELayer.RegularCoffers);
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;
}
}
}
}