Decompiled source of Lethal Extended Mods v1.0.2

plugins/GrafanaPostMod.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("GrafanaPostMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+d0235d33d91c11b52cdc48caf077160c76fa937f")]
[assembly: AssemblyProduct("GrafanaPostMod")]
[assembly: AssemblyTitle("GrafanaPostMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace GrafanaPostMod
{
	[BepInPlugin("com.pengucc.grafanapostmod", "Grafana Post Mod", "1.1.0")]
	public class GrafanaPostMod : BaseUnityPlugin
	{
		private static HttpClient httpClient;

		private ConfigFile configFile;

		private ConfigEntry<string> ApiUrlConfig;

		private ConfigEntry<float> PostIntervalConfig;

		private ConfigEntry<bool> DisableTrackingConfig;

		private Coroutine postCoroutine;

		private string lastApiUrl;

		private bool lastDisableTracking;

		private float postRequestInterval => PostIntervalConfig.Value;

		private string apiUrl => ApiUrlConfig.Value;

		private bool disableTracking => DisableTrackingConfig.Value;

		private void Awake()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, "GrafanaPostMod.cfg"), true);
			ApiUrlConfig = configFile.Bind<string>("General", "API URL", "https://stats.yourdomain.com/api/players", "The URL to POST player data to.");
			PostIntervalConfig = configFile.Bind<float>("General", "Post Interval", 15f, "Interval in seconds between POST requests.");
			DisableTrackingConfig = configFile.Bind<bool>("General", "Disable Tracking", true, "When true, POST requests are disabled.");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Grafana Post Mod is initializing...");
			((BaseUnityPlugin)this).Logger.LogInfo((object)("API URL: " + apiUrl));
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Post Interval: {postRequestInterval}s");
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Disable Tracking: {disableTracking}");
			httpClient = new HttpClient();
			lastApiUrl = apiUrl;
			lastDisableTracking = disableTracking;
			if (!disableTracking)
			{
				postCoroutine = ((MonoBehaviour)this).StartCoroutine(PostRequestCoroutine());
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Post requests are disabled by config.");
			}
			((MonoBehaviour)this).StartCoroutine(ConfigMonitorCoroutine());
		}

		private IEnumerator ConfigMonitorCoroutine()
		{
			while (true)
			{
				yield return (object)new WaitForSeconds(10f);
				try
				{
					configFile.Reload();
				}
				catch (Exception ex)
				{
					Exception ex2 = ex;
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Failed to reload config: " + ex2.Message));
					continue;
				}
				if (ApiUrlConfig != null && ApiUrlConfig.Value != lastApiUrl)
				{
					lastApiUrl = ApiUrlConfig.Value;
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Config changed: API URL -> " + lastApiUrl));
				}
				if (DisableTrackingConfig != null && DisableTrackingConfig.Value != lastDisableTracking)
				{
					lastDisableTracking = DisableTrackingConfig.Value;
					((BaseUnityPlugin)this).Logger.LogInfo((object)$"Config changed: Disable Tracking -> {lastDisableTracking}");
					if (!lastDisableTracking && postCoroutine == null)
					{
						postCoroutine = ((MonoBehaviour)this).StartCoroutine(PostRequestCoroutine());
						((BaseUnityPlugin)this).Logger.LogInfo((object)"Started POST coroutine due to config change.");
					}
					else if (lastDisableTracking && postCoroutine != null)
					{
						((MonoBehaviour)this).StopCoroutine(postCoroutine);
						postCoroutine = null;
						((BaseUnityPlugin)this).Logger.LogInfo((object)"Stopped POST coroutine due to config change.");
					}
				}
			}
		}

		private IEnumerator PostRequestCoroutine()
		{
			while (true)
			{
				yield return (object)new WaitForSeconds(postRequestInterval);
				((MonoBehaviour)this).StartCoroutine(SendPostRequest());
			}
		}

		private IEnumerator SendPostRequest()
		{
			string jsonData = "{ \"player_count\": 1 }";
			StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
			Task<HttpResponseMessage> requestTask = httpClient.PostAsync(apiUrl, content);
			yield return (object)new WaitUntil((Func<bool>)(() => requestTask.IsCompleted));
			if (requestTask.Exception != null)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Error sending POST request: " + requestTask.Exception.Message));
				yield break;
			}
			HttpResponseMessage response = requestTask.Result;
			if (!response.IsSuccessStatusCode)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Error: Received HTTP {(int)response.StatusCode} {response.ReasonPhrase}");
				yield break;
			}
			Task<string> responseBodyTask = response.Content.ReadAsStringAsync();
			yield return (object)new WaitUntil((Func<bool>)(() => responseBodyTask.IsCompleted));
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Successfully sent POST request. Response: " + responseBodyTask.Result));
		}
	}
}