Plugin.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. "48 8D 0D ?? ?? ?? ?? 8B D8 0F B6 52");
  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.ClientState.TerritoryChanged -= this.ClientStateOnTerritoryChanged;
  118. Plugin.Framework.Update -= this.FrameworkOnUpdate;
  119. Plugin.PluginInterface.UiBuilder.Draw -= this.DrawInterface;
  120. Plugin.PluginInterface.UiBuilder.Draw -= this.pluginInterface.DrawLocalHunts;
  121. Plugin.PluginInterface.UiBuilder.OpenConfigUi -= this.OpenConfigUi;
  122. this.commandManager.Dispose();
  123. }
  124. [Command("/phb")]
  125. [HelpMessage("Toggles UI\nArguments:\nreload - Reloads data\nlocal - Toggles the local hunt marks window")]
  126. public void PluginCommand(string command, string args)
  127. {
  128. if (args == "reload")
  129. {
  130. this.MobHuntEntriesReady = false;
  131. Task.Run(this.ReloadData);
  132. }
  133. else if (args == "local")
  134. {
  135. this.Configuration.ShowLocalHunts = !this.Configuration.ShowLocalHunts;
  136. this.Configuration.Save();
  137. }
  138. else
  139. {
  140. this.OpenConfigUi();
  141. }
  142. }
  143. public unsafe void ReloadData()
  144. {
  145. this.MobHuntEntries.Clear();
  146. var mobHuntList = new List<MobHuntEntry>();
  147. var mobHuntOrderSheet = Plugin.DataManager.Excel.GetSheet<MobHuntOrder>()!;
  148. foreach (var billNumber in Enum.GetValues<BillEnum>())
  149. {
  150. if (!this.MobHuntStruct->ObtainedBillEnumFlags.HasFlag((ObtainedBillEnum)(1 << (int)billNumber)))
  151. {
  152. continue;
  153. }
  154. var mobHuntOrderTypeRow =
  155. Plugin.DataManager.Excel.GetSheet<MobHuntOrderType>()!.GetRow((uint)billNumber)!;
  156. var rowId = mobHuntOrderTypeRow.OrderStart.Value!.RowId +
  157. (uint)(this.MobHuntStruct->BillOffset[mobHuntOrderTypeRow.RowId] - 1);
  158. if (rowId > mobHuntOrderSheet.RowCount)
  159. {
  160. continue;
  161. }
  162. var mobHuntOrderRows = mobHuntOrderSheet.Where(x => x.RowId == rowId);
  163. foreach (var mobHuntOrderRow in mobHuntOrderRows)
  164. {
  165. var mobHuntEntry =
  166. mobHuntList.FirstOrDefault(x => x.MobHuntId == mobHuntOrderRow.Target.Value!.Name.Row);
  167. if (mobHuntEntry == null)
  168. {
  169. mobHuntList.Add(
  170. new MobHuntEntry
  171. {
  172. Name = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(
  173. mobHuntOrderRow.Target.Value!.Name.Value!.Singular),
  174. TerritoryName =
  175. mobHuntOrderRow.Target.Value!.TerritoryType.Value!.PlaceName.Value!.Name,
  176. ExpansionName = mobHuntOrderRow.Target.Value!.TerritoryType.Value.TerritoryType.Value!
  177. .ExVersion.Value!.Name,
  178. ExpansionId = mobHuntOrderRow.Target.Value!.TerritoryType.Value.TerritoryType.Value!
  179. .ExVersion.Row,
  180. MapId = mobHuntOrderRow.Target.Value!.TerritoryType.Row,
  181. TerritoryType = mobHuntOrderRow.Target.Value!.TerritoryType.Value.TerritoryType.Row,
  182. MobHuntId = mobHuntOrderRow.Target.Value!.Name.Row,
  183. IsEliteMark = billNumber is BillEnum.ArrElite or BillEnum.HwElite or BillEnum.SbElite
  184. or BillEnum.ShbElite or BillEnum.EwElite,
  185. CurrentKillsOffset = (5 * (uint)billNumber) + mobHuntOrderRow.SubRowId,
  186. NeededKills = mobHuntOrderRow.NeededKills,
  187. Icon = Plugin.LoadIcon(mobHuntOrderRow.Target.Value.Icon)
  188. });
  189. }
  190. else
  191. {
  192. if (mobHuntEntry.NeededKills < mobHuntOrderRow.NeededKills)
  193. {
  194. mobHuntEntry.NeededKills = mobHuntOrderRow.NeededKills;
  195. }
  196. }
  197. }
  198. }
  199. foreach (var entry in mobHuntList)
  200. {
  201. var key = entry.ExpansionName ?? "Unknown";
  202. var subKey = new KeyValuePair<uint, string>(entry.TerritoryType, entry.TerritoryName ?? "Unknown");
  203. if (!this.MobHuntEntries.ContainsKey(key))
  204. {
  205. this.MobHuntEntries[key] = new Dictionary<KeyValuePair<uint, string>, List<MobHuntEntry>>();
  206. }
  207. if (!this.MobHuntEntries[key].ContainsKey(subKey))
  208. {
  209. this.MobHuntEntries[key][subKey] = new List<MobHuntEntry>();
  210. }
  211. this.MobHuntEntries[key][subKey].Add(entry);
  212. }
  213. this.ClientStateOnTerritoryChanged(null, 0);
  214. this.MobHuntEntriesReady = true;
  215. }
  216. private static TexFile? GetHdIcon(uint id)
  217. {
  218. var path = $"ui/icon/{id / 1000 * 1000:000000}/{id:000000}_hr1.tex";
  219. return Plugin.DataManager.GetFile<TexFile>(path);
  220. }
  221. private static TextureWrap LoadIcon(uint id)
  222. {
  223. var icon = Plugin.GetHdIcon(id) ?? Plugin.DataManager.GetIcon(id)!;
  224. var iconData = icon.GetRgbaImageData();
  225. return Plugin.PluginInterface.UiBuilder.LoadImageRaw(iconData, icon.Header.Width, icon.Header.Height, 4);
  226. }
  227. public void Dispose()
  228. {
  229. this.Dispose(true);
  230. GC.SuppressFinalize(this);
  231. }
  232. }
  233. }