EspConsumer.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using Dalamud.Plugin.Ipc;
  3. namespace HuntBuddy.Ipc;
  4. public class EspConsumer: ConsumerBase {
  5. public override string RequiredPlugin { get; } = "XivEsp";
  6. protected override bool Validate() {
  7. this.getUnifiedSearch.InvokeFunc();
  8. return true;
  9. }
  10. private ICallGateSubscriber<string> getUnifiedSearch = null!;
  11. private ICallGateSubscriber<string, object> setSubstringSearch = null!;
  12. private void Subscribe() {
  13. try {
  14. this.getUnifiedSearch = Service.PluginInterface.GetIpcSubscriber<string>("XivEsp.GetSearch");
  15. this.setSubstringSearch = Service.PluginInterface.GetIpcSubscriber<string, object>("XivEsp.SetSubstring");
  16. }
  17. catch (Exception ex) {
  18. Service.PluginLog.Debug($"Failed to subscribe to XivEsp\nReason: {ex}");
  19. }
  20. }
  21. public EspConsumer() => this.Subscribe();
  22. public bool CanSetSearch {
  23. get {
  24. try {
  25. string current = this.getUnifiedSearch.InvokeFunc();
  26. char type = current[0];
  27. return type is 'N' or 'S';
  28. }
  29. catch {
  30. return false;
  31. }
  32. }
  33. }
  34. public bool SearchFor(string target) {
  35. try {
  36. if (this.CanSetSearch) {
  37. this.setSubstringSearch.InvokeAction(target);
  38. return true;
  39. }
  40. else {
  41. Service.Chat.Print("Cannot override complex (glob/regex) XivEsp search");
  42. return false;
  43. }
  44. }
  45. catch (Exception ex) {
  46. Service.PluginLog.Error($"XivEsp is not responding to IPC: {ex}");
  47. Service.Chat.PrintError("XivEsp plugin is not responding", Plugin.Instance.Name);
  48. return false;
  49. }
  50. }
  51. }