generated from goatcorp/SamplePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd128be
commit e32a2ac
Showing
14 changed files
with
497 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace ECommons.MathHelpers; | ||
|
||
public enum CardinalDirection | ||
{ | ||
North, | ||
South, | ||
West, | ||
East, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
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; | ||
onPluginsChangedActions = null; | ||
} | ||
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 GitHub Actions / build
Check warning on line 60 in SneakOnBy/ECommons/DalamudReflector.cs GitHub Actions / build
|
||
GetType("Dalamud.Service`1", true).MakeGenericType(Services.PluginInterface.GetType().Assembly.GetType("Dalamud.Plugin.Internal.PluginManager", true)). | ||
GetMethod("Get").Invoke(null, BindingFlags.Default, null, Array.Empty<object>(), null); | ||
} | ||
|
||
public static object GetService(string serviceFullName) | ||
{ | ||
return Services.PluginInterface.GetType().Assembly. | ||
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(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? ""}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using System; | ||
|
||
namespace ECommons.Schedulers; | ||
|
||
public interface IScheduler : IDisposable | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.