123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using HarmonyLib;
- using IllusionPlugin;
- using System;
- using System.Diagnostics;
- using System.Reflection;
- using YUR.Fit.Core.Models;
- using YUR.ViewControllers;
- namespace BsYurHttpStatus
- {
- public sealed class Plugin : IPlugin
- {
- [Conditional("DEBUG")]
- internal static void Log(string text) => Debug.Print($"BsYurHttpStatus {text}");
- private Harmony _harmony;
- private bool _isPatched;
- private static HttpStatusServer _statusServer;
- private bool _isServerStarted;
- public string Name => "YUR Http Status";
- public string Version => "0.0.0.1";
- public void OnApplicationStart()
- {
- Log("OnApplicationStart");
- Log("Patching");
- try
- {
- var original = AccessTools.Method(typeof(ActivityViewController), "OverlayUpdateAction");
- var method = GetType().GetMethod(nameof(PatchMethod), BindingFlags.Static | BindingFlags.NonPublic);
- _harmony = new Harmony(Name);
- _harmony.Patch(original, postfix:new HarmonyMethod(method));
- _isPatched = true;
- Log("Patch success");
- }
- catch (Exception e)
- {
- Log("ERROR " + e);
- }
- if (_isPatched)
- {
- Log("Starting HTTP Server");
- try
- {
- _statusServer = new HttpStatusServer();
- _statusServer.Start();
- _isServerStarted = true;
- Log("HTTP Server started");
- }
- catch (Exception e)
- {
- Log("ERROR " + e);
- }
- }
- }
- public void OnApplicationQuit()
- {
- Log("OnApplicationQuit");
- if (_isPatched)
- {
- try
- {
- Log("Unpatching");
- _harmony?.UnpatchAll(Name);
- _isPatched = false;
- Log("Unpatch success");
- }
- catch (Exception e)
- {
- Log("ERROR " + e);
- }
- }
- if (_isServerStarted)
- {
- Log("Stopping HTTP Server");
- try
- {
- _statusServer.Stop();
- _isServerStarted = false;
- Log("HTTP Server Stopped");
- }
- catch (Exception e)
- {
- Log("ERROR " + e);
- }
- }
- }
- private static void PatchMethod(ref OverlayStatusUpdate status)
- {
- _statusServer?.Update(status);
- Log("YUR.(Est)HeartRate:" + (status.HeartRate ?? status.CalculationMetrics.EstHeartRate));
- }
- public void OnLevelWasLoaded(int level)
- {
- }
- public void OnLevelWasInitialized(int level)
- {
- }
- public void OnUpdate()
- {
- }
- public void OnFixedUpdate()
- {
- }
- }
- }
|