Plugin.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using HarmonyLib;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using IPA;
  6. using YUR.Fit.Core.Models;
  7. using YUR.ViewControllers;
  8. namespace BsYurHttpStatus
  9. {
  10. [Plugin(RuntimeOptions.SingleStartInit)]
  11. public class Plugin //: IPlugin
  12. {
  13. [Conditional("DEBUG")]
  14. internal static void Log(string text) => Debug.Print($"BsYurHttpStatus {text}");
  15. private Harmony _harmony;
  16. private bool _isPatched;
  17. private static HttpStatusServer _statusServer;
  18. private bool _isServerStarted;
  19. public string Name => "YUR Http Status";
  20. public string Version => "0.0.0.1";
  21. [OnStart]
  22. public void OnApplicationStart()
  23. {
  24. Log("OnApplicationStart");
  25. Log("Patching");
  26. try
  27. {
  28. var original = AccessTools.Method(typeof(ActivityViewController), "OverlayUpdateAction");
  29. var method = GetType().GetMethod(nameof(PatchMethod), BindingFlags.Static | BindingFlags.NonPublic);
  30. _harmony = new Harmony(Name);
  31. _harmony.Patch(original, postfix: new HarmonyMethod(method));
  32. _isPatched = true;
  33. Log("Patch success");
  34. }
  35. catch (Exception e)
  36. {
  37. Log("ERROR " + e);
  38. }
  39. if (_isPatched)
  40. {
  41. Log("Starting HTTP Server");
  42. try
  43. {
  44. _statusServer = new HttpStatusServer();
  45. _statusServer.Start();
  46. _isServerStarted = true;
  47. Log("HTTP Server started");
  48. }
  49. catch (Exception e)
  50. {
  51. Log("ERROR " + e);
  52. }
  53. }
  54. }
  55. [OnExit]
  56. public void OnApplicationQuit()
  57. {
  58. Log("OnApplicationQuit");
  59. if (_isPatched)
  60. {
  61. try
  62. {
  63. Log("Unpatching");
  64. _harmony?.UnpatchAll(Name);
  65. _isPatched = false;
  66. Log("Unpatch success");
  67. }
  68. catch (Exception e)
  69. {
  70. Log("ERROR " + e);
  71. }
  72. }
  73. if (_isServerStarted)
  74. {
  75. Log("Stopping HTTP Server");
  76. try
  77. {
  78. _statusServer.Stop();
  79. _isServerStarted = false;
  80. Log("HTTP Server Stopped");
  81. }
  82. catch (Exception e)
  83. {
  84. Log("ERROR " + e);
  85. }
  86. }
  87. }
  88. private static void PatchMethod(ref OverlayStatusUpdate status)
  89. {
  90. Log("Update:"+status.SuckFloatProperty("EstHeartRate"));
  91. _statusServer?.Update(status);
  92. }
  93. public void OnLevelWasLoaded(int level)
  94. {
  95. }
  96. public void OnLevelWasInitialized(int level)
  97. {
  98. }
  99. public void OnUpdate()
  100. {
  101. }
  102. public void OnFixedUpdate()
  103. {
  104. }
  105. }
  106. }