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