Skip to content

Commit

Permalink
update to API 9
Browse files Browse the repository at this point in the history
  • Loading branch information
McSwindler committed Nov 2, 2023
1 parent dd128be commit e32a2ac
Show file tree
Hide file tree
Showing 14 changed files with 497 additions and 30 deletions.
2 changes: 0 additions & 2 deletions SneakOnBy/DeepDungeonDex.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Dalamud.Logging;
using Dalamud.Plugin;
using ECommons.DalamudServices;
using ECommons.ExcelServices;
using ECommons.Reflection;
using FFXIVClientStructs.FFXIV.Component.GUI;
using System;
Expand Down
9 changes: 9 additions & 0 deletions SneakOnBy/ECommons/CardinalDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ECommons.MathHelpers;

public enum CardinalDirection
{
North,
South,
West,
East,
}
168 changes: 168 additions & 0 deletions SneakOnBy/ECommons/DalamudReflector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using Dalamud;
using Dalamud.Game.ClientState.Keys;
using Dalamud.Plugin;
using ECommons.Schedulers;
using SneakOnBy;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace ECommons.Reflection;

public static class DalamudReflector
{
delegate ref int GetRefValue(int vkCode);
static GetRefValue getRefValue;
static Dictionary<string, IDalamudPlugin> pluginCache;
static List<Action> onPluginsChangedActions;

internal static void Init()
{
onPluginsChangedActions = new();
pluginCache = new();
GenericHelpers.Safe(delegate
{
getRefValue = (GetRefValue)Delegate.CreateDelegate(typeof(GetRefValue), Services.KeyState,
Services.KeyState.GetType().GetMethod("GetRefValue",

Check warning on line 26 in SneakOnBy/ECommons/DalamudReflector.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'method' in 'Delegate Delegate.CreateDelegate(Type type, object? firstArgument, MethodInfo method)'.
BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] { typeof(int) }, null));
}, (e) => {
Services.PluginLog.Error(e);
});
Services.PluginInterface.ActivePluginsChanged += OnInstalledPluginsChanged;
}

internal static void Dispose()
{
if (pluginCache != null)
{
pluginCache = null;

Check warning on line 39 in SneakOnBy/ECommons/DalamudReflector.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
onPluginsChangedActions = null;

Check warning on line 40 in SneakOnBy/ECommons/DalamudReflector.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
}
Services.PluginInterface.ActivePluginsChanged -= OnInstalledPluginsChanged;
}

public static void RegisterOnInstalledPluginsChangedEvents(params Action[] actions)
{
foreach (var x in actions)
{
onPluginsChangedActions.Add(x);
}
}

public static void SetKeyState(VirtualKey key, int state)
{
getRefValue((int)key) = state;
}

public static object GetPluginManager()
{
return Services.PluginInterface.GetType().Assembly.

Check warning on line 60 in SneakOnBy/ECommons/DalamudReflector.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 60 in SneakOnBy/ECommons/DalamudReflector.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 60 in SneakOnBy/ECommons/DalamudReflector.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
GetType("Dalamud.Service`1", true).MakeGenericType(Services.PluginInterface.GetType().Assembly.GetType("Dalamud.Plugin.Internal.PluginManager", true)).

Check warning on line 61 in SneakOnBy/ECommons/DalamudReflector.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'typeArguments' in 'Type Type.MakeGenericType(params Type[] typeArguments)'.
GetMethod("Get").Invoke(null, BindingFlags.Default, null, Array.Empty<object>(), null);
}

public static object GetService(string serviceFullName)
{
return Services.PluginInterface.GetType().Assembly.

Check warning on line 67 in SneakOnBy/ECommons/DalamudReflector.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
GetType("Dalamud.Service`1", true).MakeGenericType(Services.PluginInterface.GetType().Assembly.GetType(serviceFullName, true)).
GetMethod("Get").Invoke(null, BindingFlags.Default, null, Array.Empty<object>(), null);
}

public static bool TryGetLocalPlugin(out object localPlugin, out Type type)
{
try
{
var pluginManager = GetPluginManager();
var installedPlugins = (System.Collections.IList)pluginManager.GetType().GetProperty("InstalledPlugins").GetValue(pluginManager);

foreach (var t in installedPlugins)
{
if (t != null)
{
type = t.GetType().Name == "LocalDevPlugin" ? t.GetType().BaseType : t.GetType();
if (object.ReferenceEquals(type.GetField("instance", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(t), Plugin.Instance))
{
localPlugin = t;
return true;
}
}
}
localPlugin = type = null;
return false;
}
catch (Exception e)
{
Services.PluginLog.Error(e.StackTrace);
localPlugin = type = null;
return false;
}
}

public static bool TryGetDalamudPlugin(string internalName, out IDalamudPlugin instance, bool suppressErrors = false, bool ignoreCache = false)
{
if (pluginCache == null)
{
throw new Exception("PluginCache is null. Have you initialised the DalamudReflector module on ECommons initialisation?");
}

if (!ignoreCache && pluginCache.TryGetValue(internalName, out instance) && instance != null)
{
return true;
}
try
{
var pluginManager = GetPluginManager();
var installedPlugins = (System.Collections.IList)pluginManager.GetType().GetProperty("InstalledPlugins").GetValue(pluginManager);

foreach (var t in installedPlugins)
{
if ((string)t.GetType().GetProperty("Name").GetValue(t) == internalName)
{
var type = t.GetType().Name == "LocalDevPlugin" ? t.GetType().BaseType : t.GetType();
var plugin = (IDalamudPlugin)type.GetField("instance", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(t);
if (plugin == null)
{
Services.PluginLog.Warning($"Found requested plugin {internalName} but it was null");
}
else
{
instance = plugin;
pluginCache[internalName] = plugin;
return true;
}
}
}
instance = null;
return false;
}
catch (Exception e)
{
if (!suppressErrors)
{
Services.PluginLog.Error($"Can't find {internalName} plugin: " + e.Message);
Services.PluginLog.Error(e.StackTrace);
}
instance = null;
return false;
}
}

public static string GetPluginName()
{
return Services.PluginInterface?.InternalName ?? "Not initialized";
}

internal static void OnInstalledPluginsChanged(PluginListInvalidationKind kind, bool affectedThisPlugin)
{
Services.PluginLog.Verbose("Installed plugins changed event fired");
_ = new TickScheduler(delegate
{
pluginCache.Clear();
foreach (var x in onPluginsChangedActions)
{
x();
}
});
}
}
28 changes: 28 additions & 0 deletions SneakOnBy/ECommons/GenericHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace ECommons;

public static unsafe class GenericHelpers
{
public static void Safe(System.Action a, Action<string> fail, bool suppressErrors = false)
{
try
{
a();
}
catch (Exception e)
{
try
{
fail(e.Message);
}
catch(Exception ex)
{
Services.PluginLog.Error("Error while trying to process error handler:");
Services.PluginLog.Error($"{ex.Message}\n{ex.StackTrace ?? ""}");
suppressErrors = false;
}
if (!suppressErrors) Services.PluginLog.Error($"{e.Message}\n{e.StackTrace ?? ""}");
}
}
}
7 changes: 7 additions & 0 deletions SneakOnBy/ECommons/IScheduler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;

namespace ECommons.Schedulers;

public interface IScheduler : IDisposable
{
}
132 changes: 132 additions & 0 deletions SneakOnBy/ECommons/MathHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Numerics;

namespace ECommons.MathHelpers;

public static class MathHelper
{
public static Vector3 RotateWorldPoint(Vector3 origin, float angle, Vector3 p)
{
if (angle == 0f) return p;
var s = (float)Math.Sin(angle);
var c = (float)Math.Cos(angle);

// translate point back to origin:
p.X -= origin.X;
p.Z -= origin.Z;

// rotate point
float xnew = p.X * c - p.Z * s;
float ynew = p.X * s + p.Z * c;

// translate point back:
p.X = xnew + origin.X;
p.Z = ynew + origin.Z;
return p;
}

public static float Float(this int i)
{
return (float)i;
}

public static Vector2 ToVector2(this Vector3 vector3)
{
return new Vector2(vector3.X, vector3.Z);
}

public static Vector3 ToVector3(this Vector2 vector2)
{
return vector2.ToVector3(Services.ClientState.LocalPlayer?.Position.Y ?? 0);
}

public static Vector3 ToVector3(this Vector2 vector2, float Y)
{
return new Vector3(vector2.X, Y, vector2.Y);
}

public static float GetRelativeAngle(Vector3 origin, Vector3 target)
{
return GetRelativeAngle(origin.ToVector2(), target.ToVector2());
}

public static float GetRelativeAngle(Vector2 origin, Vector2 target)
{
var vector2 = target - origin;
var vector1 = new Vector2(0, 1);
return ((MathF.Atan2(vector2.Y, vector2.X) - MathF.Atan2(vector1.Y, vector1.X)) * (180 / MathF.PI) + 360 + 180) % 360;
}

public static float RadToDeg(this float f)
{
return (f * (180 / MathF.PI) + 360) % 360;
}

public static CardinalDirection GetCardinalDirection(Vector3 origin, Vector3 target)
{
return GetCardinalDirection(GetRelativeAngle(origin, target));
}

public static CardinalDirection GetCardinalDirection(Vector2 origin, Vector2 target)
{
return GetCardinalDirection(GetRelativeAngle(origin, target));
}

public static CardinalDirection GetCardinalDirection(float angle)
{
if (angle.InRange(45, 135)) return CardinalDirection.East;
if (angle.InRange(135, 225)) return CardinalDirection.South;
if (angle.InRange(225, 315)) return CardinalDirection.West;
return CardinalDirection.North;
}

public static bool InRange(this double f, double inclusiveStart, double exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}

public static bool InRange(this float f, float inclusiveStart, float exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}

public static bool InRange(this long f, long inclusiveStart, long exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}

public static bool InRange(this int f, int inclusiveStart, int exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}

public static bool InRange(this short f, short inclusiveStart, short exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}

public static bool InRange(this byte f, byte inclusiveStart, byte exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}

public static bool InRange(this ulong f, ulong inclusiveStart, ulong exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}

public static bool InRange(this uint f, uint inclusiveStart, uint exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}

public static bool InRange(this ushort f, ushort inclusiveStart, ushort exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}

public static bool InRange(this sbyte f, sbyte inclusiveStart, sbyte exclusiveEnd)
{
return f >= inclusiveStart && f < exclusiveEnd;
}
}
Loading

0 comments on commit e32a2ac

Please sign in to comment.