Plugin.cs 3.1 KB

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