Tourist/Tourist/GameFunctions.cs

95 lines
3.4 KiB
C#
Raw Permalink Normal View History

2021-03-21 05:48:48 +00:00
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using Dalamud.Hooking;
namespace Tourist {
public class GameFunctions : IDisposable {
2023-04-05 04:19:41 +00:00
private delegate nint VistaUnlockedDelegate(ushort index, int a2, int a3);
2021-03-21 05:48:48 +00:00
2023-04-05 04:19:41 +00:00
private delegate nint CreateVfxDelegate(string name);
2021-03-21 05:48:48 +00:00
2023-04-05 04:19:41 +00:00
private delegate nint PlayVfxDelegate(nint vfx, float idk, int idk2);
2021-03-21 05:48:48 +00:00
2023-04-05 04:19:41 +00:00
internal delegate nint RemoveVfxDelegate(nint vfx);
2021-03-21 05:48:48 +00:00
private Plugin Plugin { get; }
2023-04-05 04:19:41 +00:00
private nint SightseeingMaskPointer { get; }
2021-03-21 05:48:48 +00:00
private CreateVfxDelegate CreateVfx { get; }
private PlayVfxDelegate PlayVfxInternal { get; }
internal RemoveVfxDelegate RemoveVfx { get; }
private Hook<VistaUnlockedDelegate> VistaUnlockedHook { get; }
public GameFunctions(Plugin plugin) {
this.Plugin = plugin;
var vistaUnlockedPtr = this.Plugin.SigScanner.ScanText("E8 ?? ?? ?? ?? E9 ?? ?? ?? ?? 8B CE E8 ?? ?? ?? ?? E9 ?? ?? ?? ?? 44 8B 7C 24");
2023-09-28 06:05:06 +00:00
this.VistaUnlockedHook = this.Plugin.GameInteropProvider.HookFromAddress<VistaUnlockedDelegate>(vistaUnlockedPtr, this.OnVistaUnlock);
2021-03-21 05:48:48 +00:00
this.VistaUnlockedHook.Enable();
2023-10-03 07:57:30 +00:00
var maskPtr = this.Plugin.SigScanner.GetStaticAddressFromSig("8B EA 48 8D 0D ?? ?? ?? ?? 8B D3");
2021-03-21 05:48:48 +00:00
this.SightseeingMaskPointer = maskPtr;
2021-08-23 06:36:41 +00:00
var createVfxPtr = this.Plugin.SigScanner.ScanText("E8 ?? ?? ?? ?? F3 0F 10 35 ?? ?? ?? ?? 48 89 43 08");
2021-03-21 05:48:48 +00:00
this.CreateVfx = Marshal.GetDelegateForFunctionPointer<CreateVfxDelegate>(createVfxPtr);
2021-08-23 06:36:41 +00:00
var playVfxPtr = this.Plugin.SigScanner.ScanText("E8 ?? ?? ?? ?? 8B 4B 7C 85 C9");
2021-03-21 05:48:48 +00:00
this.PlayVfxInternal = Marshal.GetDelegateForFunctionPointer<PlayVfxDelegate>(playVfxPtr);
2021-08-23 06:36:41 +00:00
var removeVfxPtr = this.Plugin.SigScanner.ScanText("40 53 48 83 EC 20 48 8B D9 48 8B 89 ?? ?? ?? ?? 48 85 C9 74 28 33 D2");
2021-03-21 05:48:48 +00:00
this.RemoveVfx = Marshal.GetDelegateForFunctionPointer<RemoveVfxDelegate>(removeVfxPtr);
}
public void Dispose() {
this.VistaUnlockedHook.Dispose();
}
2023-04-05 04:19:41 +00:00
private nint OnVistaUnlock(ushort index, int a2, int a3) {
2021-04-16 12:19:35 +00:00
try {
this.Plugin.Markers.RemoveVfx(index);
} catch (Exception ex) {
2023-09-28 06:05:06 +00:00
Plugin.Log.Error(ex, "Exception in vista unlock");
2021-04-16 12:19:35 +00:00
}
2021-03-21 05:48:48 +00:00
return this.VistaUnlockedHook.Original(index, a2, a3);
}
2023-04-05 04:19:41 +00:00
public unsafe nint SpawnVfx(string name, Vector3 position) {
2021-03-21 05:48:48 +00:00
var vfx = this.CreateVfx(name);
var pos = (float*) (vfx + 80);
*pos = position.X;
*(pos + 1) = position.Z;
*(pos + 2) = position.Y;
pos = (float*) (vfx + 640);
*pos = 0;
*(pos + 1) = 0;
*(pos + 2) = 0;
*(long*) (vfx + 56) |= 2;
*(int*) (vfx + 652) = 0;
return vfx;
}
2023-04-05 04:19:41 +00:00
internal bool PlayVfx(nint vfx) {
return this.PlayVfxInternal(vfx, 0.0f, -1) == nint.Zero;
2021-03-21 05:48:48 +00:00
}
public bool HasVistaUnlocked(short index) {
2023-04-05 04:19:41 +00:00
if (this.SightseeingMaskPointer == nint.Zero) {
2021-03-21 05:48:48 +00:00
return false;
}
var byteToCheck = index >> 3;
var bitToCheck = 1 << (index - 8 * byteToCheck);
var maskPart = Marshal.ReadByte(this.SightseeingMaskPointer + byteToCheck);
return (maskPart & bitToCheck) != 0;
}
}
}