FishNotify/FishNotify.cs

193 lines
5.7 KiB
C#
Raw Normal View History

2023-10-03 08:36:31 +00:00
using Dalamud.Game.Network;
using Dalamud.Game.Text.SeStringHandling;
2021-09-29 14:36:27 +00:00
using Dalamud.Interface.Colors;
using Dalamud.IoC;
using Dalamud.Plugin;
using ImGuiNET;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
2023-10-03 08:36:31 +00:00
using Dalamud.Plugin.Services;
2021-09-29 14:36:27 +00:00
2023-10-03 08:36:31 +00:00
namespace FishNotify;
public sealed class FishNotifyPlugin : IDalamudPlugin
2021-09-29 14:36:27 +00:00
{
2023-10-03 08:36:31 +00:00
[PluginService]
2024-07-05 20:04:26 +00:00
private IDalamudPluginInterface PluginInterface { get; set; } = null!;
2021-09-29 14:36:27 +00:00
2023-10-03 08:36:31 +00:00
[PluginService]
private IGameNetwork Network { get; set; } = null!;
2021-09-29 14:36:27 +00:00
2023-10-03 08:36:31 +00:00
[PluginService]
private IChatGui Chat { get; set; } = null!;
2023-10-03 08:36:31 +00:00
[PluginService]
private IPluginLog PluginLog { get; set; } = null!;
2023-10-03 08:36:31 +00:00
private readonly Configuration _configuration;
private bool _settingsVisible;
private int _expectedOpCode = -1;
private uint _fishCount;
2021-09-29 14:36:27 +00:00
2023-10-03 08:36:31 +00:00
public FishNotifyPlugin()
{
_configuration = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
2023-10-03 08:36:31 +00:00
Network.NetworkMessage += OnNetworkMessage;
PluginInterface.UiBuilder.Draw += OnDrawUI;
PluginInterface.UiBuilder.OpenConfigUi += OnOpenConfigUi;
2021-09-29 14:36:27 +00:00
2023-10-03 08:36:31 +00:00
var client = new HttpClient();
client.GetStringAsync("https://raw.githubusercontent.com/karashiiro/FFXIVOpcodes/master/opcodes.min.json")
.ContinueWith(ExtractOpCode);
}
2021-09-29 14:36:27 +00:00
2023-10-03 08:36:31 +00:00
public void Dispose()
{
Network.NetworkMessage -= OnNetworkMessage;
PluginInterface.UiBuilder.Draw -= OnDrawUI;
PluginInterface.UiBuilder.OpenConfigUi -= OnOpenConfigUi;
}
2021-09-29 14:36:27 +00:00
2023-10-03 08:36:31 +00:00
private void ExtractOpCode(Task<string> task)
{
try
2021-09-29 14:36:27 +00:00
{
2023-10-03 08:36:31 +00:00
var regions = JsonConvert.DeserializeObject<List<OpcodeRegion>>(task.Result);
if (regions == null)
2021-09-29 14:36:27 +00:00
{
2023-10-03 08:36:31 +00:00
PluginLog.Warning("No regions found in opcode list");
2021-09-29 14:36:27 +00:00
return;
2023-10-03 08:36:31 +00:00
}
2021-09-29 14:36:27 +00:00
2023-10-03 08:36:31 +00:00
var region = regions.Find(r => r.Region == "Global");
if (region == null || region.Lists == null)
{
PluginLog.Warning("No global region found in opcode list");
2021-09-29 14:36:27 +00:00
return;
2023-10-03 08:36:31 +00:00
}
2021-09-29 14:36:27 +00:00
2023-10-03 08:36:31 +00:00
if (!region.Lists.TryGetValue("ServerZoneIpcType", out List<OpcodeList>? serverZoneIpcTypes) || serverZoneIpcTypes == null)
2021-09-29 14:36:27 +00:00
{
2023-10-03 08:36:31 +00:00
PluginLog.Warning("No ServerZoneIpcType in opcode list");
return;
2021-09-29 14:36:27 +00:00
}
2023-10-03 08:36:31 +00:00
var eventPlay = serverZoneIpcTypes.Find(opcode => opcode.Name == "EventPlay");
if (eventPlay == null)
{
2023-10-03 08:36:31 +00:00
PluginLog.Warning("No EventPlay opcode in ServerZoneIpcType");
return;
}
2023-10-03 08:36:31 +00:00
_expectedOpCode = eventPlay.Opcode;
PluginLog.Debug($"Found EventPlay opcode {_expectedOpCode:X4}");
}
2023-10-03 08:36:31 +00:00
catch (Exception e)
{
PluginLog.Error(e, "Could not download/extract opcodes: {}", e.Message);
}
}
private void OnNetworkMessage(IntPtr dataPtr, ushort opCode, uint sourceActorId, uint targetActorId, NetworkMessageDirection direction)
{
if (direction != NetworkMessageDirection.ZoneDown || opCode != _expectedOpCode)
return;
var data = new byte[32];
Marshal.Copy(dataPtr, data, 0, data.Length);
int eventId = BitConverter.ToInt32(data, 8);
short scene = BitConverter.ToInt16(data, 12);
int param5 = BitConverter.ToInt32(data, 28);
// Fishing event?
if (eventId != 0x00150001)
return;
// Fish hooked?
if (scene != 5)
return;
2023-10-03 08:36:31 +00:00
switch (param5)
2021-09-29 14:36:27 +00:00
{
2023-10-03 08:36:31 +00:00
case 0x124:
// light tug (!)
++_fishCount;
Sounds.PlaySound(Resources.Info);
SendChatAlert("light");
break;
case 0x125:
// medium tug (!!)
++_fishCount;
Sounds.PlaySound(Resources.Alert);
SendChatAlert("medium");
break;
case 0x126:
// heavy tug (!!!)
++_fishCount;
Sounds.PlaySound(Resources.Alarm);
SendChatAlert("heavy");
break;
default:
Sounds.Stop();
break;
2021-09-29 14:36:27 +00:00
}
2023-10-03 08:36:31 +00:00
}
2023-10-03 08:36:31 +00:00
private void SendChatAlert(string size)
{
if (!_configuration.ChatAlerts)
2021-09-29 14:36:27 +00:00
{
2023-10-03 08:36:31 +00:00
return;
2021-09-29 14:36:27 +00:00
}
2023-10-03 08:36:31 +00:00
SeString message = new SeStringBuilder()
.AddUiForeground(514)
.Append("[FishNotify]")
.AddUiForegroundOff()
.Append(" You hook a fish with a ")
.AddUiForeground(514)
.Append(size)
.AddUiForegroundOff()
.Append(" bite.")
.Build();
Chat.Print(message);
2021-09-29 14:36:27 +00:00
}
2023-10-03 08:36:31 +00:00
private void OnDrawUI()
2021-09-29 14:36:27 +00:00
{
2023-10-03 08:36:31 +00:00
if (!_settingsVisible)
return;
if (ImGui.Begin("FishNotify", ref _settingsVisible, ImGuiWindowFlags.AlwaysAutoResize))
{
var chatAlerts = _configuration.ChatAlerts;
if (ImGui.Checkbox("Show chat message on hooking a fish", ref chatAlerts))
{
_configuration.ChatAlerts = chatAlerts;
PluginInterface.SavePluginConfig(_configuration);
}
if (_expectedOpCode > -1)
ImGui.TextColored(ImGuiColors.HealerGreen, $"Status: {(_fishCount == 0 ? "Unknown (not triggered yet)" : $"OK ({_fishCount} fish hooked)")}, opcode = {_expectedOpCode:X}");
else
ImGui.TextColored(ImGuiColors.DalamudRed, "Status: No opcode :(");
}
ImGui.End();
2021-09-29 14:36:27 +00:00
}
2023-10-03 08:36:31 +00:00
private void OnOpenConfigUi()
2021-09-29 14:36:27 +00:00
{
2023-10-03 08:36:31 +00:00
_settingsVisible = !_settingsVisible;
2021-09-29 14:36:27 +00:00
}
}