Code cleanup pass

This commit is contained in:
Liza 2023-03-30 22:07:22 +02:00
parent e13cf9409c
commit 0ce4e36bbf
Signed by: liza
GPG Key ID: 7199F8D727D55F67

View File

@ -12,43 +12,44 @@ using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;
namespace SliceIsRight
namespace SliceIsRight;
// ReSharper disable once UnusedType.Global
public sealed class SliceIsRightPlugin : IDalamudPlugin
{
public sealed class SliceIsRightPlugin : IDalamudPlugin
{
private const float HALF_PI = (float)Math.PI / 2f;
private uint COLOUR_BLUE = ImGui.GetColorU32(ImGui.ColorConvertFloat4ToU32(new Vector4(0.0f, 0.0f, 1f, 0.15f)));
private uint COLOUR_GREEN = ImGui.GetColorU32(ImGui.ColorConvertFloat4ToU32(new Vector4(0.0f, 1f, 0.0f, 0.15f)));
private uint COLOUR_RED = ImGui.GetColorU32(ImGui.ColorConvertFloat4ToU32(new Vector4(1, 0, 0, 0.4f)));
private const float HalfPi = (float)Math.PI / 2f;
private static readonly uint ColourBlue = ImGui.GetColorU32(ImGui.ColorConvertFloat4ToU32(new Vector4(0.0f, 0.0f, 1f, 0.15f)));
private static readonly uint ColourGreen = ImGui.GetColorU32(ImGui.ColorConvertFloat4ToU32(new Vector4(0.0f, 1f, 0.0f, 0.15f)));
private static readonly uint ColourRed = ImGui.GetColorU32(ImGui.ColorConvertFloat4ToU32(new Vector4(1, 0, 0, 0.4f)));
public string Name => "Slice is Right";
[PluginService]
[RequiredVersion("1.0")]
private DalamudPluginInterface PluginInterface { get; set; }
private DalamudPluginInterface PluginInterface { get; set; } = null!;
[PluginService]
private ObjectTable ObjectTable { get; set; }
private ObjectTable ObjectTable { get; set; } = null!;
[PluginService]
private GameGui GameGui { get; set; }
private GameGui GameGui { get; set; } = null!;
[PluginService]
private ClientState ClientState { get; set; }
private ClientState ClientState { get; set; } = null!;
private const ushort GoldSaucerTerritoryId = 144;
private bool IsInGoldSaucer { get; set; }
private readonly IDictionary<uint, DateTime> objectsAndSpawnTime = new Dictionary<uint, DateTime>();
private readonly ISet<uint> objectsToMatch = new HashSet<uint>();
private readonly IDictionary<uint, DateTime> _objectsAndSpawnTime = new Dictionary<uint, DateTime>();
private readonly ISet<uint> _objectsToMatch = new HashSet<uint>();
private const float MaxDistance = 30f;
#pragma warning disable CS8618
public SliceIsRightPlugin()
{
PluginInterface!.UiBuilder.Draw += DrawUI;
ClientState!.TerritoryChanged += TerritoryChanged;
PluginInterface.UiBuilder.Draw += DrawUi;
ClientState.TerritoryChanged += TerritoryChanged;
IsInGoldSaucer = ClientState.TerritoryType == GoldSaucerTerritoryId;
}
#pragma warning restore CS8618
@ -60,13 +61,13 @@ namespace SliceIsRight
public void Dispose()
{
PluginInterface.UiBuilder.Draw -= DrawUI;
PluginInterface.UiBuilder.Draw -= DrawUi;
ClientState.TerritoryChanged -= TerritoryChanged;
}
private void DrawUI()
private void DrawUi()
{
if (!ClientState.IsLoggedIn || !IsInGoldSaucer || ObjectTable == null)
if (!ClientState.IsLoggedIn || !IsInGoldSaucer)
return;
for (int index = 0; index < ObjectTable.Length; ++index)
@ -78,7 +79,7 @@ namespace SliceIsRight
int model = Marshal.ReadInt32(obj.Address + 128);
if (obj.ObjectKind == ObjectKind.EventObj && (model >= 2010777 && model <= 2010779))
{
RenderObject(index, obj, model);
RenderObject(obj, model);
}
else if (ClientState.LocalPlayer?.ObjectId == obj.ObjectId)
{
@ -89,23 +90,23 @@ namespace SliceIsRight
}
}
foreach (uint objectId in objectsToMatch)
objectsAndSpawnTime.Remove(objectId);
objectsToMatch.Clear();
foreach (uint objectId in _objectsToMatch)
_objectsAndSpawnTime.Remove(objectId);
_objectsToMatch.Clear();
}
private void RenderObject(int index, GameObject obj, int model, float? radius = null)
private void RenderObject(GameObject obj, int model, float? radius = null)
{
objectsToMatch.Remove(obj.ObjectId);
_objectsToMatch.Remove(obj.ObjectId);
if (objectsAndSpawnTime.TryGetValue(obj.ObjectId, out DateTime spawnTime))
if (_objectsAndSpawnTime.TryGetValue(obj.ObjectId, out DateTime spawnTime))
{
if (spawnTime.AddSeconds(5) > DateTime.Now)
return;
}
else
{
objectsAndSpawnTime.Add(obj.ObjectId, DateTime.Now);
_objectsAndSpawnTime.Add(obj.ObjectId, DateTime.Now);
return;
}
@ -113,17 +114,17 @@ namespace SliceIsRight
switch (model)
{
case 2010777:
DrawRectWorld(obj, obj.Rotation + HALF_PI, radius ?? 25f, 5f, COLOUR_BLUE);
DrawRectWorld(obj, obj.Rotation + HalfPi, radius ?? 25f, 5f, ColourBlue);
break;
case 2010778:
DrawRectWorld(obj, obj.Rotation + HALF_PI, radius ?? 25f, 5f, COLOUR_GREEN);
DrawRectWorld(obj, obj.Rotation - HALF_PI, radius ?? 25f, 5f, COLOUR_GREEN);
DrawRectWorld(obj, obj.Rotation + HalfPi, radius ?? 25f, 5f, ColourGreen);
DrawRectWorld(obj, obj.Rotation - HalfPi, radius ?? 25f, 5f, ColourGreen);
break;
case 2010779:
//default:
DrawFilledCircleWorld(obj, radius ?? 11f, COLOUR_RED);
DrawFilledCircleWorld(obj, radius ?? 11f, ColourRed);
break;
}
}
@ -169,12 +170,12 @@ namespace SliceIsRight
private void DrawRectWorld(GameObject obj, float rotation, float length, float width, uint colour)
{
BeginRender(obj.Address.ToString() + obj.Rotation.ToString());
BeginRender($"{obj.Address}{obj.Rotation}");
var center = obj.Position;
Vector2 displaySize = ImGui.GetIO().DisplaySize;
Vector3 near1 = new Vector3(center.X + width / 2 * (float)Math.Sin(HALF_PI + rotation), center.Y, center.Z + width / 2 * (float)Math.Cos(HALF_PI + rotation));
Vector3 near2 = new Vector3(center.X + width / 2 * (float)Math.Sin(rotation - HALF_PI), center.Y, center.Z + width / 2 * (float)Math.Cos(rotation - HALF_PI));
Vector3 near1 = new Vector3(center.X + width / 2 * (float)Math.Sin(HalfPi + rotation), center.Y, center.Z + width / 2 * (float)Math.Cos(HalfPi + rotation));
Vector3 near2 = new Vector3(center.X + width / 2 * (float)Math.Sin(rotation - HalfPi), center.Y, center.Z + width / 2 * (float)Math.Cos(rotation - HalfPi));
Vector3 nearCenter = new Vector3(center.X, center.Y, center.Z);
int rectangleCount = 20;
float lengthSlice = length / rectangleCount;
@ -213,5 +214,4 @@ namespace SliceIsRight
{
return Vector3.Distance(ClientState.LocalPlayer?.Position ?? Vector3.Zero, center);
}
}
}