Globetrotter/Globetrotter/TreasureMaps.cs

183 lines
6.0 KiB
C#
Raw Normal View History

2020-08-01 07:43:23 +00:00
using Dalamud.Game.Chat.SeStringHandling.Payloads;
using Dalamud.Hooking;
2020-08-01 07:43:23 +00:00
using Dalamud.Plugin;
using Lumina.Excel.GeneratedSheets;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Globetrotter {
2021-03-19 20:06:45 +00:00
internal sealed class TreasureMaps : IDisposable {
private const uint TreasureMapsCode = 0x54;
2020-08-12 04:14:59 +00:00
private static Dictionary<uint, uint> _mapToRow;
2021-03-19 20:06:45 +00:00
private Dictionary<uint, uint> MapToRow {
get {
if (_mapToRow != null) {
return _mapToRow;
}
2021-03-19 20:06:45 +00:00
var mapToRow = new Dictionary<uint, uint>();
2021-03-19 20:06:45 +00:00
foreach (var rank in this.Interface.Data.GetExcelSheet<TreasureHuntRank>()) {
var unopened = rank.ItemName.Value;
2020-08-02 02:38:47 +00:00
if (unopened == null) {
continue;
}
2020-12-09 03:26:14 +00:00
EventItem opened;
// FIXME: remove this try/catch when lumina is fixed
try {
opened = rank.KeyItemName.Value;
} catch (NullReferenceException) {
opened = null;
}
2021-03-19 20:06:45 +00:00
2020-08-02 02:38:47 +00:00
if (opened == null) {
continue;
}
mapToRow[opened.RowId] = unopened.AdditionalData;
}
_mapToRow = mapToRow;
return _mapToRow;
}
}
2020-08-01 07:43:23 +00:00
2021-03-19 20:06:45 +00:00
private DalamudPluginInterface Interface { get; }
private Configuration Config { get; }
private TreasureMapPacket _lastMap;
private delegate char HandleActorControlSelfDelegate(long a1, long a2, IntPtr dataPtr);
2021-03-19 20:06:45 +00:00
private readonly Hook<HandleActorControlSelfDelegate> _acsHook;
2020-08-01 07:43:23 +00:00
public TreasureMaps(DalamudPluginInterface pi, Configuration config) {
2021-03-19 20:06:45 +00:00
this.Interface = pi ?? throw new ArgumentNullException(nameof(pi), "DalamudPluginInterface cannot be null");
this.Config = config ?? throw new ArgumentNullException(nameof(config), "Configuration cannot be null");
2021-03-19 20:06:45 +00:00
var delegatePtr = this.Interface.TargetModuleScanner.ScanText("48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 30 48 8B D9 49 8B F8 41 0F B7 08");
if (delegatePtr == IntPtr.Zero) {
PluginLog.Log("Unable to detect treasure maps because could not find ACS handler delegate");
return;
}
2021-03-19 20:06:45 +00:00
this._acsHook = new Hook<HandleActorControlSelfDelegate>(delegatePtr, new HandleActorControlSelfDelegate(this.OnACS));
this._acsHook.Enable();
2020-08-01 07:43:23 +00:00
}
public void OnHover(object sender, ulong id) {
2021-03-19 20:06:45 +00:00
if (!this.Config.ShowOnHover || this._lastMap == null || this._lastMap.EventItemId != id) {
2020-08-01 07:43:23 +00:00
return;
}
this.OpenMapLocation();
}
private char OnACS(long a1, long a2, IntPtr dataPtr) {
2021-03-19 20:06:45 +00:00
var packet = ParsePacket(dataPtr);
2020-08-01 07:43:23 +00:00
if (packet == null) {
2021-03-19 20:06:45 +00:00
return this._acsHook.Original(a1, a2, dataPtr);
2020-08-01 07:43:23 +00:00
}
2021-03-19 20:06:45 +00:00
this._lastMap = packet;
2020-08-01 07:43:23 +00:00
2021-03-19 20:06:45 +00:00
if (this.Config.ShowOnOpen && packet.JustOpened) {
2020-08-01 07:43:23 +00:00
// this does not work because the offset in memory is not yet updated with the thing
this.OpenMapLocation();
}
2021-03-19 20:06:45 +00:00
return this._acsHook.Original(a1, a2, dataPtr);
2020-08-01 07:43:23 +00:00
}
public void OpenMapLocation() {
2021-03-19 20:06:45 +00:00
var packet = this._lastMap;
2020-08-01 07:43:23 +00:00
if (packet == null) {
return;
}
2021-03-19 20:06:45 +00:00
if (!this.MapToRow.TryGetValue(packet.EventItemId, out var rowId)) {
2020-08-01 07:43:23 +00:00
return;
}
2021-03-19 20:06:45 +00:00
var spot = this.Interface.Data.GetExcelSheet<TreasureSpot>().GetRow(rowId, packet.SubRowId);
2020-08-01 07:43:23 +00:00
2021-03-19 20:06:45 +00:00
var loc = spot?.Location?.Value;
var map = loc?.Map?.Value;
var terr = map?.TerritoryType?.Value;
2020-08-01 07:43:23 +00:00
2021-03-19 20:06:45 +00:00
if (terr == null) {
2020-08-01 07:43:23 +00:00
return;
}
2021-03-19 20:06:45 +00:00
var x = ToMapCoordinate(loc.X, map.SizeFactor);
var y = ToMapCoordinate(loc.Z, map.SizeFactor);
var mapLink = new MapLinkPayload(
this.Interface.Data,
2020-08-01 07:43:23 +00:00
terr.RowId,
map.RowId,
ConvertMapCoordinateToRawPosition(x, map.SizeFactor),
ConvertMapCoordinateToRawPosition(y, map.SizeFactor)
);
2021-03-19 20:06:45 +00:00
this.Interface.Framework.Gui.OpenMapWithMapLink(mapLink);
2020-08-01 07:43:23 +00:00
}
2021-03-19 20:06:45 +00:00
private static TreasureMapPacket ParsePacket(IntPtr dataPtr) {
2020-12-09 03:26:14 +00:00
uint category = Marshal.ReadByte(dataPtr);
2021-03-19 20:06:45 +00:00
if (category != TreasureMapsCode) {
2020-08-01 07:43:23 +00:00
return null;
}
dataPtr += 4; // skip padding
2021-03-19 20:06:45 +00:00
var param1 = (uint) Marshal.ReadInt32(dataPtr);
2020-08-01 07:43:23 +00:00
dataPtr += 4;
2021-03-19 20:06:45 +00:00
var param2 = (uint) Marshal.ReadInt32(dataPtr);
2020-08-01 07:43:23 +00:00
dataPtr += 4;
2021-03-19 20:06:45 +00:00
var param3 = (uint) Marshal.ReadInt32(dataPtr);
2020-08-01 07:43:23 +00:00
2021-03-19 20:06:45 +00:00
var eventItemId = param1;
var subRowId = param2;
var justOpened = param3 == 1;
2020-08-01 07:43:23 +00:00
return new TreasureMapPacket(eventItemId, subRowId, justOpened);
}
private static int ConvertMapCoordinateToRawPosition(float pos, float scale) {
var c = scale / 100.0f;
2020-08-25 11:07:44 +00:00
var scaledPos = (((pos - 1.0f) * c / 41.0f * 2048.0f) - 1024.0f) / c;
2020-08-01 07:43:23 +00:00
scaledPos *= 1000.0f;
2021-03-19 20:06:45 +00:00
return (int) scaledPos;
2020-08-01 07:43:23 +00:00
}
2021-03-19 20:06:45 +00:00
2020-08-01 07:43:23 +00:00
private static float ToMapCoordinate(float val, float scale) {
var c = scale / 100f;
val *= c;
2020-08-25 11:07:44 +00:00
return (41f / c * ((val + 1024f) / 2048f)) + 1;
2020-08-01 07:43:23 +00:00
}
public void Dispose() {
2021-03-19 20:06:45 +00:00
this._acsHook?.Dispose();
}
2020-08-01 07:43:23 +00:00
}
2021-03-19 20:06:45 +00:00
internal class TreasureMapPacket {
public uint EventItemId { get; }
public uint SubRowId { get; }
public bool JustOpened { get; }
2020-08-01 07:43:23 +00:00
public TreasureMapPacket(uint eventItemId, uint subRowId, bool justOpened) {
this.EventItemId = eventItemId;
this.SubRowId = subRowId;
this.JustOpened = justOpened;
}
}
}