Plugin.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Dalamud.Data;
  8. using Dalamud.Game;
  9. using Dalamud.Game.ClientState;
  10. using Dalamud.Game.Command;
  11. using Dalamud.Game.Gui;
  12. using Dalamud.IoC;
  13. using Dalamud.Plugin;
  14. using Dalamud.Utility;
  15. using Lumina.Excel.GeneratedSheets;
  16. using HuntBuddy.Attributes;
  17. using HuntBuddy.Ipc;
  18. using HuntBuddy.Structs;
  19. using ImGuiNET;
  20. using ImGuiScene;
  21. using Lumina.Data.Files;
  22. namespace HuntBuddy
  23. {
  24. public class Plugin : IDalamudPlugin
  25. {
  26. public string Name => "Hunt Buddy";
  27. [PluginService]
  28. [RequiredVersion("1.0")]
  29. public static DalamudPluginInterface PluginInterface { get; set; } = null!;
  30. [PluginService]
  31. [RequiredVersion("1.0")]
  32. public static CommandManager Commands { get; set; } = null!;
  33. [PluginService]
  34. [RequiredVersion("1.0")]
  35. public static ChatGui Chat { get; set; } = null!;
  36. [PluginService]
  37. [RequiredVersion("1.0")]
  38. public static DataManager DataManager { get; set; } = null!;
  39. [PluginService]
  40. [RequiredVersion("1.0")]
  41. public static SigScanner SigScanner { get; set; } = null!;
  42. [PluginService]
  43. [RequiredVersion("1.0")]
  44. public static GameGui GameGui { get; set; } = null!;
  45. [PluginService]
  46. [RequiredVersion("1.0")]
  47. public static ClientState ClientState { get; set; } = null!;
  48. [PluginService]
  49. [RequiredVersion("1.0")]
  50. public static Framework Framework { get; set; } = null!;
  51. private readonly PluginCommandManager<Plugin> commandManager;
  52. private readonly Interface pluginInterface;
  53. private ObtainedBillEnum lastState;
  54. public readonly Dictionary<string, Dictionary<KeyValuePair<uint, string>, List<MobHuntEntry>>> MobHuntEntries;
  55. public readonly ConcurrentBag<MobHuntEntry> CurrentAreaMobHuntEntries;
  56. public bool MobHuntEntriesReady = true;
  57. public readonly unsafe MobHuntStruct* MobHuntStruct;
  58. public readonly Configuration Configuration;
  59. public static TeleportConsumer? TeleportConsumer;
  60. public Plugin()
  61. {
  62. this.commandManager = new PluginCommandManager<Plugin>(this, Commands);
  63. this.pluginInterface = new Interface(this);
  64. this.MobHuntEntries = new Dictionary<string, Dictionary<KeyValuePair<uint, string>, List<MobHuntEntry>>>();
  65. this.CurrentAreaMobHuntEntries = new ConcurrentBag<MobHuntEntry>();
  66. this.Configuration = (Configuration)(PluginInterface.GetPluginConfig() ?? new Configuration());
  67. this.Configuration.IconBackgroundColourU32 =
  68. ImGui.ColorConvertFloat4ToU32(this.Configuration.IconBackgroundColour);
  69. unsafe
  70. {
  71. this.MobHuntStruct =
  72. (MobHuntStruct*)SigScanner.GetStaticAddressFromSig(
  73. "D1 48 8D 0D ?? ?? ?? ?? 48 83 C4 20 5F E9 ?? ?? ?? ??");
  74. }
  75. Plugin.TeleportConsumer = new TeleportConsumer();
  76. Plugin.ClientState.TerritoryChanged += this.ClientStateOnTerritoryChanged;
  77. Plugin.PluginInterface.UiBuilder.Draw += this.DrawInterface;
  78. Plugin.PluginInterface.UiBuilder.Draw += this.pluginInterface.DrawLocalHunts;
  79. Plugin.PluginInterface.UiBuilder.OpenConfigUi += this.OpenConfigUi;
  80. Plugin.Framework.Update += this.FrameworkOnUpdate;
  81. }
  82. private unsafe void FrameworkOnUpdate(Framework framework)
  83. {
  84. if (this.lastState == this.MobHuntStruct->ObtainedBillEnumFlags)
  85. {
  86. return;
  87. }
  88. this.lastState = this.MobHuntStruct->ObtainedBillEnumFlags;
  89. this.PluginCommand(string.Empty, "reload");
  90. }
  91. private void ClientStateOnTerritoryChanged(object? sender, ushort e)
  92. {
  93. this.CurrentAreaMobHuntEntries.Clear();
  94. foreach (var mobHuntEntry in this.MobHuntEntries.SelectMany(
  95. expansionEntry => expansionEntry.Value
  96. .Where(entry => entry.Key.Key == Plugin.ClientState.TerritoryType)
  97. .SelectMany(entry => entry.Value)))
  98. {
  99. this.CurrentAreaMobHuntEntries.Add(mobHuntEntry);
  100. }
  101. }
  102. private void OpenConfigUi()
  103. {
  104. this.pluginInterface.DrawInterface = !this.pluginInterface.DrawInterface;
  105. }
  106. private void DrawInterface()
  107. {
  108. this.pluginInterface.DrawInterface = this.pluginInterface.DrawInterface && this.pluginInterface.Draw();
  109. }
  110. private void Dispose(bool disposing)
  111. {
  112. if (!disposing)
  113. {
  114. return;
  115. }
  116. this.MobHuntEntriesReady = false;
  117. Plugin.Framework.Update -= this.FrameworkOnUpdate;
  118. Plugin.PluginInterface.UiBuilder.Draw -= this.DrawInterface;
  119. Plugin.PluginInterface.UiBuilder.Draw -= this.pluginInterface.DrawLocalHunts;
  120. Plugin.PluginInterface.UiBuilder.OpenConfigUi -= this.OpenConfigUi;
  121. this.commandManager.Dispose();
  122. }
  123. [Command("/phb")]
  124. [HelpMessage("Toggles UI\nArguments:\nreload - Reloads data")]
  125. public void PluginCommand(string command, string args)
  126. {
  127. if (args == "reload")
  128. {
  129. this.MobHuntEntriesReady = false;
  130. Task.Run(this.ReloadData);
  131. }
  132. else
  133. {
  134. this.OpenConfigUi();
  135. }
  136. }
  137. public unsafe void ReloadData()
  138. {
  139. this.MobHuntEntries.Clear();
  140. var mobHuntList = new List<MobHuntEntry>();
  141. var mobHuntOrderSheet = Plugin.DataManager.Excel.GetSheet<MobHuntOrder>()!;
  142. foreach (var billNumber in Enum.GetValues<BillEnum>())
  143. {
  144. if (!this.MobHuntStruct->ObtainedBillEnumFlags.HasFlag((ObtainedBillEnum)(1 << (int)billNumber)))
  145. {
  146. continue;
  147. }
  148. var mobHuntOrderTypeRow =
  149. Plugin.DataManager.Excel.GetSheet<MobHuntOrderType>()!.GetRow((uint)billNumber)!;
  150. var rowId = mobHuntOrderTypeRow.OrderStart.Value!.RowId +
  151. (uint)(this.MobHuntStruct->BillOffset[mobHuntOrderTypeRow.RowId] - 1);
  152. if (rowId > mobHuntOrderSheet.RowCount)
  153. {
  154. continue;
  155. }
  156. var mobHuntOrderRows = mobHuntOrderSheet.Where(x => x.RowId == rowId);
  157. foreach (var mobHuntOrderRow in mobHuntOrderRows)
  158. {
  159. var mobHuntEntry =
  160. mobHuntList.FirstOrDefault(x => x.MobHuntId == mobHuntOrderRow.Target.Value!.Name.Row);
  161. if (mobHuntEntry == null)
  162. {
  163. mobHuntList.Add(
  164. new MobHuntEntry
  165. {
  166. Name = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(
  167. mobHuntOrderRow.Target.Value!.Name.Value!.Singular),
  168. TerritoryName =
  169. mobHuntOrderRow.Target.Value!.TerritoryType.Value!.PlaceName.Value!.Name,
  170. ExpansionName = mobHuntOrderRow.Target.Value!.TerritoryType.Value.TerritoryType.Value!
  171. .ExVersion.Value!.Name,
  172. ExpansionId = mobHuntOrderRow.Target.Value!.TerritoryType.Value.TerritoryType.Value!
  173. .ExVersion.Row,
  174. MapId = mobHuntOrderRow.Target.Value!.TerritoryType.Row,
  175. TerritoryType = mobHuntOrderRow.Target.Value!.TerritoryType.Value.TerritoryType.Row,
  176. MobHuntId = mobHuntOrderRow.Target.Value!.Name.Row,
  177. IsEliteMark = billNumber is BillEnum.ArrElite or BillEnum.HwElite or BillEnum.SbElite
  178. or BillEnum.ShbElite or BillEnum.EwElite,
  179. CurrentKillsOffset = (5 * (uint)billNumber) + mobHuntOrderRow.SubRowId,
  180. NeededKills = mobHuntOrderRow.NeededKills,
  181. Icon = Plugin.LoadIcon(mobHuntOrderRow.Target.Value.Icon)
  182. });
  183. }
  184. else
  185. {
  186. if (mobHuntEntry.NeededKills < mobHuntOrderRow.NeededKills)
  187. {
  188. mobHuntEntry.NeededKills = mobHuntOrderRow.NeededKills;
  189. }
  190. }
  191. }
  192. }
  193. foreach (var entry in mobHuntList)
  194. {
  195. var key = entry.ExpansionName ?? "Unknown";
  196. var subKey = new KeyValuePair<uint, string>(entry.TerritoryType, entry.TerritoryName ?? "Unknown");
  197. if (!this.MobHuntEntries.ContainsKey(key))
  198. {
  199. this.MobHuntEntries[key] = new Dictionary<KeyValuePair<uint, string>, List<MobHuntEntry>>();
  200. }
  201. if (!this.MobHuntEntries[key].ContainsKey(subKey))
  202. {
  203. this.MobHuntEntries[key][subKey] = new List<MobHuntEntry>();
  204. }
  205. this.MobHuntEntries[key][subKey].Add(entry);
  206. }
  207. this.ClientStateOnTerritoryChanged(null, 0);
  208. this.MobHuntEntriesReady = true;
  209. }
  210. private static TexFile? GetHdIcon(uint id)
  211. {
  212. var path = $"ui/icon/{id / 1000 * 1000:000000}/{id:000000}_hr1.tex";
  213. return Plugin.DataManager.GetFile<TexFile>(path);
  214. }
  215. private static TextureWrap LoadIcon(uint id)
  216. {
  217. var icon = Plugin.GetHdIcon(id) ?? Plugin.DataManager.GetIcon(id)!;
  218. var iconData = icon.GetRgbaImageData();
  219. return Plugin.PluginInterface.UiBuilder.LoadImageRaw(iconData, icon.Header.Width, icon.Header.Height, 4);
  220. }
  221. public void Dispose()
  222. {
  223. this.Dispose(true);
  224. GC.SuppressFinalize(this);
  225. }
  226. }
  227. }